CommentNotifyUserPreferencesTest.php 10.8 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34
<?php

namespace Drupal\Tests\comment_notify\Functional;

use Drupal\Core\Session\AccountInterface;

/**
 * Tests the Comment Notify users preferences.
 *
 * @group comment_notify
 */
class CommentNotifyUserPreferencesTest extends CommentNotifyTestBase {

  /**
   * Authenticated User.
   *
   * @var \Drupal\user\Entity\User $authenticatedUser
   */
  protected $authenticatedUser;

  /**
   * Permissions required by the module.
   */
  protected $permissions = [
    'post comments',
    'skip comment approval',
    'subscribe to comments',
  ];

  protected function setUp() {
    parent::setUp();
    $this->authenticatedUser = $this->drupalCreateUser($this->permissions);
  }

35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115
  /**
   * The User's comment notify box should display different options depending
   * the permissions of the user.
   */
  public function testUserCommentNotifyBox() {
    // The user hasn't the subscribe to comments permission nor the 'administer
    // nodes' permission, nor has permission to create content, so it shouldn't
    // see any Comment notify settings in the profile page.
    $this->authenticatedUser = $this->drupalCreateUser(
      [
        'post comments',
        'skip comment approval',
      ]
    );
    $this->drupalLogin($this->authenticatedUser);
    $this->drupalGet($this->authenticatedUser->toUrl('edit-form')->toString());
    $this->assertFalse($this->getSession()->getPage()->hasContent(t('Comment follow-up notification settings')));
    $this->drupalLogout();

    // The user only has the 'subscribe to comments' permission, he should be
    // able to see the Comment Notify settings box but the 'Receive content
    // follow-up notification e-mails' checkbox shouldn't appear.
    $this->authenticatedUser = $this->drupalCreateUser([
      'post comments',
      'skip comment approval',
      'subscribe to comments',
    ]);
    $this->drupalLogin($this->authenticatedUser);
    $this->drupalGet($this->authenticatedUser->toUrl('edit-form')->toString());
    $this->assertTrue($this->getSession()->getPage()->hasContent(t('Comment follow-up notification settings')));
    $this->assertTrue($this->getSession()->getPage()->hasContent(t('Receive comment follow-up notification e-mails')));
    $this->assertFalse($this->getSession()->getPage()->hasContent(t('Receive content follow-up notification e-mails')));
    $this->drupalLogout();

    // The user only has the 'administer nodes' permission, he should be
    // able to see the Comment Notify settings box but the 'Comment follow-up
    // notification settings' dropdown shouldn't appear.
    $this->authenticatedUser = $this->drupalCreateUser([
      'post comments',
      'skip comment approval',
      'administer nodes',
    ]);
    $this->drupalLogin($this->authenticatedUser);
    $this->drupalGet($this->authenticatedUser->toUrl('edit-form')->toString());
    $this->assertTrue($this->getSession()->getPage()->hasContent(t('Comment follow-up notification settings')));
    $this->assertFalse($this->getSession()->getPage()->hasContent(t('Receive comment follow-up notification e-mails')));
    $this->assertTrue($this->getSession()->getPage()->hasContent(t('Receive content follow-up notification e-mails')));
    $this->drupalLogout();

    // The user only hasn't the 'administer nodes' permission nor the 'subscribe
    // to comments' permission but he can create nodes of the type article, so
    // he should be able to see the Comment Notify settings box with the
    // 'Receive content follow-up notification e-mails' checkbox.
    $this->authenticatedUser = $this->drupalCreateUser([
      'post comments',
      'skip comment approval',
      'create article content',
    ]);
    $this->drupalLogin($this->authenticatedUser);
    $this->drupalGet($this->authenticatedUser->toUrl('edit-form')->toString());
    $this->assertTrue($this->getSession()->getPage()->hasContent(t('Comment follow-up notification settings')));
    $this->assertFalse($this->getSession()->getPage()->hasContent(t('Receive comment follow-up notification e-mails')));
    $this->assertTrue($this->getSession()->getPage()->hasContent(t('Receive content follow-up notification e-mails')));
    $this->drupalLogout();

    // The has all the permissions, so he should be able to see all the Comment
    // notify settings.
    $this->authenticatedUser = $this->drupalCreateUser([
      'post comments',
      'skip comment approval',
      'create article content',
      'subscribe to comments',
    ]);
    $this->drupalLogin($this->authenticatedUser);
    $this->drupalGet($this->authenticatedUser->toUrl('edit-form')->toString());
    $this->assertTrue($this->getSession()->getPage()->hasContent(t('Comment follow-up notification settings')));
    $this->assertTrue($this->getSession()->getPage()->hasContent(t('Receive comment follow-up notification e-mails')));
    $this->assertTrue($this->getSession()->getPage()->hasContent(t('Receive content follow-up notification e-mails')));
    $this->drupalLogout();
  }

116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245
  /**
   * Tests the Comment follow-up notification settings.
   */
  public function testUserCommentPreferences() {

    // Tests that the settings are present in the user profile.
    $this->drupalLogin($this->authenticatedUser);
    $this->drupalGet($this->authenticatedUser->toUrl('edit-form')->toString());
    $this->assertTrue($this->getSession()->getPage()->hasContent(t('Comment follow-up notification settings')));
    $this->drupalLogout();
  }

  public function testsCommentFollowUpsNotifications() {
    $this->drupalLogin($this->authenticatedUser);
    $this->drupalGet($this->authenticatedUser->toUrl('edit-form')->toString());
    $this->assertTrue($this->getSession()->getPage()->hasContent(t('Receive comment follow-up notification e-mails')));

    // Test the "No notifications" option.
    $this->getSession()->getPage()->selectFieldOption('comment_notify', COMMENT_NOTIFY_DISABLED);
    $this->getSession()->getPage()->pressButton(t('Save'));
    $node = $this->drupalCreateNode(['type' => 'article']);
    $this->drupalGet($node->toUrl()->toString());
    $this->assertTrue($this->getSession()->getPage()->hasUncheckedField('Notify me when new comments are posted'));

    // Test the "All comments" option.
    $this->drupalGet($this->authenticatedUser->toUrl('edit-form')->toString());
    $this->getSession()->getPage()->selectFieldOption('comment_notify', COMMENT_NOTIFY_NODE);
    $this->getSession()->getPage()->pressButton(t('Save'));
    $this->drupalGet($node->toUrl()->toString());
    $this->assertTrue($this->getSession()->getPage()->hascheckedField('Notify me when new comments are posted'));
    $this->assertTrue($this->getSession()->getPage()->hascheckedField('All comments'));

    // Tests the "Replies to my comments" option.
    $this->drupalGet($this->authenticatedUser->toUrl('edit-form')->toString());
    $this->getSession()->getPage()->selectFieldOption('comment_notify', COMMENT_NOTIFY_COMMENT);
    $this->getSession()->getPage()->pressButton(t('Save'));
    $this->drupalGet($node->toUrl()->toString());
    $this->assertTrue($this->getSession()->getPage()->hascheckedField('Notify me when new comments are posted'));
    $this->assertTrue($this->getSession()->getPage()->hascheckedField('Replies to my comment'));

    $this->drupalLogout();

  }

  /**
   * Tests the Content follow-up notification settings.
   */
  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 = comment_notify_get_user_node_notify_preference($this->authenticatedUser->id());
    $this->assertEquals(COMMENT_NOTIFY_NODE, $node_notify_preference);
    $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_NODE]
    );

    // 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();
    drupal_static_reset('comment_notify_get_user_notification_setting');
    $node_notify_preference = comment_notify_get_user_node_notify_preference($this->authenticatedUser->id());
    $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_NODE]
    );
    $captured_emails = $this->container->get('state')->get('system.test_mail_collector');
    $this->assertEmpty($captured_emails, 'No notifications has been sent.');

  }

}