function FilterKernelTest::testNoFollowFilter

Same name and namespace in other branches
  1. 10 core/modules/filter/tests/src/Kernel/FilterKernelTest.php \Drupal\Tests\filter\Kernel\FilterKernelTest::testNoFollowFilter()
  2. 9 core/modules/filter/tests/src/Kernel/FilterKernelTest.php \Drupal\Tests\filter\Kernel\FilterKernelTest::testNoFollowFilter()
  3. 8.9.x core/modules/filter/tests/src/Kernel/FilterKernelTest.php \Drupal\Tests\filter\Kernel\FilterKernelTest::testNoFollowFilter()

Tests the spam deterrent.

File

core/modules/filter/tests/src/Kernel/FilterKernelTest.php, line 653

Class

FilterKernelTest
Tests Filter module filters individually.

Namespace

Drupal\Tests\filter\Kernel

Code

public function testNoFollowFilter() : void {
  // Get FilterHtml object.
  $filter = $this->filters['filter_html'];
  $filter->setConfiguration([
    'settings' => [
      'allowed_html' => '<a href>',
      'filter_html_help' => 1,
      'filter_html_nofollow' => 1,
    ],
  ]);
  // Test if the rel="nofollow" attribute is added, even if we try to prevent
  // it.
  $f = (string) $filter->process('<a href="http://www.example.com/">text</a>', Language::LANGCODE_NOT_SPECIFIED);
  $this->assertNormalized($f, 'rel="nofollow"', 'Spam deterrent -- no evasion.');
  $f = (string) $filter->process('<A href="http://www.example.com/">text</a>', Language::LANGCODE_NOT_SPECIFIED);
  $this->assertNormalized($f, 'rel="nofollow"', 'Spam deterrent evasion -- capital A.');
  $f = (string) $filter->process("<a/href=\"http://www.example.com/\">text</a>", Language::LANGCODE_NOT_SPECIFIED);
  $this->assertNormalized($f, 'rel="nofollow"', 'Spam deterrent evasion -- non whitespace character after tag name.');
  $f = (string) $filter->process("<\x00a\x00 href=\"http://www.example.com/\">text</a>", Language::LANGCODE_NOT_SPECIFIED);
  $this->assertNormalized($f, 'rel="nofollow"', 'Spam deterrent evasion -- some nulls.');
  $f = (string) $filter->process('<a href="http://www.example.com/" rel="follow">text</a>', Language::LANGCODE_NOT_SPECIFIED);
  $this->assertNoNormalized($f, 'rel="follow"', 'Spam deterrent evasion -- with rel set - rel="follow" removed.');
  $this->assertNormalized($f, 'rel="nofollow"', 'Spam deterrent evasion -- with rel set - rel="nofollow" added.');
}

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