From c18711195b2e2c7e86b2d131e01ce2429d28716f Mon Sep 17 00:00:00 2001 From: Coleman Watts Date: Mon, 29 Nov 2021 09:52:50 -0500 Subject: [PATCH] APIv4 - Add `is_active` extra field to Domain entity --- CRM/Core/Config.php | 2 +- .../Spec/Provider/DomainGetSpecProvider.php | 50 +++++++++++++++++ tests/phpunit/api/v4/Entity/DomainTest.php | 55 +++++++++++++++++++ 3 files changed, 106 insertions(+), 1 deletion(-) create mode 100644 Civi/Api4/Service/Spec/Provider/DomainGetSpecProvider.php create mode 100644 tests/phpunit/api/v4/Entity/DomainTest.php diff --git a/CRM/Core/Config.php b/CRM/Core/Config.php index c1db7ff809..a9041e60d0 100644 --- a/CRM/Core/Config.php +++ b/CRM/Core/Config.php @@ -222,7 +222,7 @@ class CRM_Core_Config extends CRM_Core_Config_MagicMerge { * @param int $domainID * @param bool $reset * - * @return int|null + * @return int */ public static function domainID($domainID = NULL, $reset = FALSE) { static $domain; diff --git a/Civi/Api4/Service/Spec/Provider/DomainGetSpecProvider.php b/Civi/Api4/Service/Spec/Provider/DomainGetSpecProvider.php new file mode 100644 index 0000000000..ddb52a8894 --- /dev/null +++ b/Civi/Api4/Service/Spec/Provider/DomainGetSpecProvider.php @@ -0,0 +1,50 @@ +getEntity(), 'Boolean'); + $field->setLabel(ts('Active Domain')) + ->setTitle(ts('Active')) + ->setColumnName('id') + ->setDescription(ts('Is this the current active domain')) + ->setType('Extra') + ->setSqlRenderer([__CLASS__, 'renderIsActiveDomain']); + $spec->addFieldSpec($field); + } + + /** + * @inheritDoc + */ + public function applies($entity, $action) { + return $entity === 'Domain' && $action === 'get'; + } + + /** + * @param array $field + * return string + */ + public static function renderIsActiveDomain(array $field): string { + $currentDomain = \CRM_Core_Config::domainID(); + return "IF({$field['sql_name']} = '$currentDomain', '1', '0')"; + } + +} diff --git a/tests/phpunit/api/v4/Entity/DomainTest.php b/tests/phpunit/api/v4/Entity/DomainTest.php new file mode 100644 index 0000000000..46ee3cc751 --- /dev/null +++ b/tests/phpunit/api/v4/Entity/DomainTest.php @@ -0,0 +1,55 @@ +addValue('name', 'Not current') + ->addValue('version', \CRM_Utils_System::version()) + ->execute(); + + Domain::update(FALSE) + ->addValue('name', 'Currently the current domain') + ->addWhere('is_active', '=', TRUE) + ->execute(); + + $getCurrent = Domain::get(FALSE) + ->addWhere('is_active', '=', TRUE) + ->execute()->single(); + + $this->assertEquals('Currently the current domain', $getCurrent['name']); + + $getAll = Domain::get(FALSE) + ->addSelect('*', 'is_active') + ->execute()->indexBy('name'); + + $this->assertTrue($getAll['Currently the current domain']['is_active']); + $this->assertFalse($getAll['Not current']['is_active']); + } + +} -- 2.25.1