You are here

public function TwigEnvironmentTest::testTemplateClassname in Drupal 10

Same name and namespace in other branches
  1. 8 core/tests/Drupal/KernelTests/Core/Theme/TwigEnvironmentTest.php \Drupal\KernelTests\Core\Theme\TwigEnvironmentTest::testTemplateClassname()
  2. 9 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() {

  /** @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);
}