You are here

public function SubscribersTest::testSendMessage in Message Subscribe 8

Same name in this branch
  1. 8 tests/src/Unit/SubscribersTest.php \Drupal\Tests\message_subscribe\Unit\SubscribersTest::testSendMessage()
  2. 8 tests/src/Kernel/SubscribersTest.php \Drupal\Tests\message_subscribe\Kernel\SubscribersTest::testSendMessage()

Tests sendMessage method.

@covers ::sendMessage

File

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

Class

SubscribersTest
Test getting subscribes from context.

Namespace

Drupal\Tests\message_subscribe\Kernel

Code

public function testSendMessage() {

  // Enable a notifier.
  $this
    ->config('message_subscribe.settings')
    ->set('default_notifiers', [
    'email',
  ])
    ->save();

  // Add a few more users.
  $flags = $this->flagService
    ->getAllFlags();
  foreach (range(4, 10) as $i) {
    $this->users[$i] = $this
      ->createUser([
      'access content',
      'flag subscribe_node',
      'unflag subscribe_node',
      'flag subscribe_user',
      'unflag subscribe_user',
    ]);
    $this->flagService
      ->flag($flags['subscribe_node'], $this->nodes[0], $this->users[$i]);
  }

  // Send notifications for node 1.
  // Pass in the save message argument to the notifier.
  $notify_options = [
    'email' => [
      'save on fail' => TRUE,
      'save on success' => TRUE,
    ],
  ];
  $subscribe_options = [
    'notify message owner' => TRUE,
  ];
  $message = Message::create([
    'template' => $this->template
      ->id(),
  ]);
  $this->messageSubscribers
    ->sendMessage($this->nodes[0], $message, $notify_options, $subscribe_options);

  // Verify that each of the users has a copy of the message.
  $mails = $this
    ->getMails();
  $no_message_count = $message_count = 0;
  foreach ($this->users as $account) {

    /** @var \Drupal\Core\Entity\Query\QueryInterface $query */
    $query = $this->container
      ->get('entity_type.manager')
      ->getStorage('message')
      ->getQuery();
    $query
      ->condition('uid', $account
      ->id());
    $result = $query
      ->execute();

    // Users 2 through 3 won't have access.
    if (!$account
      ->hasPermission('access content') || $account
      ->isBlocked()) {
      $this
        ->assertEmpty($result);
      $no_message_count++;
    }
    else {
      $this
        ->assertEquals(1, count($result), '1 message was saved for user ' . $account
        ->id());
      $message_count++;
    }
  }
  $this
    ->assertEquals(2, $no_message_count);
  $this
    ->assertEquals(8, $message_count);
  $this
    ->assertEquals(count($mails), $message_count);
}