You are here

protected function AssertContentTrait::assertUniqueTextHelper in Drupal 9

Same name and namespace in other branches
  1. 8 core/tests/Drupal/KernelTests/AssertContentTrait.php \Drupal\KernelTests\AssertContentTrait::assertUniqueTextHelper()
  2. 10 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: use \Drupal\Component\Render\FormattableMarkup to embed variables in the message text, not t(). If left blank, a default message will be displayed.

string $group: (optional) The group this message is in, which is displayed in a column in test output. Use 'Debug' to indicate this is debugging output. Do not translate this string. Defaults to 'Other'; most tests do not override this default. Defaults to 'Other'.

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 696

Class

AssertContentTrait
Provides test methods to assert content.

Namespace

Drupal\KernelTests

Code

protected function assertUniqueTextHelper($text, $message = '', $group = 'Other', $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;
}