TwigNamespaceTest.php in Drupal 10
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',
'olivero',
]);
$this->twig = \Drupal::service('twig');
}
public function assertTwigTemplate($value, string $message = '') : void {
$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('@olivero/layout/page.html.twig'), 'Found page.html.twig in Olivero 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');
}
}