You are here

protected function MultiFormTest::assertNoDuplicateIds in Drupal 8

Asserts that each HTML ID is used for just a single element on the page.

Parameters

string $message: (optional) A message to display with the assertion.

1 call to MultiFormTest::assertNoDuplicateIds()
MultiFormTest::testMultiForm in core/tests/Drupal/FunctionalJavascriptTests/Ajax/MultiFormTest.php
Tests that pages with the 'node_page_form' included twice work correctly.

File

core/tests/Drupal/FunctionalJavascriptTests/Ajax/MultiFormTest.php, line 122

Class

MultiFormTest
Tests that AJAX-enabled forms work when multiple instances of the same form are on a page.

Namespace

Drupal\FunctionalJavascriptTests\Ajax

Code

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);
  $seen_ids = [];
  foreach ($elements as $element) {
    $id = $element
      ->getAttribute('id');
    if (isset($seen_ids[$id])) {
      $this
        ->fail($message);
      return;
    }
    $seen_ids[$id] = TRUE;
  }
  $this
    ->assertTrue(TRUE, $message);
}