diff -u b/core/tests/Drupal/Tests/BrowserTestBase.php b/core/tests/Drupal/Tests/BrowserTestBase.php --- b/core/tests/Drupal/Tests/BrowserTestBase.php +++ b/core/tests/Drupal/Tests/BrowserTestBase.php @@ -707,17 +707,10 @@ * (optional) A message to display with the assertion. */ protected function assertNoDuplicateIds($message = '') { - $args = ['@url' => $this->getUrl()]; - - if (!$elements = $this->xpath('//*[@id]')) { - $this->fail(new FormattableMarkup('The page @url contains no HTML IDs.', $args)); - return; - } - - $message = $message ?: new FormattableMarkup('The page @url does not contain duplicate HTML IDs', $args); + $message = $message ?: new FormattableMarkup('The page @url does not contain duplicate HTML IDs', ['@url' => $this->getUrl()]); $seen_ids = []; - foreach ($elements as $element) { + foreach ($this->xpath('//*[@id]') as $element) { $id = $element->getAttribute('id'); if (isset($seen_ids[$id])) { $this->fail($message); only in patch2: unchanged: --- a/core/modules/system/tests/modules/test_page_test/src/Controller/Test.php +++ b/core/modules/system/tests/modules/test_page_test/src/Controller/Test.php @@ -128,4 +128,54 @@ public function metaRefresh() { return new RedirectResponse(Url::fromRoute('test_page_test.test_page', [], ['absolute' => TRUE])->toString(), 302); } + /** + * Returns a page render array with 2 elements with the same HTML IDs. + * + * @return array + * A render array as expected by + * \Drupal\Core\Render\RendererInterface::render(). + */ + public function renderPageWithDuplicateIds() { + return [ + '#type' => 'container', + 'title' => [ + '#type' => 'html_tag', + '#tag' => 'h1', + '#value' => 'Hello', + '#attributes' => ['id' => 'page-element'], + ], + 'description' => [ + '#type' => 'html_tag', + '#tag' => 'h2', + '#value' => 'World', + '#attributes' => ['id' => 'page-element'], + ], + ]; + } + + /** + * Returns a page render array with 2 elements with the unique HTML IDs. + * + * @return array + * A render array as expected by + * \Drupal\Core\Render\RendererInterface::render(). + */ + public function renderPageWithoutDuplicateIds() { + return [ + '#type' => 'container', + 'title' => [ + '#type' => 'html_tag', + '#tag' => 'h1', + '#value' => 'Hello', + '#attributes' => ['id' => 'page-element-title'], + ], + 'description' => [ + '#type' => 'html_tag', + '#tag' => 'h2', + '#value' => 'World', + '#attributes' => ['id' => 'page-element-description'], + ], + ]; + } + } only in patch2: unchanged: --- a/core/modules/system/tests/modules/test_page_test/test_page_test.routing.yml +++ b/core/modules/system/tests/modules/test_page_test/test_page_test.routing.yml @@ -90,3 +90,17 @@ test_page_test.meta_refresh: _controller: '\Drupal\test_page_test\Controller\Test::metaRefresh' requirements: _access: 'TRUE' + +test_page_test.page_with_duplicate_ids: + path: '/test-page-with-duplicate-ids' + defaults: + _controller: '\Drupal\test_page_test\Controller\Test::renderPageWithDuplicateIds' + requirements: + _access: 'TRUE' + +test_page_test.page_without_duplicate_ids: + path: '/test-page-without-duplicate-ids' + defaults: + _controller: '\Drupal\test_page_test\Controller\Test::renderPageWithoutDuplicateIds' + requirements: + _access: 'TRUE' only in patch2: unchanged: --- a/core/tests/Drupal/FunctionalTests/BrowserTestBaseTest.php +++ b/core/tests/Drupal/FunctionalTests/BrowserTestBaseTest.php @@ -8,6 +8,7 @@ use Drupal\Core\Url; use Drupal\Tests\BrowserTestBase; use Drupal\Tests\Traits\Core\CronRunTrait; +use PHPUnit\Framework\AssertionFailedError; /** * Tests BrowserTestBase functionality. @@ -707,4 +708,18 @@ public function testHtkey() { $this->assertSession()->statusCodeEquals(403); } + /** + * Tests assertNoDuplicateIds() functionality. + * + * @see \Drupal\Tests\BrowserTestBase::assertNoDuplicateIds() + */ + public function testAssertNoDuplicateIds() { + $this->drupalGet(Url::fromRoute('test_page_test.page_without_duplicate_ids')); + $this->assertNoDuplicateIds(); + + $this->drupalGet(Url::fromRoute('test_page_test.page_with_duplicate_ids')); + $this->setExpectedException(AssertionFailedError::class); + $this->assertNoDuplicateIds(); + } + }