You are here

function SimplenewsSendTest::testSendNowCron in Simplenews 8

Send a newsletter without using cron.

File

src/Tests/SimplenewsSendTest.php, line 270
Simplenews send test functions.

Class

SimplenewsSendTest
Test cases for creating and sending newsletters.

Namespace

Drupal\simplenews\Tests

Code

function testSendNowCron() {

  // Verify that the newsletter settings are shown.
  $this
    ->drupalGet('node/add/simplenews_issue');
  $this
    ->assertText(t('Create Newsletter Issue'));
  $edit = array(
    'title[0][value]' => $this
      ->randomString(10),
    'simplenews_issue' => 'default',
  );

  // Try preview first.
  $this
    ->drupalPostForm(NULL, $edit, t('Preview'));
  $this
    ->clickLink(t('Back to content editing'));

  // Then save.
  $this
    ->drupalPostForm(NULL, array(), t('Save'));
  $this
    ->assertTrue(preg_match('|node/(\\d+)$|', $this
    ->getUrl(), $matches), 'Node created');
  $node = Node::load($matches[1]);
  $this
    ->clickLink(t('Newsletter'));
  $this
    ->assertText(t('Send'));
  $this
    ->assertText(t('Test'));

  // Verify state.
  \Drupal::entityTypeManager()
    ->getStorage('node')
    ->resetCache();
  $node = Node::load($node
    ->id());
  $this
    ->assertEqual(SIMPLENEWS_STATUS_SEND_NOT, $node->simplenews_issue->status, t('Newsletter not sent yet.'));

  // Send now.
  $this
    ->drupalPostForm(NULL, array(), t('Send now'));

  // Verify state.
  \Drupal::entityTypeManager()
    ->getStorage('node')
    ->resetCache();
  $node = Node::load($node
    ->id());
  $this
    ->assertEqual(SIMPLENEWS_STATUS_SEND_PENDING, $node->simplenews_issue->status, t('Newsletter sending pending.'));

  // Verify that no mails have been sent yet.
  $mails = $this
    ->drupalGetMails();
  $this
    ->assertEqual(0, count($mails), t('No mails were sent yet.'));
  $spooled = \Drupal::database()
    ->query('SELECT COUNT(*) FROM {simplenews_mail_spool} WHERE entity_id = :nid AND entity_type = :type', array(
    ':nid' => $node
      ->id(),
    ':type' => 'node',
  ))
    ->fetchField();
  $this
    ->assertEqual(5, $spooled, t('5 mails have been added to the mail spool'));

  // Check warning message on node edit form.
  $this
    ->clickLink(t('Edit'));
  $this
    ->assertText(t('This newsletter issue is currently being sent. Any changes will be reflected in the e-mails which have not been sent yet.'));

  // Run cron.
  simplenews_cron();

  // Verify state.
  \Drupal::entityTypeManager()
    ->getStorage('node')
    ->resetCache();
  $node = Node::load($node
    ->id());
  $this
    ->assertEqual(SIMPLENEWS_STATUS_SEND_READY, $node->simplenews_issue->status, t('Newsletter sending finished.'));
  $spooled = \Drupal::database()
    ->query('SELECT COUNT(*) FROM {simplenews_mail_spool} WHERE entity_id = :nid AND entity_type = :type', array(
    ':nid' => $node
      ->id(),
    ':type' => 'node',
  ))
    ->fetchField();
  $this
    ->assertEqual(0, $spooled, t('No mails remaining in spool.'));

  // Verify mails.
  $mails = $this
    ->drupalGetMails();
  $this
    ->assertEqual(5, count($mails), t('All mails were sent.'));
  foreach ($mails as $mail) {

    // @todo Temporarily strip tags from mail subjects until
    //   https://www.drupal.org/node/2575791 is fixed.
    $this
      ->assertEqual($mail['subject'], '[Default newsletter] ' . strip_tags($edit['title[0][value]']), t('Mail has correct subject'));
    $this
      ->assertTrue(isset($this->subscribers[$mail['to']]), t('Found valid recipient'));
    unset($this->subscribers[$mail['to']]);
  }
  $this
    ->assertEqual(0, count($this->subscribers), t('all subscribers have been received a mail'));
}