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