You are here

protected function AssertContentTrait::assertUniqueTextHelper in Zircon Profile 8

Same name and namespace in other branches
  1. 8.0 core/modules/simpletest/src/AssertContentTrait.php \Drupal\simpletest\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\Utility\SafeMarkup::format() 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, FALSE on fail.

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

File

core/modules/simpletest/src/AssertContentTrait.php, line 695
Contains \Drupal\simpletest\AssertContentTrait.

Class

AssertContentTrait
Provides test methods to assert content.

Namespace

Drupal\simpletest

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) {
    return $this
      ->assert(FALSE, $message, $group);
  }
  $offset = $first_occurrence + strlen($text);
  $second_occurrence = strpos($this
    ->getTextContent(), $text, $offset);
  return $this
    ->assert($be_unique == ($second_occurrence === FALSE), $message, $group);
}