Updated Drupal to 8.6. This goes with the following updates because it's possible...
[yaffs-website] / web / core / modules / system / tests / src / Functional / Theme / TwigDebugMarkupTest.php
1 <?php
2
3 namespace Drupal\Tests\system\Functional\Theme;
4
5 use Drupal\Component\Utility\Html;
6 use Drupal\Tests\BrowserTestBase;
7
8 /**
9  * Tests for Twig debug markup.
10  *
11  * @group Theme
12  */
13 class TwigDebugMarkupTest extends BrowserTestBase {
14
15   /**
16    * Modules to enable.
17    *
18    * @var array
19    */
20   public static $modules = ['theme_test', 'node'];
21
22   /**
23    * Tests debug markup added to Twig template output.
24    */
25   public function testTwigDebugMarkup() {
26     /** @var \Drupal\Core\Render\RendererInterface $renderer */
27     $renderer = $this->container->get('renderer');
28     $extension = twig_extension();
29     \Drupal::service('theme_handler')->install(['test_theme']);
30     $this->config('system.theme')->set('default', 'test_theme')->save();
31     $this->drupalCreateContentType(['type' => 'page']);
32     // Enable debug, rebuild the service container, and clear all caches.
33     $parameters = $this->container->getParameter('twig.config');
34     $parameters['debug'] = TRUE;
35     $this->setContainerParameter('twig.config', $parameters);
36     $this->rebuildContainer();
37     $this->resetAll();
38
39     $cache = $this->container->get('theme.registry')->get();
40     // Create array of Twig templates.
41     $templates = drupal_find_theme_templates($cache, $extension, drupal_get_path('theme', 'test_theme'));
42     $templates += drupal_find_theme_templates($cache, $extension, drupal_get_path('module', 'node'));
43
44     // Create a node and test different features of the debug markup.
45     $node = $this->drupalCreateNode();
46     $build = node_view($node);
47     $output = $renderer->renderRoot($build);
48     $this->assertTrue(strpos($output, '<!-- THEME DEBUG -->') !== FALSE, 'Twig debug markup found in theme output when debug is enabled.');
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->assertContains(Html::escape('node--<script type="text/javascript">alert(\'yo\');</script>'), (string) $output);
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 }