diff --git a/core/lib/Drupal/Component/Utility/Twig.php b/core/lib/Drupal/Component/Utility/Twig.php index 9a749ae..42fb23d 100644 --- a/core/lib/Drupal/Component/Utility/Twig.php +++ b/core/lib/Drupal/Component/Utility/Twig.php @@ -34,10 +34,11 @@ public static function isValidVariable($variable) { * The variable to be tested. */ public static function assertValidVariable($variable, $message = '') { - if (empty($message)) { - $message = '"' . Html::escape($variable) . '" is not a valid Twig variable.'; - } - assert(Twig::isValidVariable($variable), $message); + // Asserts passed as a string are not evaluated when assertion checking + // is off. This allows us to skip the expensive preg_match() in + // isValidVariable when not running in dev. Also, we need the fully + // qualified namespace as the string is evaluated in assert's namespace. + assert('\Drupal\Component\Utility\Twig::isValidVariable($variable)', $message ?: '"' . Html::escape($variable) . '" is not a valid Twig variable.'); } } diff --git a/core/modules/views/src/Tests/Plugin/PluginBaseTest.php b/core/modules/views/src/Tests/Plugin/PluginBaseTest.php index 60359e8..cf9534c 100644 --- a/core/modules/views/src/Tests/Plugin/PluginBaseTest.php +++ b/core/modules/views/src/Tests/Plugin/PluginBaseTest.php @@ -93,6 +93,7 @@ public function testViewsInvalidTokens() { \Drupal::service('renderer')->executeInRenderContext(new RenderContext(), function () use ($text, $tokens) { return $this->testPluginBase->viewsTokenReplace($text, $tokens); }); + $this->fail('Failed to assert that "foo-bar" is an illegal Twig variable.'); } catch(\Exception $e) { $this->assertIdentical('"foo-bar" is not a valid Twig variable.', $e->getMessage());