You are here

public function SimplenewsAdministrationTest::testNewsletterIssuesOverview in Simplenews 8.2

Same name and namespace in other branches
  1. 3.x tests/src/Functional/SimplenewsAdministrationTest.php \Drupal\Tests\simplenews\Functional\SimplenewsAdministrationTest::testNewsletterIssuesOverview()

Test newsletter issue overview.

File

tests/src/Functional/SimplenewsAdministrationTest.php, line 921

Class

SimplenewsAdministrationTest
Managing of newsletter categories and content types.

Namespace

Drupal\Tests\simplenews\Functional

Code

public function testNewsletterIssuesOverview() {

  // Verify newsletter overview isn't available without permission.
  $this
    ->drupalGet('admin/content/simplenews');
  $this
    ->assertResponse(403);
  $admin_user = $this
    ->drupalCreateUser([
    'administer newsletters',
    'create simplenews_issue content',
    'administer simplenews subscriptions',
    'administer nodes',
    'send newsletter',
  ]);
  $this
    ->drupalLogin($admin_user);

  // Create a newsletter.
  $name = $this
    ->randomMachineName();
  $edit = [
    'name' => $name,
    'id' => mb_strtolower($name),
  ];
  $this
    ->drupalPostForm('admin/config/services/simplenews/add', $edit, t('Save'));

  // Create a newsletter issue and publish.
  $edit = [
    'title[0][value]' => 'Test_issue_1',
    'simplenews_issue[target_id]' => mb_strtolower($name),
  ];
  $this
    ->drupalPostForm('node/add/simplenews_issue', $edit, t('Save'));

  // Create another newsletter issue and keep unpublished.
  $edit = [
    'title[0][value]' => 'Test_issue_2',
    'simplenews_issue[target_id]' => mb_strtolower($name),
    'status[value]' => FALSE,
  ];
  $this
    ->drupalPostForm('node/add/simplenews_issue', $edit, t('Save'));

  // Test mass subscribe with previously unsubscribed users.
  for ($i = 0; $i < 3; $i++) {
    $subscribers[] = $this
      ->randomEmail();
  }
  $edit = [
    'emails' => implode(', ', $subscribers),
    'newsletters[' . mb_strtolower($name) . ']' => TRUE,
  ];
  $this
    ->drupalPostForm('admin/people/simplenews/import', $edit, t('Subscribe'));
  $this
    ->drupalGet('admin/content/simplenews');

  // Check the correct values are present in the view.
  $rows = $this
    ->xpath('//tbody/tr');

  // Check the number of results in the view.
  $this
    ->assertEqual(2, count($rows));
  foreach ($rows as $row) {
    if ($row
      ->find('xpath', '/td[2]/a')
      ->getText() == 'Test_issue_2') {
      $this
        ->assertEqual($name, trim($row
        ->find('xpath', '/td[3]/a')
        ->getText()));
      $this
        ->assertEqual('Newsletter issue will be sent to 3 subscribers.', trim($row
        ->find('xpath', '/td[6]/span')
        ->getAttribute('title')));
      $this
        ->assertEqual('✖', trim($row
        ->find('xpath', '/td[4]')
        ->getText()));
      $this
        ->assertEqual('0/3', trim($row
        ->find('xpath', '/td[6]/span')
        ->getText()));
    }
    else {
      $this
        ->assertEqual('✔', trim($row
        ->find('xpath', '/td[4]')
        ->getText()));
    }
  }

  // Send newsletter issues using bulk operations.
  $edit = [
    'node_bulk_form[0]' => TRUE,
    'node_bulk_form[1]' => TRUE,
    'action' => 'simplenews_send_action',
  ];
  $this
    ->drupalPostForm(NULL, $edit, t('Apply to selected items'));

  // Check the relevant messages.
  $this
    ->assertText('Newsletter issue Test_issue_2 will be sent when published.');
  $this
    ->assertText('Newsletter issue Test_issue_1 pending.');
  $rows = $this
    ->xpath('//tbody/tr');

  // Assert the status message of each newsletter.
  foreach ($rows as $row) {
    if ($row
      ->find('xpath', '/td[2]/a')
      ->getText() == 'Test_issue_2') {
      $this
        ->assertEqual('Newsletter issue will be sent to 3 subscribers on publish.', trim($row
        ->find('xpath', '/td[6]/span')
        ->getAttribute('title')));
    }
    else {
      $this
        ->assertEqual('Newsletter issue is pending, 0 mails sent out of 3, 0 errors.', trim($row
        ->find('xpath', '/td[6]/img')
        ->getAttribute('title')));
      $this
        ->assertEqual(file_url_transform_relative(file_create_url(drupal_get_path('module', 'simplenews') . '/images/sn-cron.png')), trim($row
        ->find('xpath', '/td[6]/img')
        ->getAttribute('src')));
    }
  }

  // Stop sending the pending newsletters.
  $edit = [
    'node_bulk_form[0]' => TRUE,
    'node_bulk_form[1]' => TRUE,
    'action' => 'simplenews_stop_action',
  ];
  $this
    ->drupalPostForm(NULL, $edit, t('Apply to selected items'));

  // Check the stop message.
  $this
    ->assertText('Sending of Test_issue_1 was stopped. 3 pending email(s) were deleted.');
  $rows = $this
    ->xpath('//tbody/tr');

  // Check the send status of each issue.
  foreach ($rows as $row) {
    if ($row
      ->find('xpath', '/td[2]/a')
      ->getText() == 'Test_issue_2') {
      $this
        ->assertEqual('Newsletter issue will be sent to 3 subscribers on publish.', trim($row
        ->find('xpath', '/td[6]/span')
        ->getAttribute('title')));
    }
    else {
      $this
        ->assertEqual('Newsletter issue will be sent to 3 subscribers.', trim($row
        ->find('xpath', '/td[6]/span')
        ->getAttribute('title')));
    }
  }

  // Send newsletter issues using bulk operations.
  $edit = [
    'node_bulk_form[0]' => TRUE,
    'node_bulk_form[1]' => TRUE,
    'action' => 'simplenews_send_action',
  ];
  $this
    ->drupalPostForm(NULL, $edit, t('Apply to selected items'));

  // Run cron to send the mails.
  $this
    ->cronRun();
  $this
    ->drupalGet('admin/content/simplenews');
  $rows = $this
    ->xpath('//tbody/tr');

  // Check the send status of each issue.
  foreach ($rows as $row) {
    if ($row
      ->find('xpath', '/td[2]/a')
      ->getText() == 'Test_issue_2') {
      $this
        ->assertEqual('Newsletter issue will be sent to 3 subscribers on publish.', trim($row
        ->find('xpath', '/td[6]/span')
        ->getAttribute('title')));
    }
    else {
      $this
        ->assertEqual('Newsletter issue sent to 3 subscribers, 0 errors.', trim($row
        ->find('xpath', '/td[6]/img')
        ->getAttribute('title')));
      $this
        ->assertEqual(file_url_transform_relative(file_create_url(drupal_get_path('module', 'simplenews') . '/images/sn-sent.png')), trim($row
        ->find('xpath', '/td[6]/img')
        ->getAttribute('src')));
    }
  }
}