Mercurial > hg > isophonics-drupal-site
comparison core/modules/comment/tests/src/Functional/CommentAnonymousTest.php @ 0:4c8ae668cc8c
Initial import (non-working)
author | Chris Cannam |
---|---|
date | Wed, 29 Nov 2017 16:09:58 +0000 |
parents | |
children | af1871eacc83 |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:4c8ae668cc8c |
---|---|
1 <?php | |
2 | |
3 namespace Drupal\Tests\comment\Functional; | |
4 | |
5 use Drupal\user\RoleInterface; | |
6 | |
7 /** | |
8 * Tests anonymous commenting. | |
9 * | |
10 * @group comment | |
11 */ | |
12 class CommentAnonymousTest extends CommentTestBase { | |
13 | |
14 protected function setUp() { | |
15 parent::setUp(); | |
16 | |
17 // Enable anonymous and authenticated user comments. | |
18 user_role_grant_permissions(RoleInterface::ANONYMOUS_ID, [ | |
19 'access comments', | |
20 'post comments', | |
21 'skip comment approval', | |
22 ]); | |
23 user_role_grant_permissions(RoleInterface::AUTHENTICATED_ID, [ | |
24 'access comments', | |
25 'post comments', | |
26 'skip comment approval', | |
27 ]); | |
28 } | |
29 | |
30 /** | |
31 * Tests anonymous comment functionality. | |
32 */ | |
33 public function testAnonymous() { | |
34 $this->drupalLogin($this->adminUser); | |
35 $this->setCommentAnonymous(COMMENT_ANONYMOUS_MAYNOT_CONTACT); | |
36 $this->drupalLogout(); | |
37 | |
38 // Preview comments (with `skip comment approval` permission). | |
39 $edit = []; | |
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.'); | |
49 | |
50 // Preview comments (without `skip comment approval` permission). | |
51 user_role_revoke_permissions(RoleInterface::ANONYMOUS_ID, ['skip comment approval']); | |
52 $edit = []; | |
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']); | |
63 | |
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.'); | |
67 | |
68 // Ensure anonymous users cannot post in the name of registered users. | |
69 $edit = [ | |
70 'name' => $this->adminUser->getUsername(), | |
71 'comment_body[0][value]' => $this->randomMachineName(), | |
72 ]; | |
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(), | |
76 ])); | |
77 | |
78 // Allow contact info. | |
79 $this->drupalLogin($this->adminUser); | |
80 $this->setCommentAnonymous(COMMENT_ANONYMOUS_MAY_CONTACT); | |
81 | |
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(); | |
87 | |
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.'); | |
91 | |
92 // Check the presence of expected cache tags. | |
93 $this->assertCacheTag('config:field.field.node.article.comment'); | |
94 $this->assertCacheTag('config:user.settings'); | |
95 | |
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.'); | |
98 | |
99 // Ensure anonymous users cannot post in the name of registered users. | |
100 $edit = [ | |
101 'name' => $this->adminUser->getUsername(), | |
102 'mail' => $this->randomMachineName() . '@example.com', | |
103 'subject[0][value]' => $this->randomMachineName(), | |
104 'comment_body[0][value]' => $this->randomMachineName(), | |
105 ]; | |
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(), | |
109 ])); | |
110 | |
111 // Require contact info. | |
112 $this->drupalLogin($this->adminUser); | |
113 $this->setCommentAnonymous(COMMENT_ANONYMOUS_MUST_CONTACT); | |
114 $this->drupalLogout(); | |
115 | |
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.'); | |
119 | |
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.'); | |
124 | |
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.'); | |
130 | |
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."); | |
137 | |
138 // Unpublish comment. | |
139 $this->performCommentOperation($anonymous_comment3, 'unpublish'); | |
140 | |
141 $this->drupalGet('admin/content/comment/approval'); | |
142 $this->assertRaw('comments[' . $anonymous_comment3->id() . ']', 'Comment was unpublished.'); | |
143 | |
144 // Publish comment. | |
145 $this->performCommentOperation($anonymous_comment3, 'publish', TRUE); | |
146 | |
147 $this->drupalGet('admin/content/comment'); | |
148 $this->assertRaw('comments[' . $anonymous_comment3->id() . ']', 'Comment was published.'); | |
149 | |
150 // Delete comment. | |
151 $this->performCommentOperation($anonymous_comment3, 'delete'); | |
152 | |
153 $this->drupalGet('admin/content/comment'); | |
154 $this->assertNoRaw('comments[' . $anonymous_comment3->id() . ']', 'Comment was deleted.'); | |
155 $this->drupalLogout(); | |
156 | |
157 // Comment 3 was deleted. | |
158 $this->drupalGet('comment/reply/node/' . $this->node->id() . '/comment/' . $anonymous_comment3->id()); | |
159 $this->assertResponse(403); | |
160 | |
161 // Reset. | |
162 user_role_change_permissions(RoleInterface::ANONYMOUS_ID, [ | |
163 'access comments' => FALSE, | |
164 'post comments' => FALSE, | |
165 'skip comment approval' => FALSE, | |
166 ]); | |
167 | |
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.'); | |
174 | |
175 // Attempt to view node-comment form while disallowed. | |
176 $this->drupalGet('comment/reply/node/' . $this->node->id() . '/comment'); | |
177 $this->assertResponse(403); | |
178 | |
179 user_role_change_permissions(RoleInterface::ANONYMOUS_ID, [ | |
180 'access comments' => TRUE, | |
181 'post comments' => FALSE, | |
182 'skip comment approval' => FALSE, | |
183 ]); | |
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.'); | |
188 | |
189 user_role_change_permissions(RoleInterface::ANONYMOUS_ID, [ | |
190 'access comments' => FALSE, | |
191 'post comments' => TRUE, | |
192 'skip comment approval' => TRUE, | |
193 ]); | |
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.'); | |
198 | |
199 $this->drupalGet('comment/reply/node/' . $this->node->id() . '/comment/' . $anonymous_comment2->id()); | |
200 $this->assertResponse(403); | |
201 } | |
202 | |
203 } |