View source
<?php
namespace Drupal\Tests\message_digest_ui\Kernel;
use Drupal\message\Entity\Message;
use Drupal\message_subscribe\Subscribers\DeliveryCandidate;
use Drupal\Tests\message_subscribe_email\Kernel\MessageSubscribeEmailTestBase;
class MessageDigestUiTest extends MessageSubscribeEmailTestBase {
public static $modules = [
'message_digest',
'message_digest_ui',
'options',
'field',
];
public function setUp() {
parent::setUp();
$this
->installConfig([
'message_digest_ui',
'message_subscribe_email',
'message_subscribe',
]);
$this->messageSubscribers = $this->container
->get('message_subscribe.subscribers');
$permissions = [
'flag subscribe_node',
'unflag subscribe_node',
'flag email_node',
'unflag email_node',
];
foreach (range(3, 4) as $i) {
$this->users[$i] = $this
->createUser($permissions);
}
$type = $this->nodes[1]
->getType();
$this->nodes[2] = $this
->createNode([
'type' => $type,
]);
}
public function testNotificationsDefault() {
$this
->assertEmpty($this->users[2]->message_digest->value);
$flag = $this->flagService
->getFlagById('subscribe_node');
$this->flagService
->flag($flag, $this->nodes[1], $this->users[2]);
$message = Message::create([
'template' => $this->messageTemplate
->id(),
]);
$uids = $this->messageSubscribers
->getSubscribers($this->nodes[1], $message);
$expected_uids = [
$this->users[2]
->id() => new DeliveryCandidate([
'subscribe_node',
], [
'email',
], $this->users[2]
->id()),
];
$this
->assertEquals($expected_uids, $uids, 'All expected subscribers were fetched.');
}
public function testNotifiersDigest() {
$this->users[2]->message_digest = 'message_digest:daily';
$this->users[2]
->save();
$this
->assertEquals('message_digest:daily', $this->users[2]->message_digest->value);
$flag = $this->flagService
->getFlagById('subscribe_node');
$this->container
->set('current_user', $this->users[2]);
$this->flagService
->flag($flag, $this->nodes[1], $this->users[2]);
$this->container
->set('current_user', $this->users[3]);
$this->flagService
->flag($flag, $this->nodes[1], $this->users[3]);
$this->flagService
->flag($flag, $this->nodes[2], $this->users[3]);
$flaggings = $this->flagService
->getAllEntityFlaggings($this->nodes[2], $this->users[3]);
$this
->assertEquals(2, count($flaggings));
$this
->assertEmpty($flaggings[6]->message_digest->value);
$flaggings[6]->message_digest = 'message_digest:daily';
$flaggings[6]
->save();
$flaggings = $this->flagService
->getAllEntityFlaggings($this->nodes[1], $this->users[2]);
$this
->assertEquals(2, count($flaggings));
$email_flag = $flaggings[2];
$this
->assertEquals('message_digest:daily', $email_flag->message_digest->value);
$expected_uids = [
$this->users[2]
->id() => new DeliveryCandidate([
'subscribe_node',
], [
'message_digest:daily',
], $this->users[2]
->id()),
$this->users[3]
->id() => new DeliveryCandidate([
'subscribe_node',
], [
'email',
], $this->users[3]
->id()),
];
$message = Message::create([
'template' => $this->messageTemplate
->id(),
]);
$uids = $this->messageSubscribers
->getSubscribers($this->nodes[1], $message);
$this
->assertEquals($expected_uids, $uids, 'All expected subscribers were fetched.');
}
public function testWithContexts() {
$this->users[2]->message_digest = 'message_digest:daily';
$this->users[2]
->save();
$this
->assertEquals('message_digest:daily', $this->users[2]->message_digest->value);
$this->users[3]->message_digest = 'message_digest:daily';
$this->users[3]
->save();
$flag = $this->flagService
->getFlagById('subscribe_node');
$this->container
->set('current_user', $this->users[2]);
$this->flagService
->flag($flag, $this->nodes[1], $this->users[2]);
$this->container
->set('current_user', $this->users[3]);
$this->flagService
->flag($flag, $this->nodes[1], $this->users[3]);
$this->flagService
->flag($flag, $this->nodes[2], $this->users[3]);
$this->flagService
->flag($flag, $this->nodes[2], $this->users[2]);
$flaggings = $this->flagService
->getAllEntityFlaggings($this->nodes[1], $this->users[2]);
$flaggings[2]->message_digest = '0';
$flaggings[2]
->save();
$flaggings = $this->flagService
->getAllEntityFlaggings($this->nodes[1], $this->users[3]);
$flaggings[4]->message_digest = 'message_digest:weekly';
$flaggings[4]
->save();
$context = [
'node' => [
$this->nodes[1]
->id(),
$this->nodes[2]
->id(),
],
];
$expected_uids = [
$this->users[2]
->id() => new DeliveryCandidate([
'subscribe_node',
], [
'email',
], $this->users[2]
->id()),
$this->users[3]
->id() => new DeliveryCandidate([
'subscribe_node',
], [
'message_digest:daily',
], $this->users[3]
->id()),
];
$message = Message::create([
'template' => $this->messageTemplate
->id(),
]);
$uids = $this->messageSubscribers
->getSubscribers($this->nodes[1], $message, [], $context);
$this
->assertEquals($expected_uids, $uids, 'All expected subscribers were fetched.');
}
}