function FilterKernelTest::testUrlFilterContent

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

Tests URL filter on longer content.

Filters based on regular expressions should also be tested with a more complex content than just isolated test lines. The most common errors are:

  • accidental '*' (greedy) match instead of '*?' (minimal) match.
  • only matching first occurrence instead of all.
  • newlines not matching '.*'.

This test covers:

  • Document with multiple newlines and paragraphs (two newlines).
  • Mix of several HTML tags, invalid non-HTML tags, tags to ignore and HTML comments.
  • Empty HTML tags (BR, IMG).
  • Mix of absolute and partial URLs, and email addresses in one content.
  • Input that exceeds PCRE backtracking limit.

File

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

Class

FilterKernelTest
Tests Filter module filters individually.

Namespace

Drupal\Tests\filter\Kernel

Code

public function testUrlFilterContent() : void {
  // Get FilterUrl object.
  $filter = $this->filters['filter_url'];
  $filter->setConfiguration([
    'settings' => [
      'filter_url_length' => 496,
    ],
  ]);
  $path = __DIR__ . '/../..';
  $input = file_get_contents($path . '/filter.url-input.txt');
  $expected = file_get_contents($path . '/filter.url-output.txt');
  $result = _filter_url($input, $filter);
  $this->assertSame($expected, $result, 'Complex HTML document was correctly processed.');
  $pcre_backtrack_limit = ini_get('pcre.backtrack_limit');
  // Setting this limit to the smallest possible value should cause PCRE
  // errors and break the various preg_* functions used by _filter_url().
  ini_set('pcre.backtrack_limit', 1);
  // If PCRE errors occur, _filter_url() should return the exact same text.
  // Case of a small and simple HTML document.
  $input = $expected = '<p>www.test.com</p>';
  $result = _filter_url($input, $filter);
  $this->assertSame($expected, $result, 'Simple HTML document was left intact when PCRE errors occurred.');
  // Case of a complex HTML document.
  $input = $expected = file_get_contents($path . '/filter.url-input.txt');
  $result = _filter_url($input, $filter);
  $this->assertSame($expected, $result, 'Complex HTML document was left intact when PCRE errors occurred.');
  // Setting limit back to default.
  ini_set('pcre.backtrack_limit', $pcre_backtrack_limit);
}

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