You are here

public function CommentNotifyNotificationsTest::testEntityNotification in Comment Notify 8

Tests that the notifications are working on a different entity than a node.

File

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

Class

CommentNotifyNotificationsTest
Tests that all the notifications are sent as expected.

Namespace

Drupal\Tests\comment_notify\Functional

Code

public function testEntityNotification() {

  /** @var \Drupal\taxonomy\Entity\Vocabulary $vocabulary */
  $vocabulary = $this
    ->createVocabulary();
  $this
    ->addDefaultCommentField('taxonomy_term', $vocabulary
    ->id(), 'field_comment_taxonomy', CommentItemInterface::OPEN, 'comment_type_2');
  $comment_field = FieldConfig::loadByName('taxonomy_term', $vocabulary
    ->id(), 'field_comment_taxonomy');
  $comment_field
    ->setSetting('anonymous', CommentInterface::ANONYMOUS_MAY_CONTACT);
  $comment_field
    ->save();

  /** @var \Drupal\Core\Config\Config $config */
  $config = $this->container
    ->get('config.factory')
    ->getEditable('comment_notify.settings');
  $config
    ->set('bundle_types', [
    'taxonomy_term--' . $vocabulary
      ->id() . '--field_comment_taxonomy',
  ]);
  $config
    ->save();

  // Allow anonymous users to post comments and get notifications.
  user_role_grant_permissions(AccountInterface::ANONYMOUS_ROLE, [
    'access comments',
    'access content',
    'post comments',
    'skip comment approval',
    'subscribe to comments',
  ]);
  $permissions = array_merge($this->permissions, [
    'access user profiles',
  ]);
  $user1 = $this
    ->drupalCreateUser($permissions);
  $this->container
    ->get('comment_notify.user_settings')
    ->saveSettings($user1
    ->id(), COMMENT_NOTIFY_DISABLED, COMMENT_NOTIFY_ENTITY);
  $user2 = $this
    ->drupalCreateUser($permissions);
  $this->container
    ->get('comment_notify.user_settings')
    ->saveSettings($user2
    ->id(), COMMENT_NOTIFY_DISABLED, COMMENT_NOTIFY_COMMENT);
  $term = $this
    ->createTerm($vocabulary);
  $term2 = $this
    ->createTerm($vocabulary);

  // User1 Should get notification for any new comment on the entity.
  $this
    ->drupalLogin($user1);
  $this
    ->postComment($term
    ->toUrl()
    ->toString(), $this
    ->randomMachineName(), $this
    ->randomMachineName(), [
    'notify' => TRUE,
    'notify_type' => COMMENT_NOTIFY_ENTITY,
  ]);
  $this
    ->drupalLogout();
  $this
    ->postComment($term
    ->toUrl()
    ->toString(), $this
    ->randomMachineName(), $this
    ->randomMachineName(), [
    'notify' => FALSE,
    'notify_type' => COMMENT_NOTIFY_ENTITY,
  ], [
    'name' => $this
      ->randomMachineName(),
    'mail' => $this
      ->getRandomEmailAddress(),
  ]);
  $this
    ->assertMail('to', $user1
    ->getEmail(), t('Message was sent to the user1 user.'));
  $this->container
    ->get('state')
    ->set('system.test_mail_collector', []);

  // User 2 should get a notification only when someone reply to its comment.
  $this
    ->drupalLogin($user2);
  $comment = $this
    ->postComment($term2
    ->toUrl()
    ->toString(), $this
    ->randomMachineName(), $this
    ->randomMachineName(), [
    'notify' => TRUE,
    'notify_type' => COMMENT_NOTIFY_COMMENT,
  ]);
  $this
    ->drupalLogout();
  $this
    ->postComment($term2
    ->toUrl()
    ->toString(), $this
    ->randomMachineName(), $this
    ->randomMachineName(), [
    'notify' => FALSE,
    'notify_type' => COMMENT_NOTIFY_ENTITY,
  ], [
    'name' => $this
      ->randomMachineName(),
    'mail' => $this
      ->getRandomEmailAddress(),
  ]);
  $this
    ->assertEmpty($this
    ->getMails(), 'No notification was sent');
  $this
    ->drupalGet('/comment/reply/taxonomy_term/' . $term2
    ->id() . '/field_comment_taxonomy/' . $comment['id']);

  // /comment/reply/taxonomy_term/1/field_comment_taxonomy/2.
  $this
    ->postComment('/comment/reply/taxonomy_term/' . $term2
    ->id() . '/field_comment_taxonomy/' . $comment['id'], $this
    ->randomMachineName(), $this
    ->randomMachineName(), [
    'notify' => FALSE,
    'notify_type' => COMMENT_NOTIFY_ENTITY,
  ], [
    'name' => $this
      ->randomMachineName(),
    'mail' => $this
      ->getRandomEmailAddress(),
  ]);
  $this
    ->assertMail('to', $user2
    ->getEmail(), t('Message was sent to the user1 user.'));
  $this->container
    ->get('state')
    ->set('system.test_mail_collector', []);
}