function FilterKernelTest::testAlignAndCaptionFilters

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

Tests the combination of the align and caption filters.

File

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

Class

FilterKernelTest
Tests Filter module filters individually.

Namespace

Drupal\Tests\filter\Kernel

Code

public function testAlignAndCaptionFilters() : void {
  /** @var \Drupal\Core\Render\RendererInterface $renderer */
  $renderer = \Drupal::service('renderer');
  $align_filter = $this->filters['filter_align'];
  $caption_filter = $this->filters['filter_caption'];
  $test = function ($input) use ($align_filter, $caption_filter, $renderer) {
    return $renderer->executeInRenderContext(new RenderContext(), function () use ($input, $align_filter, $caption_filter) {
      return $caption_filter->process($align_filter->process($input, 'und'), 'und');
    });
  };
  $attached_library = [
    'library' => [
      'filter/caption',
    ],
  ];
  // Both data-caption and data-align attributes: all 3 allowed values for the
  // data-align attribute.
  $input = '<img src="llama.jpg" data-caption="Loquacious llama!" data-align="left" />';
  $expected = '<figure role="group" class="align-left">
<img src="llama.jpg">
<figcaption>Loquacious llama!</figcaption>
</figure>
';
  $output = $test($input);
  $this->assertSame($expected, $output->getProcessedText());
  $this->assertSame($attached_library, $output->getAttachments());
  $input = '<img src="llama.jpg" data-caption="Loquacious llama!" data-align="center" />';
  $expected = '<figure role="group" class="align-center">
<img src="llama.jpg">
<figcaption>Loquacious llama!</figcaption>
</figure>
';
  $output = $test($input);
  $this->assertSame($expected, $output->getProcessedText());
  $this->assertSame($attached_library, $output->getAttachments());
  $input = '<img src="llama.jpg" data-caption="Loquacious llama!" data-align="right" />';
  $expected = '<figure role="group" class="align-right">
<img src="llama.jpg">
<figcaption>Loquacious llama!</figcaption>
</figure>
';
  $output = $test($input);
  $this->assertSame($expected, $output->getProcessedText());
  $this->assertSame($attached_library, $output->getAttachments());
  // Both data-caption and data-align attributes, but a disallowed data-align
  // attribute value.
  $input = '<img src="llama.jpg" data-caption="Loquacious llama!" data-align="left foobar" />';
  $expected = '<figure role="group">
<img src="llama.jpg">
<figcaption>Loquacious llama!</figcaption>
</figure>
';
  $output = $test($input);
  $this->assertSame($expected, $output->getProcessedText());
  $this->assertSame($attached_library, $output->getAttachments());
  // Ensure both filters together work for linked images.
  $input = '<a href="http://example.com/llamas/are/awesome/but/kittens/are/cool/too"><img src="llama.jpg" data-caption="Loquacious llama!" data-align="center" /></a>';
  $expected = '<figure role="group" class="align-center">
<a href="http://example.com/llamas/are/awesome/but/kittens/are/cool/too"><img src="llama.jpg"></a>
<figcaption>Loquacious llama!</figcaption>
</figure>
';
  $output = $test($input);
  $this->assertSame($expected, $output->getProcessedText());
  $this->assertSame($attached_library, $output->getAttachments());
}

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