diff --git a/libs/plugins/modifiercompiler.default.php b/libs/plugins/modifiercompiler.default.php index c4bd5dd4..dbd87715 100644 --- a/libs/plugins/modifiercompiler.default.php +++ b/libs/plugins/modifiercompiler.default.php @@ -28,7 +28,7 @@ function smarty_modifiercompiler_default($params, Smarty_Internal_TemplateCompil foreach ($params as $param) { if ($compiler->syntaxMatchesVariable($output)) { - $output = '(empty(' . $output . ') ? ' . $param . ' : ' . $output . ')'; + $output = '(!isset(' . $output . ') || ' . $output . ' === \'\' ? ' . $param . ' : ' . $output . ')'; } else { $output = '(($tmp = ' . $output . ')===null||$tmp===\'\' ? ' . $param . ' : $tmp)'; } diff --git a/tests/UnitTests/TemplateSource/TagTests/PluginModifier/PluginModifierDefaultTest.php b/tests/UnitTests/TemplateSource/TagTests/PluginModifier/PluginModifierDefaultTest.php index 9bf51612..1a0cb6ee 100644 --- a/tests/UnitTests/TemplateSource/TagTests/PluginModifier/PluginModifierDefaultTest.php +++ b/tests/UnitTests/TemplateSource/TagTests/PluginModifier/PluginModifierDefaultTest.php @@ -63,20 +63,31 @@ class PluginModifierDefaultTest extends PHPUnit_Smarty $this->assertEquals('B', $this->smarty->fetch($tpl)); } - public function testNull() + /** + * @dataProvider dataFalsyValues + */ + public function testFalsyValues($falsyvalue, $expected) { $tpl = $this->smarty->createTemplate('string:{$s|default:"B"}'); - $this->smarty->assign('s', null); - $this->assertEquals('B', $this->smarty->fetch($tpl)); + $this->smarty->assign('s', $falsyvalue); + $this->assertEquals($expected, $this->smarty->fetch($tpl)); } - public function testFalse() - { - $tpl = $this->smarty->createTemplate('string:{$s|default:"B"}'); - $this->smarty->assign('s', false); - $this->assertEquals('B', $this->smarty->fetch($tpl)); + /** + * Data for ::testFalsyValues + */ + public function dataFalsyValues() { + return array( + array(false, false), + array(0, 0), + array('0', '0'), + array(array(), 'Array'), + array(null, 'B'), + array('', 'B'), + ); } + public function testFunctionCall() { $tpl = $this->smarty->createTemplate('string:{strlen("a")|default:"B"}'); @@ -137,7 +148,4 @@ class PluginModifierDefaultTest extends PHPUnit_Smarty ); } - - - }