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