From 15fbb5418fa718445472358bf07ed1af8e65cfd0 Mon Sep 17 00:00:00 2001 From: Tim Otten Date: Tue, 19 Apr 2022 00:06:43 -0700 Subject: [PATCH] ManagedEntitiesTest - Add coverage for filtering by module --- CRM/Utils/Hook.php | 4 +- .../phpunit/CRM/Core/ManagedEntitiesTest.php | 55 +++++++++++++++++++ 2 files changed, 57 insertions(+), 2 deletions(-) diff --git a/CRM/Utils/Hook.php b/CRM/Utils/Hook.php index bb8b464bee..9b43e025f2 100644 --- a/CRM/Utils/Hook.php +++ b/CRM/Utils/Hook.php @@ -746,8 +746,8 @@ abstract class CRM_Utils_Hook { 'civicrm_managed' ); if ($modules) { - $entities = array_filter($entities, function($decl) use ($modules) { - return in_array($decl['module'], $modules, TRUE); + $entities = array_filter($entities, function($entity) use ($modules) { + return in_array($entity['module'], $modules, TRUE); }); } } diff --git a/tests/phpunit/CRM/Core/ManagedEntitiesTest.php b/tests/phpunit/CRM/Core/ManagedEntitiesTest.php index 90e1b3240d..b890f2374c 100644 --- a/tests/phpunit/CRM/Core/ManagedEntitiesTest.php +++ b/tests/phpunit/CRM/Core/ManagedEntitiesTest.php @@ -103,6 +103,28 @@ class CRM_Core_ManagedEntitiesTest extends CiviUnitTestCase { 'update' => 'never', ], ]; + $this->fixtures['com.example.two-CustomGroup'] = [ + 'module' => 'com.example.two', + 'name' => 'CustomGroup', + 'entity' => 'CustomGroup', + 'params' => [ + 'version' => 3, + 'name' => 'test_custom_group_two', + 'title' => 'Test custom group two', + 'extends' => 'Individual', + ], + ]; + $this->fixtures['com.example.three-CustomGroup'] = [ + 'module' => 'com.example.three', + 'name' => 'CustomGroup', + 'entity' => 'CustomGroup', + 'params' => [ + 'version' => 3, + 'name' => 'test_custom_group_three', + 'title' => 'Test custom group three', + 'extends' => 'Individual', + ], + ]; $this->apiKernel = \Civi::service('civi_api_kernel'); $this->adhocProvider = new \Civi\API\Provider\AdhocProvider(3, 'CustomSearch'); @@ -581,4 +603,37 @@ class CRM_Core_ManagedEntitiesTest extends CiviUnitTestCase { } + /** + * The hook_managed signature expanded slightly (adding the `$modules` filter). + * Pre-existing implementations may over-report (ie return entities despite the `$modules` filter). + * This test ensures that the framework respects the `$modules` filter (even if specific implementations don't). + */ + public function testHookManaged_FilterModule() { + $this->managedEntities = [ + $this->fixtures['com.example.one-bar'], + $this->fixtures['com.example.two-CustomGroup'], + $this->fixtures['com.example.three-CustomGroup'], + ]; + + $entitiesAll = []; + CRM_Utils_Hook::managed($entitiesAll); + $this->assertEquals($this->managedEntities, $entitiesAll); + $this->assertEquals(3, count($entitiesAll)); + + $entitiesTwoOnly = []; + CRM_Utils_Hook::managed($entitiesTwoOnly, ['com.example.two']); + $this->assertEquals([$this->fixtures['com.example.two-CustomGroup']], array_values($entitiesTwoOnly)); + $this->assertEquals(1, count($entitiesTwoOnly)); + + $entitiesTwoExtra = []; + CRM_Utils_Hook::managed($entitiesTwoExtra, ['com.example.two', 'com.example.extra']); + $this->assertEquals([$this->fixtures['com.example.two-CustomGroup']], array_values($entitiesTwoExtra)); + $this->assertEquals(1, count($entitiesTwoExtra)); + + $entitiesTwoThree = []; + CRM_Utils_Hook::managed($entitiesTwoThree, ['com.example.two', 'com.example.three']); + $this->assertEquals([$this->fixtures['com.example.two-CustomGroup'], $this->fixtures['com.example.three-CustomGroup']], array_values($entitiesTwoThree)); + $this->assertEquals(2, count($entitiesTwoThree)); + } + } -- 2.25.1