You are here

public function SimplenewsSendTest::testSendFail in Simplenews 8.2

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

Tests failing to send mails from cron.

File

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

Class

SimplenewsSendTest
Test cases for creating and sending newsletters.

Namespace

Drupal\Tests\simplenews\Functional

Code

public function testSendFail() {

  // Create and send an issue.
  $issue = Node::create([
    'type' => 'simplenews_issue',
    'title' => $this
      ->randomString(10),
    'simplenews_issue' => [
      'target_id' => $this
        ->getRandomNewsletter(),
    ],
  ]);
  $issue
    ->save();
  \Drupal::service('simplenews.spool_storage')
    ->addIssue($issue);

  // Force some mails to fail, then abort.
  \Drupal::messenger()
    ->deleteAll();
  $results_alter = [
    SpoolStorageInterface::STATUS_PENDING,
    SpoolStorageInterface::STATUS_FAILED,
    -1,
  ];
  $this->container
    ->get('state')
    ->set('simplenews.test_result_alter', $results_alter);
  simplenews_cron();

  // Check there is no error message.
  $this
    ->assertEqual(count(\Drupal::messenger()
    ->messagesByType(MessengerInterface::TYPE_ERROR)), 0, t('No error messages printed'));

  // Check the status on the newsletter tab.  The pending mail should be
  // retried
  $this
    ->drupalGet('node/1/simplenews');
  $this
    ->assertText('Newsletter issue is pending, 0 mails sent out of 5, 1 errors.');

  // Allow one mail to succeed, and the pending mail should be treated as an
  // error.
  $results_alter = [
    SpoolStorageInterface::STATUS_DONE,
    SpoolStorageInterface::STATUS_PENDING,
    SpoolStorageInterface::STATUS_FAILED,
  ];
  $this->container
    ->get('state')
    ->set('simplenews.test_result_alter', $results_alter);
  simplenews_cron();
  $this
    ->drupalGet('node/1/simplenews');
  $this
    ->assertText('Newsletter issue sent to 2 subscribers, 3 errors.');
}