function TwigIncludeTest::testTemplateInclusion
Same name in other branches
- 9 core/modules/system/tests/src/Kernel/Theme/TwigIncludeTest.php \Drupal\Tests\system\Kernel\Theme\TwigIncludeTest::testTemplateInclusion()
- 10 core/modules/system/tests/src/Kernel/Theme/TwigIncludeTest.php \Drupal\Tests\system\Kernel\Theme\TwigIncludeTest::testTemplateInclusion()
Tests template inclusion extension checking.
See also
\Drupal\Core\Template\Loader\FilesystemLoader::findTemplate()
File
-
core/
modules/ system/ tests/ src/ Kernel/ Theme/ TwigIncludeTest.php, line 35
Class
- TwigIncludeTest
- Tests including files in Twig templates.
Namespace
Drupal\Tests\system\Kernel\ThemeCode
public function testTemplateInclusion() : void {
$this->enableModules([
'system',
]);
/** @var \Drupal\Core\Render\RendererInterface $renderer */
$renderer = \Drupal::service('renderer');
$element['test'] = [
'#type' => 'inline_template',
'#template' => "{% include '@system/container.html.twig' %}",
];
$this->assertSame("<div></div>\n", (string) $renderer->renderRoot($element));
// Test that SQL files cannot be included in Twig templates by default.
$element = [];
$element['test'] = [
'#type' => 'inline_template',
'#template' => "{% include '@__main__/core/tests/fixtures/files/sql-2.sql' %}",
];
try {
$renderer->renderRoot($element);
$this->fail('Expected exception not thrown');
} catch (LoaderError $e) {
$this->assertStringContainsString('Template "@__main__/core/tests/fixtures/files/sql-2.sql" is not defined', $e->getMessage());
}
/** @var \Drupal\Core\Template\Loader\FilesystemLoader $loader */
$loader = \Drupal::service('twig.loader.filesystem');
try {
$loader->getSourceContext('@__main__\\/core/tests/fixtures/files/sql-2.sql');
$this->fail('Expected exception not thrown');
} catch (LoaderError $e) {
$this->assertStringContainsString('Template @__main__\\/core/tests/fixtures/files/sql-2.sql has an invalid file extension (sql). Only templates ending in one of css, html, js, svg, twig are allowed. Set the twig.config.allowed_file_extensions container parameter to customize the allowed file extensions', $e->getMessage());
}
// Allow SQL files to be included.
$twig_config = $this->container
->getParameter('twig.config');
$twig_config['allowed_file_extensions'][] = 'sql';
$this->twigConfig = $twig_config;
// @todo This used to call shutdown() and boot(). rebuildContainer() is
// needed until we stop pushing the request twice and only popping it once.
// @see https://www.drupal.org/i/2613044
$this->container
->get('kernel')
->rebuildContainer();
/** @var \Drupal\Core\Template\Loader\FilesystemLoader $loader */
$loader = \Drupal::service('twig.loader.filesystem');
$source = $loader->getSourceContext('@__main__\\/core/tests/fixtures/files/sql-2.sql');
$this->assertSame(file_get_contents('core/tests/fixtures/files/sql-2.sql'), $source->getCode());
// Test the fallback to the default list of extensions provided by the
// class.
$this->assertSame([
'css',
'html',
'js',
'svg',
'twig',
'sql',
], \Drupal::getContainer()->getParameter('twig.config')['allowed_file_extensions']);
unset($twig_config['allowed_file_extensions']);
$this->twigConfig = $twig_config;
// @todo This used to call shutdown() and boot(). rebuildContainer() is
// needed until we stop pushing the request twice and only popping it once.
// @see https://www.drupal.org/i/2613044
$this->container
->get('kernel')
->rebuildContainer();
$this->assertArrayNotHasKey('allowed_file_extensions', \Drupal::getContainer()->getParameter('twig.config'));
/** @var \Drupal\Core\Template\Loader\FilesystemLoader $loader */
$loader = \Drupal::service('twig.loader.filesystem');
try {
$loader->getSourceContext('@__main__\\/core/tests/fixtures/files/sql-2.sql');
$this->fail('Expected exception not thrown');
} catch (LoaderError $e) {
$this->assertStringContainsString('Template @__main__\\/core/tests/fixtures/files/sql-2.sql has an invalid file extension (sql). Only templates ending in one of css, html, js, svg, twig are allowed. Set the twig.config.allowed_file_extensions container parameter to customize the allowed file extensions', $e->getMessage());
}
// Test a file with no extension.
file_put_contents($this->siteDirectory . '/test_file', 'This is a test!');
/** @var \Drupal\Core\Template\Loader\FilesystemLoader $loader */
$loader = \Drupal::service('twig.loader.filesystem');
try {
$loader->getSourceContext('@__main__\\/' . $this->siteDirectory . '/test_file');
$this->fail('Expected exception not thrown');
} catch (LoaderError $e) {
$this->assertStringContainsString('test_file has an invalid file extension (no file extension). Only templates ending in one of css, html, js, svg, twig are allowed. Set the twig.config.allowed_file_extensions container parameter to customize the allowed file extensions', $e->getMessage());
}
// Allow files with no extension.
$twig_config['allowed_file_extensions'] = [
'twig',
'',
];
$this->twigConfig = $twig_config;
// @todo This used to call shutdown() and boot(). rebuildContainer() is
// needed until we stop pushing the request twice and only popping it once.
// @see https://www.drupal.org/i/2613044
$this->container
->get('kernel')
->rebuildContainer();
/** @var \Drupal\Core\Template\Loader\FilesystemLoader $loader */
$loader = \Drupal::service('twig.loader.filesystem');
$source = $loader->getSourceContext('@__main__\\/' . $this->siteDirectory . '/test_file');
$this->assertSame('This is a test!', $source->getCode());
// Ensure the error message makes sense when no file extension is allowed.
try {
$loader->getSourceContext('@__main__\\/core/tests/fixtures/files/sql-2.sql');
$this->fail('Expected exception not thrown');
} catch (LoaderError $e) {
$this->assertStringContainsString('Template @__main__\\/core/tests/fixtures/files/sql-2.sql has an invalid file extension (sql). Only templates ending in one of twig, or no file extension are allowed. Set the twig.config.allowed_file_extensions container parameter to customize the allowed file extensions', $e->getMessage());
}
}
Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.