From 5641e30c2680be624fc8f951218dd6e6f8390a73 Mon Sep 17 00:00:00 2001 From: Tim Otten Date: Tue, 30 Nov 2021 14:26:42 -0800 Subject: [PATCH] mixin/setting-php - Import --- .../example/CRM/Shimmy/Utils.php | 17 ++++++ .../example/settings/Shimmy.setting.php | 26 +++++++++ .../example/tests/mixin/SettingsTest.php | 58 +++++++++++++++++++ mixin/setting-php@1/mixin.php | 32 ++++++++++ 4 files changed, 133 insertions(+) create mode 100644 mixin/setting-php@1/example/CRM/Shimmy/Utils.php create mode 100644 mixin/setting-php@1/example/settings/Shimmy.setting.php create mode 100644 mixin/setting-php@1/example/tests/mixin/SettingsTest.php create mode 100644 mixin/setting-php@1/mixin.php diff --git a/mixin/setting-php@1/example/CRM/Shimmy/Utils.php b/mixin/setting-php@1/example/CRM/Shimmy/Utils.php new file mode 100644 index 0000000000..1e4fb0771b --- /dev/null +++ b/mixin/setting-php@1/example/CRM/Shimmy/Utils.php @@ -0,0 +1,17 @@ + E::ts('First example'), + 'second' => E::ts('Second example'), + ]; + } + +} diff --git a/mixin/setting-php@1/example/settings/Shimmy.setting.php b/mixin/setting-php@1/example/settings/Shimmy.setting.php new file mode 100644 index 0000000000..fc338875e3 --- /dev/null +++ b/mixin/setting-php@1/example/settings/Shimmy.setting.php @@ -0,0 +1,26 @@ + [ + 'group_name' => 'Shimmy Preferences', + 'group' => 'shimmy', + 'name' => 'shimmy_example', + 'type' => 'String', + 'html_type' => 'select', + 'html_attributes' => [ + 'class' => 'crm-select2', + ], + 'pseudoconstant' => [ + 'callback' => 'CRM_Shimmy_Utils::getExampleOptions', + ], + 'default' => 'first', + 'add' => '4.7', + 'title' => E::ts('Shimmy editor layout'), + 'is_domain' => 1, + 'is_contact' => 0, + 'description' => E::ts('What is a shimmy example?'), + 'help_text' => NULL, + 'settings_pages' => ['shimmy' => ['weight' => 10]], + ], +]; diff --git a/mixin/setting-php@1/example/tests/mixin/SettingsTest.php b/mixin/setting-php@1/example/tests/mixin/SettingsTest.php new file mode 100644 index 0000000000..2ea5e9981b --- /dev/null +++ b/mixin/setting-php@1/example/tests/mixin/SettingsTest.php @@ -0,0 +1,58 @@ +assertFileExists(static::getPath('/settings/Shimmy.setting.php'), 'The shimmy extension must have a Menu XML file.'); + } + + public function testInstalled($cv) { + // The menu item is registered... + $items = $cv->api4('Setting', 'getFields', ['where' => [['name', '=', 'shimmy_example']], 'loadOptions' => TRUE]); + $this->assertEquals('shimmy_example', $items[0]['name']); + $this->assertEquals('select', $items[0]['html_type']); + $this->assertEquals('First example', $items[0]['options']['first']); + $this->assertEquals('Second example', $items[0]['options']['second']); + + // And it supports reading/writing... + $cv->api3('Setting', 'revert', ['name' => 'shimmy_example']); /* FIXME: Prior installations don't cleanup... */ + $value = $cv->api3('Setting', 'getvalue', ['name' => 'shimmy_example']); + $this->assertEquals('first', $value); + $r = $cv->api3('Setting', 'create', ['shimmy_example' => 'second']); + $this->assertEquals(0, $r['is_error']); + $value = $cv->api3('Setting', 'getvalue', ['name' => 'shimmy_example']); + $this->assertEquals('second', $value); + } + + public function testDisabled($cv) { + $items = $cv->api4('Setting', 'getFields', ['where' => [['name', '=', 'shimmy_example']], 'loadOptions' => TRUE]); + $this->assertEmpty($items); + + $value = $cv->api3('Setting', 'getvalue', ['name' => 'shimmy_example']); + $this->assertEquals('second', $value); + } + + public function testUninstalled($cv) { + $items = $cv->api4('Setting', 'getFields', ['where' => [['name', '=', 'shimmy_example']], 'loadOptions' => TRUE]); + $this->assertEmpty($items); + + // Uninstall should probably drop old settings, but it hasn't traditionally, so we won't check for the moment. + // FIXME // $value = cv('ev \'return Civi::settings()->get("shimmy_example");\'', 'raw'); + // FIXME // $this->assertEquals('null', trim($value)); + } + + protected static function getPath($suffix = ''): string { + return dirname(__DIR__, 2) . $suffix; + } + +} diff --git a/mixin/setting-php@1/mixin.php b/mixin/setting-php@1/mixin.php new file mode 100644 index 0000000000..7195af40de --- /dev/null +++ b/mixin/setting-php@1/mixin.php @@ -0,0 +1,32 @@ +addListener('hook_civicrm_alterSettingsFolders', function ($e) use ($mixInfo) { + // When deactivating on a polyfill/pre-mixin system, listeners may not cleanup automatically. + if (!$mixInfo->isActive()) { + return; + } + + $settingsDir = $mixInfo->getPath('settings'); + if (!in_array($settingsDir, $e->settingsFolders) && is_dir($settingsDir)) { + $e->settingsFolders[] = $settingsDir; + } + }); + +}; -- 2.25.1