Mercurial > hg > cmmr2012-drupal-site
comparison core/modules/comment/tests/src/Functional/CommentInterfaceTest.php @ 0:c75dbcec494b
Initial commit from drush-created site
author | Chris Cannam |
---|---|
date | Thu, 05 Jul 2018 14:24:15 +0000 |
parents | |
children | a9cd425dd02b |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:c75dbcec494b |
---|---|
1 <?php | |
2 | |
3 namespace Drupal\Tests\comment\Functional; | |
4 | |
5 use Drupal\comment\CommentManagerInterface; | |
6 use Drupal\comment\Plugin\Field\FieldType\CommentItemInterface; | |
7 use Drupal\comment\Entity\Comment; | |
8 use Drupal\Component\Utility\Unicode; | |
9 use Drupal\Core\Entity\Entity\EntityViewDisplay; | |
10 use Drupal\Core\Entity\Entity\EntityViewMode; | |
11 use Drupal\user\RoleInterface; | |
12 use Drupal\filter\Entity\FilterFormat; | |
13 | |
14 /** | |
15 * Tests comment user interfaces. | |
16 * | |
17 * @group comment | |
18 */ | |
19 class CommentInterfaceTest extends CommentTestBase { | |
20 | |
21 /** | |
22 * Set up comments to have subject and preview disabled. | |
23 */ | |
24 protected function setUp() { | |
25 parent::setUp(); | |
26 $this->drupalLogin($this->adminUser); | |
27 // Make sure that comment field title is not displayed when there's no | |
28 // comments posted. | |
29 $this->drupalGet($this->node->urlInfo()); | |
30 $this->assertNoPattern('@<h2[^>]*>Comments</h2>@', 'Comments title is not displayed.'); | |
31 | |
32 // Set comments to have subject and preview disabled. | |
33 $this->setCommentPreview(DRUPAL_DISABLED); | |
34 $this->setCommentForm(TRUE); | |
35 $this->setCommentSubject(FALSE); | |
36 $this->setCommentSettings('default_mode', CommentManagerInterface::COMMENT_MODE_THREADED, 'Comment paging changed.'); | |
37 $this->drupalLogout(); | |
38 } | |
39 | |
40 /** | |
41 * Tests the comment interface. | |
42 */ | |
43 public function testCommentInterface() { | |
44 | |
45 // Post comment #1 without subject or preview. | |
46 $this->drupalLogin($this->webUser); | |
47 $comment_text = $this->randomMachineName(); | |
48 $comment = $this->postComment($this->node, $comment_text); | |
49 $this->assertTrue($this->commentExists($comment), 'Comment found.'); | |
50 | |
51 // Test the comment field title is displayed when there's comments. | |
52 $this->drupalGet($this->node->urlInfo()); | |
53 $this->assertPattern('@<h2[^>]*>Comments</h2>@', 'Comments title is displayed.'); | |
54 | |
55 // Set comments to have subject and preview to required. | |
56 $this->drupalLogout(); | |
57 $this->drupalLogin($this->adminUser); | |
58 $this->setCommentSubject(TRUE); | |
59 $this->setCommentPreview(DRUPAL_REQUIRED); | |
60 $this->drupalLogout(); | |
61 | |
62 // Create comment #2 that allows subject and requires preview. | |
63 $this->drupalLogin($this->webUser); | |
64 $subject_text = $this->randomMachineName(); | |
65 $comment_text = $this->randomMachineName(); | |
66 $comment = $this->postComment($this->node, $comment_text, $subject_text, TRUE); | |
67 $this->assertTrue($this->commentExists($comment), 'Comment found.'); | |
68 | |
69 // Comment as anonymous with preview required. | |
70 $this->drupalLogout(); | |
71 user_role_grant_permissions(RoleInterface::ANONYMOUS_ID, ['access content', 'access comments', 'post comments', 'skip comment approval']); | |
72 $anonymous_comment = $this->postComment($this->node, $this->randomMachineName(), $this->randomMachineName(), TRUE); | |
73 $this->assertTrue($this->commentExists($anonymous_comment), 'Comment found.'); | |
74 $anonymous_comment->delete(); | |
75 | |
76 // Check comment display. | |
77 $this->drupalLogin($this->webUser); | |
78 $this->drupalGet('node/' . $this->node->id()); | |
79 $this->assertText($subject_text, 'Individual comment subject found.'); | |
80 $this->assertText($comment_text, 'Individual comment body found.'); | |
81 $arguments = [ | |
82 ':link' => base_path() . 'comment/' . $comment->id() . '#comment-' . $comment->id(), | |
83 ]; | |
84 $pattern_permalink = '//footer[contains(@class,"comment__meta")]/a[contains(@href,:link) and text()="Permalink"]'; | |
85 $permalink = $this->xpath($pattern_permalink, $arguments); | |
86 $this->assertTrue(!empty($permalink), 'Permalink link found.'); | |
87 | |
88 // Set comments to have subject and preview to optional. | |
89 $this->drupalLogout(); | |
90 $this->drupalLogin($this->adminUser); | |
91 $this->setCommentSubject(TRUE); | |
92 $this->setCommentPreview(DRUPAL_OPTIONAL); | |
93 | |
94 $this->drupalGet('comment/' . $comment->id() . '/edit'); | |
95 $this->assertTitle(t('Edit comment @title | Drupal', [ | |
96 '@title' => $comment->getSubject(), | |
97 ])); | |
98 | |
99 // Test changing the comment author to "Anonymous". | |
100 $comment = $this->postComment(NULL, $comment->comment_body->value, $comment->getSubject(), ['uid' => '']); | |
101 $this->assertTrue($comment->getAuthorName() == t('Anonymous') && $comment->getOwnerId() == 0, 'Comment author successfully changed to anonymous.'); | |
102 | |
103 // Test changing the comment author to an unverified user. | |
104 $random_name = $this->randomMachineName(); | |
105 $this->drupalGet('comment/' . $comment->id() . '/edit'); | |
106 $comment = $this->postComment(NULL, $comment->comment_body->value, $comment->getSubject(), ['name' => $random_name]); | |
107 $this->drupalGet('node/' . $this->node->id()); | |
108 $this->assertText($random_name . ' (' . t('not verified') . ')', 'Comment author successfully changed to an unverified user.'); | |
109 | |
110 // Test changing the comment author to a verified user. | |
111 $this->drupalGet('comment/' . $comment->id() . '/edit'); | |
112 $comment = $this->postComment(NULL, $comment->comment_body->value, $comment->getSubject(), ['uid' => $this->webUser->getUsername() . ' (' . $this->webUser->id() . ')']); | |
113 $this->assertTrue($comment->getAuthorName() == $this->webUser->getUsername() && $comment->getOwnerId() == $this->webUser->id(), 'Comment author successfully changed to a registered user.'); | |
114 | |
115 $this->drupalLogout(); | |
116 | |
117 // Reply to comment #2 creating comment #3 with optional preview and no | |
118 // subject though field enabled. | |
119 $this->drupalLogin($this->webUser); | |
120 // Deliberately use the wrong url to test | |
121 // \Drupal\comment\Controller\CommentController::redirectNode(). | |
122 $this->drupalGet('comment/' . $this->node->id() . '/reply'); | |
123 // Verify we were correctly redirected. | |
124 $this->assertUrl(\Drupal::url('comment.reply', ['entity_type' => 'node', 'entity' => $this->node->id(), 'field_name' => 'comment'], ['absolute' => TRUE])); | |
125 $this->drupalGet('comment/reply/node/' . $this->node->id() . '/comment/' . $comment->id()); | |
126 $this->assertText($subject_text, 'Individual comment-reply subject found.'); | |
127 $this->assertText($comment_text, 'Individual comment-reply body found.'); | |
128 $reply = $this->postComment(NULL, $this->randomMachineName(), '', TRUE); | |
129 $reply_loaded = Comment::load($reply->id()); | |
130 $this->assertTrue($this->commentExists($reply, TRUE), 'Reply found.'); | |
131 $this->assertEqual($comment->id(), $reply_loaded->getParentComment()->id(), 'Pid of a reply to a comment is set correctly.'); | |
132 // Check the thread of reply grows correctly. | |
133 $this->assertEqual(rtrim($comment->getThread(), '/') . '.00/', $reply_loaded->getThread()); | |
134 | |
135 // Second reply to comment #2 creating comment #4. | |
136 $this->drupalGet('comment/reply/node/' . $this->node->id() . '/comment/' . $comment->id()); | |
137 $this->assertText($comment->getSubject(), 'Individual comment-reply subject found.'); | |
138 $this->assertText($comment->comment_body->value, 'Individual comment-reply body found.'); | |
139 $reply = $this->postComment(NULL, $this->randomMachineName(), $this->randomMachineName(), TRUE); | |
140 $reply_loaded = Comment::load($reply->id()); | |
141 $this->assertTrue($this->commentExists($reply, TRUE), 'Second reply found.'); | |
142 // Check the thread of second reply grows correctly. | |
143 $this->assertEqual(rtrim($comment->getThread(), '/') . '.01/', $reply_loaded->getThread()); | |
144 | |
145 // Reply to comment #4 creating comment #5. | |
146 $this->drupalGet('comment/reply/node/' . $this->node->id() . '/comment/' . $reply_loaded->id()); | |
147 $this->assertText($reply_loaded->getSubject(), 'Individual comment-reply subject found.'); | |
148 $this->assertText($reply_loaded->comment_body->value, 'Individual comment-reply body found.'); | |
149 $reply = $this->postComment(NULL, $this->randomMachineName(), $this->randomMachineName(), TRUE); | |
150 $reply_loaded = Comment::load($reply->id()); | |
151 $this->assertTrue($this->commentExists($reply, TRUE), 'Second reply found.'); | |
152 // Check the thread of reply to second reply grows correctly. | |
153 $this->assertEqual(rtrim($comment->getThread(), '/') . '.01.00/', $reply_loaded->getThread()); | |
154 | |
155 // Edit reply. | |
156 $this->drupalGet('comment/' . $reply->id() . '/edit'); | |
157 $reply = $this->postComment(NULL, $this->randomMachineName(), $this->randomMachineName(), TRUE); | |
158 $this->assertTrue($this->commentExists($reply, TRUE), 'Modified reply found.'); | |
159 | |
160 // Confirm a new comment is posted to the correct page. | |
161 $this->setCommentsPerPage(2); | |
162 $comment_new_page = $this->postComment($this->node, $this->randomMachineName(), $this->randomMachineName(), TRUE); | |
163 $this->assertTrue($this->commentExists($comment_new_page), 'Page one exists. %s'); | |
164 $this->drupalGet('node/' . $this->node->id(), ['query' => ['page' => 2]]); | |
165 $this->assertTrue($this->commentExists($reply, TRUE), 'Page two exists. %s'); | |
166 $this->setCommentsPerPage(50); | |
167 | |
168 // Attempt to reply to an unpublished comment. | |
169 $reply_loaded->setPublished(FALSE); | |
170 $reply_loaded->save(); | |
171 $this->drupalGet('comment/reply/node/' . $this->node->id() . '/comment/' . $reply_loaded->id()); | |
172 $this->assertResponse(403); | |
173 | |
174 // Attempt to post to node with comments disabled. | |
175 $this->node = $this->drupalCreateNode(['type' => 'article', 'promote' => 1, 'comment' => [['status' => CommentItemInterface::HIDDEN]]]); | |
176 $this->assertTrue($this->node, 'Article node created.'); | |
177 $this->drupalGet('comment/reply/node/' . $this->node->id() . '/comment'); | |
178 $this->assertResponse(403); | |
179 $this->assertNoField('edit-comment', 'Comment body field found.'); | |
180 | |
181 // Attempt to post to node with read-only comments. | |
182 $this->node = $this->drupalCreateNode(['type' => 'article', 'promote' => 1, 'comment' => [['status' => CommentItemInterface::CLOSED]]]); | |
183 $this->assertTrue($this->node, 'Article node created.'); | |
184 $this->drupalGet('comment/reply/node/' . $this->node->id() . '/comment'); | |
185 $this->assertResponse(403); | |
186 $this->assertNoField('edit-comment', 'Comment body field found.'); | |
187 | |
188 // Attempt to post to node with comments enabled (check field names etc). | |
189 $this->node = $this->drupalCreateNode(['type' => 'article', 'promote' => 1, 'comment' => [['status' => CommentItemInterface::OPEN]]]); | |
190 $this->assertTrue($this->node, 'Article node created.'); | |
191 $this->drupalGet('comment/reply/node/' . $this->node->id() . '/comment'); | |
192 $this->assertNoText('This discussion is closed', 'Posting to node with comments enabled'); | |
193 $this->assertField('edit-comment-body-0-value', 'Comment body field found.'); | |
194 | |
195 // Delete comment and make sure that reply is also removed. | |
196 $this->drupalLogout(); | |
197 $this->drupalLogin($this->adminUser); | |
198 $this->deleteComment($comment); | |
199 $this->deleteComment($comment_new_page); | |
200 | |
201 $this->drupalGet('node/' . $this->node->id()); | |
202 $this->assertFalse($this->commentExists($comment), 'Comment not found.'); | |
203 $this->assertFalse($this->commentExists($reply, TRUE), 'Reply not found.'); | |
204 | |
205 // Enabled comment form on node page. | |
206 $this->drupalLogin($this->adminUser); | |
207 $this->setCommentForm(TRUE); | |
208 $this->drupalLogout(); | |
209 | |
210 // Submit comment through node form. | |
211 $this->drupalLogin($this->webUser); | |
212 $this->drupalGet('node/' . $this->node->id()); | |
213 $form_comment = $this->postComment(NULL, $this->randomMachineName(), $this->randomMachineName(), TRUE); | |
214 $this->assertTrue($this->commentExists($form_comment), 'Form comment found.'); | |
215 | |
216 // Disable comment form on node page. | |
217 $this->drupalLogout(); | |
218 $this->drupalLogin($this->adminUser); | |
219 $this->setCommentForm(FALSE); | |
220 } | |
221 | |
222 /** | |
223 * Test that the subject is automatically filled if disabled or left blank. | |
224 * | |
225 * When the subject field is blank or disabled, the first 29 characters of the | |
226 * comment body are used for the subject. If this would break within a word, | |
227 * then the break is put at the previous word boundary instead. | |
228 */ | |
229 public function testAutoFilledSubject() { | |
230 $this->drupalLogin($this->webUser); | |
231 $this->drupalGet('node/' . $this->node->id()); | |
232 | |
233 // Break when there is a word boundary before 29 characters. | |
234 $body_text = 'Lorem ipsum Lorem ipsum Loreming ipsum Lorem ipsum'; | |
235 $comment1 = $this->postComment(NULL, $body_text, '', TRUE); | |
236 $this->assertTrue($this->commentExists($comment1), 'Form comment found.'); | |
237 $this->assertEqual('Lorem ipsum Lorem ipsum…', $comment1->getSubject()); | |
238 | |
239 // Break at 29 characters where there's no boundary before that. | |
240 $body_text2 = 'LoremipsumloremipsumLoremingipsumLoremipsum'; | |
241 $comment2 = $this->postComment(NULL, $body_text2, '', TRUE); | |
242 $this->assertEqual('LoremipsumloremipsumLoreming…', $comment2->getSubject()); | |
243 } | |
244 | |
245 /** | |
246 * Test that automatic subject is correctly created from HTML comment text. | |
247 * | |
248 * This is the same test as in CommentInterfaceTest::testAutoFilledSubject() | |
249 * with the additional check that HTML is stripped appropriately prior to | |
250 * character-counting. | |
251 */ | |
252 public function testAutoFilledHtmlSubject() { | |
253 // Set up two default (i.e. filtered HTML) input formats, because then we | |
254 // can select one of them. Then create a user that can use these formats, | |
255 // log the user in, and then GET the node page on which to test the | |
256 // comments. | |
257 $filtered_html_format = FilterFormat::create([ | |
258 'format' => 'filtered_html', | |
259 'name' => 'Filtered HTML', | |
260 ]); | |
261 $filtered_html_format->save(); | |
262 $full_html_format = FilterFormat::create([ | |
263 'format' => 'full_html', | |
264 'name' => 'Full HTML', | |
265 ]); | |
266 $full_html_format->save(); | |
267 $html_user = $this->drupalCreateUser([ | |
268 'access comments', | |
269 'post comments', | |
270 'edit own comments', | |
271 'skip comment approval', | |
272 'access content', | |
273 $filtered_html_format->getPermissionName(), | |
274 $full_html_format->getPermissionName(), | |
275 ]); | |
276 $this->drupalLogin($html_user); | |
277 $this->drupalGet('node/' . $this->node->id()); | |
278 | |
279 // HTML should not be included in the character count. | |
280 $body_text1 = '<span></span><strong> </strong><span> </span><strong></strong>Hello World<br />'; | |
281 $edit1 = [ | |
282 'comment_body[0][value]' => $body_text1, | |
283 'comment_body[0][format]' => 'filtered_html', | |
284 ]; | |
285 $this->drupalPostForm(NULL, $edit1, t('Save')); | |
286 $this->assertEqual('Hello World', Comment::load(1)->getSubject()); | |
287 | |
288 // If there's nothing other than HTML, the subject should be '(No subject)'. | |
289 $body_text2 = '<span></span><strong> </strong><span> </span><strong></strong> <br />'; | |
290 $edit2 = [ | |
291 'comment_body[0][value]' => $body_text2, | |
292 'comment_body[0][format]' => 'filtered_html', | |
293 ]; | |
294 $this->drupalPostForm(NULL, $edit2, t('Save')); | |
295 $this->assertEqual('(No subject)', Comment::load(2)->getSubject()); | |
296 } | |
297 | |
298 /** | |
299 * Tests the comment formatter configured with a custom comment view mode. | |
300 */ | |
301 public function testViewMode() { | |
302 $this->drupalLogin($this->webUser); | |
303 $this->drupalGet($this->node->toUrl()); | |
304 $comment_text = $this->randomMachineName(); | |
305 // Post a comment. | |
306 $this->postComment($this->node, $comment_text); | |
307 | |
308 // Comment displayed in 'default' display mode found and has body text. | |
309 $comment_element = $this->cssSelect('.comment-wrapper'); | |
310 $this->assertTrue(!empty($comment_element)); | |
311 $this->assertRaw('<p>' . $comment_text . '</p>'); | |
312 | |
313 // Create a new comment entity view mode. | |
314 $mode = Unicode::strtolower($this->randomMachineName()); | |
315 EntityViewMode::create([ | |
316 'targetEntityType' => 'comment', | |
317 'id' => "comment.$mode", | |
318 ])->save(); | |
319 // Create the corresponding entity view display for article node-type. Note | |
320 // that this new view display mode doesn't contain the comment body. | |
321 EntityViewDisplay::create([ | |
322 'targetEntityType' => 'comment', | |
323 'bundle' => 'comment', | |
324 'mode' => $mode, | |
325 ])->setStatus(TRUE)->save(); | |
326 | |
327 /** @var \Drupal\Core\Entity\Display\EntityViewDisplayInterface $node_display */ | |
328 $node_display = EntityViewDisplay::load('node.article.default'); | |
329 $formatter = $node_display->getComponent('comment'); | |
330 // Change the node comment field formatter to use $mode mode instead of | |
331 // 'default' mode. | |
332 $formatter['settings']['view_mode'] = $mode; | |
333 $node_display | |
334 ->setComponent('comment', $formatter) | |
335 ->save(); | |
336 | |
337 // Reloading the node page to show the same node with its same comment but | |
338 // with a different display mode. | |
339 $this->drupalGet($this->node->toUrl()); | |
340 // The comment should exist but without the body text because we used $mode | |
341 // mode this time. | |
342 $comment_element = $this->cssSelect('.comment-wrapper'); | |
343 $this->assertTrue(!empty($comment_element)); | |
344 $this->assertNoRaw('<p>' . $comment_text . '</p>'); | |
345 } | |
346 | |
347 } |