You are here

public function TextSummaryTest::testNormalization in Drupal 9

Same name and namespace in other branches
  1. 10 core/modules/text/tests/src/Kernel/TextSummaryTest.php \Drupal\Tests\text\Kernel\TextSummaryTest::testNormalization()

Test text normalization when filter_html or filter_htmlcorrector enabled.

File

core/modules/text/tests/src/Kernel/TextSummaryTest.php, line 310

Class

TextSummaryTest
Tests text_summary() with different strings and lengths.

Namespace

Drupal\Tests\text\Kernel

Code

public function testNormalization() {
  FilterFormat::create([
    'format' => 'filter_html_enabled',
    'filters' => [
      'filter_html' => [
        'status' => 1,
        'settings' => [
          'allowed_html' => '<strong>',
        ],
      ],
    ],
  ])
    ->save();
  FilterFormat::create([
    'format' => 'filter_htmlcorrector_enabled',
    'filters' => [
      'filter_htmlcorrector' => [
        'status' => 1,
      ],
    ],
  ])
    ->save();
  FilterFormat::create([
    'format' => 'neither_filter_enabled',
    'filters' => [],
  ])
    ->save();
  $filtered_markup = FilteredMarkup::create('<div><strong><span>Hello World</span></strong></div>');

  // With either HTML filter enabled, text_summary() will normalize the text
  // using HTML::normalize().
  $summary = text_summary($filtered_markup, 'filter_html_enabled', 30);
  $this
    ->assertStringContainsString('<div><strong><span>', $summary);
  $this
    ->assertStringContainsString('</span></strong></div>', $summary);
  $summary = text_summary($filtered_markup, 'filter_htmlcorrector_enabled', 30);
  $this
    ->assertStringContainsString('<div><strong><span>', $summary);
  $this
    ->assertStringContainsString('</span></strong></div>', $summary);

  // If neither filter is enabled, the text will not be normalized.
  $summary = text_summary($filtered_markup, 'neither_filter_enabled', 30);
  $this
    ->assertStringContainsString('<div><strong><span>', $summary);
  $this
    ->assertStringNotContainsString('</span></strong></div>', $summary);
}