TwigSettingsTest.php in Drupal 9
File
core/modules/system/tests/src/Functional/Theme/TwigSettingsTest.php
View source
<?php
namespace Drupal\Tests\system\Functional\Theme;
use Drupal\Tests\BrowserTestBase;
use Drupal\Core\PhpStorage\PhpStorageFactory;
class TwigSettingsTest extends BrowserTestBase {
protected static $modules = [
'theme_test',
];
protected $defaultTheme = 'stark';
public function testTwigAutoReloadOverride() {
$parameters = $this->container
->getParameter('twig.config');
$parameters['auto_reload'] = TRUE;
$this
->setContainerParameter('twig.config', $parameters);
$this
->rebuildContainer();
$this
->assertTrue($this->container
->get('twig')
->isAutoReload(), 'Automatic reloading of Twig templates enabled.');
$parameters = $this->container
->getParameter('twig.config');
$parameters['auto_reload'] = FALSE;
$this
->setContainerParameter('twig.config', $parameters);
$this
->rebuildContainer();
$this
->assertFalse($this->container
->get('twig')
->isAutoReload(), 'Automatic reloading of Twig templates disabled.');
}
public function testTwigDebugOverride() {
$parameters = $this->container
->getParameter('twig.config');
$parameters['debug'] = TRUE;
$this
->setContainerParameter('twig.config', $parameters);
$this
->rebuildContainer();
$this
->assertTrue($this->container
->get('twig')
->isDebug(), 'Twig debug enabled.');
$this
->assertTrue($this->container
->get('twig')
->isAutoReload(), 'Twig automatic reloading is enabled when debug is enabled.');
$parameters = $this->container
->getParameter('twig.config');
$parameters['auto_reload'] = FALSE;
$this
->setContainerParameter('twig.config', $parameters);
$this
->rebuildContainer();
$this
->assertFalse($this->container
->get('twig')
->isAutoReload(), 'Twig automatic reloading can be disabled when debug is enabled.');
$parameters = $this->container
->getParameter('twig.config');
$parameters['debug'] = FALSE;
$this
->setContainerParameter('twig.config', $parameters);
$this
->rebuildContainer();
$this
->assertFalse($this->container
->get('twig')
->isDebug(), 'Twig debug disabled.');
}
public function testTwigCacheOverride() {
$extension = twig_extension();
$theme_installer = $this->container
->get('theme_installer');
$theme_installer
->install([
'test_theme',
]);
$this
->config('system.theme')
->set('default', 'test_theme')
->save();
\Drupal::theme()
->setActiveTheme(\Drupal::service('theme.initialization')
->getActiveThemeByName('test_theme'));
$this->container
->set('theme.registry', NULL);
$registry = $this->container
->get('theme.registry');
$registry
->reset();
$templates = $registry
->getRuntime();
$info = $templates
->get('theme_test_template_test');
$template_filename = $info['path'] . '/' . $info['template'] . $extension;
$environment = $this->container
->get('twig');
$cache = $environment
->getCache();
$class = $environment
->getTemplateClass($template_filename);
$cache_filename = $cache
->generateKey($template_filename, $class);
$this
->drupalGet('theme-test/template-test');
$this
->assertTrue(PhpStorageFactory::get('twig')
->exists($cache_filename), 'Cached Twig template found.');
$parameters = $this->container
->getParameter('twig.config');
$parameters['cache'] = FALSE;
$this
->setContainerParameter('twig.config', $parameters);
$this
->rebuildContainer();
$this
->assertFalse($this->container
->get('twig')
->getCache(), 'Twig environment has caching disabled.');
}
public function testTwigInlineWithAutoReload() {
$parameters = $this->container
->getParameter('twig.config');
$parameters['auto_reload'] = TRUE;
$parameters['debug'] = TRUE;
$this
->setContainerParameter('twig.config', $parameters);
$this
->rebuildContainer();
$this
->drupalGet('theme-test/inline-template-test');
$this
->assertSession()
->statusCodeEquals(200);
$this
->drupalGet('theme-test/inline-template-test');
$this
->assertSession()
->statusCodeEquals(200);
}
}