3 namespace Drupal\Tests\filter\Functional;
5 use Drupal\Core\Render\RenderContext;
6 use Drupal\Tests\BrowserTestBase;
7 use Drupal\filter\FilterPluginCollection;
10 * Tests the caption filter with Twig debugging on.
14 class FilterCaptionTwigDebugTest extends BrowserTestBase {
21 public static $modules = ['system', 'filter'];
24 * @var \Drupal\filter\Plugin\FilterInterface[]
29 * Enables Twig debugging.
31 protected function debugOn() {
32 // Enable debug, rebuild the service container, and clear all caches.
33 $parameters = $this->container->getParameter('twig.config');
34 if (!$parameters['debug']) {
35 $parameters['debug'] = TRUE;
36 $this->setContainerParameter('twig.config', $parameters);
37 $this->rebuildContainer();
43 * Disables Twig debugging.
45 protected function debugOff() {
46 // Disable debug, rebuild the service container, and clear all caches.
47 $parameters = $this->container->getParameter('twig.config');
48 if ($parameters['debug']) {
49 $parameters['debug'] = FALSE;
50 $this->setContainerParameter('twig.config', $parameters);
51 $this->rebuildContainer();
59 protected function setUp() {
64 $manager = $this->container->get('plugin.manager.filter');
65 $bag = new FilterPluginCollection($manager, []);
66 $this->filters = $bag->getAll();
72 protected function tearDown() {
77 * Test the caption filter with Twig debugging on.
79 public function testCaptionFilter() {
80 /** @var \Drupal\Core\Render\RendererInterface $renderer */
81 $renderer = \Drupal::service('renderer');
82 $filter = $this->filters['filter_caption'];
84 $test = function ($input) use ($filter, $renderer) {
85 return $renderer->executeInRenderContext(new RenderContext(), function () use ($input, $filter) {
86 return $filter->process($input, 'und');
90 // No data-caption attribute.
91 $input = '<img src="llama.jpg" />';
93 $this->assertIdentical($expected, $test($input)->getProcessedText());
95 // Data-caption attribute.
96 $input = '<img src="llama.jpg" data-caption="Loquacious llama!" />';
97 $expected = '<img src="llama.jpg" /><figcaption>Loquacious llama!</figcaption>';
98 $output = $test($input);
99 $output = $output->getProcessedText();
100 $this->assertTrue(strpos($output, $expected) !== FALSE, "\"$output\" contains \"$expected\"");
101 $this->assertTrue(strpos($output, '<!-- THEME HOOK: \'filter_caption\' -->') !== FALSE, 'filter_caption theme hook debug comment is present.');