diff --git a/core/modules/big_pipe/tests/src/Functional/BigPipeTest.php b/core/modules/big_pipe/tests/src/Functional/BigPipeTest.php index 5624904e4a..78c9ba929b 100644 --- a/core/modules/big_pipe/tests/src/Functional/BigPipeTest.php +++ b/core/modules/big_pipe/tests/src/Functional/BigPipeTest.php @@ -381,13 +381,13 @@ protected function assertBigPipePlaceholders(array $expected_big_pipe_placeholde $placeholder_positions[$pos] = $big_pipe_placeholder_id; // Verify expected placeholder replacement. $expected_placeholder_replacement = '') ->save(); $this->drupalGet(''); - $site_slogan_element = $this->xpath($site_slogan_xpath); - $this->assertEquals('alert("Community carpentry");', $site_slogan_element[0]->getText(), 'The site slogan was XSS-filtered.'); + $this->assertSession()->elementTextEquals('xpath', $site_slogan_xpath, 'alert("Community carpentry");'); // Turn just the logo off. $this->config('block.block.site-branding') ->set('settings.use_site_logo', 0) ->save(); $this->drupalGet(''); - $site_logo_element = $this->xpath($site_logo_xpath); - $site_name_element = $this->xpath($site_name_xpath); - $site_slogan_element = $this->xpath($site_slogan_xpath); + // Re-test all branding elements. - $this->assertEmpty($site_logo_element, 'The branding block logo was disabled.'); - $this->assertNotEmpty($site_name_element, 'The branding block site name was found.'); - $this->assertNotEmpty($site_slogan_element, 'The branding block slogan was found.'); + $this->assertSession()->elementNotExists('xpath', $site_logo_xpath); + $this->assertSession()->elementExists('xpath', $site_name_xpath); + $this->assertSession()->elementExists('xpath', $site_slogan_xpath); $this->assertSession()->responseHeaderContains('X-Drupal-Cache-Tags', 'config:system.site'); // Turn just the site name off. @@ -81,13 +76,11 @@ public function testSystemBrandingSettings() { ->set('settings.use_site_name', 0) ->save(); $this->drupalGet(''); - $site_logo_element = $this->xpath($site_logo_xpath); - $site_name_element = $this->xpath($site_name_xpath); - $site_slogan_element = $this->xpath($site_slogan_xpath); + // Re-test all branding elements. - $this->assertNotEmpty($site_logo_element, 'The branding block logo was found.'); - $this->assertEmpty($site_name_element, 'The branding block site name was disabled.'); - $this->assertNotEmpty($site_slogan_element, 'The branding block slogan was found.'); + $this->assertSession()->elementExists('xpath', $site_logo_xpath); + $this->assertSession()->elementNotExists('xpath', $site_name_xpath); + $this->assertSession()->elementExists('xpath', $site_slogan_xpath); $this->assertSession()->responseHeaderContains('X-Drupal-Cache-Tags', 'config:system.site'); // Turn just the site slogan off. @@ -96,13 +89,11 @@ public function testSystemBrandingSettings() { ->set('settings.use_site_slogan', 0) ->save(); $this->drupalGet(''); - $site_logo_element = $this->xpath($site_logo_xpath); - $site_name_element = $this->xpath($site_name_xpath); - $site_slogan_element = $this->xpath($site_slogan_xpath); + // Re-test all branding elements. - $this->assertNotEmpty($site_logo_element, 'The branding block logo was found.'); - $this->assertNotEmpty($site_name_element, 'The branding block site name was found.'); - $this->assertEmpty($site_slogan_element, 'The branding block slogan was disabled.'); + $this->assertSession()->elementExists('xpath', $site_logo_xpath); + $this->assertSession()->elementExists('xpath', $site_name_xpath); + $this->assertSession()->elementNotExists('xpath', $site_slogan_xpath); $this->assertSession()->responseHeaderContains('X-Drupal-Cache-Tags', 'config:system.site'); // Turn the site name and the site slogan off. @@ -111,13 +102,11 @@ public function testSystemBrandingSettings() { ->set('settings.use_site_slogan', 0) ->save(); $this->drupalGet(''); - $site_logo_element = $this->xpath($site_logo_xpath); - $site_name_element = $this->xpath($site_name_xpath); - $site_slogan_element = $this->xpath($site_slogan_xpath); + // Re-test all branding elements. - $this->assertNotEmpty($site_logo_element, 'The branding block logo was found.'); - $this->assertEmpty($site_name_element, 'The branding block site name was disabled.'); - $this->assertEmpty($site_slogan_element, 'The branding block slogan was disabled.'); + $this->assertSession()->elementExists('xpath', $site_logo_xpath); + $this->assertSession()->elementNotExists('xpath', $site_name_xpath); + $this->assertSession()->elementNotExists('xpath', $site_slogan_xpath); $this->assertSession()->responseHeaderContains('X-Drupal-Cache-Tags', 'config:system.site'); } diff --git a/core/modules/block/tests/src/Functional/BlockUiTest.php b/core/modules/block/tests/src/Functional/BlockUiTest.php index 3efa21f295..febfab5aa9 100644 --- a/core/modules/block/tests/src/Functional/BlockUiTest.php +++ b/core/modules/block/tests/src/Functional/BlockUiTest.php @@ -113,14 +113,12 @@ public function testBlockAdminUiPage() { // Visit the blocks admin ui. $this->drupalGet('admin/structure/block'); // Look for the blocks table. - $blocks_table = $this->xpath("//table[@id='blocks']"); - $this->assertNotEmpty($blocks_table, 'The blocks table is being rendered.'); + $this->assertSession()->elementExists('xpath', "//table[@id='blocks']"); // Look for test blocks in the table. foreach ($this->blockValues as $delta => $values) { $block = $this->blocks[$delta]; $label = $block->label(); - $element = $this->xpath('//*[@id="blocks"]/tbody/tr[' . $values['tr'] . ']/td[1]/text()'); - $this->assertEquals($element[0]->getText(), $label, 'The "' . $label . '" block title is set inside the ' . $values['settings']['region'] . ' region.'); + $this->assertSession()->elementTextEquals('xpath', '//*[@id="blocks"]/tbody/tr[' . $values['tr'] . ']/td[1]/text()', $label); // Look for a test block region select form element. $this->assertSession()->fieldExists('blocks[' . $values['settings']['id'] . '][region]'); // Move the test block to the header region. @@ -141,8 +139,7 @@ public function testBlockAdminUiPage() { // Add a block with a machine name the same as a region name. $this->drupalPlaceBlock('system_powered_by_block', ['region' => 'header', 'id' => 'header']); $this->drupalGet('admin/structure/block'); - $element = $this->xpath('//tr[contains(@class, :class)]', [':class' => 'region-title-header']); - $this->assertNotEmpty($element); + $this->assertSession()->elementExists('xpath', '//tr[contains(@class, "region-title-header")]'); // Ensure hidden themes do not appear in the UI. Enable another non base // theme and place the local tasks block. @@ -174,17 +171,9 @@ public function testBlockAdminUiPage() { * Tests the block categories on the listing page. */ public function testCandidateBlockList() { - $arguments = [ - ':title' => 'Display message', - ':category' => 'Block test', - ':href' => 'admin/structure/block/add/test_block_instantiation/classy', - ]; - $pattern = '//tr[.//td/div[text()=:title] and .//td[text()=:category] and .//td//a[contains(@href, :href)]]'; - $this->drupalGet('admin/structure/block'); $this->clickLink('Place block'); - $elements = $this->xpath($pattern, $arguments); - $this->assertNotEmpty($elements, 'The test block appears in the category for its module.'); + $this->assertSession()->elementExists('xpath', '//tr[.//td/div[text()="Display message"] and .//td[text()="Block test"] and .//td//a[contains(@href, "admin/structure/block/add/test_block_instantiation/classy")]]'); // Trigger the custom category addition in block_test_block_alter(). $this->container->get('state')->set('block_test_info_alter', TRUE); @@ -192,9 +181,7 @@ public function testCandidateBlockList() { $this->drupalGet('admin/structure/block'); $this->clickLink('Place block'); - $arguments[':category'] = 'Custom category'; - $elements = $this->xpath($pattern, $arguments); - $this->assertNotEmpty($elements, 'The test block appears in a custom category controlled by block_test_block_alter().'); + $this->assertSession()->elementExists('xpath', '//tr[.//td/div[text()="Custom category"] and .//td[text()="Block test"] and .//td//a[contains(@href, "admin/structure/block/add/test_block_instantiation/classy")]]'); } /** @@ -220,17 +207,10 @@ public function testContextAwareBlocks() { $this->assertSession()->responseNotContains($expected_text); $block_url = 'admin/structure/block/add/test_context_aware/classy'; - $arguments = [ - ':title' => 'Test context-aware block', - ':category' => 'Block test', - ':href' => $block_url, - ]; - $pattern = '//tr[.//td/div[text()=:title] and .//td[text()=:category] and .//td//a[contains(@href, :href)]]'; $this->drupalGet('admin/structure/block'); $this->clickLink('Place block'); - $elements = $this->xpath($pattern, $arguments); - $this->assertNotEmpty($elements, 'The context-aware test block appears.'); + $this->assertSession()->elementExists('xpath', '//tr[.//td/div[text()="Test context-aware block"] and .//td[text()="Block test"] and .//td//a[contains(@href, :href)]]'); $definition = \Drupal::service('plugin.manager.block')->getDefinition('test_context_aware'); $this->assertNotEmpty($definition, 'The context-aware test block exists.'); $edit = [ @@ -360,14 +340,8 @@ public function testBlockValidateErrors() { 'settings[digits]' => 'abc', ], 'Save block'); - $arguments = [':message' => 'Only digits are allowed']; - $pattern = '//div[contains(@class,"messages messages--error")]/div[contains(text()[2],:message)]'; - $elements = $this->xpath($pattern, $arguments); - $this->assertNotEmpty($elements, 'Plugin error message found in parent form.'); - - $error_class_pattern = '//div[contains(@class,"form-item-settings-digits")]/input[contains(@class,"error")]'; - $error_class = $this->xpath($error_class_pattern); - $this->assertNotEmpty($error_class, 'Plugin error class found in parent form.'); + $this->assertSession()->elementExists('xpath', '//div[contains(@class,"messages messages--error")]/div[contains(text()[2],"Only digits are allowed")]'); + $this->assertSession()->elementExists('xpath', '//div[contains(@class,"form-item-settings-digits")]/input[contains(@class,"error")]'); } /** diff --git a/core/modules/comment/tests/src/Functional/CommentCSSTest.php b/core/modules/comment/tests/src/Functional/CommentCSSTest.php index a9096af51b..140593ca12 100644 --- a/core/modules/comment/tests/src/Functional/CommentCSSTest.php +++ b/core/modules/comment/tests/src/Functional/CommentCSSTest.php @@ -88,43 +88,43 @@ public function testCommentClasses() { // Verify the data-history-node-id attribute, which is necessary for the // by-viewer class and the "new" indicator, see below. - $this->assertCount(1, $this->xpath('//*[@data-history-node-id="' . $node->id() . '"]'), 'data-history-node-id attribute is set on node.'); + $this->assertSession()->elementsCount('xpath', '//*[@data-history-node-id="' . $node->id() . '"]', 1); // Verify classes if the comment is visible for the current user. if ($case['comment_status'] == CommentInterface::PUBLISHED || $case['user'] == 'admin') { // Verify the by-anonymous class. - $comments = $this->xpath('//*[contains(@class, "comment") and contains(@class, "by-anonymous")]'); + $comments = '//*[contains(@class, "comment") and contains(@class, "by-anonymous")]'; if ($case['comment_uid'] == 0) { - $this->assertCount(1, $comments, 'by-anonymous class found.'); + $this->assertSession()->elementsCount('xpath', $comments, 1); } else { - $this->assertCount(0, $comments, 'by-anonymous class not found.'); + $this->assertSession()->elementNotExists('xpath', $comments); } // Verify the by-node-author class. - $comments = $this->xpath('//*[contains(@class, "comment") and contains(@class, "by-node-author")]'); + $comments = '//*[contains(@class, "comment") and contains(@class, "by-node-author")]'; if ($case['comment_uid'] > 0 && $case['comment_uid'] == $case['node_uid']) { - $this->assertCount(1, $comments, 'by-node-author class found.'); + $this->assertSession()->elementsCount('xpath', $comments, 1); } else { - $this->assertCount(0, $comments, 'by-node-author class not found.'); + $this->assertSession()->elementNotExists('xpath', $comments); } // Verify the data-comment-user-id attribute, which is used by the // drupal.comment-by-viewer library to add a by-viewer when the current // user (the viewer) was the author of the comment. We do this in Java- // Script to prevent breaking the render cache. - $this->assertCount(1, $this->xpath('//*[contains(@class, "comment") and @data-comment-user-id="' . $case['comment_uid'] . '"]'), 'data-comment-user-id attribute is set on comment.'); + $this->assertSession()->elementsCount('xpath', '//*[contains(@class, "comment") and @data-comment-user-id="' . $case['comment_uid'] . '"]', 1); $this->assertSession()->responseContains($this->getModulePath('comment') . '/js/comment-by-viewer.js'); } // Verify the unpublished class. - $comments = $this->xpath('//*[contains(@class, "comment") and contains(@class, "unpublished")]'); + $comments = '//*[contains(@class, "comment") and contains(@class, "unpublished")]'; if ($case['comment_status'] == CommentInterface::NOT_PUBLISHED && $case['user'] == 'admin') { - $this->assertCount(1, $comments, 'unpublished class found.'); + $this->assertSession()->elementsCount('xpath', $comments, 1); } else { - $this->assertCount(0, $comments, 'unpublished class not found.'); + $this->assertSession()->elementNotExists('xpath', $comments); } // Verify the data-comment-timestamp attribute, which is used by the @@ -132,7 +132,7 @@ public function testCommentClasses() { // comment that was created or changed after the last time the current // user read the corresponding node. if ($case['comment_status'] == CommentInterface::PUBLISHED || $case['user'] == 'admin') { - $this->assertCount(1, $this->xpath('//*[contains(@class, "comment")]/*[@data-comment-timestamp="' . $comment->getChangedTime() . '"]'), 'data-comment-timestamp attribute is set on comment'); + $this->assertSession()->elementsCount('xpath', '//*[contains(@class, "comment")]/*[@data-comment-timestamp="' . $comment->getChangedTime() . '"]', 1); $expectedJS = ($case['user'] !== 'anonymous'); $this->assertSame($expectedJS, isset($settings['ajaxPageState']['libraries']) && in_array('comment/drupal.comment-new-indicator', explode(',', $settings['ajaxPageState']['libraries'])), 'drupal.comment-new-indicator library is present.'); } diff --git a/core/modules/comment/tests/src/Functional/CommentInterfaceTest.php b/core/modules/comment/tests/src/Functional/CommentInterfaceTest.php index 6631b26f91..6b7a158b0e 100644 --- a/core/modules/comment/tests/src/Functional/CommentInterfaceTest.php +++ b/core/modules/comment/tests/src/Functional/CommentInterfaceTest.php @@ -87,12 +87,7 @@ public function testCommentInterface() { $this->drupalGet('node/' . $this->node->id()); $this->assertSession()->pageTextContains($subject_text); $this->assertSession()->pageTextContains($comment_text); - $arguments = [ - ':link' => base_path() . 'comment/' . $comment->id() . '#comment-' . $comment->id(), - ]; - $pattern_permalink = '//footer[contains(@class,"comment__meta")]/a[contains(@href,:link) and text()="Permalink"]'; - $permalink = $this->xpath($pattern_permalink, $arguments); - $this->assertNotEmpty($permalink, 'Permalink link found.'); + $this->assertSession()->elementExists('xpath', '//footer[contains(@class,"comment__meta")]/a[contains(@href,"' . base_path() . 'comment/' . $comment->id() . '#comment-' . $comment->id() . '") and text()="Permalink"]'); // Set comments to have subject and preview to optional. $this->drupalLogout(); diff --git a/core/modules/comment/tests/src/Functional/CommentNewIndicatorTest.php b/core/modules/comment/tests/src/Functional/CommentNewIndicatorTest.php index f0548a9433..a0e6e543ec 100644 --- a/core/modules/comment/tests/src/Functional/CommentNewIndicatorTest.php +++ b/core/modules/comment/tests/src/Functional/CommentNewIndicatorTest.php @@ -66,7 +66,7 @@ public function testCommentNewCommentsIndicator() { // used by the drupal.node-new-comments-link library to determine whether // a "x new comments" link might be necessary or not. We do this in // JavaScript to prevent breaking the render cache. - $this->assertCount(0, $this->xpath('//*[@data-history-node-last-comment-timestamp]'), 'data-history-node-last-comment-timestamp attribute is not set.'); + $this->assertSession()->elementNotExists('xpath', '//*[@data-history-node-last-comment-timestamp]'); // Create a new comment. This helper function may be run with different // comment settings so use $comment->save() to avoid complex setup. @@ -94,8 +94,8 @@ public function testCommentNewCommentsIndicator() { // value, the drupal.node-new-comments-link library would determine that the // node received a comment after the user last viewed it, and hence it would // perform an HTTP request to render the "new comments" node link. - $this->assertCount(1, $this->xpath('//*[@data-history-node-last-comment-timestamp="' . $comment->getChangedTime() . '"]'), 'data-history-node-last-comment-timestamp attribute is set to the correct value.'); - $this->assertCount(1, $this->xpath('//*[@data-history-node-field-name="comment"]'), 'data-history-node-field-name attribute is set to the correct value.'); + $this->assertSession()->elementsCount('xpath', '//*[@data-history-node-last-comment-timestamp="' . $comment->getChangedTime() . '"]', 1); + $this->assertSession()->elementsCount('xpath', '//*[@data-history-node-field-name="comment"]', 1); // The data will be pre-seeded on this particular page in drupalSettings, to // avoid the need for the client to make a separate request to the server. $settings = $this->getDrupalSettings(); diff --git a/core/modules/comment/tests/src/Functional/CommentNonNodeTest.php b/core/modules/comment/tests/src/Functional/CommentNonNodeTest.php index d1e5eef164..79c20e5b71 100644 --- a/core/modules/comment/tests/src/Functional/CommentNonNodeTest.php +++ b/core/modules/comment/tests/src/Functional/CommentNonNodeTest.php @@ -288,8 +288,7 @@ public function testCommentFunctionality() { // Test breadcrumb on comment add page. $this->drupalGet('comment/reply/entity_test/' . $this->entity->id() . '/comment'); - $xpath = '//nav[@class="breadcrumb"]/ol/li[last()]/a'; - $this->assertEquals($this->entity->label(), current($this->xpath($xpath))->getText(), 'Last breadcrumb item is equal to node title on comment reply page.'); + $this->assertSession()->elementsTextEquals('xpath', '//nav[@class="breadcrumb"]/ol/li[last()]/a', $this->entity->label()); // Post a comment. /** @var \Drupal\comment\CommentInterface $comment1 */ @@ -298,18 +297,15 @@ public function testCommentFunctionality() { // Test breadcrumb on comment reply page. $this->drupalGet('comment/reply/entity_test/' . $this->entity->id() . '/comment/' . $comment1->id()); - $xpath = '//nav[@class="breadcrumb"]/ol/li[last()]/a'; - $this->assertEquals($comment1->getSubject(), current($this->xpath($xpath))->getText(), 'Last breadcrumb item is equal to comment title on comment reply page.'); + $this->assertSession()->elementsTextEquals('xpath', '//nav[@class="breadcrumb"]/ol/li[last()]/a', $comment1->getSubject()); // Test breadcrumb on comment edit page. $this->drupalGet('comment/' . $comment1->id() . '/edit'); - $xpath = '//nav[@class="breadcrumb"]/ol/li[last()]/a'; - $this->assertEquals($comment1->getSubject(), current($this->xpath($xpath))->getText(), 'Last breadcrumb item is equal to comment subject on edit page.'); + $this->assertSession()->elementsTextEquals('xpath', '//nav[@class="breadcrumb"]/ol/li[last()]/a', $comment1->getSubject()); // Test breadcrumb on comment delete page. $this->drupalGet('comment/' . $comment1->id() . '/delete'); - $xpath = '//nav[@class="breadcrumb"]/ol/li[last()]/a'; - $this->assertEquals($comment1->getSubject(), current($this->xpath($xpath))->getText(), 'Last breadcrumb item is equal to comment subject on delete confirm page.'); + $this->assertSession()->elementsTextEquals('xpath', '//nav[@class="breadcrumb"]/ol/li[last()]/a', $comment1->getSubject()); // Unpublish the comment. $this->performCommentOperation($comment1, 'unpublish'); diff --git a/core/modules/comment/tests/src/Functional/CommentPreviewTest.php b/core/modules/comment/tests/src/Functional/CommentPreviewTest.php index 23222f1fb5..255b0ecdd8 100644 --- a/core/modules/comment/tests/src/Functional/CommentPreviewTest.php +++ b/core/modules/comment/tests/src/Functional/CommentPreviewTest.php @@ -121,8 +121,7 @@ public function testCommentPreviewDuplicateSubmission() { // Store the content of this page. $this->submitForm([], 'Save'); $this->assertSession()->pageTextContains('Your comment has been posted.'); - $elements = $this->xpath('//section[contains(@class, "comment-wrapper")]/article'); - $this->assertCount(1, $elements); + $this->assertSession()->elementsCount('xpath', '//section[contains(@class, "comment-wrapper")]/article', 1); // Go back and re-submit the form. $this->getSession()->getDriver()->back(); diff --git a/core/modules/comment/tests/src/Functional/Views/CommentRowTest.php b/core/modules/comment/tests/src/Functional/Views/CommentRowTest.php index 09e157ce94..c3a9503dbe 100644 --- a/core/modules/comment/tests/src/Functional/Views/CommentRowTest.php +++ b/core/modules/comment/tests/src/Functional/Views/CommentRowTest.php @@ -26,9 +26,7 @@ class CommentRowTest extends CommentTestBase { */ public function testCommentRow() { $this->drupalGet('test-comment-row'); - - $result = $this->xpath('//article[contains(@class, "comment")]'); - $this->assertCount(1, $result, 'One rendered comment found.'); + $this->assertSession()->elementsCount('xpath', '//article[contains(@class, "comment")]', 1); } } diff --git a/core/modules/comment/tests/src/Kernel/Views/CommentUserNameTest.php b/core/modules/comment/tests/src/Kernel/Views/CommentUserNameTest.php index e65be6c6b5..8b51d06196 100644 --- a/core/modules/comment/tests/src/Kernel/Views/CommentUserNameTest.php +++ b/core/modules/comment/tests/src/Kernel/Views/CommentUserNameTest.php @@ -155,10 +155,7 @@ public function testUsername() { $this->assertLink('Anonymous comment title'); // Display plugin of the view is showing the name field. When comment // belongs to an authenticated user the name field has no value. - $comment_author = $this->xpath('//div[contains(@class, :class)]/span[normalize-space(text())=""]', [ - ':class' => 'views-field-subject', - ]); - $this->assertNotEmpty($comment_author); + $this->assertSession()->elementExists('xpath', '//div[contains(@class, "views-field-subject")]/span[normalize-space(text())=""]'); // When comment belongs to an anonymous user the name field has a value and // it is rendered correctly. $this->assertLink('barry (not verified)'); diff --git a/core/modules/config/tests/src/Functional/ConfigImportUITest.php b/core/modules/config/tests/src/Functional/ConfigImportUITest.php index a2078e6d30..85c09f97cd 100644 --- a/core/modules/config/tests/src/Functional/ConfigImportUITest.php +++ b/core/modules/config/tests/src/Functional/ConfigImportUITest.php @@ -320,8 +320,7 @@ public function testImportDiff() { $this->assertSession()->pageTextContains("404: 'herp'"); // Verify diff colors are displayed. - $result = $this->xpath('//table[contains(@class, :class)]', [':class' => 'diff']); - $this->assertCount(1, $result, "Diff UI is displaying colors."); + $this->assertSession()->elementsCount('xpath', '//table[contains(@class, "diff")]', 1); // Reset data back to original, and remove a key $sync_data = $original_data; diff --git a/core/modules/content_moderation/tests/src/Functional/ModerationFormTest.php b/core/modules/content_moderation/tests/src/Functional/ModerationFormTest.php index 391d3a0e51..3d7ad6533e 100644 --- a/core/modules/content_moderation/tests/src/Functional/ModerationFormTest.php +++ b/core/modules/content_moderation/tests/src/Functional/ModerationFormTest.php @@ -308,7 +308,7 @@ public function testContentTranslationNodeForm() { 'body[0][value]' => 'First version of the content.', 'moderation_state[0][state]' => 'draft', ], 'Save'); - $this->assertNotEmpty($this->xpath('//ul[@class="entity-moderation-form"]')); + $this->assertSession()->elementExists('xpath', '//ul[@class="entity-moderation-form"]'); $node = $this->drupalGetNodeByTitle('Some moderated content'); $this->assertNotEmpty($node->language(), 'en'); @@ -319,7 +319,7 @@ public function testContentTranslationNodeForm() { $this->drupalGet($latest_version_path); $this->assertSession()->statusCodeEquals('403'); - $this->assertEmpty($this->xpath('//ul[@class="entity-moderation-form"]')); + $this->assertSession()->elementNotExists('xpath', '//ul[@class="entity-moderation-form"]'); // Add french translation (revision 2). $this->drupalGet($translate_path); @@ -333,7 +333,7 @@ public function testContentTranslationNodeForm() { $this->drupalGet($latest_version_path, ['language' => $french]); $this->assertSession()->statusCodeEquals('403'); - $this->assertEmpty($this->xpath('//ul[@class="entity-moderation-form"]')); + $this->assertSession()->elementNotExists('xpath', '//ul[@class="entity-moderation-form"]'); // Add french pending revision (revision 3). $this->drupalGet($edit_path, ['language' => $french]); @@ -356,14 +356,14 @@ public function testContentTranslationNodeForm() { ], 'Save (this translation)'); $this->drupalGet($latest_version_path, ['language' => $french]); - $this->assertNotEmpty($this->xpath('//ul[@class="entity-moderation-form"]')); + $this->assertSession()->elementExists('xpath', '//ul[@class="entity-moderation-form"]'); $this->drupalGet($edit_path); $this->clickLink('Delete'); $this->assertSession()->buttonExists('Delete'); $this->drupalGet($latest_version_path); - $this->assertEmpty($this->xpath('//ul[@class="entity-moderation-form"]')); + $this->assertSession()->elementNotExists('xpath', '//ul[@class="entity-moderation-form"]'); // Publish the french pending revision (revision 4). $this->drupalGet($edit_path, ['language' => $french]); @@ -376,7 +376,7 @@ public function testContentTranslationNodeForm() { ], 'Save (this translation)'); $this->drupalGet($latest_version_path, ['language' => $french]); - $this->assertEmpty($this->xpath('//ul[@class="entity-moderation-form"]')); + $this->assertSession()->elementNotExists('xpath', '//ul[@class="entity-moderation-form"]'); // Publish the English pending revision (revision 5). $this->drupalGet($edit_path); @@ -389,7 +389,7 @@ public function testContentTranslationNodeForm() { ], 'Save (this translation)'); $this->drupalGet($latest_version_path); - $this->assertEmpty($this->xpath('//ul[@class="entity-moderation-form"]')); + $this->assertSession()->elementNotExists('xpath', '//ul[@class="entity-moderation-form"]'); // Make sure we are allowed to create a pending French revision. $this->drupalGet($edit_path, ['language' => $french]); @@ -408,9 +408,9 @@ public function testContentTranslationNodeForm() { ], 'Save (this translation)'); $this->drupalGet($latest_version_path); - $this->assertNotEmpty($this->xpath('//ul[@class="entity-moderation-form"]')); + $this->assertSession()->elementExists('xpath', '//ul[@class="entity-moderation-form"]'); $this->drupalGet($latest_version_path, ['language' => $french]); - $this->assertEmpty($this->xpath('//ul[@class="entity-moderation-form"]')); + $this->assertSession()->elementNotExists('xpath', '//ul[@class="entity-moderation-form"]'); // Publish the English pending revision (revision 7) $this->drupalGet($edit_path); @@ -423,7 +423,7 @@ public function testContentTranslationNodeForm() { ], 'Save (this translation)'); $this->drupalGet($latest_version_path); - $this->assertEmpty($this->xpath('//ul[@class="entity-moderation-form"]')); + $this->assertSession()->elementNotExists('xpath', '//ul[@class="entity-moderation-form"]'); // Make sure we are allowed to create a pending French revision. $this->drupalGet($edit_path, ['language' => $french]); diff --git a/core/modules/content_moderation/tests/src/Functional/ModerationLocaleTest.php b/core/modules/content_moderation/tests/src/Functional/ModerationLocaleTest.php index 0d60b435ca..08c7f151a1 100644 --- a/core/modules/content_moderation/tests/src/Functional/ModerationLocaleTest.php +++ b/core/modules/content_moderation/tests/src/Functional/ModerationLocaleTest.php @@ -547,9 +547,9 @@ public function assertNotLatestVersionPage(NodeInterface $node, bool $moderation */ public function assertModerationForm(NodeInterface $node, bool $latest_tab = TRUE): void { $this->drupalGet($node->toUrl()); - $this->assertEquals(!$latest_tab, $this->hasModerationForm()); + $this->assertTrue($this->hasModerationForm(!$latest_tab)); $this->drupalGet($node->toUrl('latest-version')); - $this->assertEquals($latest_tab, $this->hasModerationForm()); + $this->assertTrue($this->hasModerationForm($latest_tab)); } /** @@ -562,19 +562,22 @@ public function assertModerationForm(NodeInterface $node, bool $latest_tab = TRU */ public function assertNoModerationForm(NodeInterface $node): void { $this->drupalGet($node->toUrl()); - $this->assertFalse($this->hasModerationForm()); + $this->assertTrue($this->hasModerationForm()); $this->drupalGet($node->toUrl('latest-version')); $this->assertEquals(403, $this->getSession()->getStatusCode()); } /** * Checks whether the page contains the moderation form. - * - * @return bool - * TRUE if the moderation form could be find in the page, FALSE otherwise. */ - public function hasModerationForm() { - return (bool) $this->xpath('//ul[@class="entity-moderation-form"]'); + public function hasModerationForm(bool $exists = TRUE) { + if ($exists) { + $this->assertSession()->elementExists('xpath', '//ul[@class="entity-moderation-form"]'); + } + else { + $this->assertSession()->elementNotExists('xpath', '//ul[@class="entity-moderation-form"]'); + } + return TRUE; } } diff --git a/core/modules/content_translation/tests/src/Functional/ContentTranslationLinkTagTest.php b/core/modules/content_translation/tests/src/Functional/ContentTranslationLinkTagTest.php index 5c0f9d8d35..1d25cc06cf 100644 --- a/core/modules/content_translation/tests/src/Functional/ContentTranslationLinkTagTest.php +++ b/core/modules/content_translation/tests/src/Functional/ContentTranslationLinkTagTest.php @@ -154,8 +154,7 @@ public function testCanonicalAlternateTagsMissing() { $this->drupalGet($entity->toUrl('edit-form')); $this->assertSession()->statusCodeEquals(200); - $result = $this->xpath('//link[@rel="alternate" and @hreflang]'); - $this->assertEmpty($result, 'No alternate link tag found.'); + $this->assertSession()->elementNotExists('xpath', '//link[@rel="alternate" and @hreflang]'); } } diff --git a/core/modules/content_translation/tests/src/Functional/ContentTranslationSettingsTest.php b/core/modules/content_translation/tests/src/Functional/ContentTranslationSettingsTest.php index de65d5464f..cf3183fa1f 100644 --- a/core/modules/content_translation/tests/src/Functional/ContentTranslationSettingsTest.php +++ b/core/modules/content_translation/tests/src/Functional/ContentTranslationSettingsTest.php @@ -98,8 +98,7 @@ public function testSettingsUI() { 'settings[comment][comment_article][fields][uid]' => FALSE, ]; $this->assertSettings('comment', 'comment_article', FALSE, $edit); - $xpath_err = '//div[contains(@class, "error")]'; - $this->assertNotEmpty($this->xpath($xpath_err), 'Enabling translation only for entity bundles generates a form error.'); + $this->assertSession()->elementExists('xpath', '//div[contains(@class, "error")]'); // Test that the translation settings are not stored if a non-configurable // language is set as default and the language selector is hidden. @@ -111,7 +110,7 @@ public function testSettingsUI() { 'settings[comment][comment_article][fields][comment_body]' => TRUE, ]; $this->assertSettings('comment', 'comment_article', FALSE, $edit); - $this->assertNotEmpty($this->xpath($xpath_err), 'Enabling translation with a fixed non-configurable language generates a form error.'); + $this->assertSession()->elementExists('xpath', '//div[contains(@class, "error")]'); // Test that a field shared among different bundles can be enabled without // needing to make all the related bundles translatable. diff --git a/core/modules/content_translation/tests/src/Functional/ContentTranslationUITestBase.php b/core/modules/content_translation/tests/src/Functional/ContentTranslationUITestBase.php index e853e595df..cb1ee5d106 100644 --- a/core/modules/content_translation/tests/src/Functional/ContentTranslationUITestBase.php +++ b/core/modules/content_translation/tests/src/Functional/ContentTranslationUITestBase.php @@ -263,12 +263,12 @@ protected function doTestOutdatedStatus() { if ($added_langcode == $langcode) { // Verify that the retranslate flag is not checked by default. $this->assertSession()->fieldValueEquals('content_translation[retranslate]', FALSE); - $this->assertEmpty($this->xpath('//details[@id="edit-content-translation" and @open="open"]'), 'The translation tab should be collapsed by default.'); + $this->assertSession()->elementNotExists('xpath', '//details[@id="edit-content-translation" and @open="open"]'); } else { // Verify that the translate flag is checked by default. $this->assertSession()->fieldValueEquals('content_translation[outdated]', TRUE); - $this->assertNotEmpty($this->xpath('//details[@id="edit-content-translation" and @open="open"]'), 'The translation tab is correctly expanded when the translation is outdated.'); + $this->assertSession()->elementExists('xpath', '//details[@id="edit-content-translation" and @open="open"]'); $edit = ['content_translation[outdated]' => FALSE]; $this->drupalGet($url); $this->submitForm($edit, $this->getFormSubmitAction($entity, $added_langcode)); diff --git a/core/modules/content_translation/tests/src/Functional/ContentTranslationUntranslatableFieldsTest.php b/core/modules/content_translation/tests/src/Functional/ContentTranslationUntranslatableFieldsTest.php index aabea9e313..e8756d62cc 100644 --- a/core/modules/content_translation/tests/src/Functional/ContentTranslationUntranslatableFieldsTest.php +++ b/core/modules/content_translation/tests/src/Functional/ContentTranslationUntranslatableFieldsTest.php @@ -88,9 +88,9 @@ public function testHiddenWidgets() { $en_edit_url = $entity->toUrl('edit-form'); $this->drupalGet($en_edit_url); $field_xpath = '//input[@name="' . $this->fieldName . '[0][value]"]'; - $this->assertNotEmpty($this->xpath($field_xpath)); + $this->assertSession()->elementExists('xpath', $field_xpath); $clue_xpath = '//label[@for="edit-' . strtr($this->fieldName, '_', '-') . '-0-value"]/span[text()="(all languages)"]'; - $this->assertEmpty($this->xpath($clue_xpath)); + $this->assertSession()->elementNotExists('xpath', $clue_xpath); $this->assertSession()->pageTextContains('Untranslatable-but-visible test field'); // Add a translation and check that the untranslatable field widget is @@ -102,20 +102,20 @@ public function testHiddenWidgets() { 'target' => 'it', ]); $this->drupalGet($add_url); - $this->assertNotEmpty($this->xpath($field_xpath)); - $this->assertNotEmpty($this->xpath($clue_xpath)); + $this->assertSession()->elementExists('xpath', $field_xpath); + $this->assertSession()->elementExists('xpath', $clue_xpath); $this->assertSession()->pageTextContains('Untranslatable-but-visible test field'); $this->submitForm([], 'Save'); // Check that the widget is displayed along with its clue in the edit form // for both languages. $this->drupalGet($en_edit_url); - $this->assertNotEmpty($this->xpath($field_xpath)); - $this->assertNotEmpty($this->xpath($clue_xpath)); + $this->assertSession()->elementExists('xpath', $field_xpath); + $this->assertSession()->elementExists('xpath', $clue_xpath); $it_edit_url = $entity->toUrl('edit-form', ['language' => ConfigurableLanguage::load('it')]); $this->drupalGet($it_edit_url); - $this->assertNotEmpty($this->xpath($field_xpath)); - $this->assertNotEmpty($this->xpath($clue_xpath)); + $this->assertSession()->elementExists('xpath', $field_xpath); + $this->assertSession()->elementExists('xpath', $clue_xpath); // Configure untranslatable field widgets to be hidden on non-default // language edit forms. @@ -128,22 +128,19 @@ public function testHiddenWidgets() { // but no clue is displayed. $this->drupalGet($en_edit_url); $field_xpath = '//input[@name="' . $this->fieldName . '[0][value]"]'; - $this->assertNotEmpty($this->xpath($field_xpath)); - $this->assertEmpty($this->xpath($clue_xpath)); + $this->assertSession()->elementExists('xpath', $field_xpath); + $this->assertSession()->elementNotExists('xpath', $clue_xpath); $this->assertSession()->pageTextContains('Untranslatable-but-visible test field'); // Verify no widget is displayed on the non-default language edit form. $this->drupalGet($it_edit_url); - $this->assertEmpty($this->xpath($field_xpath)); - $this->assertEmpty($this->xpath($clue_xpath)); + $this->assertSession()->elementNotExists('xpath', $field_xpath); + $this->assertSession()->elementNotExists('xpath', $clue_xpath); $this->assertSession()->pageTextContains('Untranslatable-but-visible test field'); // Verify a warning is displayed. $this->assertSession()->pageTextContains('Fields that apply to all languages are hidden to avoid conflicting changes.'); - $edit_path = $entity->toUrl('edit-form')->toString(); - $link_xpath = '//a[@href=:edit_path and text()="Edit them on the original language form"]'; - $elements = $this->xpath($link_xpath, [':edit_path' => $edit_path]); - $this->assertNotEmpty($elements); + $this->assertSession()->elementExists('xpath', '//a[@href="' . $entity->toUrl('edit-form')->toString() . '" and text()="Edit them on the original language form"]'); // Configure untranslatable field widgets to be displayed on non-default // language edit forms. @@ -153,23 +150,22 @@ public function testHiddenWidgets() { // Check that the widget is displayed along with its clue in the edit form // for both languages. $this->drupalGet($en_edit_url); - $this->assertNotEmpty($this->xpath($field_xpath)); - $this->assertNotEmpty($this->xpath($clue_xpath)); + $this->assertSession()->elementExists('xpath', $field_xpath); + $this->assertSession()->elementExists('xpath', $clue_xpath); $this->drupalGet($it_edit_url); - $this->assertNotEmpty($this->xpath($field_xpath)); - $this->assertNotEmpty($this->xpath($clue_xpath)); + $this->assertSession()->elementExists('xpath', $field_xpath); + $this->assertSession()->elementExists('xpath', $clue_xpath); // Enable content moderation and verify that widgets are hidden despite them // being configured to be displayed. $this->enableContentModeration(); $this->drupalGet($it_edit_url); - $this->assertEmpty($this->xpath($field_xpath)); - $this->assertEmpty($this->xpath($clue_xpath)); + $this->assertSession()->elementNotExists('xpath', $field_xpath); + $this->assertSession()->elementNotExists('xpath', $clue_xpath); // Verify a warning is displayed. $this->assertSession()->pageTextContains('Fields that apply to all languages are hidden to avoid conflicting changes.'); - $elements = $this->xpath($link_xpath, [':edit_path' => $edit_path]); - $this->assertNotEmpty($elements); + $this->assertSession()->elementExists('xpath', '//a[@href="' . $entity->toUrl('edit-form')->toString() . '" and text()="Edit them on the original language form"]'); // Verify that checkboxes on the language content settings page are checked // and disabled for moderated bundles. diff --git a/core/modules/datetime/tests/src/Functional/DateTimeFieldTest.php b/core/modules/datetime/tests/src/Functional/DateTimeFieldTest.php index 7355414411..c4d3c45e53 100644 --- a/core/modules/datetime/tests/src/Functional/DateTimeFieldTest.php +++ b/core/modules/datetime/tests/src/Functional/DateTimeFieldTest.php @@ -918,8 +918,7 @@ public function testDateStorageSettings() { $this->drupalGet('node/add/date_content'); $this->submitForm($edit, 'Save'); $this->drupalGet('admin/structure/types/manage/date_content/fields/node.date_content.' . $field_name . '/storage'); - $result = $this->xpath("//*[@id='edit-settings-datetime-type' and contains(@disabled, 'disabled')]"); - $this->assertCount(1, $result, "Changing datetime setting is disabled."); + $this->assertSession()->elementsCount('xpath', "//*[@id='edit-settings-datetime-type' and contains(@disabled, 'disabled')]", 1); $this->assertSession()->pageTextContains('There is data for this field in the database. The field settings can no longer be changed.'); } diff --git a/core/modules/datetime_range/tests/src/Functional/DateRangeFieldTest.php b/core/modules/datetime_range/tests/src/Functional/DateRangeFieldTest.php index 3d323450b8..024042be89 100644 --- a/core/modules/datetime_range/tests/src/Functional/DateRangeFieldTest.php +++ b/core/modules/datetime_range/tests/src/Functional/DateRangeFieldTest.php @@ -1416,8 +1416,7 @@ public function testDateStorageSettings() { $this->drupalGet('node/add/date_content'); $this->submitForm($edit, 'Save'); $this->drupalGet('admin/structure/types/manage/date_content/fields/node.date_content.' . $field_name . '/storage'); - $result = $this->xpath("//*[@id='edit-settings-datetime-type' and contains(@disabled, 'disabled')]"); - $this->assertCount(1, $result, "Changing datetime setting is disabled."); + $this->assertSession()->elementsCount('xpath', "//*[@id='edit-settings-datetime-type' and contains(@disabled, 'disabled')]", 1); $this->assertSession()->pageTextContains('There is data for this field in the database. The field settings can no longer be changed.'); } diff --git a/core/modules/dblog/tests/src/Functional/DbLogTest.php b/core/modules/dblog/tests/src/Functional/DbLogTest.php index f83931cf4d..b499f6cd46 100644 --- a/core/modules/dblog/tests/src/Functional/DbLogTest.php +++ b/core/modules/dblog/tests/src/Functional/DbLogTest.php @@ -160,10 +160,11 @@ public function test403LogEventPage() { $wid = $query->execute()->fetchField(); $this->drupalGet('admin/reports/dblog/event/' . $wid); - $table = $this->xpath("//table[@class='dblog-event']"); - $this->assertCount(1, $table); + $table_xpath = "//table[@class='dblog-event']"; + $this->assertSession()->elementsCount('xpath', $table_xpath, 1); // Verify type, severity and location. + $table_xpath = $this->xpath($table_xpath); $type = $table[0]->findAll('xpath', "//tr/th[contains(text(), 'Type')]/../td"); $this->assertCount(1, $type); $this->assertEquals('access denied', $type[0]->getText()); @@ -367,8 +368,7 @@ private function verifyBreadcrumbs() { $query->addExpression('MIN([wid])'); $wid = $query->execute()->fetchField(); $this->drupalGet('admin/reports/dblog/event/' . $wid); - $xpath = '//nav[@class="breadcrumb"]/ol/li[last()]/a'; - $this->assertEquals('Recent log messages', current($this->xpath($xpath))->getText(), 'DBLogs link displayed at breadcrumb in event page.'); + $this->assertSession()->elementTextEquals('xpath', '//nav[@class="breadcrumb"]/ol/li[last()]/a', 'Recent log messages'); } /** diff --git a/core/modules/editor/tests/src/Functional/EditorLoadingTest.php b/core/modules/editor/tests/src/Functional/EditorLoadingTest.php index 51f7c5d81c..50ed06c214 100644 --- a/core/modules/editor/tests/src/Functional/EditorLoadingTest.php +++ b/core/modules/editor/tests/src/Functional/EditorLoadingTest.php @@ -150,7 +150,7 @@ public function testLoading() { [, $editor_settings_present, $editor_js_present, $body] = $this->getThingsToCheck('body'); $this->assertFalse($editor_settings_present, 'No Text Editor module settings.'); $this->assertFalse($editor_js_present, 'No Text Editor JavaScript.'); - $this->assertCount(1, $body, 'A body field exists.'); + $this->assertSession()->elementsCount('xpath', $body, 1); $this->assertSession()->elementNotExists('css', 'select.js-filter-list'); $this->drupalLogout($this->normalUser); @@ -174,7 +174,7 @@ public function testLoading() { $this->assertTrue($editor_settings_present, "Text Editor module's JavaScript settings are on the page."); $this->assertSame($expected, $settings['editor'], "Text Editor module's JavaScript settings on the page are correct."); $this->assertTrue($editor_js_present, 'Text Editor JavaScript is present.'); - $this->assertCount(1, $body, 'A body field exists.'); + $this->assertSession()->elementsCount('xpath', $body, 1); $this->assertSession()->elementsCount('css', 'select.js-filter-list', 1); $select = $this->assertSession()->elementExists('css', 'select.js-filter-list'); $this->assertSame('edit-body-0-value', $select->getAttribute('data-editor-for')); @@ -212,7 +212,7 @@ public function testLoading() { $this->assertTrue($editor_settings_present, "Text Editor module's JavaScript settings are on the page."); $this->assertSame($expected, $settings['editor'], "Text Editor module's JavaScript settings on the page are correct."); $this->assertTrue($editor_js_present, 'Text Editor JavaScript is present.'); - $this->assertCount(1, $body, 'A body field exists.'); + $this->assertSession()->elementsCount('xpath', $body, 1); $this->assertSession()->elementNotExists('css', 'select.js-filter-list'); // Verify that a single text format hidden input exists on the page and has // a "data-editor-for" attribute with the correct value. @@ -236,7 +236,7 @@ public function testLoading() { [, $editor_settings_present, $editor_js_present, $body] = $this->getThingsToCheck('body'); $this->assertTrue($editor_settings_present, 'Text Editor module settings.'); $this->assertTrue($editor_js_present, 'Text Editor JavaScript.'); - $this->assertCount(1, $body, 'A body field exists.'); + $this->assertSession()->elementsCount('xpath', $body, 1); $this->assertSession()->fieldDisabled("edit-body-0-value"); $this->assertSession()->fieldValueEquals("edit-body-0-value", 'This field has been disabled because you do not have sufficient permissions to edit it.'); $this->assertSession()->elementNotExists('css', 'select.js-filter-list'); @@ -276,7 +276,7 @@ public function testSupportedElementTypes() { [, $editor_settings_present, $editor_js_present, $field] = $this->getThingsToCheck('field-text', 'input'); $this->assertTrue($editor_settings_present, "Text Editor module's JavaScript settings are on the page."); $this->assertTrue($editor_js_present, 'Text Editor JavaScript is present.'); - $this->assertCount(1, $field, 'A text field exists.'); + $this->assertSession()->elementsCount('xpath', $field, 1); // Verify that a single text format selector exists on the page and has the // "editor" class and a "data-editor-for" attribute with the correct value. $this->assertSession()->elementsCount('css', 'select.js-filter-list', 1); @@ -295,7 +295,7 @@ public function testSupportedElementTypes() { [, $editor_settings_present, $editor_js_present, $field] = $this->getThingsToCheck('field-text', 'input'); $this->assertFalse($editor_settings_present, "Text Editor module's JavaScript settings are not on the page."); $this->assertFalse($editor_js_present, 'Text Editor JavaScript is not present.'); - $this->assertCount(1, $field, 'A text field exists.'); + $this->assertSession()->elementsCount('xpath', $field, 1); // Verify that a single text format selector exists on the page but without // the "editor" class or a "data-editor-for" attribute with the expected // value. @@ -315,7 +315,7 @@ protected function getThingsToCheck($field_name, $type = 'textarea') { // Editor.module's JS present. strpos($this->getSession()->getPage()->getContent(), $this->getModulePath('editor') . '/js/editor.js') !== FALSE, // Body field. - $this->xpath('//' . $type . '[@id="edit-' . $field_name . '-0-value"]'), + '//' . $type . '[@id="edit-' . $field_name . '-0-value"]', ]; }