3 namespace Drupal\system\Tests\Theme;
5 use Drupal\simpletest\WebTestBase;
8 * Tests Twig namespaces.
12 class TwigNamespaceTest extends WebTestBase {
19 public static $modules = ['twig_theme_test', 'twig_namespace_a', 'twig_namespace_b', 'node'];
22 * @var \Drupal\Core\Template\TwigEnvironment
26 protected function setUp() {
28 \Drupal::service('theme_handler')->install(['test_theme', 'bartik']);
29 $this->twig = \Drupal::service('twig');
33 * Checks to see if a value is a twig template.
35 public function assertTwigTemplate($value, $message = '', $group = 'Other') {
36 $this->assertTrue($value instanceof \Twig_Template, $message, $group);
40 * Tests template discovery using namespaces.
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.');
46 // Tests resolving namespaced templates in themes.
47 $this->assertTwigTemplate($this->twig->resolveTemplate('@bartik/page.html.twig'), 'Found page.html.twig in Bartik theme.');
51 * Tests template extension and includes using namespaces.
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));
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');