You are here

public function MessageDigestUiTest::testWithContexts in Message Digest 8

Test with advanced contexts.

File

message_digest_ui/tests/src/Kernel/MessageDigestUiTest.php, line 123

Class

MessageDigestUiTest
Basic tests for the Message Digest UI module.

Namespace

Drupal\Tests\message_digest_ui\Kernel

Code

public function testWithContexts() {

  // Set user 2  and 3 to receive daily digests.
  $this->users[2]->message_digest = 'message_digest:daily';
  $this->users[2]
    ->save();
  $this
    ->assertEquals('message_digest:daily', $this->users[2]->message_digest->value);
  $this->users[3]->message_digest = 'message_digest:daily';
  $this->users[3]
    ->save();

  // Subscribe users 2 and 3 to node 1.
  $flag = $this->flagService
    ->getFlagById('subscribe_node');
  $this->container
    ->set('current_user', $this->users[2]);
  $this->flagService
    ->flag($flag, $this->nodes[1], $this->users[2]);
  $this->container
    ->set('current_user', $this->users[3]);
  $this->flagService
    ->flag($flag, $this->nodes[1], $this->users[3]);

  // Subscribe users 2 and 3 to node 2.
  $this->flagService
    ->flag($flag, $this->nodes[2], $this->users[3]);
  $this->flagService
    ->flag($flag, $this->nodes[2], $this->users[2]);

  // Set user 2 to receive instant notifications for node 1.
  $flaggings = $this->flagService
    ->getAllEntityFlaggings($this->nodes[1], $this->users[2]);
  $flaggings[2]->message_digest = '0';
  $flaggings[2]
    ->save();

  // Set user 3 to receive weekly notifications for node 1. Digest should
  // still be daily below since it is smaller.
  $flaggings = $this->flagService
    ->getAllEntityFlaggings($this->nodes[1], $this->users[3]);
  $flaggings[4]->message_digest = 'message_digest:weekly';
  $flaggings[4]
    ->save();

  // Send a message about node 1 and 2. Since user 2 has daily for node 2 and
  // immediately for node 1, message should be sent immediately.
  $context = [
    'node' => [
      $this->nodes[1]
        ->id(),
      $this->nodes[2]
        ->id(),
    ],
  ];

  // Assert subscribers data.
  $expected_uids = [
    $this->users[2]
      ->id() => new DeliveryCandidate([
      'subscribe_node',
    ], [
      'email',
    ], $this->users[2]
      ->id()),
    $this->users[3]
      ->id() => new DeliveryCandidate([
      'subscribe_node',
    ], [
      'message_digest:daily',
    ], $this->users[3]
      ->id()),
  ];
  $message = Message::create([
    'template' => $this->messageTemplate
      ->id(),
  ]);
  $uids = $this->messageSubscribers
    ->getSubscribers($this->nodes[1], $message, [], $context);
  $this
    ->assertEquals($expected_uids, $uids, 'All expected subscribers were fetched.');
}