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

Initial import (non-working)
author Chris Cannam
date Wed, 29 Nov 2017 16:09:58 +0000
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 0:4c8ae668cc8c
1 <?php
2
3 namespace Drupal\system\Tests\Theme;
4
5 use Drupal\simpletest\WebTestBase;
6
7 /**
8 * Tests Twig namespaces.
9 *
10 * @group Theme
11 */
12 class TwigNamespaceTest extends WebTestBase {
13
14 /**
15 * Modules to enable.
16 *
17 * @var array
18 */
19 public static $modules = ['twig_theme_test', 'twig_namespace_a', 'twig_namespace_b', 'node'];
20
21 /**
22 * @var \Drupal\Core\Template\TwigEnvironment
23 */
24 protected $twig;
25
26 protected function setUp() {
27 parent::setUp();
28 \Drupal::service('theme_handler')->install(['test_theme', 'bartik']);
29 $this->twig = \Drupal::service('twig');
30 }
31
32 /**
33 * Checks to see if a value is a twig template.
34 */
35 public function assertTwigTemplate($value, $message = '', $group = 'Other') {
36 $this->assertTrue($value instanceof \Twig_Template, $message, $group);
37 }
38
39 /**
40 * Tests template discovery using namespaces.
41 */
42 public function testTemplateDiscovery() {
43 // Tests resolving namespaced templates in modules.
44 $this->assertTwigTemplate($this->twig->resolveTemplate('@node/node.html.twig'), 'Found node.html.twig in node module.');
45
46 // Tests resolving namespaced templates in themes.
47 $this->assertTwigTemplate($this->twig->resolveTemplate('@bartik/page.html.twig'), 'Found page.html.twig in Bartik theme.');
48 }
49
50 /**
51 * Tests template extension and includes using namespaces.
52 */
53 public function testTwigNamespaces() {
54 // Test twig @extends and @include in template files.
55 $test = ['#theme' => 'twig_namespace_test'];
56 $this->setRawContent(\Drupal::service('renderer')->renderRoot($test));
57
58 $this->assertText('This line is from twig_namespace_a/templates/test.html.twig');
59 $this->assertText('This line is from twig_namespace_b/templates/test.html.twig');
60 }
61
62 }