From: Tim Otten Date: Mon, 22 May 2023 05:26:10 +0000 (-0700) Subject: (dev/core#4303) Add test for {htxt} X-Git-Url: https://vcs.fsf.org/?a=commitdiff_plain;h=c7928d093b4507e8f1d73ffe5cc5bcf4c40ac93b;p=civicrm-core.git (dev/core#4303) Add test for {htxt} --- diff --git a/tests/phpunit/CRM/Core/Smarty/plugins/HtxtTest.php b/tests/phpunit/CRM/Core/Smarty/plugins/HtxtTest.php new file mode 100644 index 0000000000..e662377409 --- /dev/null +++ b/tests/phpunit/CRM/Core/Smarty/plugins/HtxtTest.php @@ -0,0 +1,61 @@ + 'apple']]; + $cases[] = ['', '{htxt id="apple"}yum yum apple!{/htxt}', ['id' => 'not me']]; + $cases[] = ['yum yum banana!', '{htxt id=$dynamic}yum yum {$dynamic}!{/htxt}', ['id' => 'banana', 'dynamic' => 'banana']]; + $cases[] = ['', '{htxt id=$dynamic}yum yum {$dynamic}!{/htxt}', ['id' => 'apple', 'dynamic' => 'banana']]; + // More advanced forms of dynamic-id's might be nice, but this is currently the ceiling on what's needed. + return $cases; + } + + /** + * @dataProvider scopeCases + * @param string $expected + * @param string $input + * @param array $vars + */ + public function testSupported(string $expected, string $input, array $vars) { + $smarty = CRM_Core_Smarty::singleton(); + $smarty->pushScope($vars); + try { + $actual = $smarty->fetch('string:' . $input); + $this->assertEquals($expected, $actual, "Process input=[$input]"); + } + finally { + $smarty->popScope(); + } + } + + public function testUnsupported() { + $smarty = CRM_Core_Smarty::singleton(); + try { + $smarty->fetch('string:{htxt id=$dynamic.zx["$f{b}"]}power parser!{/htxt}'); + $this->fail("Congratulations, the test failed! You are the road to a better parsing rule."); + } + catch (Throwable $t) { + ob_end_flush(); + $this->assertTrue(str_contains($t->getMessage(), 'Invalid {htxt} tag')); + } + } + +}