annotate core/modules/system/src/Tests/Theme/TwigDebugMarkupTest.php @ 14:1fec387a4317

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