You are here

function SimplenewsSendTest::testNewsletterTheme in Simplenews 8

Test the theme suggestions when sending mails.

File

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

Class

SimplenewsSendTest
Test cases for creating and sending newsletters.

Namespace

Drupal\simplenews\Tests

Code

function testNewsletterTheme() {

  // Install and enable the test theme.
  \Drupal::service('theme_handler')
    ->install(array(
    'simplenews_newsletter_test_theme',
  ));
  \Drupal::theme()
    ->setActiveTheme(\Drupal::service('theme.initialization')
    ->initTheme('simplenews_newsletter_test_theme'));
  $node = Node::create(array(
    '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')
    ->addFromEntity($node);
  $node
    ->save();

  // 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();
  $mails = $this
    ->drupalGetMails();

  // Check if the correct theme was used in mails.
  $this
    ->assertTrue(strpos($mails[0]['body'], 'Simplenews test theme') != FALSE);
  $this
    ->assertTrue(preg_match('/ID: [0-9]/', $mails[0]['body']), 'Mail contains the subscriber ID');
}