You are here

public function QueueTest::testQueue in Message Subscribe 8

Test base queue processing logic.

File

tests/src/Kernel/QueueTest.php, line 63

Class

QueueTest
Test queue integration.

Namespace

Drupal\Tests\message_subscribe\Kernel

Code

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.');
  }

  // Assert message was saved and added to 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.');

  // Assert queue-item is processed and updated. We mock subscription
  // of users to the message. It will not be sent, as the default
  // notifier is disabled.
  $item = $queue
    ->claimItem();
  $item_id = $item->item_id;

  // Add the queue information, and the user IDs to process.
  $subscribe_options['queue'] = [
    'uids' => $uids,
    'item' => $item,
    'end time' => FALSE,
  ];
  $this->messageSubscribers
    ->sendMessage($node, $message, [], $subscribe_options);

  // Reclaim the new item, and assert the "last UID" was updated.
  $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.');
}