comparison core/modules/system/src/Tests/Theme/EngineTwigTest.php @ 0:4c8ae668cc8c

Initial import (non-working)
author Chris Cannam
date Wed, 29 Nov 2017 16:09:58 +0000
parents
children 7a779792577d
comparison
equal deleted inserted replaced
-1:000000000000 0:4c8ae668cc8c
1 <?php
2
3 namespace Drupal\system\Tests\Theme;
4
5 use Drupal\Core\Render\Markup;
6 use Drupal\Core\Url;
7 use Drupal\simpletest\WebTestBase;
8
9 /**
10 * Tests Twig-specific theme functionality.
11 *
12 * @group Theme
13 */
14 class EngineTwigTest extends WebTestBase {
15
16 /**
17 * Modules to enable.
18 *
19 * @var array
20 */
21 public static $modules = ['theme_test', 'twig_theme_test'];
22
23 protected function setUp() {
24 parent::setUp();
25 \Drupal::service('theme_handler')->install(['test_theme']);
26 }
27
28 /**
29 * Tests that the Twig engine handles PHP data correctly.
30 */
31 public function testTwigVariableDataTypes() {
32 $this->config('system.theme')
33 ->set('default', 'test_theme')
34 ->save();
35 $this->drupalGet('twig-theme-test/php-variables');
36 foreach (_test_theme_twig_php_values() as $type => $value) {
37 $this->assertRaw('<li>' . $type . ': ' . $value['expected'] . '</li>');
38 }
39 }
40
41 /**
42 * Tests the url and url_generate Twig functions.
43 */
44 public function testTwigUrlGenerator() {
45 $this->drupalGet('twig-theme-test/url-generator');
46 // Find the absolute URL of the current site.
47 $url_generator = $this->container->get('url_generator');
48 $expected = [
49 'path (as route) not absolute: ' . $url_generator->generateFromRoute('user.register'),
50 'url (as route) absolute: ' . $url_generator->generateFromRoute('user.register', [], ['absolute' => TRUE]),
51 'path (as route) not absolute with fragment: ' . $url_generator->generateFromRoute('user.register', [], ['fragment' => 'bottom']),
52 'url (as route) absolute despite option: ' . $url_generator->generateFromRoute('user.register', [], ['absolute' => TRUE]),
53 'url (as route) absolute with fragment: ' . $url_generator->generateFromRoute('user.register', [], ['absolute' => TRUE, 'fragment' => 'bottom']),
54 ];
55
56 // Verify that url() has the ability to bubble cacheability metadata:
57 // absolute URLs should bubble the 'url.site' cache context. (This only
58 // needs to test that cacheability metadata is bubbled *at all*; detailed
59 // tests for *which* cacheability metadata is bubbled live elsewhere.)
60 $this->assertCacheContext('url.site');
61
62 // Make sure we got something.
63 $content = $this->getRawContent();
64 $this->assertFalse(empty($content), 'Page content is not empty');
65 foreach ($expected as $string) {
66 $this->assertRaw('<div>' . $string . '</div>');
67 }
68 }
69
70 /**
71 * Tests the link_generator Twig functions.
72 */
73 public function testTwigLinkGenerator() {
74 $this->drupalGet('twig-theme-test/link-generator');
75
76 /** @var \Drupal\Core\Utility\LinkGenerator $link_generator */
77 $link_generator = $this->container->get('link_generator');
78
79
80 $generated_url = Url::fromRoute('user.register', [], ['absolute' => TRUE])->toString(TRUE)->getGeneratedUrl();
81 $expected = [
82 'link via the linkgenerator: ' . $link_generator->generate('register', new Url('user.register', [], ['absolute' => TRUE])),
83 'link via the linkgenerator: ' . $link_generator->generate('register', new Url('user.register', [], ['absolute' => TRUE, 'attributes' => ['foo' => 'bar']])),
84 'link via the linkgenerator: ' . $link_generator->generate('register', new Url('user.register', [], ['attributes' => ['foo' => 'bar', 'id' => 'kitten']])),
85 'link via the linkgenerator: ' . $link_generator->generate('register', new Url('user.register', [], ['attributes' => ['id' => 'kitten']])),
86 'link via the linkgenerator: ' . $link_generator->generate('register', new Url('user.register', [], ['attributes' => ['class' => ['llama', 'kitten', 'panda']]])),
87 'link via the linkgenerator: ' . $link_generator->generate(Markup::create('<span>register</span>'), new Url('user.register', [], ['absolute' => TRUE])),
88 'link via the linkgenerator: <a href="' . $generated_url . '"><span>register</span><svg></svg></a>',
89 'link via the linkgenerator: ' . $link_generator->generate('register', new Url('user.register', [], ['attributes' => ['foo' => 'bar']])) . ' ' . $link_generator->generate('register', new Url('user.register', [], ['attributes' => ['foo' => 'bar']])),
90 ];
91
92 // Verify that link() has the ability to bubble cacheability metadata:
93 // absolute URLs should bubble the 'url.site' cache context. (This only
94 // needs to test that cacheability metadata is bubbled *at all*; detailed
95 // tests for *which* cacheability metadata is bubbled live elsewhere.)
96 $this->assertCacheContext('url.site');
97
98 $content = $this->getRawContent();
99 $this->assertFalse(empty($content), 'Page content is not empty');
100 foreach ($expected as $string) {
101 $this->assertRaw('<div>' . $string . '</div>');
102 }
103 }
104
105 /**
106 * Tests the magic url to string Twig functions.
107 *
108 * @see \Drupal\Core\Url
109 */
110 public function testTwigUrlToString() {
111 $this->drupalGet('twig-theme-test/url-to-string');
112
113 $expected = [
114 'rendered url: ' . Url::fromRoute('user.register')->toString(),
115 ];
116
117 $content = $this->getRawContent();
118 $this->assertFalse(empty($content), 'Page content is not empty');
119 foreach ($expected as $string) {
120 $this->assertRaw('<div>' . $string . '</div>');
121 }
122 }
123
124 /**
125 * Tests the automatic/magic calling of toString() on objects, if exists.
126 */
127 public function testTwigFileUrls() {
128 $this->drupalGet('/twig-theme-test/file-url');
129 $filepath = file_url_transform_relative(file_create_url('core/modules/system/tests/modules/twig_theme_test/twig_theme_test.js'));
130 $this->assertRaw('<div>file_url: ' . $filepath . '</div>');
131 }
132
133 /**
134 * Tests the attach of asset libraries.
135 */
136 public function testTwigAttachLibrary() {
137 $this->drupalGet('/twig-theme-test/attach-library');
138 $this->assertRaw('ckeditor.js');
139 }
140
141 /**
142 * Tests the rendering of renderables.
143 */
144 public function testRenderable() {
145 $this->drupalGet('/twig-theme-test/renderable');
146 $this->assertRaw('<div>Example markup</div>');
147 }
148
149 }