CommentAnonymousTest.php 6.92 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
<?php

/**
 * @file
 * Definition of Drupal\comment\Tests\CommentAnonymousTest.
 */

namespace Drupal\comment\Tests;

/**
 * Tests anonymous commenting.
 */
class CommentAnonymousTest extends CommentTestBase {
  public static function getInfo() {
    return array(
      'name' => 'Anonymous comments',
      'description' => 'Test anonymous comments.',
      'group' => 'Comment',
    );
  }

  function setUp() {
    parent::setUp();
24
    config('user.settings')->set('register', USER_REGISTER_VISITORS)->save();
25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42
  }

  /**
   * Tests anonymous comment functionality.
   */
  function testAnonymous() {
    $this->drupalLogin($this->admin_user);
    // Enabled anonymous user comments.
    user_role_change_permissions(DRUPAL_ANONYMOUS_RID, array(
      'access comments' => TRUE,
      'post comments' => TRUE,
      'skip comment approval' => TRUE,
    ));
    $this->setCommentAnonymous('0'); // Ensure that doesn't require contact info.
    $this->drupalLogout();

    // Post anonymous comment without contact info.
    $anonymous_comment1 = $this->postComment($this->node, $this->randomName(), $this->randomName());
43
    $this->assertTrue($this->commentExists($anonymous_comment1), 'Anonymous comment without contact info found.');
44 45 46 47 48 49 50 51

    // Allow contact info.
    $this->drupalLogin($this->admin_user);
    $this->setCommentAnonymous('1');

    // Attempt to edit anonymous comment.
    $this->drupalGet('comment/' . $anonymous_comment1->id . '/edit');
    $edited_comment = $this->postComment(NULL, $this->randomName(), $this->randomName());
52
    $this->assertTrue($this->commentExists($edited_comment, FALSE), 'Modified reply found.');
53 54 55 56
    $this->drupalLogout();

    // Post anonymous comment with contact info (optional).
    $this->drupalGet('comment/reply/' . $this->node->nid);
57
    $this->assertTrue($this->commentContactInfoAvailable(), 'Contact information available.');
58 59

    $anonymous_comment2 = $this->postComment($this->node, $this->randomName(), $this->randomName());
60
    $this->assertTrue($this->commentExists($anonymous_comment2), 'Anonymous comment with contact info (optional) found.');
61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79

    // Ensure anonymous users cannot post in the name of registered users.
    $langcode = LANGUAGE_NOT_SPECIFIED;
    $edit = array(
      'name' => $this->admin_user->name,
      'mail' => $this->randomName() . '@example.com',
      'subject' => $this->randomName(),
      "comment_body[$langcode][0][value]" => $this->randomName(),
    );
    $this->drupalPost('comment/reply/' . $this->node->nid, $edit, t('Save'));
    $this->assertText(t('The name you used belongs to a registered user.'));

    // Require contact info.
    $this->drupalLogin($this->admin_user);
    $this->setCommentAnonymous('2');
    $this->drupalLogout();

    // Try to post comment with contact info (required).
    $this->drupalGet('comment/reply/' . $this->node->nid);
80
    $this->assertTrue($this->commentContactInfoAvailable(), 'Contact information available.');
81 82

    $anonymous_comment3 = $this->postComment($this->node, $this->randomName(), $this->randomName(), TRUE);
83 84 85
    // Name should have 'Anonymous' for value by default.
    $this->assertText(t('E-mail field is required.'), 'E-mail required.');
    $this->assertFalse($this->commentExists($anonymous_comment3), 'Anonymous comment with contact info (required) not found.');
86 87 88 89 90

    // Post comment with contact info (required).
    $author_name = $this->randomName();
    $author_mail = $this->randomName() . '@example.com';
    $anonymous_comment3 = $this->postComment($this->node, $this->randomName(), $this->randomName(), array('name' => $author_name, 'mail' => $author_mail));
91
    $this->assertTrue($this->commentExists($anonymous_comment3), 'Anonymous comment with contact info (required) found.');
92 93 94 95

    // Make sure the user data appears correctly when editing the comment.
    $this->drupalLogin($this->admin_user);
    $this->drupalGet('comment/' . $anonymous_comment3->id . '/edit');
96 97
    $this->assertRaw($author_name, "The anonymous user's name is correct when editing the comment.");
    $this->assertRaw($author_mail, "The anonymous user's e-mail address is correct when editing the comment.");
98 99 100 101 102

    // Unpublish comment.
    $this->performCommentOperation($anonymous_comment3, 'unpublish');

    $this->drupalGet('admin/content/comment/approval');
103
    $this->assertRaw('comments[' . $anonymous_comment3->id . ']', 'Comment was unpublished.');
104 105 106 107 108

    // Publish comment.
    $this->performCommentOperation($anonymous_comment3, 'publish', TRUE);

    $this->drupalGet('admin/content/comment');
109
    $this->assertRaw('comments[' . $anonymous_comment3->id . ']', 'Comment was published.');
110 111 112 113 114

    // Delete comment.
    $this->performCommentOperation($anonymous_comment3, 'delete');

    $this->drupalGet('admin/content/comment');
115
    $this->assertNoRaw('comments[' . $anonymous_comment3->id . ']', 'Comment was deleted.');
116 117 118 119 120 121 122 123 124 125 126 127 128
    $this->drupalLogout();

    // Reset.
    user_role_change_permissions(DRUPAL_ANONYMOUS_RID, array(
      'access comments' => FALSE,
      'post comments' => FALSE,
      'skip comment approval' => FALSE,
    ));

    // Attempt to view comments while disallowed.
    // NOTE: if authenticated user has permission to post comments, then a
    // "Login or register to post comments" type link may be shown.
    $this->drupalGet('node/' . $this->node->nid);
129 130
    $this->assertNoPattern('@<h2[^>]*>Comments</h2>@', 'Comments were not displayed.');
    $this->assertNoLink('Add new comment', 'Link to add comment was found.');
131 132 133

    // Attempt to view node-comment form while disallowed.
    $this->drupalGet('comment/reply/' . $this->node->nid);
134 135 136
    $this->assertText('You are not authorized to post comments', 'Error attempting to post comment.');
    $this->assertNoFieldByName('subject', '', 'Subject field not found.');
    $this->assertNoFieldByName("comment_body[$langcode][0][value]", '', 'Comment field not found.');
137 138 139 140 141 142 143

    user_role_change_permissions(DRUPAL_ANONYMOUS_RID, array(
      'access comments' => TRUE,
      'post comments' => FALSE,
      'skip comment approval' => FALSE,
    ));
    $this->drupalGet('node/' . $this->node->nid);
144 145 146
    $this->assertPattern('@<h2[^>]*>Comments</h2>@', 'Comments were displayed.');
    $this->assertLink('Log in', 1, 'Link to log in was found.');
    $this->assertLink('register', 1, 'Link to register was found.');
147 148 149 150 151 152 153

    user_role_change_permissions(DRUPAL_ANONYMOUS_RID, array(
      'access comments' => FALSE,
      'post comments' => TRUE,
      'skip comment approval' => TRUE,
    ));
    $this->drupalGet('node/' . $this->node->nid);
154 155 156
    $this->assertNoPattern('@<h2[^>]*>Comments</h2>@', 'Comments were not displayed.');
    $this->assertFieldByName('subject', '', 'Subject field found.');
    $this->assertFieldByName("comment_body[$langcode][0][value]", '', 'Comment field found.');
157 158

    $this->drupalGet('comment/reply/' . $this->node->nid . '/' . $anonymous_comment3->id);
159 160
    $this->assertText('You are not authorized to view comments', 'Error attempting to post reply.');
    $this->assertNoText($author_name, 'Comment not displayed.');
161 162
  }
}