You are here

public function SubscribersTest::testHooks in Message Subscribe 8

Ensure hooks are firing correctly.

File

tests/src/Kernel/SubscribersTest.php, line 266

Class

SubscribersTest
Test getting subscribes from context.

Namespace

Drupal\Tests\message_subscribe\Kernel

Code

public function testHooks() {
  $this
    ->enableModules([
    'message_subscribe_test',
  ]);
  $message = Message::create([
    'template' => $this->template
      ->id(),
    'uid' => $this->users[1],
  ]);

  // Create a 4th user that the test module will add.
  $this->users[4] = $this
    ->createUser();
  $node = $this->nodes[0];
  $uids = $this->messageSubscribers
    ->getSubscribers($node, $message);

  // @see message_subscribe_test.module
  $this
    ->assertTrue(\Drupal::state('message_subscribe_test')
    ->get('hook_called'));
  $this
    ->assertTrue(\Drupal::state('message_subscribe_test')
    ->get('alter_hook_called'));
  $this
    ->assertEquals([
    4 => new DeliveryCandidate([
      'foo_flag',
    ], [
      'email',
    ], 4),
    10001 => new DeliveryCandidate([
      'bar_flag',
    ], [
      'email',
    ], 10001),
  ], $uids);

  // Disable the test module from adding a fake user.
  \Drupal::state('message_subscribe_test')
    ->set('disable_subscribers_alter', TRUE);

  // Send a message and verify the message alter hook is called (should be
  // called once for each subscriber, so 2 times).
  $this->messageSubscribers
    ->sendMessage($node, $message, [], [
    'entity access' => FALSE,
  ]);
  $this
    ->assertEquals(2, \Drupal::state('message_subscribe_test')
    ->get('message_alter_hook_called', FALSE));
}