You are here

public function MessageDigestTest::testNotifierDelivery in Message Digest 8

Tests the notifier sending and delivery.

@dataProvider providerTestNotifierDelivery

Parameters

bool $reference_entity: Whether or not an entity should be referenced in the message digest that is being sent.

string $expected_subject: The expected subject for the email that is being sent.

File

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

Class

MessageDigestTest
Kernel tests for Message Digest.

Namespace

Drupal\Tests\message_digest\Kernel

Code

public function testNotifierDelivery($reference_entity, $expected_subject) {

  // Set the site name, so we can check that it is used in the subject of the
  // digest e-mail.
  $this
    ->config('system.site')
    ->set('name', 'Test site')
    ->save();
  $template = $this
    ->createMessageTemplate('foo', 'Foo', 'Foo, foo', [
    'Test message',
  ]);
  $dummy = Message::create([
    'template' => $template
      ->id(),
  ]);

  /** @var \Drupal\message_digest\Plugin\Notifier\DigestInterface $digest_notifier */
  $digest_notifier = $this->notifierManager
    ->createInstance('message_digest:daily', [], $dummy);
  $configuration = [];

  // If we are referencing an entity, create a test user and reference it in
  // the message digest.
  if ($reference_entity) {
    $referenced_user = $this
      ->createUser([], 'Test user');
    $configuration = [
      'entity_type' => 'user',
      'entity_id' => $referenced_user
        ->id(),
    ];
  }

  // Create a recipient and send the message.
  $account = $this
    ->createUser();
  $dummy
    ->setOwner($account);
  $dummy
    ->save();
  $this->notifierSender
    ->send($dummy, $configuration, $digest_notifier
    ->getPluginId());
  $result = $this->container
    ->get('database')
    ->select('message_digest', 'm')
    ->fields('m')
    ->execute()
    ->fetchAllAssoc('id');
  $this
    ->assertEquals(1, count($result));
  foreach ($result as $row) {
    $this
      ->assertEquals($account
      ->id(), $row->receiver);
    $this
      ->assertEquals($digest_notifier
      ->getPluginId(), $row->notifier);
  }

  // Now deliver the message.
  $this
    ->sendDigests();
  $this
    ->assertMail('subject', $expected_subject, 'Expected email subject is set.');
  $this
    ->assertMail('body', "Test message\n\n", 'Expected email body is set.');
  $this
    ->assertMail('id', 'message_digest_digest', 'Expected email key is set.');
  $this
    ->assertMail('to', $account
    ->getEmail(), 'Expected email recipient is set.');

  // Verify that the aggregate alter hook was called.
  // @see message_digest_test_message_digest_aggregate_alter()
  $this
    ->assertTrue($this->container
    ->get('state')
    ->get('message_digest_test_message_digest_aggregate_alter', FALSE));

  // Verify that hook_message_digest_view_mode_alter() has been called.
  // @see message_digest_test_message_digest_view_mode_alter().
  $this
    ->assertTrue($this->container
    ->get('state')
    ->get('message_digest_test_message_digest_view_mode_alter', FALSE));
}