You are here

public function SimplenewsSourceTest::testSendNoCaching in Simplenews 8.2

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

Test with disabled caching.

File

tests/src/Functional/SimplenewsSourceTest.php, line 306

Class

SimplenewsSourceTest
Test cases for creating and sending newsletters.

Namespace

Drupal\Tests\simplenews\Functional

Code

public function testSendNoCaching() {
  $this
    ->setUpSubscribers(100);

  // Disable caching.
  $yaml = new Yaml();
  $directory = DRUPAL_ROOT . '/' . $this->siteDirectory;
  $content = file_get_contents($directory . '/services.yml');
  $services = $yaml
    ->parse($content);
  $services['services']['simplenews.mail_cache'] = [
    'class' => 'Drupal\\simplenews\\Mail\\MailCacheNone',
  ];
  file_put_contents($directory . '/services.yml', $yaml
    ->dump($services));
  $this
    ->rebuildContainer();
  \Drupal::moduleHandler()
    ->loadAll();
  $edit = [
    'title[0][value]' => $this
      ->randomString(10),
    'body[0][value]' => "Mail token: <strong>[simplenews-subscriber:mail]</strong>",
    'simplenews_issue[target_id]' => 'default',
  ];
  $this
    ->drupalPostForm('node/add/simplenews_issue', $edit, 'Save');
  $this
    ->assertEqual(1, preg_match('|node/(\\d+)$|', $this
    ->getUrl(), $matches), 'Node created');
  $node = Node::load($matches[1]);

  // Add node to spool.
  \Drupal::service('simplenews.spool_storage')
    ->addIssue($node);
  $before = microtime(TRUE);
  \Drupal::service('simplenews.mailer')
    ->sendSpool();
  $after = microtime(TRUE);

  // Make sure that 100 mails have been sent.
  $this
    ->assertEqual(100, count($this
    ->getMails()));

  // Test that tokens are correctly replaced.
  foreach (array_slice($this
    ->getMails(), 0, 3) as $mail) {

    // Make sure that the same mail was used in the body token as it has been
    // sent to. Also verify that the mail is plaintext.
    $this
      ->assertTrue(strpos($mail['body'], '*' . $mail['to'] . '*') !== FALSE);
    $this
      ->assertFalse(strpos($mail['body'], '<strong>'));

    // Make sure the body is only attached once.
    $this
      ->assertEqual(1, preg_match_all('/Mail token/', $mail['body'], $matches));
  }

  // Report time. @todo: Find a way to actually do some assertions here.
  $this
    ->pass(t('Mails have been sent in @sec seconds with caching disabled.', [
    '@sec' => round($after - $before, 3),
  ]));
}