public function MessageDigestTest::testNotifierAggregation in Message Digest 8
Tests message aggregation.
File
- tests/
src/ Kernel/ MessageDigestTest.php, line 126
Class
- MessageDigestTest
- Kernel tests for Message Digest.
Namespace
Drupal\Tests\message_digest\KernelCode
public function testNotifierAggregation() {
// Send several messages.
$template = $this
->createMessageTemplate('foo', 'Foo', 'Foo, foo', []);
$account = $this
->createUser();
$message_1 = Message::create([
'template' => $template
->id(),
]);
$message_2 = Message::create([
'template' => $template
->id(),
]);
/** @var \Drupal\message_digest\Plugin\Notifier\DigestInterface $digest_notifier */
$digest_notifier = $this->notifierManager
->createInstance('message_digest:weekly', [], $message_1);
$message_1
->setOwner($account);
$message_1
->save();
$message_2
->setOwner($account);
$message_2
->save();
$this->notifierSender
->send($message_1, [], $digest_notifier
->getPluginId());
$this->notifierSender
->send($message_2, [], $digest_notifier
->getPluginId());
$result = $this->container
->get('database')
->select('message_digest', 'm')
->fields('m')
->execute()
->fetchAllAssoc('id');
$this
->assertEquals(2, count($result));
foreach ($result as $row) {
$this
->assertEquals($account
->id(), $row->receiver);
$this
->assertEquals($digest_notifier
->getPluginId(), $row->notifier);
}
// Aggregate and mark processed.
$start_time = $digest_notifier
->getEndTime();
$recipients = $digest_notifier
->getRecipients();
$this
->assertEquals(1, count($recipients));
foreach ($recipients as $uid) {
$results = $digest_notifier
->aggregate($uid, $start_time);
$digest_notifier
->setLastSent();
$expected = [
'' => [
'' => [
$message_1
->id(),
$message_2
->id(),
],
],
];
$this
->assertSame($expected, $results);
}
// Since this has been marked as sent, the notifier should return FALSE
// for processing again.
$this
->assertFalse($digest_notifier
->processDigest());
// Set last sent time to 8 days in the past.
$last_run = strtotime('-8 days', $this->container
->get('datetime.time')
->getRequestTime());
$this->container
->get('state')
->set($digest_notifier
->getPluginId() . '_last_run', $last_run);
$results = $digest_notifier
->aggregate($account
->id(), $start_time);
$this
->assertSame($expected, $results);
// Aggregate should not return any results once marked sent.
$digest_notifier
->markSent($account, $message_2
->id());
$this
->assertEmpty($digest_notifier
->getRecipients());
}