Fix modifier default implementation and improve unit tests

This commit is contained in:
Simon Wisselink
2021-03-23 00:03:04 +01:00
parent ef51d44251
commit 2f3eb6514f
2 changed files with 20 additions and 12 deletions

View File

@@ -28,7 +28,7 @@ function smarty_modifiercompiler_default($params, Smarty_Internal_TemplateCompil
foreach ($params as $param) { foreach ($params as $param) {
if ($compiler->syntaxMatchesVariable($output)) { if ($compiler->syntaxMatchesVariable($output)) {
$output = '(empty(' . $output . ') ? ' . $param . ' : ' . $output . ')'; $output = '(!isset(' . $output . ') || ' . $output . ' === \'\' ? ' . $param . ' : ' . $output . ')';
} else { } else {
$output = '(($tmp = ' . $output . ')===null||$tmp===\'\' ? ' . $param . ' : $tmp)'; $output = '(($tmp = ' . $output . ')===null||$tmp===\'\' ? ' . $param . ' : $tmp)';
} }

View File

@@ -63,20 +63,31 @@ class PluginModifierDefaultTest extends PHPUnit_Smarty
$this->assertEquals('B', $this->smarty->fetch($tpl)); $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"}'); $tpl = $this->smarty->createTemplate('string:{$s|default:"B"}');
$this->smarty->assign('s', null); $this->smarty->assign('s', $falsyvalue);
$this->assertEquals('B', $this->smarty->fetch($tpl)); $this->assertEquals($expected, $this->smarty->fetch($tpl));
} }
public function testFalse() /**
{ * Data for ::testFalsyValues
$tpl = $this->smarty->createTemplate('string:{$s|default:"B"}'); */
$this->smarty->assign('s', false); public function dataFalsyValues() {
$this->assertEquals('B', $this->smarty->fetch($tpl)); return array(
array(false, false),
array(0, 0),
array('0', '0'),
array(array(), 'Array'),
array(null, 'B'),
array('', 'B'),
);
} }
public function testFunctionCall() public function testFunctionCall()
{ {
$tpl = $this->smarty->createTemplate('string:{strlen("a")|default:"B"}'); $tpl = $this->smarty->createTemplate('string:{strlen("a")|default:"B"}');
@@ -137,7 +148,4 @@ class PluginModifierDefaultTest extends PHPUnit_Smarty
); );
} }
} }