class TwigDeprecationsTest
Same name and namespace in other branches
- 11.x core/tests/Drupal/KernelTests/Core/Theme/TwigDeprecationsTest.php \Drupal\KernelTests\Core\Theme\TwigDeprecationsTest
- 10 core/tests/Drupal/KernelTests/Core/Theme/TwigDeprecationsTest.php \Drupal\KernelTests\Core\Theme\TwigDeprecationsTest
Tests deprecating variables passed to twig templates.
Attributes
#[Group('Twig')]
#[IgnoreDeprecations]
#[RunTestsInSeparateProcesses]
Hierarchy
- class \Drupal\KernelTests\KernelTestBase implements \Drupal\Core\DependencyInjection\ServiceProviderInterface uses \Drupal\Tests\DrupalTestCaseTrait, \Drupal\KernelTests\AssertContentTrait, \Drupal\Tests\RandomGeneratorTrait, \Drupal\Tests\ConfigTestTrait, \Drupal\Tests\ExtensionListTestTrait, \Drupal\Tests\TestRequirementsTrait, \Drupal\Tests\PhpUnitCompatibilityTrait, \Prophecy\PhpUnit\ProphecyTrait, \Drupal\TestTools\Extension\DeprecationBridge\ExpectDeprecationTrait extends \PHPUnit\Framework\TestCase
- class \Drupal\KernelTests\Core\Theme\TwigDeprecationsTest extends \Drupal\KernelTests\KernelTestBase
Expanded class hierarchy of TwigDeprecationsTest
See also
\Drupal\Core\Template\TwigExtension::checkDeprecations()
\Drupal\Core\Template\TwigNodeVisitorCheckDeprecations
\Drupal\Core\Template\TwigNodeCheckDeprecations
File
-
core/
tests/ Drupal/ KernelTests/ Core/ Theme/ TwigDeprecationsTest.php, line 19
Namespace
Drupal\KernelTests\Core\ThemeView source
class TwigDeprecationsTest extends KernelTestBase {
/**
* {@inheritdoc}
*/
protected static $modules = [
'system',
'theme_test',
];
/**
* Test deprecating variables at definition in hook_theme().
*/
public function testHookThemeDeprecations() : void {
$element = [
'#theme' => 'theme_test_deprecations_hook_theme',
'#foo' => 'foo',
'#bar' => 'bar',
];
// Both 'foo' and 'bar' are deprecated in theme_test_hook_theme(),
// but 'bar' is not used in theme-test-deprecations-hook-theme.html.twig.
$this->expectDeprecation($this->getDeprecationMessage('foo'));
$this->assertEquals('foo', $this->container
->get('renderer')
->renderRoot($element));
}
/**
* Test theme_test_deprecations_preprocess renders without deprecations.
*/
public function testThemeTestDeprecations() : void {
$this->assertRendered('foo|set_var|bar', []);
}
/**
* Test deprecation of undefined variable triggers no error.
*/
public function testUndefinedDeprecation() : void {
$preprocess = [
'deprecations' => [
'undefined' => $this->getDeprecationMessage('undefined'),
],
];
$this->assertRendered('foo|set_var|bar', $preprocess);
}
/**
* Test deprecation of single variable triggers error.
*/
public function testSingleDeprecation() : void {
$preprocess = [
'deprecations' => [
'foo' => $this->getDeprecationMessage('foo'),
],
];
$this->expectDeprecation($this->getDeprecationMessage('foo'));
$this->assertRendered('foo|set_var|bar', $preprocess);
}
/**
* Test deprecation of empty variable triggers error.
*/
public function testEmptyDeprecation() : void {
$preprocess = [
'foo' => '',
'deprecations' => [
'foo' => $this->getDeprecationMessage('foo'),
],
];
$this->expectDeprecation($this->getDeprecationMessage('foo'));
$this->assertRendered('|set_var|bar', $preprocess);
}
/**
* Test deprecation of multiple variables triggers errors.
*/
public function testMultipleDeprecations() : void {
$preprocess = [
'deprecations' => [
'foo' => $this->getDeprecationMessage('foo'),
'bar' => $this->getDeprecationMessage('bar'),
],
];
$this->expectDeprecation($this->getDeprecationMessage('foo'));
$this->expectDeprecation($this->getDeprecationMessage('bar'));
$this->assertRendered('foo|set_var|bar', $preprocess);
}
/**
* Test deprecation of variables assigned inside template triggers no error.
*/
public function testAssignedVariableDeprecation() : void {
$preprocess = [
'contents' => [
'content',
],
'deprecations' => [
'set_var' => $this->getDeprecationMessage('set_var'),
'for_var' => $this->getDeprecationMessage('for_var'),
],
];
$this->assertRendered('foo|set_var|content|bar', $preprocess);
}
/**
* Test deprecation of variables in parent does not leak to child.
*/
public function testParentVariableDeprecation() : void {
// 'foo' is used before the child template is processed, so this test
// shows that processing the child does not lead to parent usage being
// forgotten.
// 'gaz' is used in the child template but only deprecated in the parent
// template, so no deprecation error is triggered for it.
$preprocess = [
'contents' => [
'child' => [
'#theme' => 'theme_test_deprecations_child',
'#foo' => 'foo-child',
'#bar' => 'bar-child',
'#gaz' => 'gaz-child',
],
],
'deprecations' => [
'foo' => $this->getDeprecationMessage('foo'),
'gaz' => $this->getDeprecationMessage('gaz'),
],
];
$this->assertRendered('foo|set_var|foo-child|gaz-child|bar', $preprocess);
}
/**
* Assert that 'theme_test_deprecations_preprocess' renders expected text.
*
* @param string $expected
* The expected text.
* @param array $preprocess
* An array to merge in theme_test_deprecations_preprocess_preprocess().
*/
protected function assertRendered($expected, array $preprocess) : void {
\Drupal::state()->set('theme_test.theme_test_deprecations_preprocess', $preprocess);
$element = [
'#theme' => 'theme_test_deprecations_preprocess',
'#foo' => 'foo',
'#bar' => 'bar',
'#gaz' => 'gaz',
'#set_var' => 'overridden',
];
$this->assertEquals($expected, $this->container
->get('renderer')
->renderRoot($element));
}
/**
* Get an example deprecation message for a named variable.
*/
protected function getDeprecationMessage($variable) : string {
return "'{$variable}' is deprecated in drupal:X.0.0 and is removed from drupal:Y.0.0. Use 'new_{$variable}' instead. See https://www.example.com.";
}
}
Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.