You are here

public function MessageDigestTest::testManagerProcessing in Message Digest 8

Tests the message digest manager processing.

File

tests/src/Kernel/MessageDigestTest.php, line 375

Class

MessageDigestTest
Kernel tests for Message Digest.

Namespace

Drupal\Tests\message_digest\Kernel

Code

public function testManagerProcessing() {
  $this->digestManager
    ->processDigests();
  $request_time = $this->container
    ->get('datetime.time')
    ->getRequestTime();
  $this
    ->assertEquals($request_time, $this->container
    ->get('state')
    ->get('message_digest:daily_last_run'));
  $this
    ->assertEquals($request_time, $this->container
    ->get('state')
    ->get('message_digest:weekly_last_run'));
  $this
    ->assertEmpty($this
    ->getMails());

  // Actually add some messages now, and reset last sent time.
  $last_run = strtotime('-8 days', $this->container
    ->get('datetime.time')
    ->getRequestTime());
  $this->container
    ->get('state')
    ->set('message_digest:weekly_last_run', $last_run);
  $template = $this
    ->createMessageTemplate('foo', 'Foo', 'Foo, foo', $this
    ->getMessageText());
  $account = $this
    ->createUser();
  $messages = [];

  // Send 10 messages.
  foreach (range(1, 10) as $i) {
    $message = Message::create([
      'template' => $template
        ->id(),
    ]);
    $message
      ->setOwner($account);
    $message
      ->save();
    $digest_notifier = $this->notifierManager
      ->createInstance('message_digest:weekly', [], $message);
    $this->notifierSender
      ->send($message, [], $digest_notifier
      ->getPluginId());
    $messages[$i] = $message;
  }

  // Ensure cron function actually calls the process method.
  $this->container
    ->get('cron')
    ->run();
  $emails = $this
    ->getMails();
  $this
    ->assertEquals(1, count($emails));
  $this
    ->assertMail('to', $account
    ->getEmail());

  // Since the core mail service converts HTML to text, that should be be the
  // case here too.
  $expected = MailFormatHelper::wrapMail(MailFormatHelper::htmlToText("  <div>\n    <div>\n  Test subject\n</div>\n\n  </div>\n  <div>\n    <div>\n  <div class=\"foo-bar\">Some sweet <a href=\"http://localhost/\">message</a>.\n</div>\n\n  </div>\n<hr />  <div>\n    <div>\n  Test subject\n</div>\n\n  </div>\n  <div>\n    <div>\n  <div class=\"foo-bar\">Some sweet <a href=\"http://localhost/\">message</a>.\n</div>\n\n  </div>\n<hr />  <div>\n    <div>\n  Test subject\n</div>\n\n  </div>\n  <div>\n    <div>\n  <div class=\"foo-bar\">Some sweet <a href=\"http://localhost/\">message</a>.\n</div>\n\n  </div>\n<hr />  <div>\n    <div>\n  Test subject\n</div>\n\n  </div>\n  <div>\n    <div>\n  <div class=\"foo-bar\">Some sweet <a href=\"http://localhost/\">message</a>.\n</div>\n\n  </div>\n<hr />  <div>\n    <div>\n  Test subject\n</div>\n\n  </div>\n  <div>\n    <div>\n  <div class=\"foo-bar\">Some sweet <a href=\"http://localhost/\">message</a>.\n</div>\n\n  </div>\n<hr />  <div>\n    <div>\n  Test subject\n</div>\n\n  </div>\n  <div>\n    <div>\n  <div class=\"foo-bar\">Some sweet <a href=\"http://localhost/\">message</a>.\n</div>\n\n  </div>\n<hr />  <div>\n    <div>\n  Test subject\n</div>\n\n  </div>\n  <div>\n    <div>\n  <div class=\"foo-bar\">Some sweet <a href=\"http://localhost/\">message</a>.\n</div>\n\n  </div>\n<hr />  <div>\n    <div>\n  Test subject\n</div>\n\n  </div>\n  <div>\n    <div>\n  <div class=\"foo-bar\">Some sweet <a href=\"http://localhost/\">message</a>.\n</div>\n\n  </div>\n<hr />  <div>\n    <div>\n  Test subject\n</div>\n\n  </div>\n  <div>\n    <div>\n  <div class=\"foo-bar\">Some sweet <a href=\"http://localhost/\">message</a>.\n</div>\n\n  </div>\n<hr />  <div>\n    <div>\n  Test subject\n</div>\n\n  </div>\n  <div>\n    <div>\n  <div class=\"foo-bar\">Some sweet <a href=\"http://localhost/\">message</a>.\n</div>\n\n  </div>\n\n"));
  $email = reset($emails);
  $this
    ->assertEquals($expected, $email['body']);
}