Commit c3a97f59 authored by catch's avatar catch
Browse files

Issue #3220255 by Spokje, mondrake, longwave: Convert assertions involving use...

Issue #3220255 by Spokje, mondrake, longwave: Convert assertions involving use of xpath on links to WebAssert
parent 61e0890f
......@@ -162,11 +162,14 @@ public function testAddBlockFromLibraryWithWeight() {
'plugin_id' => $block_name,
'theme' => $default_theme,
]);
$links = $this->xpath('//a[contains(@href, :href)]', [':href' => $add_url->toString()]);
$this->assertCount(1, $links, 'Found one matching link.');
$this->assertEquals(t('Place block'), $links[0]->getText(), 'Found the expected link text.');
list($path, $query_string) = explode('?', $links[0]->getAttribute('href'), 2);
// Verify that one link is found, with the the expected link text.
$xpath = $this->assertSession()->buildXPathQuery('//a[contains(@href, :href)]', [':href' => $add_url->toString()]);
$this->assertSession()->elementsCount('xpath', $xpath, 1);
$this->assertSession()->elementTextEquals('xpath', $xpath, 'Place block');
$link = $this->getSession()->getPage()->find('xpath', $xpath);
list($path, $query_string) = explode('?', $link->getAttribute('href'), 2);
parse_str($query_string, $query_parts);
$this->assertEquals($weight, $query_parts['weight'], 'Found the expected weight query string.');
......@@ -178,7 +181,7 @@ public function testAddBlockFromLibraryWithWeight() {
'settings[label]' => $title,
];
// Create the block using the link parsed from the library page.
$this->drupalGet($this->getAbsoluteUrl($links[0]->getAttribute('href')));
$this->drupalGet($this->getAbsoluteUrl($link->getAttribute('href')));
$this->submitForm($edit, 'Save block');
// Ensure that the block was created with the expected weight.
......
......@@ -81,9 +81,6 @@ public function testBlockCategory() {
$this->submitForm($edit, 'Save and edit');
$pattern = '//tr[.//td[text()=:category] and .//td//a[contains(@href, :href)]]';
// Test that the block was given a default category corresponding to its
// base table.
$arguments = [
':href' => Url::fromRoute('block.admin_add', [
'plugin_id' => 'views_block:' . $edit['id'] . '-block_1',
......@@ -91,10 +88,12 @@ public function testBlockCategory() {
])->toString(),
':category' => 'Lists (Views)',
];
// Test that the block was given a default category corresponding to its
// base table.
$this->drupalGet('admin/structure/block');
$this->clickLink('Place block');
$elements = $this->xpath($pattern, $arguments);
$this->assertTrue(!empty($elements), 'The test block appears in the category for its base table.');
$this->assertSession()->elementExists('xpath', $this->assertSession()->buildXPathQuery($pattern, $arguments));
// Duplicate the block before changing the category.
$this->drupalGet('admin/structure/views/view/' . $edit['id'] . '/edit/block_1');
......@@ -103,8 +102,7 @@ public function testBlockCategory() {
// Change the block category to a random string.
$this->drupalGet('admin/structure/views/view/' . $edit['id'] . '/edit/block_1');
$link = $this->xpath('//a[@id="views-block-1-block-category" and normalize-space(text())=:category]', $arguments);
$this->assertTrue(!empty($link));
$this->assertSession()->elementTextEquals('named', ['id', 'views-block-1-block-category'], 'Lists (Views)');
$this->clickLink('Lists (Views)');
$category = $this->randomString();
$this->submitForm(['block_category' => $category], 'Apply');
......@@ -119,9 +117,10 @@ public function testBlockCategory() {
$arguments[':category'] = $category;
$this->drupalGet('admin/structure/block');
$this->clickLink('Place block');
$elements = $this->xpath($pattern, $arguments);
$this->assertTrue(!empty($elements), 'The test block appears in the custom category.');
$this->assertSession()->elementExists('xpath', $this->assertSession()->buildXPathQuery($pattern, $arguments));
// Test that the first duplicated test block remains in the original
// category.
$arguments = [
':href' => Url::fromRoute('block.admin_add', [
'plugin_id' => 'views_block:' . $edit['id'] . '-block_2',
......@@ -129,9 +128,10 @@ public function testBlockCategory() {
])->toString(),
':category' => 'Lists (Views)',
];
$elements = $this->xpath($pattern, $arguments);
$this->assertTrue(!empty($elements), 'The first duplicated test block remains in the original category.');
$this->assertSession()->elementExists('xpath', $this->assertSession()->buildXPathQuery($pattern, $arguments));
// Test that the second duplicated test block appears in the custom
// category.
$arguments = [
':href' => Url::fromRoute('block.admin_add', [
'plugin_id' => 'views_block:' . $edit['id'] . '-block_3',
......@@ -139,8 +139,7 @@ public function testBlockCategory() {
])->toString(),
':category' => $category,
];
$elements = $this->xpath($pattern, $arguments);
$this->assertTrue(!empty($elements), 'The second duplicated test block appears in the custom category.');
$this->assertSession()->elementExists('xpath', $this->assertSession()->buildXPathQuery($pattern, $arguments));
}
/**
......
......@@ -635,9 +635,11 @@ public function testAdminBookNodeListing() {
$this->drupalGet('admin/structure/book/' . $this->book->id());
$this->assertSession()->pageTextContains($this->book->label());
$elements = $this->xpath('//table//ul[@class="dropbutton"]/li/a');
$this->assertEquals('View', $elements[0]->getText(), 'View link is found from the list.');
$this->assertSameSize($nodes, $elements, 'All the book pages are displayed on the book outline page.');
// Test that the view link is found from the list.
$this->assertSession()->elementTextEquals('xpath', '//table//ul[@class="dropbutton"]/li/a', 'View');
// Test that all the book pages are displayed on the book outline page.
$this->assertSession()->elementsCount('xpath', '//table//ul[@class="dropbutton"]/li/a', count($nodes));
// Unpublish a book in the hierarchy.
$nodes[0]->setUnPublished();
......@@ -645,8 +647,7 @@ public function testAdminBookNodeListing() {
// Node should still appear on the outline for admins.
$this->drupalGet('admin/structure/book/' . $this->book->id());
$elements = $this->xpath('//table//ul[@class="dropbutton"]/li/a');
$this->assertSameSize($nodes, $elements, 'All the book pages are displayed on the book outline page.');
$this->assertSession()->elementsCount('xpath', '//table//ul[@class="dropbutton"]/li/a', count($nodes));
// Saving a book page not as the current version shouldn't effect the book.
$old_title = $nodes[1]->getTitle();
......@@ -656,8 +657,7 @@ public function testAdminBookNodeListing() {
$nodes[1]->setTitle($new_title);
$nodes[1]->save();
$this->drupalGet('admin/structure/book/' . $this->book->id());
$elements = $this->xpath('//table//ul[@class="dropbutton"]/li/a');
$this->assertSameSize($nodes, $elements, 'All the book pages are displayed on the book outline page.');
$this->assertSession()->elementsCount('xpath', '//table//ul[@class="dropbutton"]/li/a', count($nodes));
$this->assertSession()->responseNotContains($new_title);
$this->assertSession()->responseContains($old_title);
}
......
......@@ -125,8 +125,7 @@ public function testCommentPreviewDuplicateSubmission() {
$submit_button = $this->assertSession()->buttonExists('Save');
$submit_button->click();
$this->assertSession()->pageTextContains('Your comment has been posted.');
$elements = $this->xpath('//section[contains(@class, "comment-wrapper")]/article');
$this->assertCount(2, $elements);
$this->assertSession()->elementsCount('xpath', '//section[contains(@class, "comment-wrapper")]/article', 2);
}
/**
......
......@@ -128,21 +128,23 @@ public function testApprovalAdminInterface() {
$this->submitForm([], 'Apply to selected items');
$this->assertSession()->pageTextContains('Select one or more comments to perform the update on.');
$subject_link = $this->xpath('//table/tbody/tr/td/a[contains(@href, :href) and contains(@title, :title) and text()=:text]', [
// Test that comment listing shows the correct subject link.
$this->assertSession()->elementExists('xpath', $this->assertSession()->buildXPathQuery('//table/tbody/tr/td/a[contains(@href, :href) and contains(@title, :title) and text()=:text]', [
':href' => $comments[0]->permalink()->toString(),
':title' => Unicode::truncate($comments[0]->get('comment_body')->value, 128),
':text' => $comments[0]->getSubject(),
]);
$this->assertTrue(!empty($subject_link), 'Comment listing shows the correct subject link.');
]));
// Verify that anonymous author name is displayed correctly.
$this->assertSession()->pageTextContains($author_name . ' (not verified)');
$subject_link = $this->xpath('//table/tbody/tr/td/a[contains(@href, :href) and contains(@title, :title) and text()=:text]', [
// Test that comment listing shows the correct subject link.
$this->assertSession()->elementExists('xpath', $this->assertSession()->buildXPathQuery('//table/tbody/tr/td/a[contains(@href, :href) and contains(@title, :title) and text()=:text]', [
':href' => $anonymous_comment4->permalink()->toString(),
':title' => Unicode::truncate($body, 128),
':text' => $subject,
]);
$this->assertTrue(!empty($subject_link), 'Comment listing shows the correct subject link.');
]));
// Verify that anonymous author name is displayed correctly.
$this->assertSession()->pageTextContains($author_name . ' (not verified)');
......@@ -214,16 +216,16 @@ public function testCommentedEntityLabel() {
$this->drupalLogin($this->adminUser);
$this->drupalGet('admin/content/comment');
$comment_author_link = $this->xpath('//table/tbody/tr[1]/td/a[contains(@href, :href) and text()=:text]', [
// Test that comment listing links to comment author.
$this->assertSession()->elementExists('xpath', $this->assertSession()->buildXPathQuery('//table/tbody/tr[1]/td/a[contains(@href, :href) and text()=:text]', [
':href' => $this->webUser->toUrl()->toString(),
':text' => $this->webUser->label(),
]);
$this->assertTrue(!empty($comment_author_link), 'Comment listing links to comment author.');
$comment_author_link = $this->xpath('//table/tbody/tr[2]/td/a[contains(@href, :href) and text()=:text]', [
]));
$this->assertSession()->elementExists('xpath', $this->assertSession()->buildXPathQuery('//table/tbody/tr[2]/td/a[contains(@href, :href) and text()=:text]', [
':href' => $this->webUser->toUrl()->toString(),
':text' => $this->webUser->label(),
]);
$this->assertTrue(!empty($comment_author_link), 'Comment listing links to comment author.');
]));
// Admin page contains label of both entities.
$this->assertSession()->pageTextContains(Html::escape($this->node->label()));
$this->assertSession()->pageTextContains(Html::escape($block_content->label()));
......
......@@ -69,11 +69,8 @@ public function testThemeDiscovery() {
$this->drupalLogin($this->adminUser);
$this->drupalGet('admin/appearance');
$elements = $this->xpath('//a[normalize-space()=:label and contains(@href, :theme)]', [
':label' => 'Install and set as default',
':theme' => $theme,
]);
$this->drupalGet($GLOBALS['base_root'] . $elements[0]->getAttribute('href'), ['external' => TRUE]);
$element = $this->assertSession()->elementExists('xpath', "//a[normalize-space()='Install and set as default' and contains(@href, '{$theme}')]");
$this->drupalGet($GLOBALS['base_root'] . $element->getAttribute('href'), ['external' => TRUE]);
$translation_base_url = 'admin/config/development/performance/translate';
$this->drupalGet($translation_base_url);
......
......@@ -118,12 +118,8 @@ public function testSiteWideContact() {
// Cannot use ::assertNoLinkByHref as it does partial url matching and with
// field_ui enabled admin/structure/contact/manage/personal/fields exists.
// @todo: See https://www.drupal.org/node/2031223 for the above.
$edit_link = $this->xpath('//a[@href=:href]', [
':href' => Url::fromRoute('entity.contact_form.edit_form', ['contact_form' => 'personal'])->toString(),
]);
$this->assertTrue(empty($edit_link), new FormattableMarkup('No link containing href %href found.',
['%href' => 'admin/structure/contact/manage/personal']
));
$url = Url::fromRoute('entity.contact_form.edit_form', ['contact_form' => 'personal'])->toString();
$this->assertSession()->elementNotExists('xpath', "//a[@href='{$url}']");
$this->assertSession()->linkByHrefNotExists('admin/structure/contact/manage/personal/delete');
$this->drupalGet('admin/structure/contact/manage/personal');
......@@ -299,12 +295,11 @@ public function testSiteWideContact() {
// Test field UI and field integration.
$this->drupalGet('admin/structure/contact');
$view_link = $this->xpath('//table/tbody/tr/td/a[contains(@href, :href) and text()=:text]', [
// Test contact listing links to contact form.
$this->assertSession()->elementExists('xpath', $this->assertSession()->buildXPathQuery('//table/tbody/tr/td/a[contains(@href, :href) and text()=:text]', [
':href' => Url::fromRoute('entity.contact_form.canonical', ['contact_form' => $contact_form])->toString(),
':text' => $label,
]
);
$this->assertTrue(!empty($view_link), 'Contact listing links to contact form.');
]));
// Find out in which row the form we want to add a field to is.
foreach ($this->xpath('//table/tbody/tr') as $row) {
......
......@@ -226,12 +226,12 @@ protected function doTestTranslationOverview() {
foreach ($this->langcodes as $langcode) {
if ($entity->hasTranslation($langcode)) {
$language = new Language(['id' => $langcode]);
// Test that label is correctly shown for translation.
$view_url = $entity->toUrl('canonical', ['language' => $language])->toString();
$elements = $this->xpath('//table//a[@href=:href]', [':href' => $view_url]);
$this->assertEquals($entity->getTranslation($langcode)->label(), $elements[0]->getText(), new FormattableMarkup('Label correctly shown for %language translation.', ['%language' => $langcode]));
$this->assertSession()->elementTextEquals('xpath', "//table//a[@href='{$view_url}']", $entity->getTranslation($langcode)->label() ?? '');
// Test that edit link is correct for translation.
$edit_path = $entity->toUrl('edit-form', ['language' => $language])->toString();
$elements = $this->xpath('//table//ul[@class="dropbutton"]/li/a[@href=:href]', [':href' => $edit_path]);
$this->assertEquals(t('Edit'), $elements[0]->getText(), new FormattableMarkup('Edit link correct for %language translation.', ['%language' => $langcode]));
$this->assertSession()->elementTextEquals('xpath', "//table//ul[@class='dropbutton']/li/a[@href='{$edit_path}']", 'Edit');
}
}
}
......
......@@ -74,7 +74,8 @@ public function testFieldUIRoutes() {
$this->drupalGet('admin/config/people/accounts/form-display/register');
$this->assertSession()->titleEquals('Manage form display | Drupal');
$this->assertLocalTasks();
$this->assertCount(1, $this->xpath('//ul/li[1]/a[contains(text(), :text)]', [':text' => 'Default']), 'Default secondary tab is in first position.');
// Test that default secondary tab is in first position.
$this->assertSession()->elementsCount('xpath', "//ul/li[1]/a[contains(text(), 'Default')]", 1);
// Create new view mode and verify it's available on the Manage Display
// screen after enabling it.
......
......@@ -315,19 +315,15 @@ protected function doTestLanguageLinkActiveClassAuthenticated() {
$this->drupalGet($path);
// Language code 'none' link should be active.
$langcode = 'none';
$links = $this->xpath('//a[@id = :id and @data-drupal-link-system-path = :path]', [':id' => 'no_lang_link', ':path' => $path]);
$this->assertTrue(isset($links[0]), 'A link generated by $function_name to the current $current_language page with langcode $langcode has the correct attributes that will allow the drupal.active-link library to mark it as active.');
$this->assertSession()->elementAttributeContains('named', ['id', 'no_lang_link'], 'data-drupal-link-system-path', $path);
// Language code 'en' link should be active.
$langcode = 'en';
$links = $this->xpath('//a[@id = :id and @hreflang = :lang and @data-drupal-link-system-path = :path]', [':id' => 'en_link', ':lang' => 'en', ':path' => $path]);
$this->assertTrue(isset($links[0]), 'A link generated by $function_name to the current $current_language page with langcode $langcode has the correct attributes that will allow the drupal.active-link library to mark it as active.');
$this->assertSession()->elementAttributeContains('named', ['id', 'en_link'], 'hreflang', 'en');
$this->assertSession()->elementAttributeContains('named', ['id', 'en_link'], 'data-drupal-link-system-path', $path);
// Language code 'fr' link should not be active.
$langcode = 'fr';
$links = $this->xpath('//a[@id = :id and @hreflang = :lang and @data-drupal-link-system-path = :path]', [':id' => 'fr_link', ':lang' => 'fr', ':path' => $path]);
$this->assertTrue(isset($links[0]), 'A link generated by $function_name to the current $current_language page with langcode $langcode has the correct attributes that will allow the drupal.active-link library to NOT mark it as active.');
$this->assertSession()->elementAttributeContains('named', ['id', 'fr_link'], 'hreflang', 'fr');
$this->assertSession()->elementAttributeContains('named', ['id', 'fr_link'], 'data-drupal-link-system-path', $path);
// Verify that drupalSettings contains the correct values.
$settings = $this->getDrupalSettings();
......@@ -340,19 +336,15 @@ protected function doTestLanguageLinkActiveClassAuthenticated() {
$this->drupalGet('fr/language_test/type-link-active-class');
// Language code 'none' link should be active.
$langcode = 'none';
$links = $this->xpath('//a[@id = :id and @data-drupal-link-system-path = :path]', [':id' => 'no_lang_link', ':path' => $path]);
$this->assertTrue(isset($links[0]), 'A link generated by $function_name to the current $current_language page with langcode $langcode has the correct attributes that will allow the drupal.active-link library to mark it as active.');
$this->assertSession()->elementAttributeContains('named', ['id', 'no_lang_link'], 'data-drupal-link-system-path', $path);
// Language code 'en' link should not be active.
$langcode = 'en';
$links = $this->xpath('//a[@id = :id and @hreflang = :lang and @data-drupal-link-system-path = :path]', [':id' => 'en_link', ':lang' => 'en', ':path' => $path]);
$this->assertTrue(isset($links[0]), 'A link generated by $function_name to the current $current_language page with langcode $langcode has the correct attributes that will allow the drupal.active-link library to NOT mark it as active.');
$this->assertSession()->elementAttributeContains('named', ['id', 'en_link'], 'hreflang', 'en');
$this->assertSession()->elementAttributeContains('named', ['id', 'en_link'], 'data-drupal-link-system-path', $path);
// Language code 'fr' link should be active.
$langcode = 'fr';
$links = $this->xpath('//a[@id = :id and @hreflang = :lang and @data-drupal-link-system-path = :path]', [':id' => 'fr_link', ':lang' => 'fr', ':path' => $path]);
$this->assertTrue(isset($links[0]), 'A link generated by $function_name to the current $current_language page with langcode $langcode has the correct attributes that will allow the drupal.active-link library to mark it as active.');
$this->assertSession()->elementAttributeContains('named', ['id', 'fr_link'], 'hreflang', 'fr');
$this->assertSession()->elementAttributeContains('named', ['id', 'fr_link'], 'data-drupal-link-system-path', $path);
// Verify that drupalSettings contains the correct values.
$settings = $this->getDrupalSettings();
......@@ -376,38 +368,26 @@ protected function doTestLanguageLinkActiveClassAnonymous() {
$this->drupalGet('language_test/type-link-active-class');
// Language code 'none' link should be active.
$langcode = 'none';
$links = $this->xpath('//a[@id = :id and contains(@class, :class)]', [':id' => 'no_lang_link', ':class' => 'is-active']);
$this->assertTrue(isset($links[0]), 'A link generated by $function_name to the current $current_language page with langcode $langcode is marked active.');
$this->assertSession()->elementExists('xpath', "//a[@id = 'no_lang_link' and contains(@class, 'is-active')]");
// Language code 'en' link should be active.
$langcode = 'en';
$links = $this->xpath('//a[@id = :id and contains(@class, :class)]', [':id' => 'en_link', ':class' => 'is-active']);
$this->assertTrue(isset($links[0]), 'A link generated by $function_name to the current $current_language page with langcode $langcode is marked active.');
$this->assertSession()->elementExists('xpath', "//a[@id = 'en_link' and contains(@class, 'is-active')]");
// Language code 'fr' link should not be active.
$langcode = 'fr';
$links = $this->xpath('//a[@id = :id and not(contains(@class, :class))]', [':id' => 'fr_link', ':class' => 'is-active']);
$this->assertTrue(isset($links[0]), 'A link generated by $function_name to the current $current_language page with langcode $langcode is NOT marked active.');
$this->assertSession()->elementExists('xpath', "//a[@id = 'fr_link' and not(contains(@class, 'is-active'))]");
// Test links generated by the link generator on a French page.
$current_language = 'French';
$this->drupalGet('fr/language_test/type-link-active-class');
// Language code 'none' link should be active.
$langcode = 'none';
$links = $this->xpath('//a[@id = :id and contains(@class, :class)]', [':id' => 'no_lang_link', ':class' => 'is-active']);
$this->assertTrue(isset($links[0]), 'A link generated by $function_name to the current $current_language page with langcode $langcode is marked active.');
$this->assertSession()->elementExists('xpath', "//a[@id = 'no_lang_link' and contains(@class, 'is-active')]");
// Language code 'en' link should not be active.
$langcode = 'en';
$links = $this->xpath('//a[@id = :id and not(contains(@class, :class))]', [':id' => 'en_link', ':class' => 'is-active']);
$this->assertTrue(isset($links[0]), 'A link generated by $function_name to the current $current_language page with langcode $langcode is NOT marked active.');
$this->assertSession()->elementExists('xpath', "//a[@id = 'en_link' and not(contains(@class, 'is-active'))]");
// Language code 'fr' link should be active.
$langcode = 'fr';
$links = $this->xpath('//a[@id = :id and contains(@class, :class)]', [':id' => 'fr_link', ':class' => 'is-active']);
$this->assertTrue(isset($links[0]), 'A link generated by $function_name to the current $current_language page with langcode $langcode is marked active.');
$this->assertSession()->elementExists('xpath', "//a[@id = 'fr_link' and contains(@class, 'is-active')]");
}
/**
......
......@@ -138,10 +138,14 @@ public function testPathLanguageConfiguration() {
// Test that both node titles link to our path alias.
$this->drupalGet('admin/content');
$custom_path_url = Url::fromUserInput('/' . $custom_path)->toString();
$elements = $this->xpath('//a[@href=:href and normalize-space(text())=:title]', [':href' => $custom_path_url, ':title' => $first_node->label()]);
$this->assertTrue(!empty($elements), 'First node links to the path alias.');
$elements = $this->xpath('//a[@href=:href and normalize-space(text())=:title]', [':href' => $custom_path_url, ':title' => $second_node->label()]);
$this->assertTrue(!empty($elements), 'Second node links to the path alias.');
$this->assertSession()->elementExists('xpath', $this->assertSession()->buildXPathQuery('//a[@href=:href and normalize-space(text())=:title]', [
':href' => $custom_path_url,
':title' => $first_node->label(),
]));
$this->assertSession()->elementExists('xpath', $this->assertSession()->buildXPathQuery('//a[@href=:href and normalize-space(text())=:title]', [
':href' => $custom_path_url,
':title' => $second_node->label(),
]));
// Confirm that the custom path leads to the first node.
$this->drupalGet($custom_path);
......
......@@ -103,8 +103,11 @@ public function testContentAdminSort() {
$this->drupalGet('admin/content');
foreach ($nodes_query as $delta => $string) {
$elements = $this->xpath('//table[contains(@class, :class)]/tbody/tr[' . ($delta + 1) . ']/td[2]/a[normalize-space(text())=:label]', [':class' => 'views-table', ':label' => $string]);
$this->assertTrue(!empty($elements), 'The node was found in the correct order.');
// Verify that the node was found in the correct order.
$this->assertSession()->elementExists('xpath', $this->assertSession()->buildXPathQuery('//table[contains(@class, :class)]/tbody/tr[' . ($delta + 1) . ']/td[2]/a[normalize-space(text())=:label]', [
':class' => 'views-table',
':label' => $string,
]));
}
// Compare the rendered HTML node list to a query for the nodes ordered by
......@@ -117,8 +120,11 @@ public function testContentAdminSort() {
$this->drupalGet('admin/content', ['query' => ['sort' => 'asc', 'order' => 'title']]);
foreach ($nodes_query as $delta => $string) {
$elements = $this->xpath('//table[contains(@class, :class)]/tbody/tr[' . ($delta + 1) . ']/td[2]/a[normalize-space(text())=:label]', [':class' => 'views-table', ':label' => $string]);
$this->assertTrue(!empty($elements), 'The node was found in the correct order.');
// Verify that the node was found in the correct order.
$this->assertSession()->elementExists('xpath', $this->assertSession()->buildXPathQuery('//table[contains(@class, :class)]/tbody/tr[' . ($delta + 1) . ']/td[2]/a[normalize-space(text())=:label]', [
':class' => 'views-table',
':label' => $string,
]));
}
}
......
......@@ -171,10 +171,9 @@ public function testNodeRevisionsTabWithDefaultRevision() {
// Verify that the latest affected revision having been a default revision
// is displayed as the current one.
$this->assertSession()->linkByHrefNotExists('/node/' . $node_id . '/revisions/1/revert');
$elements = $this->xpath('//tr[contains(@class, "revision-current")]/td/a[1]');
// The site may be installed in a subdirectory, so check if the URL is
// contained in the retrieved one.
$this->assertStringContainsString('/node/1', current($elements)->getAttribute('href'));
$this->assertSession()->elementAttributeContains('xpath', '//tr[contains(@class, "revision-current")]/td/a[1]', 'href', '/node/1');
// Verify that the default revision can be an older revision than the latest
// one.
......
......@@ -391,9 +391,7 @@ public function testNodeAlias() {
// using \Drupal\Tests\BrowserTestBase::assertSession()->addressEquals()
// would actually make the test pass unconditionally on the testbot (or
// anywhere else where Drupal is installed in a subdirectory).
$link_xpath = $this->xpath('//a[normalize-space(text())=:label]', [':label' => $node6->getTitle()]);
$link_href = $link_xpath[0]->getAttribute('href');
$this->assertEquals($link_href, base_path() . $alias);
$this->assertSession()->elementAttributeContains('xpath', "//a[normalize-space(text())='{$node6->getTitle()}']", 'href', base_path() . $alias);
$this->clickLink($node6->getTitle());
$this->assertSession()->statusCodeEquals(404);
}
......
......@@ -163,8 +163,7 @@ public function testLanguageAttributes() {
$node = $this->searchableNodes[1]->getTranslation('es');
$this->assertSession()->elementExists('xpath', '//div[@class="layout-content"]//ol/li/h3[contains(@lang, "es")]');
$result = $this->xpath('//div[@class="layout-content"]//ol/li/h3[contains(@lang, "es")]/a');
$this->assertEquals($node->getTitle(), $result[0]->getText());
$this->assertSession()->elementTextEquals('xpath', '//div[@class="layout-content"]//ol/li/h3[contains(@lang, "es")]/a', $node->getTitle());
$this->assertSession()->elementExists('xpath', '//div[@class="layout-content"]//ol/li/p[contains(@lang, "es")]');
// Visit the search form in Spanish language.
......@@ -172,8 +171,7 @@ public function testLanguageAttributes() {
$this->submitForm(['keys' => 'First node'], 'Search');
$this->assertSession()->elementExists('xpath', '//div[@class="layout-content"]//ol/li/h3[contains(@lang, "en")]');
$node = $this->searchableNodes[0]->getTranslation('en');
$result = $this->xpath('//div[@class="layout-content"]//ol/li/h3[contains(@lang, "en")]/a');
$this->assertEquals($node->getTitle(), $result[0]->getText());
$this->assertSession()->elementTextEquals('xpath', '//div[@class="layout-content"]//ol/li/h3[contains(@lang, "en")]/a', $node->getTitle());
$this->assertSession()->elementExists('xpath', '//div[@class="layout-content"]//ol/li/p[contains(@lang, "en")]');
}
......
......@@ -192,8 +192,7 @@ public function testShortcutQuickLink() {
$this->assertSession()->pageTextContains('Added a shortcut for Create Basic page.');
// Assure that Article does not have its shortcut indicated as set.
$this->drupalGet('node/add/article');
$link = $this->xpath('//a[normalize-space()=:label]', [':label' => 'Remove from Default shortcuts']);
$this->assertTrue(empty($link), 'Link Remove to Default shortcuts not found for Create Article page.');
$this->assertSession()->elementNotExists('xpath', "//a[normalize-space()='Remove from Default shortcuts']");
// Add Shortcut for Article.
$this->clickLink('Add to Default shortcuts');
$this->assertSession()->pageTextContains('Added a shortcut for Create Article.');
......@@ -325,24 +324,21 @@ public function testNoShortcutLink() {
->save();
$this->drupalGet('page-that-does-not-exist');
$result = $this->xpath('//a[contains(@class, "shortcut-action--add")]');
$this->assertTrue(empty($result), 'Add to shortcuts link was not shown on a page not found.');
// Test that add to shortcuts link is not shown on a page not found.
$this->assertSession()->elementNotExists('xpath', '//a[contains(@class, "shortcut-action--add")]');
// The user does not have access to this path.
$this->drupalGet('admin/modules');
$result = $this->xpath('//a[contains(@class, "shortcut-action--add")]');
$this->assertTrue(empty($result), 'Add to shortcuts link was not shown on a page the user does not have access to.');
$this->assertSession()->elementNotExists('xpath', '//a[contains(@class, "shortcut-action--add")]');
// Verify that the testing mechanism works by verifying the shortcut link
// appears on admin/content.
$this->drupalGet('admin/content');
$result = $this->xpath('//a[contains(@class, "shortcut-action--remove")]');
$this->assertTrue(!empty($result), 'Remove from shortcuts link was shown on a page the user does have access to.');
$this->assertSession()->elementExists('xpath', '//a[contains(@class, "shortcut-action--remove")]');
// Verify that the shortcut link appears on routing only pages.
$this->drupalGet('router_test/test2');
$result = $this->xpath('//a[contains(@class, "shortcut-action--add")]');
$this->assertTrue(!empty($result), 'Add to shortcuts link was shown on a page the user does have access to.');
$this->assertSession()->elementExists('xpath', '//a[contains(@class, "shortcut-action--add")]');
}
/**
......
......@@ -83,8 +83,7 @@ protected function doTestBasicTranslation() {
$this->drupalGet('<front>', ['language' => $language]);
$expected_path = \Drupal::urlGenerator()->generateFromRoute('user.page', [], ['language' => $language]);
$label = $entity->getTranslation($langcode)->label();
$elements = $this->xpath('//nav[contains(@class, "toolbar-lining")]/ul[@class="toolbar-menu"]/li/a[contains(@href, :href) and normalize-space(text())=:label]', [':href' => $expected_path, ':label' => $label]);
$this->assertTrue(!empty($elements), new FormattableMarkup('Translated @language shortcut link @label found.', ['@label' => $label, '@language' => $language->getName()]));
$this->assertSession()->elementExists('xpath', "//nav[contains(@class, 'toolbar-lining')]/ul[@class='toolbar-menu']/li/a[contains(@href, '{$expected_path}') and normalize-space(text())='{$label}']");
}
}
}
......
......@@ -41,21 +41,48 @@ public function testActiveLinkAttributes() {
$path = 'common-test/type-link-active-class';
$this->drupalGet($path, $options_no_query);
$links = $this->xpath('//a[@href = :href and contains(@class, :class)]', [':href' => Url::fromRoute('common_test.l_active_class', [], $options_no_query)->toString(), ':class' => 'is-active']);
$this->assertTrue(isset($links[0]), 'A link generated by the link generator to the current page is marked active.');
$links = $this->xpath('//a[@href = :href and not(contains(@class, :class))]', [':href' => Url::fromRoute('common_test.l_active_class', [], $options_query)->toString(), ':class' => 'is-active']);
$this->assertTrue(isset($links[0]), 'A link generated by the link generator to the current page with a query string when the current page has no query string is not marked active.');
// Test that a link generated by the link generator to the current page is
// marked active.
$this->assertSession()->elementExists('xpath', $this->assertSession()->buildXPathQuery(
'//a[@href = :href and contains(@class, "is-active")]', [
':href' => Url::fromRoute('common_test.l_active_class', [], $options_no_query)->toString(),
]
));
// Test that a link generated by the link generator to the current page
// with a query string when the current page has no query string is not
// marked active.
$this->assertSession()->elementExists('xpath', $this->assertSession()->buildXPathQuery(
'//a[@href = :href and not(contains(@class, "is-active"))]', [
':href' => Url::fromRoute('common_test.l_active_class', [], $options_query)->toString(),
]
));
$this->drupalGet($path, $options_query);
$links = $this->xpath('//a[@href = :href and contains(@class, :class)]', [':href' => Url::fromRoute('common_test.l_active_class', [], $options_query)->toString(), ':class' => 'is-active']);
$this->assertTrue(isset($links[0]), 'A link generated by the link generator to the current page with a query string that matches the current query string is marked active.');
$links = $this->xpath('//a[@href = :href and contains(@class, :class)]', [':href' => Url::fromRoute('common_test.l_active_class', [], $options_query_reverse)->toString(), ':class' => 'is-active']);
$this->assertTrue(isset($links[0]), 'A link generated by the link generator to the current page with a query string that has matching parameters to the current query string but in a different order is marked active.');
$links = $this->xpath('//a[@href = :href and not(contains(@class, :class))]', [':href' => Url::fromRoute('common_test.l_active_class', [], $options_no_query)->toString(), ':class' => 'is-active']);
$this->assertTrue(isset($links[0]), 'A link generated by the link generator to the current page without a query string when the current page has a query string is not marked active.');
// Test that a link generated by the link generator to the current page with
// a query string that matches the current query string is marked active.
$this->assertSession()->elementExists('xpath', $this->assertSession()->buildXPathQuery(
'//a[@href = :href and contains(@class, "is-active")]', [
':href' => Url::fromRoute('common_test.l_active_class', [], $options_query)->toString(),
]