From 420a0aa9283395805d1449d2dcc2d3f9a86b8028 Mon Sep 17 00:00:00 2001 From: Coleman Watts Date: Wed, 8 Dec 2021 19:47:38 -0500 Subject: [PATCH] Fixes dev/core#2984 - Give clear error message when entity's component is disabled Previously, using an API from a disabled component would fail in unexpected ways with cryptic error messages. Now it's clearly not allowed. --- Civi/API/Request.php | 7 ++++++- tests/phpunit/api/v4/Entity/EntityTest.php | 10 ++++++++++ 2 files changed, 16 insertions(+), 1 deletion(-) diff --git a/Civi/API/Request.php b/Civi/API/Request.php index 65d5c28035..45616d4d12 100644 --- a/Civi/API/Request.php +++ b/Civi/API/Request.php @@ -48,13 +48,18 @@ class Request { // Load the API kernel service for registering API providers, as // otherwise subscribers to the civi.api4.createRequest event registered // through the EventSubscriberInterface will not be registered. - $kernel = \Civi::service('civi_api_kernel'); + \Civi::service('civi_api_kernel'); $e = new CreateApi4RequestEvent($entity); \Civi::dispatcher()->dispatch('civi.api4.createRequest', $e); $callable = [$e->className, $action]; if (!$e->className || !is_callable($callable)) { throw new \Civi\API\Exception\NotImplementedException("API ($entity, $action) does not exist (join the API team and implement it!)"); } + // Check enabled components + $daoName = \CRM_Core_DAO_AllCoreTables::getFullName($entity); + if ($daoName && defined("{$daoName}::COMPONENT") && !array_key_exists($daoName::COMPONENT, \CRM_Core_Component::getEnabledComponents())) { + throw new \Civi\API\Exception\NotImplementedException("$entity API is not available because " . $daoName::COMPONENT . " component is disabled"); + } $apiRequest = call_user_func_array($callable, $e->args); foreach ($params as $name => $param) { $setter = 'set' . ucfirst($name); diff --git a/tests/phpunit/api/v4/Entity/EntityTest.php b/tests/phpunit/api/v4/Entity/EntityTest.php index b239e257ab..06139637f2 100644 --- a/tests/phpunit/api/v4/Entity/EntityTest.php +++ b/tests/phpunit/api/v4/Entity/EntityTest.php @@ -19,6 +19,7 @@ namespace api\v4\Entity; +use Civi\API\Exception\NotImplementedException; use Civi\Api4\Entity; use api\v4\UnitTestCase; @@ -56,6 +57,15 @@ class EntityTest extends UnitTestCase { $this->assertArrayNotHasKey('Participant', $result, "Entity::get should not have Participant when CiviEvent disabled"); + // Trying to use a CiviEvent API will fail when component is disabled + try { + \Civi\Api4\Participant::get(FALSE)->execute(); + $this->fail(); + } + catch (NotImplementedException $e) { + $this->assertStringContainsString('CiviEvent', $e->getMessage()); + } + \CRM_Core_BAO_ConfigSetting::enableComponent('CiviEvent'); $result = Entity::get(FALSE) ->execute() -- 2.25.1