function TwigEnvironmentTest::testCacheFilename

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

Ensures that cacheFilename() varies by extensions + deployment identifier.

File

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

Class

TwigEnvironmentTest
Tests the twig environment.

Namespace

Drupal\KernelTests\Core\Theme

Code

public function testCacheFilename() {
    
    /** @var \Drupal\Core\Template\TwigEnvironment $environment */
    // Note: Later we refetch the twig service in order to bypass its internal
    // static cache.
    $environment = \Drupal::service('twig');
    $template_path = 'core/modules/system/templates/container.html.twig';
    // A template basename greater than the constant
    // TwigPhpStorageCache::SUFFIX_SUBSTRING_LENGTH should get truncated.
    $cache = $environment->getCache();
    $long_name = 'core/modules/system/templates/block--system-messages-block.html.twig';
    $this->assertGreaterThan(TwigPhpStorageCache::SUFFIX_SUBSTRING_LENGTH, strlen(basename($long_name)));
    $class = $environment->getTemplateClass($long_name);
    $key = $cache->generateKey($long_name, $class);
    $prefix = $environment->getTwigCachePrefix();
    // The key should consist of the prefix, an underscore, and two strings
    // each truncated to length TwigPhpStorageCache::SUFFIX_SUBSTRING_LENGTH
    // separated by an underscore.
    $expected = strlen($prefix) + 2 + 2 * TwigPhpStorageCache::SUFFIX_SUBSTRING_LENGTH;
    $this->assertEquals($expected, strlen($key));
    $cache = $environment->getCache();
    $class = $environment->getTemplateClass($template_path);
    $original_filename = $cache->generateKey($template_path, $class);
    \Drupal::service('module_installer')->install([
        'twig_extension_test',
    ]);
    $environment = \Drupal::service('twig');
    $cache = $environment->getCache();
    $class = $environment->getTemplateClass($template_path);
    $new_extension_filename = $cache->generateKey($template_path, $class);
    \Drupal::getContainer()->set('twig', NULL);
    $this->assertNotEqual($new_extension_filename, $original_filename);
}

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