installConfig(['system']); $manager = $this->container->get('plugin.manager.filter'); $bag = new FilterPluginCollection($manager, []); $this->filters = $bag->getAll(); } /** * Tests the align filter. */ public function testAlignFilter() { $filter = $this->filters['filter_align']; $test = function ($input) use ($filter) { return $filter->process($input, 'und'); }; // No data-align attribute. $input = ''; $expected = $input; $this->assertIdentical($expected, $test($input)->getProcessedText()); // Data-align attribute: all 3 allowed values. $input = ''; $expected = ''; $this->assertIdentical($expected, $test($input)->getProcessedText()); $input = ''; $expected = ''; $this->assertIdentical($expected, $test($input)->getProcessedText()); $input = ''; $expected = ''; $this->assertIdentical($expected, $test($input)->getProcessedText()); // Data-align attribute: a disallowed value. $input = ''; $expected = ''; $this->assertIdentical($expected, $test($input)->getProcessedText()); // Empty data-align attribute. $input = ''; $expected = ''; $this->assertIdentical($expected, $test($input)->getProcessedText()); // Ensure the filter also works with uncommon yet valid attribute quoting. $input = ''; $expected = ''; $output = $test($input); $this->assertIdentical($expected, $output->getProcessedText()); // Security test: attempt to inject an additional class. $input = ''; $expected = ''; $output = $test($input); $this->assertIdentical($expected, $output->getProcessedText()); // Security test: attempt an XSS. $input = ''; $expected = ''; $output = $test($input); $this->assertIdentical($expected, $output->getProcessedText()); } /** * Tests the caption filter. */ public function testCaptionFilter() { /** @var \Drupal\Core\Render\RendererInterface $renderer */ $renderer = \Drupal::service('renderer'); $filter = $this->filters['filter_caption']; $test = function ($input) use ($filter, $renderer) { return $renderer->executeInRenderContext(new RenderContext(), function () use ($input, $filter) { return $filter->process($input, 'und'); }); }; $attached_library = [ 'library' => [ 'filter/caption', ], ]; // No data-caption attribute. $input = ''; $expected = $input; $this->assertIdentical($expected, $test($input)->getProcessedText()); // Data-caption attribute. $input = ''; $expected = '
Loquacious llama!
'; $output = $test($input); $this->assertIdentical($expected, $output->getProcessedText()); $this->assertIdentical($attached_library, $output->getAttachments()); // Empty data-caption attribute. $input = ''; $expected = ''; $this->assertIdentical($expected, $test($input)->getProcessedText()); // HTML entities in the caption. $input = ''; $expected = '
“Loquacious llama!”
'; $output = $test($input); $this->assertIdentical($expected, $output->getProcessedText()); $this->assertIdentical($attached_library, $output->getAttachments()); // HTML encoded as HTML entities in data-caption attribute. $input = ''; $expected = '
Loquacious llama!
'; $output = $test($input); $this->assertIdentical($expected, $output->getProcessedText()); $this->assertIdentical($attached_library, $output->getAttachments()); // HTML (not encoded as HTML entities) in data-caption attribute, which is // not allowed by the HTML spec, but may happen when people manually write // HTML, so we explicitly support it. $input = ''; $expected = '
Loquacious llama!
'; $output = $test($input); $this->assertIdentical($expected, $output->getProcessedText()); $this->assertIdentical($attached_library, $output->getAttachments()); // Security test: attempt an XSS. $input = ''; $expected = '
alert(\'Loquacious llama!\')
'; $output = $test($input); $this->assertIdentical($expected, $output->getProcessedText()); $this->assertIdentical($attached_library, $output->getAttachments()); // Ensure the filter also works with uncommon yet valid attribute quoting. $input = ''; $expected = '
Loquacious llama!
'; $output = $test($input); $this->assertIdentical($expected, $output->getProcessedText()); $this->assertIdentical($attached_library, $output->getAttachments()); // Finally, ensure that this also works on any other tag. $input = '