You are here

public function SubscribersTest::testGetSubscribers in Message Subscribe 8

Test getting the subscribers list.

File

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

Class

SubscribersTest
Test getting subscribes from context.

Namespace

Drupal\Tests\message_subscribe\Kernel

Code

public function testGetSubscribers() {
  $message = Message::create([
    'template' => $this->template
      ->id(),
    'uid' => $this->users[1],
  ]);
  $node = $this->nodes[0];
  $user2 = $this->users[2];
  $user_blocked = $this->users[3];
  $uids = $this->messageSubscribers
    ->getSubscribers($node, $message);

  // Assert subscribers data.
  $expected_uids = [
    $user2
      ->id() => new DeliveryCandidate([
      'subscribe_node',
      'subscribe_user',
    ], [], $user2
      ->id()),
  ];
  $this
    ->assertEquals($uids, $expected_uids, 'All expected subscribers were fetched.');

  // Test none of users will get message if only blocked user is subscribed.
  $message = Message::create([
    'template' => $this->template
      ->id(),
    'uid' => $this->users[1],
  ]);
  $node1 = $this->nodes[1];
  $uids = $this->messageSubscribers
    ->getSubscribers($node1, $message);

  // Assert subscribers data.
  $expected_uids = [];
  $this
    ->assertEquals($uids, $expected_uids, 'All expected subscribers were fetched.');

  // Test notifying all users, including those who are blocked.
  $subscribe_options['notify blocked users'] = TRUE;
  $uids = $this->messageSubscribers
    ->getSubscribers($node, $message, $subscribe_options);
  $expected_uids = [
    $user2
      ->id() => new DeliveryCandidate([
      'subscribe_node',
      'subscribe_user',
    ], [], $user2
      ->id()),
    $user_blocked
      ->id() => new DeliveryCandidate([
      'subscribe_node',
    ], [], $user_blocked
      ->id()),
  ];
  $this
    ->assertEquals($uids, $expected_uids, 'All expected subscribers were fetched, including blocked users.');
  $user3 = $this
    ->createUser([
    'flag subscribe_node',
    'unflag subscribe_node',
    'flag subscribe_user',
    'unflag subscribe_user',
  ]);
  $user4 = $this
    ->createUser([
    'flag subscribe_node',
    'unflag subscribe_node',
    'flag subscribe_user',
    'unflag subscribe_user',
  ]);
  $flags = $this->flagService
    ->getAllFlags();
  $this->flagService
    ->flag($flags['subscribe_node'], $node, $user3);
  $this->flagService
    ->flag($flags['subscribe_node'], $node, $user4);

  // Get subscribers from a given "last uid".
  $subscribe_options = [
    'last uid' => $user2
      ->id(),
  ];
  $uids = $this->messageSubscribers
    ->getSubscribers($node, $message, $subscribe_options);
  $this
    ->assertEquals(array_keys($uids), [
    $user3
      ->id(),
    $user4
      ->id(),
  ], 'All subscribers from "last uid" were fetched.');

  // Get a range of subscribers.
  $subscribe_options['range'] = 1;
  $uids = $this->messageSubscribers
    ->getSubscribers($node, $message, $subscribe_options);
  $this
    ->assertEquals(array_keys($uids), [
    $user3
      ->id(),
  ], 'All subscribers from "last uid" and "range" were fetched.');
}