You are here

public function CommentNotifyNotificationsTest::testCommentTypeNotification in Comment Notify 8

Tests the notifications are sent correctly with multiple comment types.

File

tests/src/Functional/CommentNotifyNotificationsTest.php, line 86

Class

CommentNotifyNotificationsTest
Tests that all the notifications are sent as expected.

Namespace

Drupal\Tests\comment_notify\Functional

Code

public function testCommentTypeNotification() {

  // Add a second comment type.
  $this
    ->addDefaultCommentField('node', 'article', 'field_comment', CommentItemInterface::OPEN, 'comment_type_2');

  /** @var \Drupal\Core\Config\Config $config */
  $config = $this->container
    ->get('config.factory')
    ->getEditable('comment_notify.settings');
  $config
    ->set('bundle_types', [
    'node--article--comment',
    'node--article--field_comment',
  ]);
  $config
    ->save();
  $user1 = $this
    ->drupalCreateUser($this->permissions);
  $user2 = $this
    ->drupalCreateUser($this->permissions);
  $node = $this
    ->drupalCreateNode([
    'type' => 'article',
    'uid' => $this->adminUser,
  ]);

  // Comment of the comment type 1.
  $comment1 = Comment::create([
    'comment_type' => 'comment',
    'langcode' => 'und',
    'entity_id' => $node
      ->id(),
    'entity_type' => $node
      ->getEntityTypeId(),
    'uid' => $user1
      ->id(),
    'subject' => $this
      ->randomMachineName(),
    'status' => 1,
    'field_name' => 'comment',
    'comment_body' => [
      'summary' => '',
      'value' => $this
        ->randomMachineName(),
      'format' => 'basic_html',
    ],
  ]);
  $comment1
    ->save($comment1);
  $notify_hash = \Drupal::csrfToken()
    ->get('127.0.0.1' . $comment1
    ->id());
  comment_notify_add_notification($comment1
    ->id(), TRUE, $notify_hash, 1);

  // Comment of the comment type 2.
  $comment2 = Comment::create([
    'comment_type' => 'comment_type_2',
    'langcode' => 'und',
    'entity_id' => $node
      ->id(),
    'entity_type' => $node
      ->getEntityTypeId(),
    'uid' => $user2
      ->id(),
    'subject' => $this
      ->randomMachineName(),
    'status' => 1,
    'field_name' => 'field_comment',
    'comment_body' => [
      'summary' => '',
      'value' => $this
        ->randomMachineName(),
      'format' => 'basic_html',
    ],
  ]);
  $comment2
    ->save($comment2);
  $notify_hash = \Drupal::csrfToken()
    ->get('127.0.0.1' . $comment1
    ->id());
  comment_notify_add_notification($comment2
    ->id(), TRUE, $notify_hash, 1);
  $this
    ->assertEmpty($this
    ->getMails(), 'No notifications has been sent.');

  // User 1 reply user 2 in comment type 2, user 2 should get a notification.
  $this
    ->drupalLogin($user1);
  $this
    ->postComment("/comment/reply/node/{$node->id()}/field_comment/{$comment2->id()}", $this
    ->randomMachineName(), $this
    ->randomMachineName(), [
    'notify' => FALSE,
    'notify_type' => COMMENT_NOTIFY_COMMENT,
  ]);
  $this
    ->drupalLogout();
  $this
    ->assertMail('to', $user2
    ->getEmail(), t('Message was sent to the user2 user.'));
  $this->container
    ->get('state')
    ->set('system.test_mail_collector', []);

  // User 2 reply user 1 in comment type 1, user 1 should get a notification.
  $this
    ->drupalLogin($user2);
  $this
    ->postComment("/comment/reply/node/{$node->id()}/comment/{$comment1->id()}", $this
    ->randomMachineName(), $this
    ->randomMachineName(), [
    'notify' => FALSE,
    'notify_type' => COMMENT_NOTIFY_COMMENT,
  ]);
  $this
    ->drupalLogout();
  $this
    ->assertMail('to', $user1
    ->getEmail(), t('Message was sent to the user1 user.'));
  $this->container
    ->get('state')
    ->set('system.test_mail_collector', []);
}