You are here

function TwigSettingsTest::testTwigCacheOverride in Zircon Profile 8

Same name and namespace in other branches
  1. 8.0 core/modules/system/src/Tests/Theme/TwigSettingsTest.php \Drupal\system\Tests\Theme\TwigSettingsTest::testTwigCacheOverride()

Ensures Twig template cache setting can be overridden.

File

core/modules/system/src/Tests/Theme/TwigSettingsTest.php, line 82
Contains \Drupal\system\Tests\Theme\TwigSettingsTest.

Class

TwigSettingsTest
Tests overriding Twig engine settings via settings.php.

Namespace

Drupal\system\Tests\Theme

Code

function testTwigCacheOverride() {
  $extension = twig_extension();
  $theme_handler = $this->container
    ->get('theme_handler');
  $theme_handler
    ->install(array(
    'test_theme',
  ));
  $theme_handler
    ->setDefault('test_theme');

  // The registry still works on theme globals, so set them here.
  \Drupal::theme()
    ->setActiveTheme(\Drupal::service('theme.initialization')
    ->getActiveThemeByName('test_theme'));

  // Reset the theme registry, so that the new theme is used.
  $this->container
    ->set('theme.registry', NULL);

  // Load array of Twig templates.
  // reset() is necessary to invalidate caches tagged with 'theme_registry'.
  $registry = $this->container
    ->get('theme.registry');
  $registry
    ->reset();
  $templates = $registry
    ->getRuntime();

  // Get the template filename and the cache filename for
  // theme_test.template_test.html.twig.
  $info = $templates
    ->get('theme_test_template_test');
  $template_filename = $info['path'] . '/' . $info['template'] . $extension;
  $cache_filename = $this->container
    ->get('twig')
    ->getCacheFilename($template_filename);

  // Navigate to the page and make sure the template gets cached.
  $this
    ->drupalGet('theme-test/template-test');
  $this
    ->assertTrue(PhpStorageFactory::get('twig')
    ->exists($cache_filename), 'Cached Twig template found.');

  // Disable the Twig cache and rebuild the service container.
  $parameters = $this->container
    ->getParameter('twig.config');
  $parameters['cache'] = FALSE;
  $this
    ->setContainerParameter('twig.config', $parameters);
  $this
    ->rebuildContainer();

  // This should return false after rebuilding the service container.
  $this
    ->assertFalse($this->container
    ->get('twig')
    ->getCache(), 'Twig environment has caching disabled.');
}