mixin/setting-php - Import
authorTim Otten <totten@civicrm.org>
Tue, 30 Nov 2021 22:26:42 +0000 (14:26 -0800)
committerTim Otten <totten@civicrm.org>
Wed, 1 Dec 2021 03:36:08 +0000 (19:36 -0800)
mixin/setting-php@1/example/CRM/Shimmy/Utils.php [new file with mode: 0644]
mixin/setting-php@1/example/settings/Shimmy.setting.php [new file with mode: 0644]
mixin/setting-php@1/example/tests/mixin/SettingsTest.php [new file with mode: 0644]
mixin/setting-php@1/mixin.php [new file with mode: 0644]

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 (file)
index 0000000..1e4fb07
--- /dev/null
@@ -0,0 +1,17 @@
+<?php
+
+use CRM_Shimmy_ExtensionUtil as E;
+
+class CRM_Shimmy_Utils {
+
+  /**
+   * @return array
+   */
+  public static function getExampleOptions(): array {
+    return [
+      'first' => 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 (file)
index 0000000..fc33887
--- /dev/null
@@ -0,0 +1,26 @@
+<?php
+use CRM_Shimmy_ExtensionUtil as E;
+
+return [
+  'shimmy_example' => [
+    '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 (file)
index 0000000..2ea5e99
--- /dev/null
@@ -0,0 +1,58 @@
+<?php
+
+namespace Civi\Shimmy\Mixins;
+
+/**
+ * Assert that the `settings/*.setting.php` mixin is working properly.
+ *
+ * This class defines the assertions to run when installing or uninstalling the extension.
+ * It use called as part of E2E_Shimmy_LifecycleTest.
+ *
+ * @see E2E_Shimmy_LifecycleTest
+ */
+class SettingsTest extends \PHPUnit\Framework\Assert {
+
+  public function testPreConditions($cv) {
+    $this->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 (file)
index 0000000..7195af4
--- /dev/null
@@ -0,0 +1,32 @@
+<?php
+
+/**
+ * Auto-register "settings/*.setting.php" files.
+ *
+ * @mixinName setting-php
+ * @mixinVersion 1.0.0
+ *
+ * @param CRM_Extension_MixInfo $mixInfo
+ *   On newer deployments, this will be an instance of MixInfo. On older deployments, Civix may polyfill with a work-a-like.
+ * @param \CRM_Extension_BootCache $bootCache
+ *   On newer deployments, this will be an instance of MixInfo. On older deployments, Civix may polyfill with a work-a-like.
+ */
+return function ($mixInfo, $bootCache) {
+
+  /**
+   * @param \Civi\Core\Event\GenericHookEvent $e
+   * @see CRM_Utils_Hook::alterSettingsFolders()
+   */
+  Civi::dispatcher()->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;
+    }
+  });
+
+};