From d2bc455ec2f2a9891e234436aaf545e814445bf1 Mon Sep 17 00:00:00 2001 From: Tim Otten Date: Tue, 17 Jan 2023 19:13:57 -0800 Subject: [PATCH] Add mixin/smarty-v2 --- .../example/templates/CRM/Shimmy/Example.tpl | 1 + .../example/tests/mixin/SmartyTest.php | 66 +++++++++++++++++++ mixin/smarty-v2@1/mixin.php | 32 +++++++++ 3 files changed, 99 insertions(+) create mode 100644 mixin/smarty-v2@1/example/templates/CRM/Shimmy/Example.tpl create mode 100644 mixin/smarty-v2@1/example/tests/mixin/SmartyTest.php create mode 100644 mixin/smarty-v2@1/mixin.php diff --git a/mixin/smarty-v2@1/example/templates/CRM/Shimmy/Example.tpl b/mixin/smarty-v2@1/example/templates/CRM/Shimmy/Example.tpl new file mode 100644 index 0000000000..4c58b1981b --- /dev/null +++ b/mixin/smarty-v2@1/example/templates/CRM/Shimmy/Example.tpl @@ -0,0 +1 @@ +

{ts}OK{/ts}

diff --git a/mixin/smarty-v2@1/example/tests/mixin/SmartyTest.php b/mixin/smarty-v2@1/example/tests/mixin/SmartyTest.php new file mode 100644 index 0000000000..1eb0a29fe9 --- /dev/null +++ b/mixin/smarty-v2@1/example/tests/mixin/SmartyTest.php @@ -0,0 +1,66 @@ +assertFileExists(static::getPath('/templates/CRM/Shimmy/Example.tpl'), 'The shimmy extension must have example TPL files.'); + } + + public function testInstalled($cv): void { + $out = $this->renderExample($cv, 'CRM/Shimmy/Example.tpl'); + $this->assertEquals('

OK

', trim($out)); + } + + public function testDisabled($cv): void { + if ($cv->isLocal()) { + // Historically, Smarty templates have been left active for duration of same-process (post-disabling). + // We'll ignore testing this edge-case until someone decides that a change in behavior is better. + return; + } + + $out = $this->renderExample($cv, 'CRM/Shimmy/Example.tpl'); + $this->assertEquals('', trim($out)); + } + + public function testUninstalled($cv): void { + // Same as disabled.... + $this->testDisabled($cv); + } + + protected static function getPath($suffix = ''): string { + return dirname(__DIR__, 2) . $suffix; + } + + /** + * Render a template with the system-under-test. + * + * @param $cv + * @param string $name + * @return string + */ + protected function renderExample($cv, string $name) { + try { + putenv('SHIMMY_FOOBAR=' . $name); + try { + return $cv->phpEval('return CRM_Core_Smarty::singleton()->fetch(getenv("SHIMMY_FOOBAR"));'); + } + catch (\Throwable $e) { + return ''; + } + } + finally { + putenv('SHIMMY_FOOBAR'); + } + } + +} diff --git a/mixin/smarty-v2@1/mixin.php b/mixin/smarty-v2@1/mixin.php new file mode 100644 index 0000000000..14e750e92d --- /dev/null +++ b/mixin/smarty-v2@1/mixin.php @@ -0,0 +1,32 @@ +getPath('templates'); + if (!file_exists($dir)) { + return; + } + + // Is it good or bad that this hasn't different static-guard-i-ness than the old 'hook_config' boilerplate? + + if ($mixInfo->isActive()) { + \Civi::dispatcher()->addListener('hook_civicrm_config', function () use ($dir) { + \CRM_Core_Smarty::singleton()->addTemplateDir($dir); + }); + } + elseif (CRM_Extension_System::singleton()->getManager()->extensionIsBeingInstalledOrEnabled($mixInfo->longName)) { + \CRM_Core_Smarty::singleton()->addTemplateDir($dir); + } + +}; -- 2.25.1