f5423e7dd7140373f30c36b66b0bb66bd7372f20
[yaffs-website] / web / core / modules / filter / tests / src / Functional / FilterCaptionTwigDebugTest.php
1 <?php
2
3 namespace Drupal\Tests\filter\Functional;
4
5 use Drupal\Core\Render\RenderContext;
6 use Drupal\Tests\BrowserTestBase;
7 use Drupal\filter\FilterPluginCollection;
8
9 /**
10  * Tests the caption filter with Twig debugging on.
11  *
12  * @group filter
13  */
14 class FilterCaptionTwigDebugTest extends BrowserTestBase {
15
16   /**
17    * Modules to enable.
18    *
19    * @var array
20    */
21   public static $modules = ['system', 'filter'];
22
23   /**
24    * @var \Drupal\filter\Plugin\FilterInterface[]
25    */
26   protected $filters;
27
28   /**
29    * Enables Twig debugging.
30    */
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();
38       $this->resetAll();
39     }
40   }
41
42   /**
43    * Disables Twig debugging.
44    */
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();
52       $this->resetAll();
53     }
54   }
55
56   /**
57    * {@inheritdoc}
58    */
59   protected function setUp() {
60     parent::setUp();
61
62     $this->debugOn();
63
64     $manager = $this->container->get('plugin.manager.filter');
65     $bag = new FilterPluginCollection($manager, []);
66     $this->filters = $bag->getAll();
67   }
68
69   /**
70    * {@inheritdoc}
71    */
72   protected function tearDown() {
73     $this->debugOff();
74   }
75
76   /**
77    * Test the caption filter with Twig debugging on.
78    */
79   public function testCaptionFilter() {
80     /** @var \Drupal\Core\Render\RendererInterface $renderer */
81     $renderer = \Drupal::service('renderer');
82     $filter = $this->filters['filter_caption'];
83
84     $test = function ($input) use ($filter, $renderer) {
85       return $renderer->executeInRenderContext(new RenderContext(), function () use ($input, $filter) {
86         return $filter->process($input, 'und');
87       });
88     };
89
90     // No data-caption attribute.
91     $input = '<img src="llama.jpg" />';
92     $expected = $input;
93     $this->assertIdentical($expected, $test($input)->getProcessedText());
94
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.');
102   }
103
104 }