annotate core/modules/comment/tests/src/Functional/CommentPagerTest.php @ 5:12f9dff5fda9 tip

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