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) {
if ($compiler->syntaxMatchesVariable($output)) {
$output = '(empty(' . $output . ') ? ' . $param . ' : ' . $output . ')';
$output = '(!isset(' . $output . ') || ' . $output . ' === \'\' ? ' . $param . ' : ' . $output . ')';
} else {
$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));
}
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
);
}
}