You are here

public function SimplenewsSendTest::testSendNowCronThrottle in Simplenews 3.x

Same name and namespace in other branches
  1. 8.2 tests/src/Functional/SimplenewsSendTest.php \Drupal\Tests\simplenews\Functional\SimplenewsSendTest::testSendNowCronThrottle()

Send a newsletter using cron and a low throttle.

File

tests/src/Functional/SimplenewsSendTest.php, line 187

Class

SimplenewsSendTest
Test cases for creating and sending newsletters.

Namespace

Drupal\Tests\simplenews\Functional

Code

public function testSendNowCronThrottle() {
  $config = $this
    ->config('simplenews.settings');
  $config
    ->set('mail.throttle', 3);
  $config
    ->save();

  // Verify that the newsletter settings are shown.
  $this
    ->drupalGet('node/add/simplenews_issue');
  $this
    ->assertText(t('Create Newsletter Issue'));
  $edit = [
    'title[0][value]' => $this
      ->randomString(10),
    'simplenews_issue[target_id]' => 'default',
  ];
  $this
    ->submitForm($edit, 'Save');
  $this
    ->assertEqual(1, 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
    ->submitForm([], '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
    ->getMails();
  $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', [
    ':nid' => $node
      ->id(),
    ':type' => 'node',
  ])
    ->fetchField();
  $this
    ->assertEqual(5, $spooled, t('5 mails have been added to the mail spool'));

  // Run cron for the first time.
  simplenews_cron();

  // 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.'));
  $this
    ->assertEqual(3, $node->simplenews_issue->sent_count, 'subscriber count is correct');
  $spooled = \Drupal::database()
    ->query('SELECT COUNT(*) FROM {simplenews_mail_spool} WHERE entity_id = :nid AND entity_type = :type', [
    ':nid' => $node
      ->id(),
    ':type' => 'node',
  ])
    ->fetchField();
  $this
    ->assertEqual(2, $spooled, t('2 mails remaining in spool.'));

  // Run cron for the second time.
  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', [
    ':nid' => $node
      ->id(),
    ':type' => 'node',
  ])
    ->fetchField();
  $this
    ->assertEqual(0, $spooled, t('No mails remaining in spool.'));

  // Verify mails.
  $mails = $this
    ->getMails();
  $this
    ->assertEqual(5, count($mails), t('All mails were sent.'));
  foreach ($mails as $mail) {
    $this
      ->assertEqual($mail['subject'], '[Default newsletter] ' . $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'));
  $this
    ->assertEqual(5, $node->simplenews_issue->sent_count);
}