annotate core/modules/comment/tests/src/Functional/CommentPagerTest.php @ 19:fa3358dc1485 tip

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