function AssertContentTrait::assertUniqueTextHelper

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

Helper for assertUniqueText and assertNoUniqueText.

It is not recommended to call this function directly.

Parameters

string|\Drupal\Component\Render\MarkupInterface $text: Plain text to look for.

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.

bool $be_unique: (optional) TRUE if this text should be found only once, FALSE if it should be found more than once. Defaults to FALSE.

Return value

bool TRUE on pass.

2 calls to AssertContentTrait::assertUniqueTextHelper()
AssertContentTrait::assertNoUniqueText in core/tests/Drupal/KernelTests/AssertContentTrait.php
Passes if the text is found MORE THAN ONCE on the text version of the page.
AssertContentTrait::assertUniqueText in core/tests/Drupal/KernelTests/AssertContentTrait.php
Passes if the text is found ONLY ONCE on the text version of the page.

File

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

Class

AssertContentTrait
Provides test methods to assert content.

Namespace

Drupal\KernelTests

Code

protected function assertUniqueTextHelper($text, $message = '', $group = NULL, $be_unique = FALSE) {
    // Cast MarkupInterface objects to string.
    $text = (string) $text;
    if (!$message) {
        $message = '"' . $text . '"' . ($be_unique ? ' found only once' : ' found more than once');
    }
    $first_occurrence = strpos($this->getTextContent(), $text);
    if ($first_occurrence === FALSE) {
        $this->fail($message);
    }
    $offset = $first_occurrence + strlen($text);
    $second_occurrence = strpos($this->getTextContent(), $text, $offset);
    $this->assertEquals($be_unique, $second_occurrence === FALSE, $message);
    return TRUE;
}

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