class TwigNamespaceTest
Same name and namespace in other branches
- 11.x core/modules/system/tests/src/Kernel/Theme/TwigNamespaceTest.php \Drupal\Tests\system\Kernel\Theme\TwigNamespaceTest
- 10 core/modules/system/tests/src/Kernel/Theme/TwigNamespaceTest.php \Drupal\Tests\system\Kernel\Theme\TwigNamespaceTest
- 8.9.x core/modules/system/tests/src/Kernel/Theme/TwigNamespaceTest.php \Drupal\Tests\system\Kernel\Theme\TwigNamespaceTest
Tests Twig namespaces.
@group Theme
Hierarchy
- class \Drupal\KernelTests\KernelTestBase implements \Drupal\Core\DependencyInjection\ServiceProviderInterface uses \Drupal\KernelTests\AssertLegacyTrait, \Drupal\KernelTests\AssertContentTrait, \Drupal\Tests\RandomGeneratorTrait, \Drupal\Tests\ConfigTestTrait, \Drupal\Tests\ExtensionListTestTrait, \Drupal\Tests\TestRequirementsTrait, \Drupal\Tests\Traits\PhpUnitWarnings, \Drupal\Tests\PhpUnitCompatibilityTrait, \Symfony\Bridge\PhpUnit\ExpectDeprecationTrait extends \PHPUnit\Framework\TestCase
- class \Drupal\Tests\system\Kernel\Theme\TwigNamespaceTest extends \Drupal\KernelTests\KernelTestBase
Expanded class hierarchy of TwigNamespaceTest
File
-
core/
modules/ system/ tests/ src/ Kernel/ Theme/ TwigNamespaceTest.php, line 13
Namespace
Drupal\Tests\system\Kernel\ThemeView source
class TwigNamespaceTest extends KernelTestBase {
/**
* Modules to enable.
*
* @var array
*/
protected static $modules = [
'twig_theme_test',
'twig_namespace_a',
'twig_namespace_b',
'node',
];
/**
* @var \Drupal\Core\Template\TwigEnvironment
*/
protected $twig;
/**
* {@inheritdoc}
*/
protected function setUp() : void {
parent::setUp();
\Drupal::service('theme_installer')->install([
'test_theme',
'olivero',
]);
$this->twig = \Drupal::service('twig');
}
/**
* Checks to see if a value is a twig template.
*
* @internal
*/
public function assertTwigTemplate($value, string $message = '') : void {
$this->assertInstanceOf(TemplateWrapper::class, $value, $message);
}
/**
* Tests template discovery using namespaces.
*/
public function testTemplateDiscovery() {
// Tests resolving namespaced templates in modules.
$this->assertTwigTemplate($this->twig
->load('@node/node.html.twig'), 'Found node.html.twig in node module.');
// Tests resolving namespaced templates in themes.
$this->assertTwigTemplate($this->twig
->load('@olivero/layout/page.html.twig'), 'Found page.html.twig in Olivero theme.');
}
/**
* Tests template extension and includes using namespaces.
*/
public function testTwigNamespaces() {
// Test twig @extends and @include in template files.
$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');
}
}
Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.