You are here

protected function AssertLegacyTrait::assertText in Drupal 9

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

Passes if the page (with HTML stripped) contains the text.

Note that stripping HTML tags also removes their attributes, such as the values of text fields.

Parameters

string $text: Plain text to look for.

Deprecated

in drupal:8.2.0 and is removed from drupal:10.0.0. Use

  • $this->assertSession()->responseContains() for non-HTML responses, like XML or Json.
  • $this->assertSession()->pageTextContains() for HTML responses. Unlike the deprecated assertText(), the passed text should be HTML decoded, exactly as a human sees it in the browser.

See also

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

1 call to AssertLegacyTrait::assertText()
BrowserTestBaseTest::testAssertText in core/tests/Drupal/FunctionalTests/BrowserTestBaseTest.php
Tests deprecated assertText.

File

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

Class

AssertLegacyTrait
Provides convenience methods for assertions in browser tests.

Namespace

Drupal\FunctionalTests

Code

protected function assertText($text) {
  @trigger_error('AssertLegacyTrait::assertText() is deprecated in drupal:8.2.0 and is removed from drupal:10.0.0. Use $this->assertSession()->responseContains() or $this->assertSession()->pageTextContains() instead. See https://www.drupal.org/node/3129738', E_USER_DEPRECATED);
  if (func_num_args() > 1) {
    @trigger_error('Calling AssertLegacyTrait::assertText() with more than one argument is deprecated in drupal:8.2.0 and the method is removed from drupal:10.0.0. Use $this->assertSession()->responseContains() or $this->assertSession()->pageTextContains() instead. See https://www.drupal.org/node/3129738', E_USER_DEPRECATED);
  }

  // Cast MarkupInterface to string.
  $text = (string) $text;
  $content_type = $this
    ->getSession()
    ->getResponseHeader('Content-type');

  // In case of a Non-HTML response (example: XML) check the original
  // response.
  if (strpos($content_type, 'html') === FALSE) {
    $this
      ->assertSession()
      ->responseContains($text);
  }
  else {

    // 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', '', $this
      ->getSession()
      ->getPage()
      ->getContent());
    $page_text = Xss::filter($raw_content, []);
    $this
      ->assertStringContainsString($text, $page_text, "\"{$text}\" found");
  }
}