Skip to content

Commit

Permalink
verify that native PHP functions cannot be used as a modifier and ver…
Browse files Browse the repository at this point in the history
…ify that an easy userland workaround exists. Fixes #813.
  • Loading branch information
wisskid committed Jan 31, 2023
1 parent 18a8068 commit 0962a34
Showing 1 changed file with 43 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,49 @@ public function testUnregisterModifierOtherRegistered()
$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)
Expand Down

0 comments on commit 0962a34

Please sign in to comment.