3 namespace Drupal\Tests\comment\Functional;
5 use Drupal\user\RoleInterface;
8 * Tests anonymous commenting.
12 class CommentAnonymousTest extends CommentTestBase {
14 protected function setUp() {
17 // Enable anonymous and authenticated user comments.
18 user_role_grant_permissions(RoleInterface::ANONYMOUS_ID, [
21 'skip comment approval',
23 user_role_grant_permissions(RoleInterface::AUTHENTICATED_ID, [
26 'skip comment approval',
31 * Tests anonymous comment functionality.
33 public function testAnonymous() {
34 $this->drupalLogin($this->adminUser);
35 $this->setCommentAnonymous(COMMENT_ANONYMOUS_MAYNOT_CONTACT);
36 $this->drupalLogout();
38 // Preview comments (with `skip comment approval` permission).
40 $title = 'comment title with skip comment approval';
41 $body = 'comment body with skip comment approval';
42 $edit['subject[0][value]'] = $title;
43 $edit['comment_body[0][value]'] = $body;
44 $this->drupalPostForm($this->node->urlInfo(), $edit, t('Preview'));
45 // Cannot use assertRaw here since both title and body are in the form.
46 $preview = (string) $this->cssSelect('.preview')[0]->getHtml();
47 $this->assertTrue(strpos($preview, $title) !== FALSE, 'Anonymous user can preview comment title.');
48 $this->assertTrue(strpos($preview, $body) !== FALSE, 'Anonymous user can preview comment body.');
50 // Preview comments (without `skip comment approval` permission).
51 user_role_revoke_permissions(RoleInterface::ANONYMOUS_ID, ['skip comment approval']);
53 $title = 'comment title without skip comment approval';
54 $body = 'comment body without skip comment approval';
55 $edit['subject[0][value]'] = $title;
56 $edit['comment_body[0][value]'] = $body;
57 $this->drupalPostForm($this->node->urlInfo(), $edit, t('Preview'));
58 // Cannot use assertRaw here since both title and body are in the form.
59 $preview = (string) $this->cssSelect('.preview')[0]->getHtml();
60 $this->assertTrue(strpos($preview, $title) !== FALSE, 'Anonymous user can preview comment title.');
61 $this->assertTrue(strpos($preview, $body) !== FALSE, 'Anonymous user can preview comment body.');
62 user_role_grant_permissions(RoleInterface::ANONYMOUS_ID, ['skip comment approval']);
64 // Post anonymous comment without contact info.
65 $anonymous_comment1 = $this->postComment($this->node, $this->randomMachineName(), $this->randomMachineName());
66 $this->assertTrue($this->commentExists($anonymous_comment1), 'Anonymous comment without contact info found.');
68 // Ensure anonymous users cannot post in the name of registered users.
70 'name' => $this->adminUser->getUsername(),
71 'comment_body[0][value]' => $this->randomMachineName(),
73 $this->drupalPostForm('comment/reply/node/' . $this->node->id() . '/comment', $edit, t('Save'));
74 $this->assertRaw(t('The name you used (%name) belongs to a registered user.', [
75 '%name' => $this->adminUser->getUsername(),
78 // Allow contact info.
79 $this->drupalLogin($this->adminUser);
80 $this->setCommentAnonymous(COMMENT_ANONYMOUS_MAY_CONTACT);
82 // Attempt to edit anonymous comment.
83 $this->drupalGet('comment/' . $anonymous_comment1->id() . '/edit');
84 $edited_comment = $this->postComment(NULL, $this->randomMachineName(), $this->randomMachineName());
85 $this->assertTrue($this->commentExists($edited_comment, FALSE), 'Modified reply found.');
86 $this->drupalLogout();
88 // Post anonymous comment with contact info (optional).
89 $this->drupalGet('comment/reply/node/' . $this->node->id() . '/comment');
90 $this->assertTrue($this->commentContactInfoAvailable(), 'Contact information available.');
92 // Check the presence of expected cache tags.
93 $this->assertCacheTag('config:field.field.node.article.comment');
94 $this->assertCacheTag('config:user.settings');
96 $anonymous_comment2 = $this->postComment($this->node, $this->randomMachineName(), $this->randomMachineName());
97 $this->assertTrue($this->commentExists($anonymous_comment2), 'Anonymous comment with contact info (optional) found.');
99 // Ensure anonymous users cannot post in the name of registered users.
101 'name' => $this->adminUser->getUsername(),
102 'mail' => $this->randomMachineName() . '@example.com',
103 'subject[0][value]' => $this->randomMachineName(),
104 'comment_body[0][value]' => $this->randomMachineName(),
106 $this->drupalPostForm('comment/reply/node/' . $this->node->id() . '/comment', $edit, t('Save'));
107 $this->assertRaw(t('The name you used (%name) belongs to a registered user.', [
108 '%name' => $this->adminUser->getUsername(),
111 // Require contact info.
112 $this->drupalLogin($this->adminUser);
113 $this->setCommentAnonymous(COMMENT_ANONYMOUS_MUST_CONTACT);
114 $this->drupalLogout();
116 // Try to post comment with contact info (required).
117 $this->drupalGet('comment/reply/node/' . $this->node->id() . '/comment');
118 $this->assertTrue($this->commentContactInfoAvailable(), 'Contact information available.');
120 $anonymous_comment3 = $this->postComment($this->node, $this->randomMachineName(), $this->randomMachineName(), TRUE);
121 // Name should have 'Anonymous' for value by default.
122 $this->assertText(t('Email field is required.'), 'Email required.');
123 $this->assertFalse($this->commentExists($anonymous_comment3), 'Anonymous comment with contact info (required) not found.');
125 // Post comment with contact info (required).
126 $author_name = $this->randomMachineName();
127 $author_mail = $this->randomMachineName() . '@example.com';
128 $anonymous_comment3 = $this->postComment($this->node, $this->randomMachineName(), $this->randomMachineName(), ['name' => $author_name, 'mail' => $author_mail]);
129 $this->assertTrue($this->commentExists($anonymous_comment3), 'Anonymous comment with contact info (required) found.');
131 // Make sure the user data appears correctly when editing the comment.
132 $this->drupalLogin($this->adminUser);
133 $this->drupalGet('comment/' . $anonymous_comment3->id() . '/edit');
134 $this->assertRaw($author_name, "The anonymous user's name is correct when editing the comment.");
135 $this->assertFieldByName('uid', '', 'The author field is empty (i.e. anonymous) when editing the comment.');
136 $this->assertRaw($author_mail, "The anonymous user's email address is correct when editing the comment.");
138 // Unpublish comment.
139 $this->performCommentOperation($anonymous_comment3, 'unpublish');
141 $this->drupalGet('admin/content/comment/approval');
142 $this->assertRaw('comments[' . $anonymous_comment3->id() . ']', 'Comment was unpublished.');
145 $this->performCommentOperation($anonymous_comment3, 'publish', TRUE);
147 $this->drupalGet('admin/content/comment');
148 $this->assertRaw('comments[' . $anonymous_comment3->id() . ']', 'Comment was published.');
151 $this->performCommentOperation($anonymous_comment3, 'delete');
153 $this->drupalGet('admin/content/comment');
154 $this->assertNoRaw('comments[' . $anonymous_comment3->id() . ']', 'Comment was deleted.');
155 $this->drupalLogout();
157 // Comment 3 was deleted.
158 $this->drupalGet('comment/reply/node/' . $this->node->id() . '/comment/' . $anonymous_comment3->id());
159 $this->assertResponse(403);
162 user_role_change_permissions(RoleInterface::ANONYMOUS_ID, [
163 'access comments' => FALSE,
164 'post comments' => FALSE,
165 'skip comment approval' => FALSE,
168 // Attempt to view comments while disallowed.
169 // NOTE: if authenticated user has permission to post comments, then a
170 // "Login or register to post comments" type link may be shown.
171 $this->drupalGet('node/' . $this->node->id());
172 $this->assertNoPattern('@<h2[^>]*>Comments</h2>@', 'Comments were not displayed.');
173 $this->assertNoLink('Add new comment', 'Link to add comment was found.');
175 // Attempt to view node-comment form while disallowed.
176 $this->drupalGet('comment/reply/node/' . $this->node->id() . '/comment');
177 $this->assertResponse(403);
179 user_role_change_permissions(RoleInterface::ANONYMOUS_ID, [
180 'access comments' => TRUE,
181 'post comments' => FALSE,
182 'skip comment approval' => FALSE,
184 $this->drupalGet('node/' . $this->node->id());
185 $this->assertPattern('@<h2[^>]*>Comments</h2>@', 'Comments were displayed.');
186 $this->assertLink('Log in', 1, 'Link to login was found.');
187 $this->assertLink('register', 1, 'Link to register was found.');
189 user_role_change_permissions(RoleInterface::ANONYMOUS_ID, [
190 'access comments' => FALSE,
191 'post comments' => TRUE,
192 'skip comment approval' => TRUE,
194 $this->drupalGet('node/' . $this->node->id());
195 $this->assertNoPattern('@<h2[^>]*>Comments</h2>@', 'Comments were not displayed.');
196 $this->assertFieldByName('subject[0][value]', '', 'Subject field found.');
197 $this->assertFieldByName('comment_body[0][value]', '', 'Comment field found.');
199 $this->drupalGet('comment/reply/node/' . $this->node->id() . '/comment/' . $anonymous_comment2->id());
200 $this->assertResponse(403);