Mercurial > hg > isophonics-drupal-site
comparison core/modules/comment/src/Tests/CommentPagerTest.php @ 0:4c8ae668cc8c
Initial import (non-working)
author | Chris Cannam |
---|---|
date | Wed, 29 Nov 2017 16:09:58 +0000 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:4c8ae668cc8c |
---|---|
1 <?php | |
2 | |
3 namespace Drupal\comment\Tests; | |
4 | |
5 use Drupal\comment\CommentManagerInterface; | |
6 use Drupal\Component\Utility\SafeMarkup; | |
7 use Drupal\node\Entity\Node; | |
8 | |
9 /** | |
10 * Tests paging of comments and their settings. | |
11 * | |
12 * @group comment | |
13 */ | |
14 class CommentPagerTest extends CommentTestBase { | |
15 /** | |
16 * Confirms comment paging works correctly with flat and threaded comments. | |
17 */ | |
18 public function testCommentPaging() { | |
19 $this->drupalLogin($this->adminUser); | |
20 | |
21 // Set comment variables. | |
22 $this->setCommentForm(TRUE); | |
23 $this->setCommentSubject(TRUE); | |
24 $this->setCommentPreview(DRUPAL_DISABLED); | |
25 | |
26 // Create a node and three comments. | |
27 $node = $this->drupalCreateNode(['type' => 'article', 'promote' => 1]); | |
28 $comments = []; | |
29 $comments[] = $this->postComment($node, $this->randomMachineName(), $this->randomMachineName(), TRUE); | |
30 $comments[] = $this->postComment($node, $this->randomMachineName(), $this->randomMachineName(), TRUE); | |
31 $comments[] = $this->postComment($node, $this->randomMachineName(), $this->randomMachineName(), TRUE); | |
32 | |
33 $this->setCommentSettings('default_mode', CommentManagerInterface::COMMENT_MODE_FLAT, 'Comment paging changed.'); | |
34 | |
35 // Set comments to one per page so that we are able to test paging without | |
36 // needing to insert large numbers of comments. | |
37 $this->setCommentsPerPage(1); | |
38 | |
39 // Check the first page of the node, and confirm the correct comments are | |
40 // shown. | |
41 $this->drupalGet('node/' . $node->id()); | |
42 $this->assertRaw(t('next'), 'Paging links found.'); | |
43 $this->assertTrue($this->commentExists($comments[0]), 'Comment 1 appears on page 1.'); | |
44 $this->assertFalse($this->commentExists($comments[1]), 'Comment 2 does not appear on page 1.'); | |
45 $this->assertFalse($this->commentExists($comments[2]), 'Comment 3 does not appear on page 1.'); | |
46 | |
47 // Check the second page. | |
48 $this->drupalGet('node/' . $node->id(), ['query' => ['page' => 1]]); | |
49 $this->assertTrue($this->commentExists($comments[1]), 'Comment 2 appears on page 2.'); | |
50 $this->assertFalse($this->commentExists($comments[0]), 'Comment 1 does not appear on page 2.'); | |
51 $this->assertFalse($this->commentExists($comments[2]), 'Comment 3 does not appear on page 2.'); | |
52 | |
53 // Check the third page. | |
54 $this->drupalGet('node/' . $node->id(), ['query' => ['page' => 2]]); | |
55 $this->assertTrue($this->commentExists($comments[2]), 'Comment 3 appears on page 3.'); | |
56 $this->assertFalse($this->commentExists($comments[0]), 'Comment 1 does not appear on page 3.'); | |
57 $this->assertFalse($this->commentExists($comments[1]), 'Comment 2 does not appear on page 3.'); | |
58 | |
59 // Post a reply to the oldest comment and test again. | |
60 $oldest_comment = reset($comments); | |
61 $this->drupalGet('comment/reply/node/' . $node->id() . '/comment/' . $oldest_comment->id()); | |
62 $reply = $this->postComment(NULL, $this->randomMachineName(), $this->randomMachineName(), TRUE); | |
63 | |
64 $this->setCommentsPerPage(2); | |
65 // We are still in flat view - the replies should not be on the first page, | |
66 // even though they are replies to the oldest comment. | |
67 $this->drupalGet('node/' . $node->id(), ['query' => ['page' => 0]]); | |
68 $this->assertFalse($this->commentExists($reply, TRUE), 'In flat mode, reply does not appear on page 1.'); | |
69 | |
70 // If we switch to threaded mode, the replies on the oldest comment | |
71 // should be bumped to the first page and comment 6 should be bumped | |
72 // to the second page. | |
73 $this->setCommentSettings('default_mode', CommentManagerInterface::COMMENT_MODE_THREADED, 'Switched to threaded mode.'); | |
74 $this->drupalGet('node/' . $node->id(), ['query' => ['page' => 0]]); | |
75 $this->assertTrue($this->commentExists($reply, TRUE), 'In threaded mode, reply appears on page 1.'); | |
76 $this->assertFalse($this->commentExists($comments[1]), 'In threaded mode, comment 2 has been bumped off of page 1.'); | |
77 | |
78 // If (# replies > # comments per page) in threaded expanded view, | |
79 // the overage should be bumped. | |
80 $reply2 = $this->postComment(NULL, $this->randomMachineName(), $this->randomMachineName(), TRUE); | |
81 $this->drupalGet('node/' . $node->id(), ['query' => ['page' => 0]]); | |
82 $this->assertFalse($this->commentExists($reply2, TRUE), 'In threaded mode where # replies > # comments per page, the newest reply does not appear on page 1.'); | |
83 | |
84 // Test that the page build process does not somehow generate errors when | |
85 // # comments per page is set to 0. | |
86 $this->setCommentsPerPage(0); | |
87 $this->drupalGet('node/' . $node->id(), ['query' => ['page' => 0]]); | |
88 $this->assertFalse($this->commentExists($reply2, TRUE), 'Threaded mode works correctly when comments per page is 0.'); | |
89 | |
90 $this->drupalLogout(); | |
91 } | |
92 | |
93 /** | |
94 * Confirms comment paging works correctly with flat and threaded comments. | |
95 */ | |
96 public function testCommentPermalink() { | |
97 $this->drupalLogin($this->adminUser); | |
98 | |
99 // Set comment variables. | |
100 $this->setCommentForm(TRUE); | |
101 $this->setCommentSubject(TRUE); | |
102 $this->setCommentPreview(DRUPAL_DISABLED); | |
103 | |
104 // Create a node and three comments. | |
105 $node = $this->drupalCreateNode(['type' => 'article', 'promote' => 1]); | |
106 $comments = []; | |
107 $comments[] = $this->postComment($node, 'comment 1: ' . $this->randomMachineName(), $this->randomMachineName(), TRUE); | |
108 $comments[] = $this->postComment($node, 'comment 2: ' . $this->randomMachineName(), $this->randomMachineName(), TRUE); | |
109 $comments[] = $this->postComment($node, 'comment 3: ' . $this->randomMachineName(), $this->randomMachineName(), TRUE); | |
110 | |
111 $this->setCommentSettings('default_mode', CommentManagerInterface::COMMENT_MODE_FLAT, 'Comment paging changed.'); | |
112 | |
113 // Set comments to one per page so that we are able to test paging without | |
114 // needing to insert large numbers of comments. | |
115 $this->setCommentsPerPage(1); | |
116 | |
117 // Navigate to each comment permalink as anonymous and assert it appears on | |
118 // the page. | |
119 foreach ($comments as $index => $comment) { | |
120 $this->drupalGet($comment->toUrl()); | |
121 $this->assertTrue($this->commentExists($comment), sprintf('Comment %d appears on page %d.', $index + 1, $index + 1)); | |
122 } | |
123 } | |
124 | |
125 /** | |
126 * Tests comment ordering and threading. | |
127 */ | |
128 public function testCommentOrderingThreading() { | |
129 $this->drupalLogin($this->adminUser); | |
130 | |
131 // Set comment variables. | |
132 $this->setCommentForm(TRUE); | |
133 $this->setCommentSubject(TRUE); | |
134 $this->setCommentPreview(DRUPAL_DISABLED); | |
135 | |
136 // Display all the comments on the same page. | |
137 $this->setCommentsPerPage(1000); | |
138 | |
139 // Create a node and three comments. | |
140 $node = $this->drupalCreateNode(['type' => 'article', 'promote' => 1]); | |
141 $comments = []; | |
142 $comments[] = $this->postComment($node, $this->randomMachineName(), $this->randomMachineName(), TRUE); | |
143 $comments[] = $this->postComment($node, $this->randomMachineName(), $this->randomMachineName(), TRUE); | |
144 $comments[] = $this->postComment($node, $this->randomMachineName(), $this->randomMachineName(), TRUE); | |
145 | |
146 // Post a reply to the second comment. | |
147 $this->drupalGet('comment/reply/node/' . $node->id() . '/comment/' . $comments[1]->id()); | |
148 $comments[] = $this->postComment(NULL, $this->randomMachineName(), $this->randomMachineName(), TRUE); | |
149 | |
150 // Post a reply to the first comment. | |
151 $this->drupalGet('comment/reply/node/' . $node->id() . '/comment/' . $comments[0]->id()); | |
152 $comments[] = $this->postComment(NULL, $this->randomMachineName(), $this->randomMachineName(), TRUE); | |
153 | |
154 // Post a reply to the last comment. | |
155 $this->drupalGet('comment/reply/node/' . $node->id() . '/comment/' . $comments[2]->id()); | |
156 $comments[] = $this->postComment(NULL, $this->randomMachineName(), $this->randomMachineName(), TRUE); | |
157 | |
158 // Post a reply to the second comment. | |
159 $this->drupalGet('comment/reply/node/' . $node->id() . '/comment/' . $comments[3]->id()); | |
160 $comments[] = $this->postComment(NULL, $this->randomMachineName(), $this->randomMachineName(), TRUE); | |
161 | |
162 // At this point, the comment tree is: | |
163 // - 0 | |
164 // - 4 | |
165 // - 1 | |
166 // - 3 | |
167 // - 6 | |
168 // - 2 | |
169 // - 5 | |
170 | |
171 $this->setCommentSettings('default_mode', CommentManagerInterface::COMMENT_MODE_FLAT, 'Comment paging changed.'); | |
172 | |
173 $expected_order = [ | |
174 0, | |
175 1, | |
176 2, | |
177 3, | |
178 4, | |
179 5, | |
180 6, | |
181 ]; | |
182 $this->drupalGet('node/' . $node->id()); | |
183 $this->assertCommentOrder($comments, $expected_order); | |
184 | |
185 $this->setCommentSettings('default_mode', CommentManagerInterface::COMMENT_MODE_THREADED, 'Switched to threaded mode.'); | |
186 | |
187 $expected_order = [ | |
188 0, | |
189 4, | |
190 1, | |
191 3, | |
192 6, | |
193 2, | |
194 5, | |
195 ]; | |
196 $this->drupalGet('node/' . $node->id()); | |
197 $this->assertCommentOrder($comments, $expected_order); | |
198 } | |
199 | |
200 /** | |
201 * Asserts that the comments are displayed in the correct order. | |
202 * | |
203 * @param \Drupal\comment\CommentInterface[] $comments | |
204 * An array of comments, must be of the type CommentInterface. | |
205 * @param array $expected_order | |
206 * An array of keys from $comments describing the expected order. | |
207 */ | |
208 public function assertCommentOrder(array $comments, array $expected_order) { | |
209 $expected_cids = []; | |
210 | |
211 // First, rekey the expected order by cid. | |
212 foreach ($expected_order as $key) { | |
213 $expected_cids[] = $comments[$key]->id(); | |
214 } | |
215 | |
216 $comment_anchors = $this->xpath('//a[starts-with(@id,"comment-")]'); | |
217 $result_order = []; | |
218 foreach ($comment_anchors as $anchor) { | |
219 $result_order[] = substr($anchor['id'], 8); | |
220 } | |
221 return $this->assertEqual($expected_cids, $result_order, format_string('Comment order: expected @expected, returned @returned.', ['@expected' => implode(',', $expected_cids), '@returned' => implode(',', $result_order)])); | |
222 } | |
223 | |
224 /** | |
225 * Tests calculation of first page with new comment. | |
226 */ | |
227 public function testCommentNewPageIndicator() { | |
228 $this->drupalLogin($this->adminUser); | |
229 | |
230 // Set comment variables. | |
231 $this->setCommentForm(TRUE); | |
232 $this->setCommentSubject(TRUE); | |
233 $this->setCommentPreview(DRUPAL_DISABLED); | |
234 | |
235 // Set comments to one per page so that we are able to test paging without | |
236 // needing to insert large numbers of comments. | |
237 $this->setCommentsPerPage(1); | |
238 | |
239 // Create a node and three comments. | |
240 $node = $this->drupalCreateNode(['type' => 'article', 'promote' => 1]); | |
241 $comments = []; | |
242 $comments[] = $this->postComment($node, $this->randomMachineName(), $this->randomMachineName(), TRUE); | |
243 $comments[] = $this->postComment($node, $this->randomMachineName(), $this->randomMachineName(), TRUE); | |
244 $comments[] = $this->postComment($node, $this->randomMachineName(), $this->randomMachineName(), TRUE); | |
245 | |
246 // Post a reply to the second comment. | |
247 $this->drupalGet('comment/reply/node/' . $node->id() . '/comment/' . $comments[1]->id()); | |
248 $comments[] = $this->postComment(NULL, $this->randomMachineName(), $this->randomMachineName(), TRUE); | |
249 | |
250 // Post a reply to the first comment. | |
251 $this->drupalGet('comment/reply/node/' . $node->id() . '/comment/' . $comments[0]->id()); | |
252 $comments[] = $this->postComment(NULL, $this->randomMachineName(), $this->randomMachineName(), TRUE); | |
253 | |
254 // Post a reply to the last comment. | |
255 $this->drupalGet('comment/reply/node/' . $node->id() . '/comment/' . $comments[2]->id()); | |
256 $comments[] = $this->postComment(NULL, $this->randomMachineName(), $this->randomMachineName(), TRUE); | |
257 | |
258 // At this point, the comment tree is: | |
259 // - 0 | |
260 // - 4 | |
261 // - 1 | |
262 // - 3 | |
263 // - 2 | |
264 // - 5 | |
265 | |
266 $this->setCommentSettings('default_mode', CommentManagerInterface::COMMENT_MODE_FLAT, 'Comment paging changed.'); | |
267 | |
268 $expected_pages = [ | |
269 // Page of comment 5 | |
270 1 => 5, | |
271 // Page of comment 4 | |
272 2 => 4, | |
273 // Page of comment 3 | |
274 3 => 3, | |
275 // Page of comment 2 | |
276 4 => 2, | |
277 // Page of comment 1 | |
278 5 => 1, | |
279 // Page of comment 0 | |
280 6 => 0, | |
281 ]; | |
282 | |
283 $node = Node::load($node->id()); | |
284 foreach ($expected_pages as $new_replies => $expected_page) { | |
285 $returned_page = \Drupal::entityManager()->getStorage('comment') | |
286 ->getNewCommentPageNumber($node->get('comment')->comment_count, $new_replies, $node, 'comment'); | |
287 $this->assertIdentical($expected_page, $returned_page, format_string('Flat mode, @new replies: expected page @expected, returned page @returned.', ['@new' => $new_replies, '@expected' => $expected_page, '@returned' => $returned_page])); | |
288 } | |
289 | |
290 $this->setCommentSettings('default_mode', CommentManagerInterface::COMMENT_MODE_THREADED, 'Switched to threaded mode.'); | |
291 | |
292 $expected_pages = [ | |
293 // Page of comment 5 | |
294 1 => 5, | |
295 // Page of comment 4 | |
296 2 => 1, | |
297 // Page of comment 4 | |
298 3 => 1, | |
299 // Page of comment 4 | |
300 4 => 1, | |
301 // Page of comment 4 | |
302 5 => 1, | |
303 // Page of comment 0 | |
304 6 => 0, | |
305 ]; | |
306 | |
307 \Drupal::entityManager()->getStorage('node')->resetCache([$node->id()]); | |
308 $node = Node::load($node->id()); | |
309 foreach ($expected_pages as $new_replies => $expected_page) { | |
310 $returned_page = \Drupal::entityManager()->getStorage('comment') | |
311 ->getNewCommentPageNumber($node->get('comment')->comment_count, $new_replies, $node, 'comment'); | |
312 $this->assertEqual($expected_page, $returned_page, format_string('Threaded mode, @new replies: expected page @expected, returned page @returned.', ['@new' => $new_replies, '@expected' => $expected_page, '@returned' => $returned_page])); | |
313 } | |
314 } | |
315 | |
316 /** | |
317 * Confirms comment paging works correctly with two pagers. | |
318 */ | |
319 public function testTwoPagers() { | |
320 // Add another field to article content-type. | |
321 $this->addDefaultCommentField('node', 'article', 'comment_2'); | |
322 // Set default to display comment list with unique pager id. | |
323 entity_get_display('node', 'article', 'default') | |
324 ->setComponent('comment_2', [ | |
325 'label' => 'hidden', | |
326 'type' => 'comment_default', | |
327 'weight' => 30, | |
328 'settings' => [ | |
329 'pager_id' => 1, | |
330 'view_mode' => 'default', | |
331 ] | |
332 ]) | |
333 ->save(); | |
334 | |
335 // Make sure pager appears in formatter summary and settings form. | |
336 $account = $this->drupalCreateUser(['administer node display']); | |
337 $this->drupalLogin($account); | |
338 $this->drupalGet('admin/structure/types/manage/article/display'); | |
339 $this->assertNoText(t('Pager ID: @id', ['@id' => 0]), 'No summary for standard pager'); | |
340 $this->assertText(t('Pager ID: @id', ['@id' => 1])); | |
341 $this->drupalPostAjaxForm(NULL, [], 'comment_settings_edit'); | |
342 // Change default pager to 2. | |
343 $this->drupalPostForm(NULL, ['fields[comment][settings_edit_form][settings][pager_id]' => 2], t('Save')); | |
344 $this->assertText(t('Pager ID: @id', ['@id' => 2])); | |
345 // Revert the changes. | |
346 $this->drupalPostAjaxForm(NULL, [], 'comment_settings_edit'); | |
347 $this->drupalPostForm(NULL, ['fields[comment][settings_edit_form][settings][pager_id]' => 0], t('Save')); | |
348 $this->assertNoText(t('Pager ID: @id', ['@id' => 0]), 'No summary for standard pager'); | |
349 | |
350 $this->drupalLogin($this->adminUser); | |
351 | |
352 // Add a new node with both comment fields open. | |
353 $node = $this->drupalCreateNode(['type' => 'article', 'promote' => 1, 'uid' => $this->webUser->id()]); | |
354 // Set comment options. | |
355 $comments = []; | |
356 foreach (['comment', 'comment_2'] as $field_name) { | |
357 $this->setCommentForm(TRUE, $field_name); | |
358 $this->setCommentPreview(DRUPAL_OPTIONAL, $field_name); | |
359 $this->setCommentSettings('default_mode', CommentManagerInterface::COMMENT_MODE_FLAT, 'Comment paging changed.', $field_name); | |
360 | |
361 // Set comments to one per page so that we are able to test paging without | |
362 // needing to insert large numbers of comments. | |
363 $this->setCommentsPerPage(1, $field_name); | |
364 for ($i = 0; $i < 3; $i++) { | |
365 $comment = t('Comment @count on field @field', [ | |
366 '@count' => $i + 1, | |
367 '@field' => $field_name, | |
368 ]); | |
369 $comments[] = $this->postComment($node, $comment, $comment, TRUE, $field_name); | |
370 } | |
371 } | |
372 | |
373 // Check the first page of the node, and confirm the correct comments are | |
374 // shown. | |
375 $this->drupalGet('node/' . $node->id()); | |
376 $this->assertRaw(t('next'), 'Paging links found.'); | |
377 $this->assertRaw('Comment 1 on field comment'); | |
378 $this->assertRaw('Comment 1 on field comment_2'); | |
379 // Navigate to next page of field 1. | |
380 $this->clickLinkWithXPath('//h3/a[normalize-space(text())=:label]/ancestor::section[1]//a[@rel="next"]', [':label' => 'Comment 1 on field comment']); | |
381 // Check only one pager updated. | |
382 $this->assertRaw('Comment 2 on field comment'); | |
383 $this->assertRaw('Comment 1 on field comment_2'); | |
384 // Return to page 1. | |
385 $this->drupalGet('node/' . $node->id()); | |
386 // Navigate to next page of field 2. | |
387 $this->clickLinkWithXPath('//h3/a[normalize-space(text())=:label]/ancestor::section[1]//a[@rel="next"]', [':label' => 'Comment 1 on field comment_2']); | |
388 // Check only one pager updated. | |
389 $this->assertRaw('Comment 1 on field comment'); | |
390 $this->assertRaw('Comment 2 on field comment_2'); | |
391 // Navigate to next page of field 1. | |
392 $this->clickLinkWithXPath('//h3/a[normalize-space(text())=:label]/ancestor::section[1]//a[@rel="next"]', [':label' => 'Comment 1 on field comment']); | |
393 // Check only one pager updated. | |
394 $this->assertRaw('Comment 2 on field comment'); | |
395 $this->assertRaw('Comment 2 on field comment_2'); | |
396 } | |
397 | |
398 /** | |
399 * Follows a link found at a give xpath query. | |
400 * | |
401 * Will click the first link found with the given xpath query by default, | |
402 * or a later one if an index is given. | |
403 * | |
404 * If the link is discovered and clicked, the test passes. Fail otherwise. | |
405 * | |
406 * @param string $xpath | |
407 * Xpath query that targets an anchor tag, or set of anchor tags. | |
408 * @param array $arguments | |
409 * An array of arguments with keys in the form ':name' matching the | |
410 * placeholders in the query. The values may be either strings or numeric | |
411 * values. | |
412 * @param int $index | |
413 * Link position counting from zero. | |
414 * | |
415 * @return string|false | |
416 * Page contents on success, or FALSE on failure. | |
417 * | |
418 * @see WebTestBase::clickLink() | |
419 */ | |
420 protected function clickLinkWithXPath($xpath, $arguments = [], $index = 0) { | |
421 $url_before = $this->getUrl(); | |
422 $urls = $this->xpath($xpath, $arguments); | |
423 if (isset($urls[$index])) { | |
424 $url_target = $this->getAbsoluteUrl($urls[$index]['href']); | |
425 $this->pass(SafeMarkup::format('Clicked link %label (@url_target) from @url_before', ['%label' => $xpath, '@url_target' => $url_target, '@url_before' => $url_before]), 'Browser'); | |
426 return $this->drupalGet($url_target); | |
427 } | |
428 $this->fail(SafeMarkup::format('Link %label does not exist on @url_before', ['%label' => $xpath, '@url_before' => $url_before]), 'Browser'); | |
429 return FALSE; | |
430 } | |
431 | |
432 } |