View source
<?php
namespace Drupal\Tests\message_subscribe\Kernel;
use Drupal\message\Entity\Message;
use Drupal\message\Entity\MessageTemplate;
use Drupal\message_subscribe\Exception\MessageSubscribeException;
use Drupal\message_subscribe\Subscribers\DeliveryCandidate;
class QueueTest extends MessageSubscribeTestBase {
protected $node;
public function setUp() {
parent::setUp();
$this
->config('message_subscribe.settings')
->set('default_notifiers', [])
->set('use_queue', TRUE)
->save();
$this->messageSubscribers = $this->container
->get('message_subscribe.subscribers');
$message_type = MessageTemplate::create([
'template' => 'foo',
'message_text' => [
'value' => 'Example text.',
],
]);
$message_type
->save();
$type = $this
->createContentType();
$node_type = $type
->id();
$user1 = $this
->createUser();
$settings = [];
$settings['type'] = $node_type;
$settings['uid'] = $user1
->id();
$this->node = $this
->createNode($settings);
}
public function testQueue() {
$node = $this->node;
$message = Message::create([
'template' => 'foo',
]);
$subscribe_options = [];
$subscribe_options['save message'] = FALSE;
try {
$this->messageSubscribers
->sendMessage($node, $message, [], $subscribe_options);
$this
->fail('Can add a non-saved message to the queue.');
} catch (MessageSubscribeException $e) {
$this
->assertTrue(TRUE, 'Cannot add a non-saved message to the queue.');
}
$uids = array_fill(1, 10, new DeliveryCandidate([], [], 1));
foreach ($uids as $uid => $candidate) {
$candidate
->setAccountId($uid);
}
$subscribe_options = [
'uids' => $uids,
'skip context' => TRUE,
'range' => 3,
];
$queue = \Drupal::queue('message_subscribe');
$this
->assertEquals($queue
->numberOfItems(), 0, 'Queue is empty');
$this->messageSubscribers
->sendMessage($node, $message, [], $subscribe_options);
$this
->assertTrue((bool) $message
->id(), 'Message was saved');
$this
->assertEquals($queue
->numberOfItems(), 1, 'Message added to queue.');
$item = $queue
->claimItem();
$item_id = $item->item_id;
$subscribe_options['queue'] = [
'uids' => $uids,
'item' => $item,
'end time' => FALSE,
];
$this->messageSubscribers
->sendMessage($node, $message, [], $subscribe_options);
$item = $queue
->claimItem();
$this
->assertNotEquals($item_id, $item->item_id, 'Queue item was updated.');
$this
->assertEquals($item->data['subscribe_options']['last uid'], 3, 'Last processed user ID was updated.');
}
public function testQueueCron() {
$node = $this->node;
$message = Message::create([
'template' => 'foo',
]);
$queue = \Drupal::queue('message_subscribe');
$this->messageSubscribers
->sendMessage($node, $message, [], []);
$this
->assertEquals($queue
->numberOfItems(), 1, 'Message item 1 added to queue.');
$this->container
->get('cron')
->run();
$this
->assertEquals($queue
->numberOfItems(), 0, 'Message item 1 processed by cron.');
$this->messageSubscribers
->sendMessage($node, $message, [], []);
$this
->assertEquals($queue
->numberOfItems(), 1, 'Message item 2 added to queue.');
$message
->delete();
$this
->assertNull($message
->load($message
->id()), 'Message entity deleted.');
$this->container
->get('cron')
->run();
$this
->assertEquals($queue
->numberOfItems(), 0, 'Message item 2 processed by cron.');
}
}