function TwigEnvironmentTest::testTemplateClassname

Same name in other branches
  1. 9 core/tests/Drupal/KernelTests/Core/Theme/TwigEnvironmentTest.php \Drupal\KernelTests\Core\Theme\TwigEnvironmentTest::testTemplateClassname()
  2. 8.9.x core/tests/Drupal/KernelTests/Core/Theme/TwigEnvironmentTest.php \Drupal\KernelTests\Core\Theme\TwigEnvironmentTest::testTemplateClassname()
  3. 10 core/tests/Drupal/KernelTests/Core/Theme/TwigEnvironmentTest.php \Drupal\KernelTests\Core\Theme\TwigEnvironmentTest::testTemplateClassname()

Ensures that templates resolve to the same class name and cache file.

File

core/tests/Drupal/KernelTests/Core/Theme/TwigEnvironmentTest.php, line 119

Class

TwigEnvironmentTest
Tests the twig environment.

Namespace

Drupal\KernelTests\Core\Theme

Code

public function testTemplateClassname() : void {
    
    /** @var \Drupal\Core\Template\TwigEnvironment $environment */
    $environment = \Drupal::service('twig');
    // Test using an include template path.
    $name_include = 'container.html.twig';
    $class_include = $environment->getTemplateClass($name_include);
    $key_include = $environment->getCache()
        ->generateKey($name_include, $class_include);
    // Test using a namespaced template path.
    $name_namespaced = '@system/container.html.twig';
    $class_namespaced = $environment->getTemplateClass($name_namespaced);
    $key_namespaced = $environment->getCache()
        ->generateKey($name_namespaced, $class_namespaced);
    // Test using a direct filesystem template path.
    $name_direct = 'core/modules/system/templates/container.html.twig';
    $class_direct = $environment->getTemplateClass($name_direct);
    $key_direct = $environment->getCache()
        ->generateKey($name_direct, $class_direct);
    // All three should be equal for both cases.
    $this->assertEquals($class_include, $class_namespaced);
    $this->assertEquals($class_namespaced, $class_direct);
    $this->assertEquals($key_include, $key_namespaced);
    $this->assertEquals($key_namespaced, $key_direct);
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.