Upgraded drupal core with security updates
[yaffs-website] / web / core / modules / system / src / Tests / Theme / TwigDebugMarkupTest.php
1 <?php
2
3 namespace Drupal\system\Tests\Theme;
4
5 use Drupal\simpletest\WebTestBase;
6
7 /**
8  * Tests for Twig debug markup.
9  *
10  * @group Theme
11  */
12 class TwigDebugMarkupTest extends WebTestBase {
13
14   /**
15    * Modules to enable.
16    *
17    * @var array
18    */
19   public static $modules = ['theme_test', 'node'];
20
21   /**
22    * Tests debug markup added to Twig template output.
23    */
24   public function testTwigDebugMarkup() {
25     /** @var \Drupal\Core\Render\RendererInterface $renderer */
26     $renderer = $this->container->get('renderer');
27     $extension = twig_extension();
28     \Drupal::service('theme_handler')->install(['test_theme']);
29     $this->config('system.theme')->set('default', 'test_theme')->save();
30     $this->drupalCreateContentType(['type' => 'page']);
31     // Enable debug, rebuild the service container, and clear all caches.
32     $parameters = $this->container->getParameter('twig.config');
33     $parameters['debug'] = TRUE;
34     $this->setContainerParameter('twig.config', $parameters);
35     $this->rebuildContainer();
36     $this->resetAll();
37
38     $cache = $this->container->get('theme.registry')->get();
39     // Create array of Twig templates.
40     $templates = drupal_find_theme_templates($cache, $extension, drupal_get_path('theme', 'test_theme'));
41     $templates += drupal_find_theme_templates($cache, $extension, drupal_get_path('module', 'node'));
42
43     // Create a node and test different features of the debug markup.
44     $node = $this->drupalCreateNode();
45     $build = node_view($node);
46     $output = $renderer->renderRoot($build);
47     $this->assertTrue(strpos($output, '<!-- THEME DEBUG -->') !== FALSE, 'Twig debug markup found in theme output when debug is enabled.');
48     $this->setRawContent($output);
49     $this->assertTrue(strpos($output, "THEME HOOK: 'node'") !== FALSE, 'Theme call information found.');
50     $this->assertTrue(strpos($output, '* node--1--full' . $extension . PHP_EOL . '   x node--1' . $extension . PHP_EOL . '   * node--page--full' . $extension . PHP_EOL . '   * node--page' . $extension . PHP_EOL . '   * node--full' . $extension . PHP_EOL . '   * node' . $extension) !== FALSE, 'Suggested template files found in order and node ID specific template shown as current template.');
51     $this->assertEscaped('node--<script type="text/javascript">alert(\'yo\');</script>');
52     $template_filename = $templates['node__1']['path'] . '/' . $templates['node__1']['template'] . $extension;
53     $this->assertTrue(strpos($output, "BEGIN OUTPUT from '$template_filename'") !== FALSE, 'Full path to current template file found.');
54
55     // Create another node and make sure the template suggestions shown in the
56     // debug markup are correct.
57     $node2 = $this->drupalCreateNode();
58     $build = node_view($node2);
59     $output = $renderer->renderRoot($build);
60     $this->assertTrue(strpos($output, '* node--2--full' . $extension . PHP_EOL . '   * node--2' . $extension . PHP_EOL . '   * node--page--full' . $extension . PHP_EOL . '   * node--page' . $extension . PHP_EOL . '   * node--full' . $extension . PHP_EOL . '   x node' . $extension) !== FALSE, 'Suggested template files found in order and base template shown as current template.');
61
62     // Create another node and make sure the template suggestions shown in the
63     // debug markup are correct.
64     $node3 = $this->drupalCreateNode();
65     $build = ['#theme' => 'node__foo__bar'];
66     $build += node_view($node3);
67     $output = $renderer->renderRoot($build);
68     $this->assertTrue(strpos($output, "THEME HOOK: 'node__foo__bar'") !== FALSE, 'Theme call information found.');
69     $this->assertTrue(strpos($output, '* node--foo--bar' . $extension . PHP_EOL . '   * node--foo' . $extension . PHP_EOL . '   * node--&lt;script type=&quot;text/javascript&quot;&gt;alert(&#039;yo&#039;);&lt;/script&gt;' . $extension . PHP_EOL . '   * node--3--full' . $extension . PHP_EOL . '   * node--3' . $extension . PHP_EOL . '   * node--page--full' . $extension . PHP_EOL . '   * node--page' . $extension . PHP_EOL . '   * node--full' . $extension . PHP_EOL . '   x node' . $extension) !== FALSE, 'Suggested template files found in order and base template shown as current template.');
70
71     // Disable debug, rebuild the service container, and clear all caches.
72     $parameters = $this->container->getParameter('twig.config');
73     $parameters['debug'] = FALSE;
74     $this->setContainerParameter('twig.config', $parameters);
75     $this->rebuildContainer();
76     $this->resetAll();
77
78     $build = node_view($node);
79     $output = $renderer->renderRoot($build);
80     $this->assertFalse(strpos($output, '<!-- THEME DEBUG -->') !== FALSE, 'Twig debug markup not found in theme output when debug is disabled.');
81   }
82
83 }