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