View source
<?php
namespace Drupal\Tests\message_subscribe_email\Kernel;
use Drupal\Core\Test\AssertMailTrait;
use Drupal\message\Entity\Message;
use Drupal\message_subscribe\Subscribers\DeliveryCandidate;
class MessageSubscribeEmailSubscribersTest extends MessageSubscribeEmailTestBase {
use AssertMailTrait;
public function setUp() {
parent::setUp();
$flag = $this->flagService
->getFlagById('subscribe_node');
foreach (range(1, 2) as $i) {
$this->users[$i]->message_subscribe_email = 0;
$this->users[$i]
->save();
$this->flagService
->flag($flag, $this->nodes[1], $this->users[$i]);
}
$flag = $this->flagService
->getFlagById('email_node');
$this->flagService
->flag($flag, $this->nodes[1], $this->users[1]);
}
public function testGetSubscribers() {
$message = Message::create([
'template' => $this->messageTemplate
->id(),
]);
$node = $this->nodes[1];
$user1 = $this->users[1];
$user2 = $this->users[2];
$uids = $this->messageSubscribers
->getSubscribers($node, $message);
$expected_uids = [
$user1
->id() => new DeliveryCandidate([
'subscribe_node',
], [
'email',
], $user1
->id()),
$user2
->id() => new DeliveryCandidate([
'subscribe_node',
], [], $user2
->id()),
];
$this
->assertEquals($expected_uids, $uids, 'All expected subscribers were fetched.');
$subscribe_options = [
'uids' => $uids,
];
$this->messageSubscribers
->sendMessage($node, $message, [], $subscribe_options);
$mails = $this
->getMails();
$this
->assertEquals(1, count($mails), 'Only one user was sent an email.');
$this
->assertEquals('message_notify_' . $this->messageTemplate
->id(), $mails[0]['id']);
}
public function testWithDefaultNotifiers() {
$this
->config('message_subscribe.settings')
->set('default_notifiers', [
'email',
])
->save();
$message = Message::create([
'template' => $this->messageTemplate
->id(),
]);
$node = $this->nodes[1];
$user1 = $this->users[1];
$user2 = $this->users[2];
$uids = $this->messageSubscribers
->getSubscribers($node, $message);
$expected_uids = [
$user1
->id() => new DeliveryCandidate([
'subscribe_node',
], [
'email',
], $user1
->id()),
$user2
->id() => new DeliveryCandidate([
'subscribe_node',
], [], $user2
->id()),
];
$this
->assertEquals($expected_uids, $uids, 'All expected subscribers were fetched.');
$subscribe_options = [
'uids' => $uids,
];
$this->messageSubscribers
->sendMessage($node, $message, [], $subscribe_options);
$mails = $this
->getMails();
$this
->assertEquals(1, count($mails), 'Only user 1 was sent an email.');
$this
->assertEquals('message_notify_' . $this->messageTemplate
->id(), $mails[0]['id']);
}
}