You are here

public function SimplenewsSendTest::testProgrammaticNewsletter in Simplenews 8.2

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

Creates and sends a node using the API.

File

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

Class

SimplenewsSendTest
Test cases for creating and sending newsletters.

Namespace

Drupal\Tests\simplenews\Functional

Code

public function testProgrammaticNewsletter() {

  // Create a very basic node.
  $node = Node::create([
    'type' => 'simplenews_issue',
    'title' => $this
      ->randomString(10),
    'uid' => 0,
    'status' => 1,
  ]);
  $node->simplenews_issue->target_id = $this
    ->getRandomNewsletter();
  $node->simplenews_issue->handler = 'simplenews_all';
  $node
    ->save();

  // Send the node.
  \Drupal::service('simplenews.spool_storage')
    ->addIssue($node);

  // Send mails.
  \Drupal::service('simplenews.mailer')
    ->sendSpool();
  \Drupal::service('simplenews.spool_storage')
    ->clear();

  // Update sent status for newsletter admin panel.
  \Drupal::service('simplenews.mailer')
    ->updateSendStatus();

  // 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] ' . $node
      ->getTitle(), 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'));

  // Create another node.
  $node = Node::create([
    'type' => 'simplenews_issue',
    'title' => $this
      ->randomString(10),
    'uid' => 0,
    'status' => 1,
  ]);
  $node->simplenews_issue->target_id = $this
    ->getRandomNewsletter();
  $node->simplenews_issue->handler = 'simplenews_all';
  $node
    ->save();

  // Send the node.
  \Drupal::service('simplenews.spool_storage')
    ->addIssue($node);

  // Make sure that they have been added.
  $this
    ->assertEqual(\Drupal::service('simplenews.spool_storage')
    ->countMails(), 5);

  // Mark them as 'in progress', fake a currently running send process.
  $this
    ->assertEqual(count(\Drupal::service('simplenews.spool_storage')
    ->getMails(2)), 2);

  // Those two should be excluded if we get mails a second time.
  $this
    ->assertEqual(count(\Drupal::service('simplenews.spool_storage')
    ->getMails()), 3);

  // The count should still include all the mails because they are still
  // in the spool.  This is needed for correct operation of code such as
  // Mailer::updateSendStatus().
  $this
    ->assertEqual(\Drupal::service('simplenews.spool_storage')
    ->countMails(), 5);
}