You are here

public function MessageDigestTest::testMessageCleanup in Message Digest 8

Test old message purging.

File

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

Class

MessageDigestTest
Kernel tests for Message Digest.

Namespace

Drupal\Tests\message_digest\Kernel

Code

public function testMessageCleanup() {
  $template = $this
    ->createMessageTemplate('foo', 'Foo', 'Foo, foo', []);
  $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;
  }
  $digest = $digest_notifier
    ->aggregate($account
    ->id(), $digest_notifier
    ->getEndTime());
  $this
    ->assertEquals(10, count($digest['']['']));
  $digest_notifier
    ->markSent($account, $messages[10]
    ->id());

  // Delete 5 messages.
  foreach (range(1, 5) as $i) {
    $messages[$i]
      ->delete();
  }
  $this->digestManager
    ->cleanupOldMessages();
  $result = $this->container
    ->get('database')
    ->select('message_digest', 'md')
    ->fields('md')
    ->execute()
    ->fetchAllAssoc('id');
  $this
    ->assertEquals(5, count($result));
  foreach ($result as $row) {
    $this
      ->assertGreaterThan(5, $row->mid);
  }
}