TwigNamespaceTest.php in Drupal 9
File
core/modules/system/tests/src/Kernel/Theme/TwigNamespaceTest.php
View source
<?php
namespace Drupal\Tests\system\Kernel\Theme;
use Drupal\KernelTests\KernelTestBase;
use Twig\TemplateWrapper;
class TwigNamespaceTest extends KernelTestBase {
protected static $modules = [
'twig_theme_test',
'twig_namespace_a',
'twig_namespace_b',
'node',
];
protected $twig;
protected function setUp() : void {
parent::setUp();
\Drupal::service('theme_installer')
->install([
'test_theme',
'bartik',
]);
$this->twig = \Drupal::service('twig');
}
public function assertTwigTemplate($value, $message = '') {
$this
->assertInstanceOf(TemplateWrapper::class, $value, $message);
}
public function testTemplateDiscovery() {
$this
->assertTwigTemplate($this->twig
->load('@node/node.html.twig'), 'Found node.html.twig in node module.');
$this
->assertTwigTemplate($this->twig
->load('@bartik/page.html.twig'), 'Found page.html.twig in Bartik theme.');
}
public function testTwigNamespaces() {
$test = [
'#theme' => 'twig_namespace_test',
];
$this
->setRawContent(\Drupal::service('renderer')
->renderRoot($test));
$this
->assertText('This line is from twig_namespace_a/templates/test.html.twig');
$this
->assertText('This line is from twig_namespace_b/templates/test.html.twig');
}
}