You are here

public function MessageDigestTest::testDigestGrouping in Message Digest 8

Test grouping by entity type, and ID.

File

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

Class

MessageDigestTest
Kernel tests for Message Digest.

Namespace

Drupal\Tests\message_digest\Kernel

Code

public function testDigestGrouping() {
  $template = $this
    ->createMessageTemplate('foo', 'Foo', 'Foo, foo', []);
  $account = $this
    ->createUser();

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

  // Now send some grouped by entity type.
  $configuration = [
    'entity_type' => 'foo',
    'entity_id' => 'bar',
  ];
  $grouped_messages = [];
  foreach (range(1, 3) as $i) {
    $message = Message::create([
      'template' => $template
        ->id(),
    ]);
    $message
      ->setOwner($account);
    $message
      ->save();
    $grouped_messages[$i] = $message;

    /** @var \Drupal\message_digest\Plugin\Notifier\DigestInterface $digest_notifier */
    $digest_notifier = $this->notifierManager
      ->createInstance('message_digest:weekly', $configuration, $message);
    $this->notifierSender
      ->send($message, $configuration, $digest_notifier
      ->getPluginId());
  }

  // Aggregate and mark processed.
  $results = $digest_notifier
    ->aggregate($account
    ->id(), $digest_notifier
    ->getEndTime());
  $digest_notifier
    ->setLastSent();
  $expected = [
    '' => [
      '' => [
        '1',
        '2',
        '3',
      ],
    ],
    'foo' => [
      'bar' => [
        '4',
        '5',
        '6',
      ],
    ],
  ];
  $this
    ->assertSame($expected, $results);
}