CommentNotifyAnonymousTest.php 6.47 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13
<?php

namespace Drupal\Tests\comment_notify\Functional;

use Drupal\Core\Session\AccountInterface;

/**
 * Comment notifications tests as anonymous user.
 *
 * @group comment_notify
 */
class CommentNotifyAnonymousTest extends CommentNotifyTestBase {

14 15 16
  /**
   * {@inheritdoc}
   */
17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33
  protected function setUp() {
    parent::setUp();

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

  /**
34
   * Tests that the mail is required for anonymous users.
35 36 37 38 39
   */
  public function testMail() {
    /** @var \Drupal\node\Entity\Node $node */
    $node = $this->drupalCreateNode(['type' => 'article']);

40
    $subscribe = ['notify' => TRUE, 'notify_type' => COMMENT_NOTIFY_ENTITY];
41 42 43 44 45 46 47 48 49 50 51
    $this->drupalGet($node->toUrl());
    $this->postComment($node->toUrl()->toString(), $this->randomMachineName(), $this->randomMachineName(), $subscribe);
    $this->assertTrue($this->getSession()->getPage()->hasContent(t('If you want to subscribe to comments you must supply a valid e-mail address.')));
  }

  /**
   * Tests the "All comments" notification option used by an anonymous user.
   */
  public function testAnonymousAllCommentsTest() {
    /** @var \Drupal\node\Entity\Node $node */
    $node = $this->drupalCreateNode(['type' => 'article']);
52
    $subscribe = ['notify' => TRUE, 'notify_type' => COMMENT_NOTIFY_ENTITY];
53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70
    $contact = ['name' => $this->randomMachineName(), 'mail' => $this->getRandomEmailAddress()];
    $comment = $this->postComment(
      $node->toUrl()->toString(),
      $this->randomMachineName(),
      $this->randomMachineName(),
      $subscribe,
      $contact
    );

    // Confirm that the notification is saved.
    $result = comment_notify_get_notification_type($comment['id']);
    $this->assertEquals($result, $subscribe['notify_type'], 'All Comments option was saved properly.');

    // Tests that the user receives the email if a new comment is posted.
    $this->postComment(
      $node->toUrl()->toString(),
      $this->randomMachineName(),
      $this->randomMachineName(),
71
      ['notify' => FALSE, 'notify_type' => COMMENT_NOTIFY_ENTITY],
72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89
      ['name' => $this->randomMachineName(), 'mail' => $this->getRandomEmailAddress()]
    );
    $this->assertMail('to', $contact['mail'], t('Message was sent to the anonymous user.'));

    // Test the unsubscribe link.
    $mails = $this->getMails();
    preg_match("/\/comment_notify\/disable\/.+/", $mails[0]['body'], $output);
    $this->drupalGet($output[0]);
    $this->assertTrue($this->getSession()->getPage()->hasContent("Your comment follow-up notification for this post was disabled. Thanks."));
    // Confirm that the notification has been disabled.
    $result = comment_notify_get_notification_type($comment['id']);
    $this->assertEquals($result, COMMENT_NOTIFY_DISABLED, 'The notification has been disabled');
    // Tests that the user stopped receiving notifications.
    $this->container->get('state')->set('system.test_mail_collector', []);
    $this->postComment(
      $node->toUrl()->toString(),
      $this->randomMachineName(),
      $this->randomMachineName(),
90
      ['notify' => FALSE, 'notify_type' => COMMENT_NOTIFY_ENTITY],
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
      ['name' => $this->randomMachineName(), 'mail' => $this->getRandomEmailAddress()]
    );
    $captured_emails = $this->container->get('state')->get('system.test_mail_collector');
    $this->assertEmpty($captured_emails, 'No notifications has been sent.');
  }

  /**
   * Tests the "Replies to my comment" option used by anonymous user.
   */
  public function testAnonymousRepliesTest() {
    // Create a node with comments allowed.
    $node = $this->drupalCreateNode(['type' => 'article']);
    $subscribe = ['notify' => TRUE, 'notify_type' => COMMENT_NOTIFY_COMMENT];
    $contact = ['name' => $this->randomMachineName(), 'mail' => $this->getRandomEmailAddress()];
    $comment = $this->postComment($node->toUrl()->toString(), $this->randomMachineName(), $this->randomMachineName(), $subscribe, $contact);

    // Confirm that the notification is saved.
    $result = comment_notify_get_notification_type($comment['id']);
    $this->assertEquals($result, $subscribe['notify_type'], 'Replies to my comment option was saved properly.');

    // Tests that the user doesn't receive a mail if a new comment is posted.
    $this->postComment(
      $node->toUrl()->toString(),
      $this->randomMachineName(),
      $this->randomMachineName(),
116
      ['notify' => FALSE, 'notify_type' => COMMENT_NOTIFY_ENTITY],
117 118 119 120 121 122 123 124 125 126
      ['name' => $this->randomMachineName(), 'mail' => $this->getRandomEmailAddress()]
    );
    $captured_emails = $this->container->get('state')->get('system.test_mail_collector');
    $this->assertEmpty($captured_emails, 'No notifications has been sent.');

    // Tests that the user receives a mail if a reply has been posted.
    $this->postComment(
      "/comment/reply/node/{$node->id()}/comment/{$comment['id']}",
      $this->randomMachineName(),
      $this->randomMachineName(),
127
      ['notify' => FALSE, 'notify_type' => COMMENT_NOTIFY_ENTITY],
128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145
      ['name' => $this->randomMachineName(), 'mail' => $this->getRandomEmailAddress()]
    );
    $this->assertMail('to', $contact['mail'], t('Message was sent to the anonymous user.'));

    // Test the unsubscribe link.
    $mails = $this->getMails();
    preg_match("/\/comment_notify\/disable\/.+/", $mails[0]['body'], $output);
    $this->drupalGet($output[0]);
    $this->assertTrue($this->getSession()->getPage()->hasContent("Your comment follow-up notification for this post was disabled. Thanks."));
    // Confirm that the notification has been disabled.
    $result = comment_notify_get_notification_type($comment['id']);
    $this->assertEquals($result, COMMENT_NOTIFY_DISABLED, 'The notification has been disabled');
    // Tests that the user stopped receiving notifications.
    $this->container->get('state')->set('system.test_mail_collector', []);
    $this->postComment(
      "/comment/reply/node/{$node->id()}/comment/{$comment['id']}",
      $this->randomMachineName(),
      $this->randomMachineName(),
146
      ['notify' => FALSE, 'notify_type' => COMMENT_NOTIFY_ENTITY],
147 148 149 150 151 152 153
      ['name' => $this->randomMachineName(), 'mail' => $this->getRandomEmailAddress()]
    );
    $captured_emails = $this->container->get('state')->get('system.test_mail_collector');
    $this->assertEmpty($captured_emails, 'No notifications has been sent.');
  }

}