From 510b347782f6f42596402fee42f676bd35d87f16 Mon Sep 17 00:00:00 2001 From: Coleman Watts Date: Thu, 10 Feb 2022 20:19:53 -0500 Subject: [PATCH] APiv4 - Add Extension.get --- Civi/Api4/Extension.php | 105 ++++++++++++++++++ tests/phpunit/api/v4/Entity/ExtensionTest.php | 44 ++++++++ 2 files changed, 149 insertions(+) create mode 100644 Civi/Api4/Extension.php create mode 100644 tests/phpunit/api/v4/Entity/ExtensionTest.php diff --git a/Civi/Api4/Extension.php b/Civi/Api4/Extension.php new file mode 100644 index 0000000000..503253b8d3 --- /dev/null +++ b/Civi/Api4/Extension.php @@ -0,0 +1,105 @@ +getManager()->getStatuses(); + $mapper = \CRM_Extension_System::singleton()->getMapper(); + $result = []; + foreach ($statuses as $key => $status) { + try { + $obj = $mapper->keyToInfo($key); + $info = \CRM_Extension_System::createExtendedInfo($obj); + $result[] = $info; + } + catch (\CRM_Extension_Exception $ex) { + \Civi::log()->error(sprintf('Failed to read extension (%1). Please refresh the extension list.', [1 => $key])); + } + } + return $result; + }))->setCheckPermissions($checkPermissions); + } + + /** + * @param bool $checkPermissions + * @return Generic\BasicGetFieldsAction + */ + public static function getFields($checkPermissions = TRUE) { + return (new Generic\BasicGetFieldsAction(static::getEntityName(), __FUNCTION__, function() { + return [ + [ + 'name' => 'key', + 'description' => 'Long, unique extension identifier', + ], + [ + 'name' => 'file', + 'description' => 'Short, unique extension identifier', + ], + [ + 'name' => 'label', + 'description' => 'User-facing extension title', + ], + [ + 'name' => 'description', + 'description' => 'Additional information about the extension', + ], + [ + 'name' => 'version', + 'description' => 'Current version number (string)', + ], + [ + 'name' => 'tags', + 'data_type' => 'Array', + 'description' => "Tags which characterize the extension's purpose or functionality", + ], + [ + 'name' => 'status', + 'description' => 'Extension enabled/disabled/uninstalled status', + 'options' => [ + \CRM_Extension_Manager::STATUS_UNINSTALLED => ts('Uninstalled'), + \CRM_Extension_Manager::STATUS_DISABLED => ts('Disabled'), + \CRM_Extension_Manager::STATUS_INSTALLED => ts('Enabled'), + \CRM_Extension_Manager::STATUS_DISABLED_MISSING => ts('Disabled (Missing)'), + \CRM_Extension_Manager::STATUS_INSTALLED_MISSING => ts('Enabled (Missing)'), + ], + ], + ]; + }))->setCheckPermissions($checkPermissions); + } + + /** + * @inheritDoc + */ + public static function getInfo() { + $info = parent::getInfo(); + $info['title'] = ts('Extension'); + $info['title_plural'] = ts('Extensions'); + $info['primary_key'] = ['key']; + $info['label_field'] = 'label'; + return $info; + } + +} diff --git a/tests/phpunit/api/v4/Entity/ExtensionTest.php b/tests/phpunit/api/v4/Entity/ExtensionTest.php new file mode 100644 index 0000000000..bfd626acc1 --- /dev/null +++ b/tests/phpunit/api/v4/Entity/ExtensionTest.php @@ -0,0 +1,44 @@ +addWhere('key', '=', 'test.extension.manager.moduletest') + ->execute()->single(); + $this->assertEquals('test_extension_manager_moduletest', $moduleTest['label']); + $this->assertEquals(['mock'], $moduleTest['tags']); + + $moduleTest = Extension::get(FALSE) + ->addWhere('file', '=', 'moduletest') + ->execute()->single(); + $this->assertEquals('test_extension_manager_moduletest', $moduleTest['label']); + $this->assertEquals(['mock'], $moduleTest['tags']); + } + +} -- 2.25.1