You are here

public function CommentNotifyUserPreferencesTest::testUserNodePreferences in Comment Notify 8

Tests the Content follow-up notification settings.

File

tests/src/Functional/CommentNotifyUserPreferencesTest.php, line 172

Class

CommentNotifyUserPreferencesTest
Tests the Comment Notify users preferences.

Namespace

Drupal\Tests\comment_notify\Functional

Code

public function testUserNodePreferences() {

  // Tests that the option not present in the user profile unless the user has
  // the 'administer nodes' permission.
  $this
    ->drupalLogin($this->authenticatedUser);
  $this
    ->drupalGet($this->authenticatedUser
    ->toUrl('edit-form')
    ->toString());
  $this
    ->assertFalse($this
    ->getSession()
    ->getPage()
    ->hasContent(t('Receive content follow-up notification e-mails')));
  $this
    ->drupalLogout();

  // Tests that the option is present in the user profile if the user has the
  // 'administer nodes' permission.
  $permissions = array_merge($this->permissions, [
    'administer nodes',
  ]);
  $this->authenticatedUser = $this
    ->createUser($permissions);
  $this
    ->drupalLogin($this->authenticatedUser);
  $this
    ->drupalGet($this->authenticatedUser
    ->toUrl('edit-form')
    ->toString());
  $this
    ->assertTrue($this
    ->getSession()
    ->getPage()
    ->hasContent(t('Receive content follow-up notification e-mails')));
  $this
    ->getSession()
    ->getPage()
    ->checkField('Receive content follow-up notification e-mails');
  $this
    ->getSession()
    ->getPage()
    ->pressButton(t('Save'));
  $node_notify_preference = $this->container
    ->get('comment_notify.user_settings')
    ->getSetting($this->authenticatedUser
    ->id(), 'entity_notify');
  $this
    ->assertEquals(COMMENT_NOTIFY_ENTITY, $node_notify_preference);
  $this
    ->assertSession()
    ->checkboxChecked(t('Receive content follow-up notification e-mails'));
  $this
    ->drupalLogout();

  // Tests that the notification is sent when the content created by the user
  // receives a new comment.
  $node = $this
    ->drupalCreateNode([
    'type' => 'article',
    'uid' => $this->authenticatedUser,
  ]);

  // Write a comment as anonymous user.
  user_role_grant_permissions(AccountInterface::ANONYMOUS_ROLE, [
    'access comments',
    'access content',
    'post comments',
    'skip comment approval',
    'subscribe to comments',
  ]);
  $this
    ->postComment($node
    ->toUrl()
    ->toString(), $this
    ->randomMachineName(), $this
    ->randomMachineName(), [
    'notify' => FALSE,
    'notify_type' => COMMENT_NOTIFY_ENTITY,
  ]);

  // Test that the notification was sent.
  $this
    ->assertMail('to', $this->authenticatedUser
    ->getEmail(), t('Message was sent to the user.'));

  // Tests that the notification is not sent when the user hasn't selected
  // the content notifications setting.
  $this
    ->drupalLogin($this->authenticatedUser);
  $this
    ->drupalGet($this->authenticatedUser
    ->toUrl('edit-form')
    ->toString());
  $this
    ->assertTrue($this
    ->getSession()
    ->getPage()
    ->hasContent(t('Receive content follow-up notification e-mails')));
  $this
    ->getSession()
    ->getPage()
    ->uncheckField('Receive content follow-up notification e-mails');
  $this
    ->getSession()
    ->getPage()
    ->pressButton(t('Save'));
  $this
    ->assertTrue($this
    ->getSession()
    ->getPage()
    ->hasUncheckedField('Receive content follow-up notification e-mails'));
  $this
    ->drupalLogout();
  $node_notify_preference = $this->container
    ->get('comment_notify.user_settings')
    ->getSetting($this->authenticatedUser
    ->id(), 'entity_notify');
  $this
    ->assertEquals(COMMENT_NOTIFY_DISABLED, $node_notify_preference);
  $this->container
    ->get('state')
    ->set('system.test_mail_collector', []);
  $node = $this
    ->drupalCreateNode([
    'type' => 'article',
    'uid' => $this->authenticatedUser,
  ]);
  $this
    ->postComment($node
    ->toUrl()
    ->toString(), $this
    ->randomMachineName(), $this
    ->randomMachineName(), [
    'notify' => FALSE,
    'notify_type' => COMMENT_NOTIFY_ENTITY,
  ]);
  $captured_emails = $this->container
    ->get('state')
    ->get('system.test_mail_collector');
  $this
    ->assertEmpty($captured_emails, 'No notifications has been sent.');
}