function AssertContentTrait::assertNoDuplicateIds

Same name in other branches
  1. 9 core/tests/Drupal/KernelTests/AssertContentTrait.php \Drupal\KernelTests\AssertContentTrait::assertNoDuplicateIds()
  2. 8.9.x core/tests/Drupal/KernelTests/AssertContentTrait.php \Drupal\KernelTests\AssertContentTrait::assertNoDuplicateIds()
  3. 10 core/tests/Drupal/KernelTests/AssertContentTrait.php \Drupal\KernelTests\AssertContentTrait::assertNoDuplicateIds()

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

Parameters

string $message: (optional) A message to display with the assertion. Do not translate messages with t(). Use double quotes and embed variables directly in message text, or use sprintf() if necessary. Avoid the use of \Drupal\Component\Render\FormattableMarkup unless you cast the object to a string. If left blank, a default message will be displayed.

string $group: Deprecated.

array $ids_to_skip: An optional array of ids to skip when checking for duplicates. It is always a bug to have duplicate HTML IDs, so this parameter is to enable incremental fixing of core code. Whenever a test passes this parameter, it should add a "todo" comment above the call to this function explaining the legacy bug that the test wishes to ignore and including a link to an issue that is working to fix that legacy bug.

Return value

bool TRUE on pass.

File

core/tests/Drupal/KernelTests/AssertContentTrait.php, line 1294

Class

AssertContentTrait
Provides test methods to assert content.

Namespace

Drupal\KernelTests

Code

protected function assertNoDuplicateIds($message = '', $group = NULL, $ids_to_skip = []) : bool {
    $status = TRUE;
    foreach ($this->xpath('//*[@id]') as $element) {
        $id = (string) $element['id'];
        if (isset($seen_ids[$id]) && !in_array($id, $ids_to_skip)) {
            $this->fail(new FormattableMarkup('The HTML ID %id is unique.', [
                '%id' => $id,
            ]));
            $status = FALSE;
        }
        $seen_ids[$id] = TRUE;
    }
    $this->assertTrue($status, $message);
    return TRUE;
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.