Skip to content

Commit

Permalink
APiv4 - Add Extension.get
Browse files Browse the repository at this point in the history
  • Loading branch information
colemanw committed Feb 15, 2022
1 parent 3c76945 commit 510b347
Show file tree
Hide file tree
Showing 2 changed files with 149 additions and 0 deletions.
105 changes: 105 additions & 0 deletions Civi/Api4/Extension.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
<?php
/*
+--------------------------------------------------------------------+
| Copyright CiviCRM LLC. All rights reserved. |
| |
| This work is published under the GNU AGPLv3 license with some |
| permitted exceptions and without any warranty. For full license |
| and copyright information, see https://civicrm.org/licensing |
+--------------------------------------------------------------------+
*/
namespace Civi\Api4;

/**
* Extensions - add-on modules extend the functionality of CiviCRM.
*
* @see https://docs.civicrm.org/user/en/latest/introduction/extensions/
* @searchable secondary
* @since 5.48
* @package Civi\Api4
*/
class Extension extends Generic\AbstractEntity {

/**
* @param bool $checkPermissions
* @return Generic\BasicGetAction
*/
public static function get($checkPermissions = TRUE) {
return (new Generic\BasicGetAction(__CLASS__, __FUNCTION__, function($action) {
$statuses = \CRM_Extension_System::singleton()->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;
}

}
44 changes: 44 additions & 0 deletions tests/phpunit/api/v4/Entity/ExtensionTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<?php

/*
+--------------------------------------------------------------------+
| Copyright CiviCRM LLC. All rights reserved. |
| |
| This work is published under the GNU AGPLv3 license with some |
| permitted exceptions and without any warranty. For full license |
| and copyright information, see https://civicrm.org/licensing |
+--------------------------------------------------------------------+
*/

/**
*
* @package CRM
* @copyright CiviCRM LLC https://civicrm.org/licensing
*/


namespace api\v4\Entity;

use api\v4\UnitTestCase;
use Civi\Api4\Extension;

/**
* @group headless
*/
class ExtensionTest extends UnitTestCase {

public function testGet() {
$moduleTest = Extension::get(FALSE)
->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']);
}

}

0 comments on commit 510b347

Please sign in to comment.