From d900a0ef4a216962adfb0f7e5d934af236b3d61f Mon Sep 17 00:00:00 2001 From: Simon Wisselink Date: Fri, 13 Jan 2023 19:23:12 +0100 Subject: [PATCH 1/3] Added variable scope test for multi-level extends resource --- .../Extends/ExtendsResourceTest.php | 19 ++++++++++++++++++- .../Extends/templates/050_child.tpl | 1 + .../Extends/templates/050_grandchild.tpl | 1 + .../Extends/templates/050_parent.tpl | 2 ++ .../templates/CachingTests/nocache_lib.tpl | 6 ------ .../templates/CachingTests/test0.tpl | 4 ---- .../templates/CachingTests/test1.tpl | 7 ------- .../templates/CachingTests/test2.tpl | 3 --- .../templates/CachingTests/test3.tpl | 4 ---- .../templates/CachingTests/test4.tpl | 3 --- .../templates/CachingTests/test5.tpl | 4 ---- .../templates/CachingTests/test6.tpl | 8 -------- .../templates/CachingTests/test7.tpl | 2 -- 13 files changed, 22 insertions(+), 42 deletions(-) create mode 100644 tests/UnitTests/ResourceTests/Extends/templates/050_child.tpl create mode 100644 tests/UnitTests/ResourceTests/Extends/templates/050_grandchild.tpl create mode 100644 tests/UnitTests/ResourceTests/Extends/templates/050_parent.tpl delete mode 100644 tests/UnitTests/TemplateSource/TagTests/TemplateFunction/templates/CachingTests/nocache_lib.tpl delete mode 100644 tests/UnitTests/TemplateSource/TagTests/TemplateFunction/templates/CachingTests/test0.tpl delete mode 100644 tests/UnitTests/TemplateSource/TagTests/TemplateFunction/templates/CachingTests/test1.tpl delete mode 100644 tests/UnitTests/TemplateSource/TagTests/TemplateFunction/templates/CachingTests/test2.tpl delete mode 100644 tests/UnitTests/TemplateSource/TagTests/TemplateFunction/templates/CachingTests/test3.tpl delete mode 100644 tests/UnitTests/TemplateSource/TagTests/TemplateFunction/templates/CachingTests/test4.tpl delete mode 100644 tests/UnitTests/TemplateSource/TagTests/TemplateFunction/templates/CachingTests/test5.tpl delete mode 100644 tests/UnitTests/TemplateSource/TagTests/TemplateFunction/templates/CachingTests/test6.tpl delete mode 100644 tests/UnitTests/TemplateSource/TagTests/TemplateFunction/templates/CachingTests/test7.tpl diff --git a/tests/UnitTests/ResourceTests/Extends/ExtendsResourceTest.php b/tests/UnitTests/ResourceTests/Extends/ExtendsResourceTest.php index 0494ead0..e3816d12 100644 --- a/tests/UnitTests/ResourceTests/Extends/ExtendsResourceTest.php +++ b/tests/UnitTests/ResourceTests/Extends/ExtendsResourceTest.php @@ -91,6 +91,23 @@ class ExtendsResourceTest extends PHPUnit_Smarty $this->assertStringContainsString("test:{$testNumber} compiled:{$compileTestNumber} rendered:{$renderTestNumber}", $result, $testName . ' - fetch() failure'); } + /** + * @dataProvider data + */ + public function testCompileBlockIncreaseInChild_050($caching, $merge, $testNumber, $compileTestNumber, $renderTestNumber, $testName) + { + $this->smarty->registerFilter('pre', array($this, 'compiledPrefilter')); + $this->smarty->assign('test', $testNumber); + $this->smarty->caching = $caching; + $this->smarty->merge_compiled_includes = $merge; + if ($merge) { + $this->smarty->compile_id = 1; + } + $result = $this->smarty->fetch('extends:050_parent.tpl|050_child.tpl|050_grandchild.tpl'); + $this->assertStringContainsString("var-bar-var", $result, $testName . ' - content'); + $this->assertStringContainsString("test:{$testNumber} compiled:{$compileTestNumber} rendered:{$renderTestNumber}", $result, $testName . ' - fetch() failure'); + } + /** * test grandchild/child/parent dependency test1 * @runInSeparateProcess @@ -259,7 +276,7 @@ class ExtendsResourceTest extends PHPUnit_Smarty */ array(false, false, 1, 1, 1, 'no caching, no merge - new'), array(false, false, 2, 1, 2, 'no caching, no merge - exits'), - array(true, false, 3, 3, 3, 'caching, no merge - new'), + array(true, false, 3, 3, 3, 'caching, no merge - new'), // 2 array(true, false, 4, 3, 3, 'caching, no merge - exits'), array(false, true, 5, 5, 5, 'no caching, merge - new'), array(false, true, 6, 5, 6, 'no caching, merge - exits'), diff --git a/tests/UnitTests/ResourceTests/Extends/templates/050_child.tpl b/tests/UnitTests/ResourceTests/Extends/templates/050_child.tpl new file mode 100644 index 00000000..887c4ad5 --- /dev/null +++ b/tests/UnitTests/ResourceTests/Extends/templates/050_child.tpl @@ -0,0 +1 @@ +{$notfoo="notbar"} diff --git a/tests/UnitTests/ResourceTests/Extends/templates/050_grandchild.tpl b/tests/UnitTests/ResourceTests/Extends/templates/050_grandchild.tpl new file mode 100644 index 00000000..2f8199de --- /dev/null +++ b/tests/UnitTests/ResourceTests/Extends/templates/050_grandchild.tpl @@ -0,0 +1 @@ +{$foo="bar"} diff --git a/tests/UnitTests/ResourceTests/Extends/templates/050_parent.tpl b/tests/UnitTests/ResourceTests/Extends/templates/050_parent.tpl new file mode 100644 index 00000000..85d2795a --- /dev/null +++ b/tests/UnitTests/ResourceTests/Extends/templates/050_parent.tpl @@ -0,0 +1,2 @@ +test:{$test nocache} compiled:# rendered:{$test} +{block name='test'}var-{$foo}-var{/block} diff --git a/tests/UnitTests/TemplateSource/TagTests/TemplateFunction/templates/CachingTests/nocache_lib.tpl b/tests/UnitTests/TemplateSource/TagTests/TemplateFunction/templates/CachingTests/nocache_lib.tpl deleted file mode 100644 index 8602140e..00000000 --- a/tests/UnitTests/TemplateSource/TagTests/TemplateFunction/templates/CachingTests/nocache_lib.tpl +++ /dev/null @@ -1,6 +0,0 @@ -{function 'nocache1' default1='d1' default2='d2'} - default1={$default1} - default2={$default1 nocache} - p1={$p1} - p2={$p2 nocache} -{/function} \ No newline at end of file diff --git a/tests/UnitTests/TemplateSource/TagTests/TemplateFunction/templates/CachingTests/test0.tpl b/tests/UnitTests/TemplateSource/TagTests/TemplateFunction/templates/CachingTests/test0.tpl deleted file mode 100644 index ae27c661..00000000 --- a/tests/UnitTests/TemplateSource/TagTests/TemplateFunction/templates/CachingTests/test0.tpl +++ /dev/null @@ -1,4 +0,0 @@ -Test 1 -
-{include 'test7.tpl' compile_id = 4} - diff --git a/tests/UnitTests/TemplateSource/TagTests/TemplateFunction/templates/CachingTests/test1.tpl b/tests/UnitTests/TemplateSource/TagTests/TemplateFunction/templates/CachingTests/test1.tpl deleted file mode 100644 index c8992cca..00000000 --- a/tests/UnitTests/TemplateSource/TagTests/TemplateFunction/templates/CachingTests/test1.tpl +++ /dev/null @@ -1,7 +0,0 @@ -Test 1 -
-{include 'test7.tpl' compile_id = $id} -{include 'test6.tpl'} -{include file="test2.tpl"} -{include 'test6.tpl'} - diff --git a/tests/UnitTests/TemplateSource/TagTests/TemplateFunction/templates/CachingTests/test2.tpl b/tests/UnitTests/TemplateSource/TagTests/TemplateFunction/templates/CachingTests/test2.tpl deleted file mode 100644 index 9b526949..00000000 --- a/tests/UnitTests/TemplateSource/TagTests/TemplateFunction/templates/CachingTests/test2.tpl +++ /dev/null @@ -1,3 +0,0 @@ -Test 2 -
-{include file="test3.tpl" compile_id=4} diff --git a/tests/UnitTests/TemplateSource/TagTests/TemplateFunction/templates/CachingTests/test3.tpl b/tests/UnitTests/TemplateSource/TagTests/TemplateFunction/templates/CachingTests/test3.tpl deleted file mode 100644 index c4ca3221..00000000 --- a/tests/UnitTests/TemplateSource/TagTests/TemplateFunction/templates/CachingTests/test3.tpl +++ /dev/null @@ -1,4 +0,0 @@ -Test 3 {call name='f' nocache} -
-{include file="test4.tpl" compile_id=9} -{include file="test5.tpl" cache_id="7"} \ No newline at end of file diff --git a/tests/UnitTests/TemplateSource/TagTests/TemplateFunction/templates/CachingTests/test4.tpl b/tests/UnitTests/TemplateSource/TagTests/TemplateFunction/templates/CachingTests/test4.tpl deleted file mode 100644 index 13b0cbbb..00000000 --- a/tests/UnitTests/TemplateSource/TagTests/TemplateFunction/templates/CachingTests/test4.tpl +++ /dev/null @@ -1,3 +0,0 @@ -Test 4 -
-{include file="test5.tpl" cache_id="7"} \ No newline at end of file diff --git a/tests/UnitTests/TemplateSource/TagTests/TemplateFunction/templates/CachingTests/test5.tpl b/tests/UnitTests/TemplateSource/TagTests/TemplateFunction/templates/CachingTests/test5.tpl deleted file mode 100644 index 0a65c4c8..00000000 --- a/tests/UnitTests/TemplateSource/TagTests/TemplateFunction/templates/CachingTests/test5.tpl +++ /dev/null @@ -1,4 +0,0 @@ -test 5 -
- -{call 'f'} diff --git a/tests/UnitTests/TemplateSource/TagTests/TemplateFunction/templates/CachingTests/test6.tpl b/tests/UnitTests/TemplateSource/TagTests/TemplateFunction/templates/CachingTests/test6.tpl deleted file mode 100644 index d7379f81..00000000 --- a/tests/UnitTests/TemplateSource/TagTests/TemplateFunction/templates/CachingTests/test6.tpl +++ /dev/null @@ -1,8 +0,0 @@ -test 6 -
-{function name='f'} -
- function f {$id} {$foo} -
-{/function} -
Time 6 = {$t6}
\ No newline at end of file diff --git a/tests/UnitTests/TemplateSource/TagTests/TemplateFunction/templates/CachingTests/test7.tpl b/tests/UnitTests/TemplateSource/TagTests/TemplateFunction/templates/CachingTests/test7.tpl deleted file mode 100644 index cc378526..00000000 --- a/tests/UnitTests/TemplateSource/TagTests/TemplateFunction/templates/CachingTests/test7.tpl +++ /dev/null @@ -1,2 +0,0 @@ -test 7 -
extern \ No newline at end of file From 73ff8fd3d0ecc004121dbc5fc611dabf38c13f59 Mon Sep 17 00:00:00 2001 From: Simon Wisselink Date: Fri, 13 Jan 2023 22:19:17 +0100 Subject: [PATCH 2/3] Fix unit tests that broke because now is now in 2023 --- .../PluginFunction/PluginFunctionHtmlSelectDateTest.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/UnitTests/TemplateSource/TagTests/PluginFunction/PluginFunctionHtmlSelectDateTest.php b/tests/UnitTests/TemplateSource/TagTests/PluginFunction/PluginFunctionHtmlSelectDateTest.php index 260ff45c..633a1c81 100644 --- a/tests/UnitTests/TemplateSource/TagTests/PluginFunction/PluginFunctionHtmlSelectDateTest.php +++ b/tests/UnitTests/TemplateSource/TagTests/PluginFunction/PluginFunctionHtmlSelectDateTest.php @@ -401,7 +401,7 @@ class PluginFunctionHtmlSelectDateTest extends PHPUnit_Smarty $result = '' . $n . '' . $n . ''; - $tpl = $this->smarty->createTemplate('eval:{html_select_date time="2022-02-" day_empty="day" start_year=2005}'); + $tpl = $this->smarty->createTemplate('eval:{html_select_date time="' . date('Y', $this->now) . '-02-" day_empty="day" start_year=2005}'); $this->assertEquals($result, $tpl->fetch()); } @@ -410,7 +410,7 @@ class PluginFunctionHtmlSelectDateTest extends PHPUnit_Smarty $result = '' . $n . '' . $n . ''; - $tpl = $this->smarty->createTemplate('eval:{html_select_date time="2022--20" month_empty="month" start_year=2005}'); + $tpl = $this->smarty->createTemplate('eval:{html_select_date time="' . date('Y', $this->now) . '--20" month_empty="month" start_year=2005}'); $this->assertEquals($result, $tpl->fetch()); } From 5988116c8146d284d5d93495991a06197428b67b Mon Sep 17 00:00:00 2001 From: Adrien Poupa Date: Mon, 16 Jan 2023 18:24:23 -0500 Subject: [PATCH 3/3] PHP 8.1 deprecation warnings on null strings in modifiers (#834) --- CHANGELOG.md | 5 +++-- libs/plugins/modifier.truncate.php | 2 +- libs/plugins/modifiercompiler.count_characters.php | 4 ++-- libs/plugins/modifiercompiler.count_words.php | 2 +- libs/plugins/modifiercompiler.lower.php | 4 ++-- libs/plugins/modifiercompiler.upper.php | 4 ++-- libs/plugins/outputfilter.trimwhitespace.php | 2 +- libs/plugins/shared.escape_special_chars.php | 2 +- libs/plugins/variablefilter.htmlspecialchars.php | 2 +- 9 files changed, 14 insertions(+), 13 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 00723903..d300faff 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -10,7 +10,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 - `$smarty->muteUndefinedOrNullWarnings()` now also mutes PHP7 notices for undefined array indexes [#736](https://github.com/smarty-php/smarty/issues/736) - `$smarty->muteUndefinedOrNullWarnings()` now treats undefined vars and array access of a null or false variables equivalent across all supported PHP versions -- `$smarty->muteUndefinedOrNullWarnings()` now allows dereferencing of non-objects accross all supported PHP versions [#831](https://github.com/smarty-php/smarty/issues/831) +- `$smarty->muteUndefinedOrNullWarnings()` now allows dereferencing of non-objects across all supported PHP versions [#831](https://github.com/smarty-php/smarty/issues/831) +- PHP 8.1 deprecation warnings on null strings in modifiers [#834](https://github.com/smarty-php/smarty/pull/834) + ## [4.3.0] - 2022-11-22 ### Added @@ -32,7 +34,6 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 - Adapt Smarty upper/lower functions to be codesafe (e.g. for Turkish locale) [#586](https://github.com/smarty-php/smarty/pull/586) - Bug fix for underscore and limited length in template name in custom resources [#581](https://github.com/smarty-php/smarty/pull/581) - ## [4.2.1] - 2022-09-14 ### Security diff --git a/libs/plugins/modifier.truncate.php b/libs/plugins/modifier.truncate.php index 80dcdb53..d253e4a6 100644 --- a/libs/plugins/modifier.truncate.php +++ b/libs/plugins/modifier.truncate.php @@ -26,7 +26,7 @@ */ function smarty_modifier_truncate($string, $length = 80, $etc = '...', $break_words = false, $middle = false) { - if ($length === 0) { + if ($length === 0 || $string === null) { return ''; } if (Smarty::$_MBSTRING) { diff --git a/libs/plugins/modifiercompiler.count_characters.php b/libs/plugins/modifiercompiler.count_characters.php index b5d97e27..7798cabc 100644 --- a/libs/plugins/modifiercompiler.count_characters.php +++ b/libs/plugins/modifiercompiler.count_characters.php @@ -25,8 +25,8 @@ function smarty_modifiercompiler_count_characters($params) return 'preg_match_all(\'/[^\s]/' . Smarty::$_UTF8_MODIFIER . '\',' . $params[ 0 ] . ', $tmp)'; } if (Smarty::$_MBSTRING) { - return 'mb_strlen(' . $params[ 0 ] . ', \'' . addslashes(Smarty::$_CHARSET) . '\')'; + return 'mb_strlen((string) ' . $params[ 0 ] . ', \'' . addslashes(Smarty::$_CHARSET) . '\')'; } // no MBString fallback - return 'strlen(' . $params[ 0 ] . ')'; + return 'strlen((string) ' . $params[ 0 ] . ')'; } diff --git a/libs/plugins/modifiercompiler.count_words.php b/libs/plugins/modifiercompiler.count_words.php index cf2e5022..48a3daa1 100644 --- a/libs/plugins/modifiercompiler.count_words.php +++ b/libs/plugins/modifiercompiler.count_words.php @@ -27,5 +27,5 @@ function smarty_modifiercompiler_count_words($params) $params[ 0 ] . ', $tmp)'; } // no MBString fallback - return 'str_word_count(' . $params[ 0 ] . ')'; + return 'str_word_count((string) ' . $params[ 0 ] . ')'; } diff --git a/libs/plugins/modifiercompiler.lower.php b/libs/plugins/modifiercompiler.lower.php index ac9cc39d..4144e561 100644 --- a/libs/plugins/modifiercompiler.lower.php +++ b/libs/plugins/modifiercompiler.lower.php @@ -22,8 +22,8 @@ function smarty_modifiercompiler_lower($params) { if (Smarty::$_MBSTRING) { - return 'mb_strtolower(' . $params[ 0 ] . ', \'' . addslashes(Smarty::$_CHARSET) . '\')'; + return 'mb_strtolower((string) ' . $params[ 0 ] . ', \'' . addslashes(Smarty::$_CHARSET) . '\')'; } // no MBString fallback - return 'strtolower(' . $params[ 0 ] . ')'; + return 'strtolower((string) ' . $params[ 0 ] . ')'; } diff --git a/libs/plugins/modifiercompiler.upper.php b/libs/plugins/modifiercompiler.upper.php index 31a90a05..96030518 100644 --- a/libs/plugins/modifiercompiler.upper.php +++ b/libs/plugins/modifiercompiler.upper.php @@ -21,8 +21,8 @@ function smarty_modifiercompiler_upper($params) { if (Smarty::$_MBSTRING) { - return 'mb_strtoupper(' . $params[ 0 ] . ' ?? \'\', \'' . addslashes(Smarty::$_CHARSET) . '\')'; + return 'mb_strtoupper((string) ' . $params[ 0 ] . ' ?? \'\', \'' . addslashes(Smarty::$_CHARSET) . '\')'; } // no MBString fallback - return 'strtoupper(' . $params[ 0 ] . ' ?? \'\')'; + return 'strtoupper((string) ' . $params[ 0 ] . ' ?? \'\')'; } diff --git a/libs/plugins/outputfilter.trimwhitespace.php b/libs/plugins/outputfilter.trimwhitespace.php index 2f747ad5..180cdd08 100644 --- a/libs/plugins/outputfilter.trimwhitespace.php +++ b/libs/plugins/outputfilter.trimwhitespace.php @@ -62,7 +62,7 @@ function smarty_outputfilter_trimwhitespace($source) } } $expressions = array(// replace multiple spaces between tags by a single space - // can't remove them entirely, becaue that might break poorly implemented CSS display:inline-block elements + // can't remove them entirely, because that might break poorly implemented CSS display:inline-block elements '#(:SMARTY@!@|>)\s+(?=@!@SMARTY:|<)#s' => '\1 \2', // remove spaces between attributes (but not in attribute values!) '#(([a-z0-9]\s*=\s*("[^"]*?")|(\'[^\']*?\'))|<[a-z0-9_]+)\s+([a-z/>])#is' => '\1 \5', diff --git a/libs/plugins/shared.escape_special_chars.php b/libs/plugins/shared.escape_special_chars.php index a204b092..355b02b5 100644 --- a/libs/plugins/shared.escape_special_chars.php +++ b/libs/plugins/shared.escape_special_chars.php @@ -20,7 +20,7 @@ function smarty_function_escape_special_chars($string) { if (!is_array($string)) { - $string = htmlspecialchars($string, ENT_COMPAT, Smarty::$_CHARSET, false); + $string = htmlspecialchars((string) $string, ENT_COMPAT, Smarty::$_CHARSET, false); } return $string; } diff --git a/libs/plugins/variablefilter.htmlspecialchars.php b/libs/plugins/variablefilter.htmlspecialchars.php index 3c85295d..2eb12d20 100644 --- a/libs/plugins/variablefilter.htmlspecialchars.php +++ b/libs/plugins/variablefilter.htmlspecialchars.php @@ -15,5 +15,5 @@ */ function smarty_variablefilter_htmlspecialchars($source, Smarty_Internal_Template $template) { - return htmlspecialchars($source, ENT_QUOTES, Smarty::$_CHARSET); + return htmlspecialchars((string) $source, ENT_QUOTES, Smarty::$_CHARSET); }