From de1bc77f7fad0500a8b654203c72bbb49554f55c Mon Sep 17 00:00:00 2001 From: Simon Wisselink Date: Mon, 26 Feb 2024 14:33:47 +0100 Subject: Prevent deprecation notices for implode, json_encode and substr modifiers * Prevent deprecation notices for implode, json_encode and substr modifiers. * unit tests --- CHANGELOG.md | 3 + libs/plugins/modifier.implode.php | 15 +++++ libs/plugins/modifiercompiler.json_encode.php | 11 ++++ libs/plugins/modifiercompiler.substr.php | 12 ++++ .../PluginModifier/PluginModifierImplodeTest.php | 44 +++++++++++++ .../PluginModifierJsonEncodeTest.php | 76 ++++++++++++++++++++++ .../PluginModifier/PluginModifierSubstrTest.php | 48 ++++++++++++++ 7 files changed, 209 insertions(+) create mode 100644 libs/plugins/modifier.implode.php create mode 100644 libs/plugins/modifiercompiler.json_encode.php create mode 100644 libs/plugins/modifiercompiler.substr.php create mode 100644 tests/UnitTests/TemplateSource/TagTests/PluginModifier/PluginModifierImplodeTest.php create mode 100644 tests/UnitTests/TemplateSource/TagTests/PluginModifier/PluginModifierJsonEncodeTest.php create mode 100644 tests/UnitTests/TemplateSource/TagTests/PluginModifier/PluginModifierSubstrTest.php diff --git a/CHANGELOG.md b/CHANGELOG.md index c54f7872..acd4981b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,6 +6,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ## [Unreleased] +### Changed +- Using the `|implode`, `|json_encode` and `|substr` modifiers does not generate a deprecation warning anymore as they will continue to be supported in v5 [#939](https://github.com/smarty-php/smarty/issues/939) + ### Added - PHP8.3 support [#925](https://github.com/smarty-php/smarty/issues/925) diff --git a/libs/plugins/modifier.implode.php b/libs/plugins/modifier.implode.php new file mode 100644 index 00000000..679d71d7 --- /dev/null +++ b/libs/plugins/modifier.implode.php @@ -0,0 +1,15 @@ +setUpSmarty(__DIR__); + } + + public function testDefault() + { + $tpl = $this->smarty->createTemplate('string:{""|implode:$v}'); + $tpl->assign("v", ["1", "2"]); + $this->assertEquals("12", $this->smarty->fetch($tpl)); + } + public function testWithSeparator() + { + $tpl = $this->smarty->createTemplate('string:{","|implode:$v}'); + $tpl->assign("v", ["a", "b"]); + $this->assertEquals("a,b", $this->smarty->fetch($tpl)); + } + public function testInConditional() + { + $tpl = $this->smarty->createTemplate('string:{if implode("", $v) == "abc"}good{else}bad{/if}'); + $tpl->assign("v", ['a','b','c']); + $this->assertEquals("good", $this->smarty->fetch($tpl)); + } + public function testInConditionalWithSeparator() + { + $tpl = $this->smarty->createTemplate('string:{if implode("-", $v) == "a-b-c"}good{else}bad{/if}'); + $tpl->assign("v", ['a','b','c']); + $this->assertEquals("good", $this->smarty->fetch($tpl)); + } +} diff --git a/tests/UnitTests/TemplateSource/TagTests/PluginModifier/PluginModifierJsonEncodeTest.php b/tests/UnitTests/TemplateSource/TagTests/PluginModifier/PluginModifierJsonEncodeTest.php new file mode 100644 index 00000000..965c8a6d --- /dev/null +++ b/tests/UnitTests/TemplateSource/TagTests/PluginModifier/PluginModifierJsonEncodeTest.php @@ -0,0 +1,76 @@ +setUpSmarty(__DIR__); + } + + /** + * @dataProvider dataForDefault + */ + public function testDefault($value, $expected) + { + $tpl = $this->smarty->createTemplate('string:{$v|json_encode}'); + $tpl->assign("v", $value); + $this->assertEquals($expected, $this->smarty->fetch($tpl)); + } + + /** + * @dataProvider dataForDefault + */ + public function testDefaultAsFunction($value, $expected) + { + $tpl = $this->smarty->createTemplate('string:{json_encode($v)}'); + $tpl->assign("v", $value); + $this->assertEquals($expected, $this->smarty->fetch($tpl)); + } + + public function dataForDefault() { + return [ + ["abc", '"abc"'], + [["abc"], '["abc"]'], + [["abc",["a"=>2]], '["abc",{"a":2}]'], + ]; + } + + /** + * @dataProvider dataForForceObject + */ + public function testForceObject($value, $expected) + { + $tpl = $this->smarty->createTemplate('string:{$v|json_encode:16}'); + $tpl->assign("v", $value); + $this->assertEquals($expected, $this->smarty->fetch($tpl)); + } + + /** + * @dataProvider dataForForceObject + */ + public function testForceObjectAsFunction($value, $expected) + { + $tpl = $this->smarty->createTemplate('string:{json_encode($v,16)}'); + $tpl->assign("v", $value); + $this->assertEquals($expected, $this->smarty->fetch($tpl)); + } + + public function dataForForceObject() { + return [ + ["abc", '"abc"'], + [["abc"], '{"0":"abc"}'], + [["abc",["a"=>2]], '{"0":"abc","1":{"a":2}}'], + ]; + } + +} diff --git a/tests/UnitTests/TemplateSource/TagTests/PluginModifier/PluginModifierSubstrTest.php b/tests/UnitTests/TemplateSource/TagTests/PluginModifier/PluginModifierSubstrTest.php new file mode 100644 index 00000000..42abf869 --- /dev/null +++ b/tests/UnitTests/TemplateSource/TagTests/PluginModifier/PluginModifierSubstrTest.php @@ -0,0 +1,48 @@ +setUpSmarty(__DIR__); + } + + public function testDefault() + { + $tpl = $this->smarty->createTemplate('string:{$v|substr:1}'); + $tpl->assign("v", "abc"); + $this->assertEquals("bc", $this->smarty->fetch($tpl)); + } + + public function testTwoArguments() + { + $tpl = $this->smarty->createTemplate('string:{$v|substr:1:1}'); + $tpl->assign("v", "abc"); + $this->assertEquals("b", $this->smarty->fetch($tpl)); + } + + public function testNegativeOffset() + { + $tpl = $this->smarty->createTemplate('string:{$v|substr:-1}'); + $tpl->assign("v", "abc"); + $this->assertEquals("c", $this->smarty->fetch($tpl)); + } + + public function testInConditional() + { + $tpl = $this->smarty->createTemplate('string:{if substr($v, -1) == "c"}good{else}bad{/if}'); + $tpl->assign("v", "abc"); + $this->assertEquals("good", $this->smarty->fetch($tpl)); + } + +} -- cgit v1.3