From 6996cbb56664012bf5ce0b5b04e8f3c7be63ff3f Mon Sep 17 00:00:00 2001 From: Tim Otten Date: Tue, 6 Jun 2023 21:11:38 -0700 Subject: [PATCH] (NFC) Smarty - Expand test coverage for scope-cleanup. Show inconsistency. --- .../CRM/Core/Smarty/plugins/CrmScopeTest.php | 2 + tests/phpunit/CRM/Core/SmartyTest.php | 62 +++++++++++++++++++ 2 files changed, 64 insertions(+) create mode 100644 tests/phpunit/CRM/Core/SmartyTest.php diff --git a/tests/phpunit/CRM/Core/Smarty/plugins/CrmScopeTest.php b/tests/phpunit/CRM/Core/Smarty/plugins/CrmScopeTest.php index 40184d79d1..03b5d695a6 100644 --- a/tests/phpunit/CRM/Core/Smarty/plugins/CrmScopeTest.php +++ b/tests/phpunit/CRM/Core/Smarty/plugins/CrmScopeTest.php @@ -13,6 +13,8 @@ class CRM_Core_Smarty_plugins_CrmScopeTest extends CiviUnitTestCase { // Templates should normally be file names, but for unit-testing it's handy to use "string:" notation require_once 'CRM/Core/Smarty/resources/String.php'; civicrm_smarty_register_string_resource(); + + $this->useTransaction(); } /** diff --git a/tests/phpunit/CRM/Core/SmartyTest.php b/tests/phpunit/CRM/Core/SmartyTest.php new file mode 100644 index 0000000000..9ba928ed93 --- /dev/null +++ b/tests/phpunit/CRM/Core/SmartyTest.php @@ -0,0 +1,62 @@ +useTransaction(); + } + + /** + * Check that temporary Smarty variables work. + * + * This overlaps with CrmScopeTest (which actually tests more diverse scenarios). However, here we specifically check the PHP APIs + * (`fetchWith()`) and the correctness of different forms of emptiness. + * + * @see \CRM_Core_Smarty_plugins_CrmScopeTest + */ + public function testFetchWith_CleanNonExistent() { + $smarty = CRM_Core_Smarty::singleton(); + $this->assertFalse(array_key_exists('my_variable', $smarty->get_template_vars())); + + $rendered = $smarty->fetchWith('string:({$my_variable})', [ + 'my_variable' => 'temporary value', + ]); + $this->assertEquals('(temporary value)', $rendered); + + $this->assertFalse(array_key_exists('my_variable', $smarty->get_template_vars())); + } + + /** + * Check that temporary Smarty variables work. + * + * This overlaps with CrmScopeTest (which actually tests more diverse scenarios). However, here we specifically check the PHP APIs + * (`fetchWith()`) and the correctness of different forms of emptiness. + * + * @see \CRM_Core_Smarty_plugins_CrmScopeTest + */ + public function testFetchWith_CleanNull() { + $smarty = CRM_Core_Smarty::singleton(); + $smarty->assign('my_variable', NULL); + $this->assertEquals(NULL, $smarty->get_template_vars()['my_variable']); + + $tpl = 'string:({$my_variable})'; + $this->assertEquals('()', $smarty->fetchWith($tpl, [])); + $this->assertEquals('(temporary value)', $smarty->fetchWith($tpl, [ + 'my_variable' => 'temporary value', + ])); + + // Assert global state + $this->assertEquals(NULL, $smarty->get_template_vars()['my_variable']); + } + +} -- 2.25.1