You are here

public function CommentNotifyConfigPageTest::testUnsubscribePage in Comment Notify 8

Tests the Unsubscribe page.

File

tests/src/Functional/CommentNotifyConfigPageTest.php, line 192

Class

CommentNotifyConfigPageTest
Tests for the comment_notify module.

Namespace

Drupal\Tests\comment_notify\Functional

Code

public function testUnsubscribePage() {

  /** @var \Drupal\node\Entity\Node $node */
  $node = $this
    ->drupalCreateNode([
    'type' => 'article',
  ]);

  // 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',
  ]);

  // Try to unsubscribe an email which haven't notifications.
  $this
    ->drupalLogin($this->adminUser);
  $this
    ->drupalGet("admin/config/people/comment_notify/unsubscribe");
  $this
    ->getSession()
    ->getPage()
    ->fillField('Email to unsubscribe', $this
    ->getRandomEmailAddress());
  $this
    ->submitForm([], 'Unsubscribe this e-mail');
  $this
    ->assertSession()
    ->responseContains('There were no active comment notifications for that email.');
  $this
    ->drupalLogout();

  // Unsubscribe an email that belongs to an anonymous user.
  $anonymous_mail = $this
    ->getRandomEmailAddress();
  $comment = $this
    ->postComment($node
    ->toUrl()
    ->toString(), $this
    ->randomMachineName(), $this
    ->randomMachineName(), [
    'notify' => TRUE,
    'notify_type' => COMMENT_NOTIFY_ENTITY,
  ], [
    'name' => $this
      ->randomMachineName(),
    'mail' => $anonymous_mail,
  ]);
  $result = comment_notify_get_notification_type($comment['id']);
  $this
    ->assertEquals($result, COMMENT_NOTIFY_ENTITY, 'The notification was added as expected');
  $this
    ->drupalLogin($this->adminUser);
  $this
    ->drupalGet("admin/config/people/comment_notify/unsubscribe");
  $this
    ->getSession()
    ->getPage()
    ->fillField('Email to unsubscribe', $anonymous_mail);
  $this
    ->submitForm([], 'Unsubscribe this e-mail');
  $this
    ->assertSession()
    ->responseContains('Email unsubscribed from all the comment notifications.');
  $this
    ->drupalLogout();
  $result = comment_notify_get_notification_type($comment['id']);
  $this
    ->assertEquals($result, COMMENT_NOTIFY_DISABLED, 'The mail was unsubscribed as expected');

  // Unsubscribe an email that have several notifications.
  $anonymous_mail2 = $this
    ->getRandomEmailAddress();
  $comment1 = $this
    ->postComment($node
    ->toUrl()
    ->toString(), $this
    ->randomMachineName(), $this
    ->randomMachineName(), [
    'notify' => TRUE,
    'notify_type' => COMMENT_NOTIFY_ENTITY,
  ], [
    'name' => $this
      ->randomMachineName(),
    'mail' => $anonymous_mail2,
  ]);
  $result_comment1 = comment_notify_get_notification_type($comment1['id']);
  $this
    ->assertEquals($result_comment1, COMMENT_NOTIFY_ENTITY, 'The notification was added as expected');
  $comment2 = $this
    ->postComment($node
    ->toUrl()
    ->toString(), $this
    ->randomMachineName(), $this
    ->randomMachineName(), [
    'notify' => TRUE,
    'notify_type' => COMMENT_NOTIFY_COMMENT,
  ], [
    'name' => $this
      ->randomMachineName(),
    'mail' => $anonymous_mail2,
  ]);
  $result_comment2 = comment_notify_get_notification_type($comment2['id']);
  $this
    ->assertEquals($result_comment2, COMMENT_NOTIFY_COMMENT, 'The notification was added as expected');
  $this
    ->drupalLogin($this->adminUser);
  $this
    ->drupalGet("admin/config/people/comment_notify/unsubscribe");
  $this
    ->getSession()
    ->getPage()
    ->fillField('Email to unsubscribe', $anonymous_mail2);
  $this
    ->submitForm([], 'Unsubscribe this e-mail');
  $this
    ->assertSession()
    ->responseContains('Email unsubscribed from all the comment notifications.');
  $this
    ->drupalLogout();
  $result_comment1 = comment_notify_get_notification_type($comment1['id']);
  $result_comment2 = comment_notify_get_notification_type($comment2['id']);
  $this
    ->assertEquals($result_comment1, COMMENT_NOTIFY_DISABLED, 'The mail was unsubscribed as expected');
  $this
    ->assertEquals($result_comment2, COMMENT_NOTIFY_DISABLED, 'The mail was unsubscribed as expected');

  // Unsubscribe an email of a registered user.
  $user = $this
    ->drupalCreateUser([
    'access comments',
    'access content',
    'edit own comments',
    'post comments',
    'skip comment approval',
    'subscribe to comments',
  ]);
  $this
    ->drupalLogin($user);
  $comment = $this
    ->postComment($node
    ->toUrl()
    ->toString(), $this
    ->randomMachineName(), $this
    ->randomMachineName(), [
    'notify' => TRUE,
    'notify_type' => COMMENT_NOTIFY_COMMENT,
  ]);
  $this
    ->drupalLogout();
  $this
    ->drupalLogin($this->adminUser);
  $this
    ->drupalGet("admin/config/people/comment_notify/unsubscribe");
  $this
    ->getSession()
    ->getPage()
    ->fillField('Email to unsubscribe', $user
    ->getEmail());
  $this
    ->submitForm([], 'Unsubscribe this e-mail');
  $this
    ->assertSession()
    ->responseContains('Email unsubscribed from all the comment notifications.');
  $this
    ->drupalLogout();
  $result = comment_notify_get_notification_type($comment['id']);
  $this
    ->assertEquals($result, COMMENT_NOTIFY_DISABLED, 'The mail was unsubscribed as expected');
}