function AssertLegacyTrait::assertTextHelper

Same name in other branches
  1. 8.9.x 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.

Deprecated

in drupal:8.2.0 and is removed from drupal:10.0.0. Use $this->assertSession()->pageTextContains() or $this->assertSession()->pageTextNotContains() instead.

See also

https://www.drupal.org/node/3129738

1 call to AssertLegacyTrait::assertTextHelper()
AssertLegacyTraitTest::testAssertTextHelper in core/tests/Drupal/Tests/Core/Assert/AssertLegacyTraitTest.php
@covers ::assertTextHelper

File

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

Class

AssertLegacyTrait
Provides convenience methods for assertions in browser tests.

Namespace

Drupal\FunctionalTests

Code

protected function assertTextHelper($text, $not_exists = TRUE) {
    @trigger_error('AssertLegacyTrait::assertTextHelper() is deprecated in drupal:8.2.0 and is removed from drupal:10.0.0. Use $this->assertSession()->pageTextContains() or $this->assertSession()->pageTextNotContains() instead. See https://www.drupal.org/node/3129738', E_USER_DEPRECATED);
    $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.