View source
<?php
namespace Drupal\Tests\message_digest\Kernel;
use Drupal\Core\Mail\MailFormatHelper;
use Drupal\message\Entity\Message;
use Drupal\message_digest\Exception\InvalidDigestGroupingException;
class MessageDigestTest extends DigestTestBase {
public function testDigestNotifierPluginsExist() {
$count = 0;
foreach ($this->notifierManager
->getDefinitions() as $plugin_id => $plugin_definition) {
if ($plugin_definition['provider'] === 'message_digest') {
$dummy = Message::create([
'template' => 'foo',
]);
$this->notifierManager
->createInstance($plugin_id, [], $dummy);
$count++;
}
}
$this
->assertEquals(2, $count, 'There are 2 digest notifiers.');
}
public function testNotifierDelivery($reference_entity, $expected_subject) {
$this
->config('system.site')
->set('name', 'Test site')
->save();
$template = $this
->createMessageTemplate('foo', 'Foo', 'Foo, foo', [
'Test message',
]);
$dummy = Message::create([
'template' => $template
->id(),
]);
$digest_notifier = $this->notifierManager
->createInstance('message_digest:daily', [], $dummy);
$configuration = [];
if ($reference_entity) {
$referenced_user = $this
->createUser([], 'Test user');
$configuration = [
'entity_type' => 'user',
'entity_id' => $referenced_user
->id(),
];
}
$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);
}
$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.');
$this
->assertTrue($this->container
->get('state')
->get('message_digest_test_message_digest_aggregate_alter', FALSE));
$this
->assertTrue($this->container
->get('state')
->get('message_digest_test_message_digest_view_mode_alter', FALSE));
}
public function providerTestNotifierDelivery() {
return [
[
FALSE,
'Test site message digest',
],
[
TRUE,
'Test user message digest',
],
];
}
public function testNotifierAggregation() {
$template = $this
->createMessageTemplate('foo', 'Foo', 'Foo, foo', []);
$account = $this
->createUser();
$message_1 = Message::create([
'template' => $template
->id(),
]);
$message_2 = Message::create([
'template' => $template
->id(),
]);
$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);
}
$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);
}
$this
->assertFalse($digest_notifier
->processDigest());
$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);
$digest_notifier
->markSent($account, $message_2
->id());
$this
->assertEmpty($digest_notifier
->getRecipients());
}
public function testDigestGrouping() {
$template = $this
->createMessageTemplate('foo', 'Foo', 'Foo, foo', []);
$account = $this
->createUser();
$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());
}
$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;
$digest_notifier = $this->notifierManager
->createInstance('message_digest:weekly', $configuration, $message);
$this->notifierSender
->send($message, $configuration, $digest_notifier
->getPluginId());
}
$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);
}
protected function getMessageText() {
$text = [];
$text[] = [
'value' => 'Test subject',
'format' => 'filtered_html',
];
$text[] = [
'value' => '<div class="foo-bar">Some sweet <a href="[site:url]">message</a>.',
'format' => 'full_html',
];
return $text;
}
public function testInvalidEntityType() {
$configuration = [
'entity_id' => 42,
];
$template = $this
->createMessageTemplate('foo', 'Foo', 'Foo, foo', []);
$message = Message::create([
'template' => $template
->id(),
]);
$digest_notifier = $this->notifierManager
->createInstance('message_digest:weekly', $configuration, $message);
$this
->expectException(InvalidDigestGroupingException::class);
$this
->expectExceptionMessage('Tried to create a message digest without both entity_type () and entity_id (42). These either both need to be empty, or have values.');
$this->notifierSender
->send($message, $configuration, $digest_notifier
->getPluginId());
}
public function testInvalidEntityId() {
$configuration = [
'entity_type' => 'foo',
];
$template = $this
->createMessageTemplate('foo', 'Foo', 'Foo, foo', []);
$message = Message::create([
'template' => $template
->id(),
]);
$digest_notifier = $this->notifierManager
->createInstance('message_digest:weekly', $configuration, $message);
$this
->expectException(InvalidDigestGroupingException::class);
$this
->expectExceptionMessage('Tried to create a message digest without both entity_type (foo) and entity_id (). These either both need to be empty, or have values.');
$this->notifierSender
->send($message, $configuration, $digest_notifier
->getPluginId());
}
public function testMessageCleanup() {
$template = $this
->createMessageTemplate('foo', 'Foo', 'Foo, foo', []);
$account = $this
->createUser();
$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());
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);
}
}
public function testOrphanedEntityReferences() {
$template = $this
->createMessageTemplate('foo', 'Foo', 'Foo, foo', []);
$users = $messages = [];
for ($i = 0; $i < 3; $i++) {
$user = $this
->createUser();
$message = Message::create([
'template' => $template
->id(),
]);
$message
->setOwner($user);
$message
->save();
$digest_notifier = $this->notifierManager
->createInstance('message_digest:weekly', [], $message);
$this->notifierSender
->send($message, [], $digest_notifier
->getPluginId());
$users[$i] = $user;
$messages[$i] = $message;
}
$this
->assertRowCount(3);
$orphaned_message_id = $messages[0]
->id();
$messages[0]
->delete();
$this
->assertRowCount(2);
foreach ($this
->getAllMessageDigests() as $digest) {
if ($digest->mid == $orphaned_message_id) {
$this
->fail('When a message is deleted its corresponding message digest is cleaned up.');
}
}
$orphaned_user_id = $users[1]
->id();
$users[1]
->delete();
$this
->assertRowCount(1);
foreach ($this
->getAllMessageDigests() as $digest) {
if ($digest->receiver == $orphaned_user_id) {
$this
->fail('When a user is deleted its corresponding message digest is cleaned up.');
}
}
}
public function testManagerProcessing() {
$this->digestManager
->processDigests();
$request_time = $this->container
->get('datetime.time')
->getRequestTime();
$this
->assertEquals($request_time, $this->container
->get('state')
->get('message_digest:daily_last_run'));
$this
->assertEquals($request_time, $this->container
->get('state')
->get('message_digest:weekly_last_run'));
$this
->assertEmpty($this
->getMails());
$last_run = strtotime('-8 days', $this->container
->get('datetime.time')
->getRequestTime());
$this->container
->get('state')
->set('message_digest:weekly_last_run', $last_run);
$template = $this
->createMessageTemplate('foo', 'Foo', 'Foo, foo', $this
->getMessageText());
$account = $this
->createUser();
$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;
}
$this->container
->get('cron')
->run();
$emails = $this
->getMails();
$this
->assertEquals(1, count($emails));
$this
->assertMail('to', $account
->getEmail());
$expected = MailFormatHelper::wrapMail(MailFormatHelper::htmlToText(" <div>\n <div>\n Test subject\n</div>\n\n </div>\n <div>\n <div>\n <div class=\"foo-bar\">Some sweet <a href=\"http://localhost/\">message</a>.\n</div>\n\n </div>\n<hr /> <div>\n <div>\n Test subject\n</div>\n\n </div>\n <div>\n <div>\n <div class=\"foo-bar\">Some sweet <a href=\"http://localhost/\">message</a>.\n</div>\n\n </div>\n<hr /> <div>\n <div>\n Test subject\n</div>\n\n </div>\n <div>\n <div>\n <div class=\"foo-bar\">Some sweet <a href=\"http://localhost/\">message</a>.\n</div>\n\n </div>\n<hr /> <div>\n <div>\n Test subject\n</div>\n\n </div>\n <div>\n <div>\n <div class=\"foo-bar\">Some sweet <a href=\"http://localhost/\">message</a>.\n</div>\n\n </div>\n<hr /> <div>\n <div>\n Test subject\n</div>\n\n </div>\n <div>\n <div>\n <div class=\"foo-bar\">Some sweet <a href=\"http://localhost/\">message</a>.\n</div>\n\n </div>\n<hr /> <div>\n <div>\n Test subject\n</div>\n\n </div>\n <div>\n <div>\n <div class=\"foo-bar\">Some sweet <a href=\"http://localhost/\">message</a>.\n</div>\n\n </div>\n<hr /> <div>\n <div>\n Test subject\n</div>\n\n </div>\n <div>\n <div>\n <div class=\"foo-bar\">Some sweet <a href=\"http://localhost/\">message</a>.\n</div>\n\n </div>\n<hr /> <div>\n <div>\n Test subject\n</div>\n\n </div>\n <div>\n <div>\n <div class=\"foo-bar\">Some sweet <a href=\"http://localhost/\">message</a>.\n</div>\n\n </div>\n<hr /> <div>\n <div>\n Test subject\n</div>\n\n </div>\n <div>\n <div>\n <div class=\"foo-bar\">Some sweet <a href=\"http://localhost/\">message</a>.\n</div>\n\n </div>\n<hr /> <div>\n <div>\n Test subject\n</div>\n\n </div>\n <div>\n <div>\n <div class=\"foo-bar\">Some sweet <a href=\"http://localhost/\">message</a>.\n</div>\n\n </div>\n\n"));
$email = reset($emails);
$this
->assertEquals($expected, $email['body']);
}
public function testDigestSerialization() {
foreach ([
'daily',
'weekly',
] as $interval) {
$plugin_id = "message_digest:{$interval}";
$dummy = Message::create([
'template' => 'foo',
]);
$notifier = $this->notifierManager
->createInstance($plugin_id, [], $dummy);
$unserialized_notifier = unserialize(serialize($notifier));
$this
->assertEquals($plugin_id, $unserialized_notifier
->getPluginId());
}
}
public function testOrphanedMessage() {
$user = $this
->createUser();
$template = $this
->createMessageTemplate('foo', 'Foo', 'Foo, foo', []);
$message = Message::create([
'template' => $template
->id(),
]);
$digest_notifier = $this->notifierManager
->createInstance('message_digest:daily', [], $message);
$message
->setOwner($user);
$message
->save();
$user
->delete();
$this->notifierSender
->send($message, [], $digest_notifier
->getPluginId());
$this
->sendDigests();
$this
->assertEmpty($this
->getMails());
}
protected function getAllMessageDigests() {
return $this->container
->get('database')
->select('message_digest', 'm')
->fields('m')
->execute()
->fetchAllAssoc('id');
}
protected function assertRowCount($expected_count) {
$this
->assertEquals($expected_count, count($this
->getAllMessageDigests()));
}
}