Further Drupal 8.6.4 changes. Some core files were not committed before a commit...
[yaffs-website] / web / core / modules / system / tests / src / Functional / Theme / TwigExtensionTest.php
1 <?php
2
3 namespace Drupal\Tests\system\Functional\Theme;
4
5 use Drupal\Tests\BrowserTestBase;
6
7 /**
8  * Tests Twig extensions.
9  *
10  * @group Theme
11  */
12 class TwigExtensionTest extends BrowserTestBase {
13
14   /**
15    * Modules to enable.
16    *
17    * @var array
18    */
19   public static $modules = ['theme_test', 'twig_extension_test'];
20
21   protected function setUp() {
22     parent::setUp();
23     \Drupal::service('theme_handler')->install(['test_theme']);
24   }
25
26   /**
27    * Tests that the provided Twig extension loads the service appropriately.
28    */
29   public function testTwigExtensionLoaded() {
30     $twigService = \Drupal::service('twig');
31     $ext = $twigService->getExtension('twig_extension_test.test_extension');
32     $this->assertEqual(get_class($ext), 'Drupal\twig_extension_test\TwigExtension\TestExtension', 'TestExtension loaded successfully.');
33   }
34
35   /**
36    * Tests that the Twig extension's filter produces expected output.
37    */
38   public function testTwigExtensionFilter() {
39     $this->config('system.theme')
40       ->set('default', 'test_theme')
41       ->save();
42
43     $this->drupalGet('twig-extension-test/filter');
44     $this->assertText('Every plant is not a mineral.', 'Success: String filtered.');
45     // Test safe_join filter.
46     $this->assertRaw('&lt;em&gt;will be escaped&lt;/em&gt;<br/><em>will be markup</em><br/><strong>will be rendered</strong>');
47   }
48
49   /**
50    * Tests that the Twig extension's function produces expected output.
51    */
52   public function testTwigExtensionFunction() {
53     $this->config('system.theme')
54       ->set('default', 'test_theme')
55       ->save();
56
57     $this->drupalGet('twig-extension-test/function');
58     $this->assertText('THE QUICK BROWN BOX JUMPS OVER THE LAZY DOG 123.', 'Success: Text converted to uppercase.');
59     $this->assertText('the quick brown box jumps over the lazy dog 123.', 'Success: Text converted to lowercase.');
60     $this->assertNoText('The Quick Brown Fox Jumps Over The Lazy Dog 123.', 'Success: No text left behind.');
61   }
62
63   /**
64    * Tests output of integer and double 0 values of TwigExtension::escapeFilter().
65    *
66    * @see https://www.drupal.org/node/2417733
67    */
68   public function testsRenderEscapedZeroValue() {
69     /** @var \Drupal\Core\Template\TwigExtension $extension */
70     $extension = \Drupal::service('twig.extension');
71     /** @var \Drupal\Core\Template\TwigEnvironment $twig */
72     $twig = \Drupal::service('twig');
73     $this->assertIdentical($extension->escapeFilter($twig, 0), 0, 'TwigExtension::escapeFilter() returns zero correctly when provided as an integer.');
74     $this->assertIdentical($extension->escapeFilter($twig, 0.0), 0, 'TwigExtension::escapeFilter() returns zero correctly when provided as a double.');
75   }
76
77   /**
78    * Tests output of integer and double 0 values of TwigExtension->renderVar().
79    *
80    * @see https://www.drupal.org/node/2417733
81    */
82   public function testsRenderZeroValue() {
83     /** @var \Drupal\Core\Template\TwigExtension $extension */
84     $extension = \Drupal::service('twig.extension');
85     $this->assertIdentical($extension->renderVar(0), 0, 'TwigExtension::renderVar() renders zero correctly when provided as an integer.');
86     $this->assertIdentical($extension->renderVar(0.0), 0, 'TwigExtension::renderVar() renders zero correctly when provided as a double.');
87   }
88
89 }