You are here

public function MessageSubscribeEmailNotificationsTest::testEmailNotifications in Message Subscribe 8

Test opting in/out of default email notifications.

File

message_subscribe_email/tests/src/Kernel/MessageSubscribeEmailNotificationsTest.php, line 28

Class

MessageSubscribeEmailNotificationsTest
Test automatic email notification flagging.

Namespace

Drupal\Tests\message_subscribe_email\Kernel

Code

public function testEmailNotifications() {
  $message = Message::create([
    'template' => $this->messageTemplate
      ->id(),
  ]);
  $uids = $this->messageSubscribers
    ->getSubscribers($this->nodes[1], $message);

  // Assert subscribers data.
  $expected_uids = [
    $this->users[1]
      ->id() => new DeliveryCandidate([
      'subscribe_node',
    ], [
      'email',
    ], $this->users[1]
      ->id()),
  ];
  $this
    ->assertEquals($expected_uids, $uids, 'All expected subscribers were fetched.');
  $this->flagService
    ->unflag($this->flagService
    ->getFlagById('subscribe_node'), $this->nodes[1], $this->users[1]);

  // Subscribe to node 2 *with* email notifications.
  $this->flagService
    ->flag($this->flagService
    ->getFlagById('subscribe_node'), $this->nodes[2], $this->users[1]);

  // Opt out of default email notifications.
  $this->users[1]->message_subscribe_email = 0;
  $this->users[1]
    ->save();
  $this->flagService
    ->flag($this->flagService
    ->getFlagById('subscribe_node'), $this->nodes[1], $this->users[1]);
  $uids = $this->messageSubscribers
    ->getSubscribers($this->nodes[1], $message);

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

  // Test with advanced contexts, passing node 2 directly, but node 1 in the
  // context.
  $context = [
    'node' => [
      $this->nodes[1]
        ->id(),
    ],
  ];
  $uids = $this->messageSubscribers
    ->getSubscribers($this->nodes[2], $message, [], $context);
  $this
    ->assertEquals($expected_uids, $uids);
}