function AssertLegacyTrait::assertTextHelper

Same name and namespace in other branches
  1. 9 core/tests/Drupal/FunctionalTests/AssertLegacyTrait.php \Drupal\FunctionalTests\AssertLegacyTrait::assertTextHelper()

Helper for assertText and assertNoText.

Parameters

string $text: Plain text to look for.

bool $not_exists: (optional) TRUE if this text should not exist, FALSE if it should. Defaults to TRUE.

Return value

bool TRUE on pass, FALSE on fail.

2 calls to AssertLegacyTrait::assertTextHelper()
AssertLegacyTrait::assertNoText in core/tests/Drupal/FunctionalTests/AssertLegacyTrait.php
Passes if the page (with HTML stripped) does not contains the text.
AssertLegacyTrait::assertText in core/tests/Drupal/FunctionalTests/AssertLegacyTrait.php
Passes if the page (with HTML stripped) contains the text.

File

core/tests/Drupal/FunctionalTests/AssertLegacyTrait.php, line 130

Class

AssertLegacyTrait
Provides convenience methods for assertions in browser tests.

Namespace

Drupal\FunctionalTests

Code

protected function assertTextHelper($text, $not_exists = TRUE) {
    $args = [
        '@text' => $text,
    ];
    $message = $not_exists ? new FormattableMarkup('"@text" not found', $args) : new FormattableMarkup('"@text" found', $args);
    $raw_content = $this->getSession()
        ->getPage()
        ->getContent();
    // Trying to simulate what the user sees, given that it removes all text
    // inside the head tags, removes inline Javascript, fix all HTML entities,
    // removes dangerous protocols and filtering out all HTML tags, as they are
    // not visible in a normal browser.
    $raw_content = preg_replace('@<head>(.+?)</head>@si', '', $raw_content);
    $page_text = Xss::filter($raw_content, []);
    $actual = $not_exists == (strpos($page_text, (string) $text) === FALSE);
    $this->assertTrue($actual, $message);
    return $actual;
}

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