You are here

public function TokenNotificationsTest::testTokens in Content Moderation Notifications 8.3

Test token functionality.

File

tests/src/Kernel/TokenNotificationsTest.php, line 53

Class

TokenNotificationsTest
Tests with the contrib token module enabled.

Namespace

Drupal\Tests\content_moderation_notifications\Kernel

Code

public function testTokens() {

  // Add a notification.
  $notification = $this
    ->createNotification([
    'emails' => 'foo@example.com, bar@example.com',
    'transitions' => [
      'create_new_draft' => 'create_new_draft',
      'publish' => 'publish',
      'archived_published' => 'archived_published',
    ],
    'body' => [
      'value' => 'Test token replacement [node:title]. [content_moderation_notifications:from-state] | [content_moderation_notifications:workflow] | [content_moderation_notifications:to-state]!',
      'format' => 'filtered_html',
    ],
  ]);
  $entity = $this
    ->createNode([
    'type' => 'article',
  ]);
  $this
    ->assertMail('to', 'admin@example.com');
  $this
    ->assertBccRecipients('foo@example.com,bar@example.com');
  $this
    ->assertMail('id', 'content_moderation_notifications_content_moderation_notification');
  $this
    ->assertMail('subject', PlainTextOutput::renderFromHtml($notification
    ->getSubject()));
  $this
    ->assertCount(1, $this
    ->getMails());

  // Verify token replacement.
  $mail = $this
    ->getMails()[0];
  $this
    ->assertEquals('Test token replacement ' . $entity
    ->label() . ". Draft | Editorial | Draft!\n", $mail['body']);

  // Publish.
  $this->container
    ->get('state')
    ->set('system.test_mail_collector', []);
  $entity->moderation_state = 'published';
  $entity
    ->save();
  $mail = $this
    ->getMails()[0];
  $this
    ->assertEquals('Test token replacement ' . $entity
    ->label() . ". Draft | Editorial | Published!\n", $mail['body']);
}