diff --git a/.gitignore b/.gitignore index 00d98c5b..8cacd4d2 100644 --- a/.gitignore +++ b/.gitignore @@ -11,3 +11,4 @@ utilies/*.php phpunit* vendor/* composer.lock +/composer.phar diff --git a/CHANGELOG.md b/CHANGELOG.md index 4c4e93e8..c90e2b36 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ## [Unreleased] ### Changed +- default modifier uses empty instead of @ error suppression modifier when testing a variable https://github.com/smarty-php/smarty/issues/336 - modifier escape now triggers a E_USER_NOTICE when an unsupported escape type is used https://github.com/smarty-php/smarty/pull/649 ## [3.1.39] - 2021-02-17 diff --git a/libs/plugins/modifiercompiler.default.php b/libs/plugins/modifiercompiler.default.php index ae886c4b..c4bd5dd4 100644 --- a/libs/plugins/modifiercompiler.default.php +++ b/libs/plugins/modifiercompiler.default.php @@ -18,7 +18,7 @@ * * @return string with compiled code */ -function smarty_modifiercompiler_default($params) +function smarty_modifiercompiler_default($params, Smarty_Internal_TemplateCompilerBase $compiler) { $output = $params[ 0 ]; if (!isset($params[ 1 ])) { @@ -26,7 +26,13 @@ function smarty_modifiercompiler_default($params) } array_shift($params); foreach ($params as $param) { - $output = '(($tmp = @' . $output . ')===null||$tmp===\'\' ? ' . $param . ' : $tmp)'; + + if ($compiler->syntaxMatchesVariable($output)) { + $output = '(empty(' . $output . ') ? ' . $param . ' : ' . $output . ')'; + } else { + $output = '(($tmp = ' . $output . ')===null||$tmp===\'\' ? ' . $param . ' : $tmp)'; + } + } return $output; } diff --git a/libs/sysplugins/smarty_internal_templatecompilerbase.php b/libs/sysplugins/smarty_internal_templatecompilerbase.php index 3cc957de..c147fa55 100644 --- a/libs/sysplugins/smarty_internal_templatecompilerbase.php +++ b/libs/sysplugins/smarty_internal_templatecompilerbase.php @@ -674,7 +674,7 @@ abstract class Smarty_Internal_TemplateCompilerBase * @param $string * @return bool */ - private function syntaxMatchesVariable($string) { + public function syntaxMatchesVariable($string) { static $regex_pattern = '/^\$[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*((->)[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*|\[.*]*\])*$/'; return 1 === preg_match($regex_pattern, trim($string)); } diff --git a/tests/UnitTests/A_Core/MuteExpectedErrors/MuteExpectedErrorsTest.php b/tests/UnitTests/A_Core/MuteExpectedErrors/MuteExpectedErrorsTest.php index 6fbb53c1..1d8935f3 100644 --- a/tests/UnitTests/A_Core/MuteExpectedErrors/MuteExpectedErrorsTest.php +++ b/tests/UnitTests/A_Core/MuteExpectedErrors/MuteExpectedErrorsTest.php @@ -64,10 +64,10 @@ class MuteExpectedErrorsTest extends PHPUnit_Smarty $this->smarty->clearCompiledTemplate('default.tpl'); $this->smarty->fetch('default.tpl'); - $this->assertEquals(Smarty::$_IS_WINDOWS ? 2 : 2, count($this->_errors)); + $this->assertEquals(0, count($this->_errors)); @filemtime('ckxladanwijicajscaslyxck'); - $this->assertEquals(Smarty::$_IS_WINDOWS ? 3 : 3, count($this->_errors)); + $this->assertEquals(1, count($this->_errors)); restore_error_handler(); } @@ -112,11 +112,11 @@ class MuteExpectedErrorsTest extends PHPUnit_Smarty $this->smarty->clearCompiledTemplate('default.tpl'); $this->smarty->fetch('default.tpl'); - $this->assertEquals(Smarty::$_IS_WINDOWS ? 2 : 2, count($this->_errors)); + $this->assertEquals(0, count($this->_errors)); @filemtime('ckxladanwijicajscaslyxck'); $error = array(__FILE__ . ' line ' . (__LINE__ - 1)); - $this->assertEquals(Smarty::$_IS_WINDOWS ? 3 : 3, count($this->_errors)); + $this->assertEquals(1, count($this->_errors)); restore_error_handler(); } diff --git a/tests/UnitTests/ResourceTests/File/FileResourceTest.php b/tests/UnitTests/ResourceTests/File/FileResourceTest.php index 1a1d615a..150ad22d 100644 --- a/tests/UnitTests/ResourceTests/File/FileResourceTest.php +++ b/tests/UnitTests/ResourceTests/File/FileResourceTest.php @@ -110,6 +110,9 @@ class FileResourceTest extends PHPUnit_Smarty ); } + /** + * @doesNotPerformAssertions + */ public function testGetCompiledTimestampPrepare() { $tpl = $this->smarty->createTemplate('helloworld.tpl'); @@ -142,6 +145,9 @@ class FileResourceTest extends PHPUnit_Smarty $this->assertTrue($tpl->mustCompile()); } + /** + * @doesNotPerformAssertions + */ public function testMustCompileTouchedSourcePrepare() { // touch to prepare next test diff --git a/tests/UnitTests/ResourceTests/Php/PhpResourceTest.php b/tests/UnitTests/ResourceTests/Php/PhpResourceTest.php index dd4f20f0..f2d240ae 100644 --- a/tests/UnitTests/ResourceTests/Php/PhpResourceTest.php +++ b/tests/UnitTests/ResourceTests/Php/PhpResourceTest.php @@ -161,7 +161,7 @@ class PhpResourceTest extends PHPUnit_Smarty * * @runInSeparateProcess * @preserveGlobalState disabled - * + * @doesNotPerformAssertions */ public function testIsCachedTouchedSourcePrepare() { @@ -212,6 +212,7 @@ class PhpResourceTest extends PHPUnit_Smarty /** * test $smarty->is_cached + * @doesNotPerformAssertions */ public function testSmartyIsCachedPrepare() { diff --git a/tests/UnitTests/TemplateSource/Comments/templates_tmp/testComments_T1.tpl b/tests/UnitTests/TemplateSource/Comments/templates_tmp/testComments_T1.tpl new file mode 100644 index 00000000..409f3a0d --- /dev/null +++ b/tests/UnitTests/TemplateSource/Comments/templates_tmp/testComments_T1.tpl @@ -0,0 +1 @@ +{* this is a comment *} \ No newline at end of file diff --git a/tests/UnitTests/TemplateSource/Comments/templates_tmp/testComments_T10.tpl b/tests/UnitTests/TemplateSource/Comments/templates_tmp/testComments_T10.tpl new file mode 100644 index 00000000..83971092 --- /dev/null +++ b/tests/UnitTests/TemplateSource/Comments/templates_tmp/testComments_T10.tpl @@ -0,0 +1,7 @@ += +{* comment *} +{* comment *} + b +{* comment *} +{* comment *} += \ No newline at end of file diff --git a/tests/UnitTests/TemplateSource/Comments/templates_tmp/testComments_T11.tpl b/tests/UnitTests/TemplateSource/Comments/templates_tmp/testComments_T11.tpl new file mode 100644 index 00000000..dc2d807e --- /dev/null +++ b/tests/UnitTests/TemplateSource/Comments/templates_tmp/testComments_T11.tpl @@ -0,0 +1,7 @@ += +a +{* comment 1 *} +{* comment 2 *} +{* comment 3 *} +b += \ No newline at end of file diff --git a/tests/UnitTests/TemplateSource/Comments/templates_tmp/testComments_T12.tpl b/tests/UnitTests/TemplateSource/Comments/templates_tmp/testComments_T12.tpl new file mode 100644 index 00000000..7ede3c23 --- /dev/null +++ b/tests/UnitTests/TemplateSource/Comments/templates_tmp/testComments_T12.tpl @@ -0,0 +1,7 @@ += +a +{* comment 1 *} + {* comment 2 *} +{* comment 3 *} +b += \ No newline at end of file diff --git a/tests/UnitTests/TemplateSource/Comments/templates_tmp/testComments_T13.tpl b/tests/UnitTests/TemplateSource/Comments/templates_tmp/testComments_T13.tpl new file mode 100644 index 00000000..9e84515d --- /dev/null +++ b/tests/UnitTests/TemplateSource/Comments/templates_tmp/testComments_T13.tpl @@ -0,0 +1,7 @@ += +a +{* comment 1 *} +{* comment 2 *} +{* comment 3 *} +b += \ No newline at end of file diff --git a/tests/UnitTests/TemplateSource/Comments/templates_tmp/testComments_T14.tpl b/tests/UnitTests/TemplateSource/Comments/templates_tmp/testComments_T14.tpl new file mode 100644 index 00000000..9543366f --- /dev/null +++ b/tests/UnitTests/TemplateSource/Comments/templates_tmp/testComments_T14.tpl @@ -0,0 +1,7 @@ += +a +{* comment 1 *} + {* comment 2 *} +{* comment 3 *} +b += \ No newline at end of file diff --git a/tests/UnitTests/TemplateSource/Comments/templates_tmp/testComments_T2.tpl b/tests/UnitTests/TemplateSource/Comments/templates_tmp/testComments_T2.tpl new file mode 100644 index 00000000..5cd1d36b --- /dev/null +++ b/tests/UnitTests/TemplateSource/Comments/templates_tmp/testComments_T2.tpl @@ -0,0 +1 @@ +{* another $foo comment *} \ No newline at end of file diff --git a/tests/UnitTests/TemplateSource/Comments/templates_tmp/testComments_T3.tpl b/tests/UnitTests/TemplateSource/Comments/templates_tmp/testComments_T3.tpl new file mode 100644 index 00000000..3e0860e3 --- /dev/null +++ b/tests/UnitTests/TemplateSource/Comments/templates_tmp/testComments_T3.tpl @@ -0,0 +1 @@ +{* another comment *}some in between{* another comment *} \ No newline at end of file diff --git a/tests/UnitTests/TemplateSource/Comments/templates_tmp/testComments_T4.tpl b/tests/UnitTests/TemplateSource/Comments/templates_tmp/testComments_T4.tpl new file mode 100644 index 00000000..d40a5e17 --- /dev/null +++ b/tests/UnitTests/TemplateSource/Comments/templates_tmp/testComments_T4.tpl @@ -0,0 +1,2 @@ +{* multi line + comment *} \ No newline at end of file diff --git a/tests/UnitTests/TemplateSource/Comments/templates_tmp/testComments_T5.tpl b/tests/UnitTests/TemplateSource/Comments/templates_tmp/testComments_T5.tpl new file mode 100644 index 00000000..13eeeec9 --- /dev/null +++ b/tests/UnitTests/TemplateSource/Comments/templates_tmp/testComments_T5.tpl @@ -0,0 +1 @@ +{* /* foo * / *} \ No newline at end of file diff --git a/tests/UnitTests/TemplateSource/Comments/templates_tmp/testComments_T6.tpl b/tests/UnitTests/TemplateSource/Comments/templates_tmp/testComments_T6.tpl new file mode 100644 index 00000000..cb1aefd9 --- /dev/null +++ b/tests/UnitTests/TemplateSource/Comments/templates_tmp/testComments_T6.tpl @@ -0,0 +1,2 @@ +A{* comment *}B +C \ No newline at end of file diff --git a/tests/UnitTests/TemplateSource/Comments/templates_tmp/testComments_T7.tpl b/tests/UnitTests/TemplateSource/Comments/templates_tmp/testComments_T7.tpl new file mode 100644 index 00000000..95185a40 --- /dev/null +++ b/tests/UnitTests/TemplateSource/Comments/templates_tmp/testComments_T7.tpl @@ -0,0 +1,3 @@ +D{* comment *} +{* comment *}E +F \ No newline at end of file diff --git a/tests/UnitTests/TemplateSource/Comments/templates_tmp/testComments_T8.tpl b/tests/UnitTests/TemplateSource/Comments/templates_tmp/testComments_T8.tpl new file mode 100644 index 00000000..56d2b9f2 --- /dev/null +++ b/tests/UnitTests/TemplateSource/Comments/templates_tmp/testComments_T8.tpl @@ -0,0 +1,2 @@ +G{* multi +line *}H \ No newline at end of file diff --git a/tests/UnitTests/TemplateSource/Comments/templates_tmp/testComments_T9.tpl b/tests/UnitTests/TemplateSource/Comments/templates_tmp/testComments_T9.tpl new file mode 100644 index 00000000..0f4495e6 --- /dev/null +++ b/tests/UnitTests/TemplateSource/Comments/templates_tmp/testComments_T9.tpl @@ -0,0 +1,3 @@ +I{* multi +line *} +J \ No newline at end of file diff --git a/tests/UnitTests/TemplateSource/TagTests/PluginFunction/PluginFunctionHtmlImageTest.php b/tests/UnitTests/TemplateSource/TagTests/PluginFunction/PluginFunctionHtmlImageTest.php index 4293450f..43cd179d 100644 --- a/tests/UnitTests/TemplateSource/TagTests/PluginFunction/PluginFunctionHtmlImageTest.php +++ b/tests/UnitTests/TemplateSource/TagTests/PluginFunction/PluginFunctionHtmlImageTest.php @@ -20,6 +20,9 @@ class PluginFunctionHtmlImageTest extends PHPUnit_Smarty $this->setUpSmarty(dirname(__FILE__)); } + /** + * @doesNotPerformAssertions + */ public function testFoo() { // TODO: UnitTests for {html_image} diff --git a/tests/UnitTests/TemplateSource/X_Scopes/ScopeTest.php b/tests/UnitTests/TemplateSource/X_Scopes/ScopeTest.php index e546b670..6fac4716 100644 --- a/tests/UnitTests/TemplateSource/X_Scopes/ScopeTest.php +++ b/tests/UnitTests/TemplateSource/X_Scopes/ScopeTest.php @@ -291,6 +291,9 @@ class ScopeTest extends PHPUnit_Smarty 'no smarty', $i ++,),); } + /** + * @doesNotPerformAssertions + */ public function testFunctionScope() { $this->smarty->assign('scope', 'none');