Upgraded drupal core with security updates
[yaffs-website] / web / core / tests / Drupal / KernelTests / Core / Theme / TwigEnvironmentTest.php
1 <?php
2
3 namespace Drupal\KernelTests\Core\Theme;
4
5 use Drupal\Component\Utility\Crypt;
6 use Drupal\Component\Utility\Html;
7 use Drupal\Core\Site\Settings;
8 use Drupal\Core\Template\TwigPhpStorageCache;
9 use Drupal\KernelTests\KernelTestBase;
10
11 /**
12  * Tests the twig environment.
13  *
14  * @see \Drupal\Core\Template\TwigEnvironment
15  * @group Twig
16  */
17 class TwigEnvironmentTest extends KernelTestBase {
18
19   /**
20    * Modules to enable.
21    *
22    * @var array
23    */
24   public static $modules = ['system'];
25
26   /**
27    * Tests inline templates.
28    */
29   public function testInlineTemplate() {
30     /** @var \Drupal\Core\Render\RendererInterface $renderer */
31     $renderer = $this->container->get('renderer');
32     /** @var \Drupal\Core\Template\TwigEnvironment $environment */
33     $environment = \Drupal::service('twig');
34     $this->assertEqual($environment->renderInline('test-no-context'), 'test-no-context');
35     $this->assertEqual($environment->renderInline('test-with-context {{ llama }}', ['llama' => 'muuh']), 'test-with-context muuh');
36
37     $element = [];
38     $unsafe_string = '<script>alert(\'Danger! High voltage!\');</script>';
39     $element['test'] = [
40       '#type' => 'inline_template',
41       '#template' => 'test-with-context <label>{{ unsafe_content }}</label>',
42       '#context' => ['unsafe_content' => $unsafe_string],
43     ];
44     $this->assertEqual($renderer->renderRoot($element), 'test-with-context <label>' . Html::escape($unsafe_string) . '</label>');
45
46     // Enable twig_auto_reload and twig_debug.
47     $settings = Settings::getAll();
48     $settings['twig_debug'] = TRUE;
49     $settings['twig_auto_reload'] = TRUE;
50
51     new Settings($settings);
52     $this->container = \Drupal::service('kernel')->rebuildContainer();
53     \Drupal::setContainer($this->container);
54
55     $element = [];
56     $element['test'] = [
57       '#type' => 'inline_template',
58       '#template' => 'test-with-context {{ llama }}',
59       '#context' => ['llama' => 'muuh'],
60     ];
61     $element_copy = $element;
62     // Render it twice so that twig caching is triggered.
63     $this->assertEqual($renderer->renderRoot($element), 'test-with-context muuh');
64     $this->assertEqual($renderer->renderRoot($element_copy), 'test-with-context muuh');
65
66     // Tests caching of inline templates with long content to ensure the
67     // generated cache key can be used as a filename.
68     $element = [];
69     $element['test'] = [
70       '#type' => 'inline_template',
71       '#template' => 'Llamas sometimes spit and wrestle with their {{ llama }}. Kittens are soft and fuzzy and they sometimes say {{ kitten }}. Flamingos have long legs and they are usually {{ flamingo }}. Pandas eat bamboo and they are {{ panda }}. Giraffes have long necks and long tongues and they eat {{ giraffe }}.',
72       '#context' => [
73         'llama' => 'necks',
74         'kitten' => 'meow',
75         'flamingo' => 'pink',
76         'panda' => 'bears',
77         'giraffe' => 'leaves',
78       ],
79     ];
80     $expected = 'Llamas sometimes spit and wrestle with their necks. Kittens are soft and fuzzy and they sometimes say meow. Flamingos have long legs and they are usually pink. Pandas eat bamboo and they are bears. Giraffes have long necks and long tongues and they eat leaves.';
81     $element_copy = $element;
82
83     // Render it twice so that twig caching is triggered.
84     $this->assertEqual($renderer->renderRoot($element), $expected);
85     $this->assertEqual($renderer->renderRoot($element_copy), $expected);
86
87     $name = '{# inline_template_start #}' . $element['test']['#template'];
88     $prefix = $environment->getTwigCachePrefix();
89
90     $cache = $environment->getCache();
91     $class = $environment->getTemplateClass($name);
92     $expected = $prefix . '_inline-template_' . substr(Crypt::hashBase64($class), 0, TwigPhpStorageCache::SUFFIX_SUBSTRING_LENGTH);
93     $this->assertEqual($expected, $cache->generateKey($name, $class));
94   }
95
96   /**
97    * Tests that exceptions are thrown when a template is not found.
98    */
99   public function testTemplateNotFoundException() {
100     /** @var \Drupal\Core\Template\TwigEnvironment $environment */
101     $environment = \Drupal::service('twig');
102
103     try {
104       $environment->loadTemplate('this-template-does-not-exist.html.twig')->render([]);
105       $this->fail('Did not throw an exception as expected.');
106     }
107     catch (\Twig_Error_Loader $e) {
108       $this->assertTrue(strpos($e->getMessage(), 'Template "this-template-does-not-exist.html.twig" is not defined') === 0);
109     }
110   }
111
112   /**
113    * Ensures that cacheFilename() varies by extensions + deployment identifier.
114    */
115   public function testCacheFilename() {
116     /** @var \Drupal\Core\Template\TwigEnvironment $environment */
117     // Note: Later we refetch the twig service in order to bypass its internal
118     // static cache.
119     $environment = \Drupal::service('twig');
120
121     // A template basename greater than the constant
122     // TwigPhpStorageCache::SUFFIX_SUBSTRING_LENGTH should get truncated.
123     $cache = $environment->getCache();
124     $long_name = 'core/modules/system/templates/block--system-messages-block.html.twig';
125     $this->assertGreaterThan(TwigPhpStorageCache::SUFFIX_SUBSTRING_LENGTH, strlen(basename($long_name)));
126     $class = $environment->getTemplateClass($long_name);
127     $key = $cache->generateKey($long_name, $class);
128     $prefix = $environment->getTwigCachePrefix();
129     // The key should consist of the prefix, an underscore, and two strings
130     // each truncated to length TwigPhpStorageCache::SUFFIX_SUBSTRING_LENGTH
131     // separated by an underscore.
132     $expected = strlen($prefix) + 2 + 2 * TwigPhpStorageCache::SUFFIX_SUBSTRING_LENGTH;
133     $this->assertEquals($expected, strlen($key));
134
135     $original_filename = $environment->getCacheFilename('core/modules/system/templates/container.html.twig');
136     \Drupal::getContainer()->set('twig', NULL);
137
138     \Drupal::service('module_installer')->install(['twig_extension_test']);
139     $environment = \Drupal::service('twig');
140     $new_extension_filename = $environment->getCacheFilename('core/modules/system/templates/container.html.twig');
141     \Drupal::getContainer()->set('twig', NULL);
142
143     $this->assertNotEqual($new_extension_filename, $original_filename);
144   }
145
146 }