verify that native PHP functions cannot be used as a modifier and verify that an easy userland workaround exists. Fixes #813.

This commit is contained in:
Simon Wisselink
2023-01-31 10:10:32 +01:00
parent 18a8068df1
commit 0962a34670

View File

@@ -88,6 +88,49 @@ class RegisterModifierTest extends PHPUnit_Smarty
$this->smarty->unregisterPlugin(\Smarty\Smarty::PLUGIN_MODIFIER, 'testmodifier');
$this->assertIsArray($this->smarty->getRegisteredPlugin(\Smarty\Smarty::PLUGIN_BLOCK, 'testmodifier'));
}
/**
* test cannot call native PHP fuctions by default
* @dataProvider dataUnknownModifiers
*/
public function testNativePHPModifiers($template, $expectedValue)
{
$this->cleanDirs();
$this->expectException(\Smarty\CompilerException::class);
$this->expectExceptionMessage('unknown modifier');
$this->smarty->fetch('string:' . $template);
}
public function dataUnknownModifiers(): array {
return [
['{"blah"|substr:1:2}', 'la'],
['{"blah"|ucfirst}', 'Blah'],
['{"blah"|md5}', md5('blah')],
];
}
/**
* test register wildcard modifier using extension
* @dataProvider dataUnknownModifiers
*/
public function testUnregisterModifiers($template, $expectedValue)
{
$this->cleanDirs();
$this->smarty->addExtension(new WildcardExtension());
$this->assertEquals($expectedValue, $this->smarty->fetch('string:' . $template));
}
}
class WildcardExtension extends \Smarty\Extension\Base {
public function getModifierCallback(string $modifierName) {
if (is_callable($modifierName)) {
return $modifierName;
}
return null;
}
}
function mymodifier($a, $b, $c)