You are here

function SimplenewsSendTest::testSendPublishNoCron in Simplenews 8

Send a newsletter on publish without using cron.

File

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

Class

SimplenewsSendTest
Test cases for creating and sending newsletters.

Namespace

Drupal\simplenews\Tests

Code

function testSendPublishNoCron() {

  // Disable cron.
  $config = $this
    ->config('simplenews.settings');
  $config
    ->set('mail.use_cron', FALSE);
  $config
    ->save();

  // 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',
    'status[value]' => FALSE,
  );
  $this
    ->drupalPostForm(NULL, $edit, '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 on publish'));

  // Verify state.
  \Drupal::entityTypeManager()
    ->getStorage('node')
    ->resetCache(array(
    $node
      ->id(),
  ));
  $node = Node::load($node
    ->id());
  $this
    ->assertEqual(SIMPLENEWS_STATUS_SEND_PUBLISH, $node->simplenews_issue->status, t('Newsletter set up for sending on publish.'));
  $this
    ->clickLink(t('Edit'));
  $this
    ->drupalPostForm(NULL, [
    'status[value]' => TRUE,
  ], t('Save'));

  // Send on publish does not send immediately.
  \Drupal::entityTypeManager()
    ->getStorage('node')
    ->resetCache(array(
    $node
      ->id(),
  ));
  \Drupal::service('simplenews.mailer')
    ->attemptImmediateSend(array(), FALSE);

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

  // @todo test sent subscriber count.
  // Verify mails.
  $mails = $this
    ->drupalGetMails();
  $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'));
}