You are here

protected function AssertLegacyTrait::assertNoText in Drupal 9

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

Passes if the page (with HTML stripped) does not 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()->responseNotContains() for non-HTML responses, like XML or Json.
  • $this->assertSession()->pageTextNotContains() for HTML responses. Unlike the deprecated assertNoText(), 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::assertNoText()
BrowserTestBaseTest::testAssertNoText in core/tests/Drupal/FunctionalTests/BrowserTestBaseTest.php
Tests deprecated assertNoText.

File

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

Class

AssertLegacyTrait
Provides convenience methods for assertions in browser tests.

Namespace

Drupal\FunctionalTests

Code

protected function assertNoText($text) {
  @trigger_error('AssertLegacyTrait::assertNoText() is deprecated in drupal:8.2.0 and is removed from drupal:10.0.0. Use $this->assertSession()->responseNotContains() or $this->assertSession()->pageTextNotContains() instead. See https://www.drupal.org/node/3129738', E_USER_DEPRECATED);
  if (func_num_args() > 1) {
    @trigger_error('Calling AssertLegacyTrait::assertNoText() 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()->responseNotContains() or $this->assertSession()->pageTextNotContains() 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()
      ->responseNotContains($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
      ->assertStringNotContainsString($text, $page_text, "\"{$text}\" not found");
  }
}