You are here

function FilterUnitTest::testAlignAndCaptionFilters in Zircon Profile 8

Same name and namespace in other branches
  1. 8.0 core/modules/filter/src/Tests/FilterUnitTest.php \Drupal\filter\Tests\FilterUnitTest::testAlignAndCaptionFilters()

Tests the combination of the align and caption filters.

File

core/modules/filter/src/Tests/FilterUnitTest.php, line 268
Contains \Drupal\filter\Tests\FilterUnitTest.

Class

FilterUnitTest
Tests Filter module filters individually.

Namespace

Drupal\filter\Tests

Code

function testAlignAndCaptionFilters() {

  /** @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 = array(
    'library' => array(
      '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
    ->assertIdentical($expected, $output
    ->getProcessedText());
  $this
    ->assertIdentical($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
    ->assertIdentical($expected, $output
    ->getProcessedText());
  $this
    ->assertIdentical($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
    ->assertIdentical($expected, $output
    ->getProcessedText());
  $this
    ->assertIdentical($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
    ->assertIdentical($expected, $output
    ->getProcessedText());
  $this
    ->assertIdentical($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>' . "\n" . '<figcaption>Loquacious llama!</figcaption></figure>';
  $output = $test($input);
  $this
    ->assertIdentical($expected, $output
    ->getProcessedText());
  $this
    ->assertIdentical($attached_library, $output
    ->getAttachments());
}