4 * Class CRM_Core_ManagedEntitiesTest
7 class CRM_Core_ManagedEntitiesTest
extends CiviUnitTestCase
{
9 * @var \Civi\API\Kernel
14 * @var \Civi\API\Provider\AdhocProvider
16 protected $adhocProvider;
25 public function setUp(): void
{
26 $this->useTransaction(TRUE);
29 'one' => new CRM_Core_Module('com.example.one', TRUE),
30 'two' => new CRM_Core_Module('com.example.two', TRUE),
33 // Testing on drupal-demo fails because some extensions have mgd ents.
34 CRM_Core_DAO
::singleValueQuery('DELETE FROM civicrm_managed');
36 $this->fixtures
['com.example.one-foo'] = [
37 'module' => 'com.example.one',
39 'entity' => 'CustomSearch',
42 'class_name' => 'CRM_Example_One_Foo',
46 $this->fixtures
['com.example.one-bar'] = [
47 'module' => 'com.example.one',
49 'entity' => 'CustomSearch',
52 'class_name' => 'CRM_Example_One_Bar',
56 $this->fixtures
['com.example.one-CustomGroup'] = [
57 'module' => 'com.example.one',
58 'name' => 'CustomGroup',
59 'entity' => 'CustomGroup',
62 'name' => 'test_custom_group',
63 'title' => 'Test custom group',
64 'extends' => 'Individual',
67 $this->fixtures
['com.example.one-CustomField'] = [
68 'module' => 'com.example.one',
69 'name' => 'CustomField',
70 'entity' => 'CustomField',
73 'name' => 'test_custom_field',
74 'label' => 'Test custom field',
75 'custom_group_id' => 'test_custom_group',
76 'data_type' => 'String',
77 'html_type' => 'Text',
81 $this->fixtures
['com.example.one-Job'] = [
82 'module' => 'com.example.one',
88 'run_frequency' => 'Daily',
89 'api_entity' => 'Job',
90 'api_action' => 'Get',
94 $this->fixtures
['com.example.one-Contact'] = [
95 'module' => 'com.example.one',
97 'entity' => 'Contact',
100 'first_name' => 'Daffy',
101 'last_name' => 'Duck',
102 'contact_type' => 'Individual',
106 $this->fixtures
['com.example.two-CustomGroup'] = [
107 'module' => 'com.example.two',
108 'name' => 'CustomGroup',
109 'entity' => 'CustomGroup',
112 'name' => 'test_custom_group_two',
113 'title' => 'Test custom group two',
114 'extends' => 'Individual',
117 $this->fixtures
['com.example.three-CustomGroup'] = [
118 'module' => 'com.example.three',
119 'name' => 'CustomGroup',
120 'entity' => 'CustomGroup',
123 'name' => 'test_custom_group_three',
124 'title' => 'Test custom group three',
125 'extends' => 'Individual',
129 $this->apiKernel
= \Civi
::service('civi_api_kernel');
130 $this->adhocProvider
= new \Civi\API\Provider\
AdhocProvider(3, 'CustomSearch');
131 $this->apiKernel
->registerApiProvider($this->adhocProvider
);
132 $this->hookClass
->setHook('civicrm_managed', [$this, 'hookManaged']);
135 public function tearDown(): void
{
143 protected $managedEntities = [];
146 * Implements hook managed.
148 * @param array $entities
150 public function hookManaged(array &$entities): void
{
151 $entities = $this->managedEntities
;
155 * Set up an active module and, over time, the hook implementation changes
156 * to (1) create 'foo' entity, (2) create 'bar' entity', (3) remove 'foo'
159 public function testAddRemoveEntitiesModule_UpdateAlways_DeleteAlways() {
160 // create first managed entity ('foo')
161 $this->managedEntities
= [$this->fixtures
['com.example.one-foo']];
162 $me = new CRM_Core_ManagedEntities($this->modules
);
164 $foo = $me->get('com.example.one', 'foo');
165 $this->assertEquals('CRM_Example_One_Foo', $foo['name']);
166 $this->assertDBQuery(1, 'SELECT count(*) FROM civicrm_option_value WHERE name = "CRM_Example_One_Foo"');
168 // later on, hook returns an extra managed entity ('bar')
169 $this->managedEntities
[] = $this->fixtures
['com.example.one-bar'];
170 $me = new CRM_Core_ManagedEntities($this->modules
);
172 $foo = $me->get('com.example.one', 'foo');
173 $this->assertEquals('CRM_Example_One_Foo', $foo['name']);
174 $this->assertDBQuery(1, 'SELECT count(*) FROM civicrm_option_value WHERE name = "CRM_Example_One_Foo"');
175 $bar = $me->get('com.example.one', 'bar');
176 $this->assertEquals('CRM_Example_One_Bar', $bar['name']);
177 $this->assertDBQuery(1, 'SELECT count(*) FROM civicrm_option_value WHERE name = "CRM_Example_One_Bar"');
179 // and then hook changes its mind, removing 'foo' (first of two entities)
180 unset($this->managedEntities
[0]);
181 $me = new CRM_Core_ManagedEntities($this->modules
);
183 $foo = $me->get('com.example.one', 'foo');
184 $this->assertTrue($foo === NULL);
185 $this->assertDBQuery(0, 'SELECT count(*) FROM civicrm_option_value WHERE name = "CRM_Example_One_Foo"');
186 $bar = $me->get('com.example.one', 'bar');
187 $this->assertEquals('CRM_Example_One_Bar', $bar['name']);
188 $this->assertDBQuery(1, 'SELECT count(*) FROM civicrm_option_value WHERE name = "CRM_Example_One_Bar"');
190 // and then hook changes its mind, removing 'bar' (the last remaining entity)
191 unset($this->managedEntities
[1]);
192 $me = new CRM_Core_ManagedEntities($this->modules
);
194 $foo = $me->get('com.example.one', 'foo');
195 $this->assertTrue($foo === NULL);
196 $this->assertDBQuery(0, 'SELECT count(*) FROM civicrm_option_value WHERE name = "CRM_Example_One_Foo"');
197 $bar = $me->get('com.example.one', 'bar');
198 $this->assertNull($bar);
199 $this->assertDBQuery(0, 'SELECT count(*) FROM civicrm_option_value WHERE name = "CRM_Example_One_Bar"');
203 * Set up an active module with one managed-entity and, over
204 * time, the content of the entity changes
206 * @throws \CRM_Core_Exception
208 public function testModifyDeclaration_UpdateAlways(): void
{
209 // create first managed entity ('foo')
210 $this->managedEntities
= [$this->fixtures
['com.example.one-foo']];
211 $me = new CRM_Core_ManagedEntities($this->modules
);
213 $foo = $me->get('com.example.one', 'foo');
214 $this->assertEquals('CRM_Example_One_Foo', $foo['name']);
215 $this->assertDBQuery(1, 'SELECT count(*) FROM civicrm_option_value WHERE name = "CRM_Example_One_Foo"');
217 // later on, hook specification changes
218 $this->managedEntities
[0]['params']['class_name'] = 'CRM_Example_One_Foobar';
219 $me = new CRM_Core_ManagedEntities($this->modules
);
221 $foo2 = $me->get('com.example.one', 'foo');
222 $this->assertEquals('CRM_Example_One_Foobar', $foo2['name']);
223 $this->assertDBQuery(0, 'SELECT count(*) FROM civicrm_option_value WHERE name = "CRM_Example_One_Foo"');
224 $this->assertDBQuery(1, 'SELECT count(*) FROM civicrm_option_value WHERE name = "CRM_Example_One_FooBar"');
225 $this->assertEquals($foo['id'], $foo2['id']);
229 * Set up an active module with one managed-entity and, over
230 * time, the content of the entity changes
232 * @throws \CRM_Core_Exception
234 public function testModifyDeclaration_UpdateNever(): void
{
235 // create first managed entity ('foo')
236 $this->managedEntities
[] = array_merge($this->fixtures
['com.example.one-foo'], [
237 // Policy is to never update after initial creation
240 $me = new CRM_Core_ManagedEntities($this->modules
);
242 $foo = $me->get('com.example.one', 'foo');
243 $this->assertEquals('CRM_Example_One_Foo', $foo['name']);
244 $this->assertDBQuery(1, 'SELECT count(*) FROM civicrm_option_value WHERE name = "CRM_Example_One_Foo"');
246 // later on, hook specification changes
247 $this->managedEntities
[0]['params']['class_name'] = 'CRM_Example_One_Foobar';
248 $me = new CRM_Core_ManagedEntities($this->modules
);
250 $foo2 = $me->get('com.example.one', 'foo');
251 $this->assertEquals('CRM_Example_One_Foo', $foo2['name']);
252 $this->assertDBQuery(1, 'SELECT count(*) FROM civicrm_option_value WHERE name = "CRM_Example_One_Foo"');
253 $this->assertDBQuery(0, 'SELECT count(*) FROM civicrm_option_value WHERE name = "CRM_Example_One_FooBar"');
254 $this->assertEquals($foo['id'], $foo2['id']);
258 * Set up an active module with one managed-entity using the
259 * policy "cleanup=>never". When the managed-entity goes away,
260 * ensure that the policy is followed (ie the entity is not
263 * @throws \CRM_Core_Exception
265 public function testRemoveDeclaration_CleanupNever(): void
{
266 // create first managed entity ('foo')
267 $this->managedEntities
= [
268 array_merge($this->fixtures
['com.example.one-foo'], ['cleanup' => 'never']),
270 $me = new CRM_Core_ManagedEntities($this->modules
);
272 $foo = $me->get('com.example.one', 'foo');
273 $this->assertEquals('CRM_Example_One_Foo', $foo['name']);
274 $this->assertDBQuery(1, 'SELECT count(*) FROM civicrm_option_value WHERE name = "CRM_Example_One_Foo"');
276 // later on, entity definition disappears; but we decide not to do any cleanup (per policy)
277 $this->managedEntities
= [];
278 $me = new CRM_Core_ManagedEntities($this->modules
);
280 $foo2 = $me->get('com.example.one', 'foo');
281 $this->assertEquals('CRM_Example_One_Foo', $foo2['name']);
282 $this->assertDBQuery(1, 'SELECT count(*) FROM civicrm_option_value WHERE name = "CRM_Example_One_Foo"');
283 $this->assertEquals($foo['id'], $foo2['id']);
287 * Set up an active module with one managed-entity using the
288 * policy "cleanup=>unused". When the managed-entity goes away,
289 * ensure that the policy is followed (ie the entity is conditionally
292 * @throws \CRM_Core_Exception
294 public function testRemoveDeclaration_CleanupUnused(): void
{
295 // create first managed entity ('foo')
296 $this->managedEntities
= [array_merge($this->fixtures
['com.example.one-foo'], ['cleanup' => 'unused'])];
297 $me = new CRM_Core_ManagedEntities($this->modules
);
299 $foo = $me->get('com.example.one', 'foo');
300 $this->assertEquals('CRM_Example_One_Foo', $foo['name']);
301 $this->assertDBQuery(1, 'SELECT count(*) FROM civicrm_option_value WHERE name = "CRM_Example_One_Foo"');
303 // Override 'getrefcount' ==> The refcount is 1
304 $this->adhocProvider
->addAction('getrefcount', 'access CiviCRM', function ($apiRequest) {
305 return civicrm_api3_create_success([
314 // Later on, entity definition disappears; but we decide not to do any cleanup (per policy)
315 $this->managedEntities
= [];
316 $me = new CRM_Core_ManagedEntities($this->modules
);
318 $foo2 = $me->get('com.example.one', 'foo');
319 $this->assertEquals('CRM_Example_One_Foo', $foo2['name']);
320 $this->assertDBQuery(1, 'SELECT count(*) FROM civicrm_option_value WHERE name = "CRM_Example_One_Foo"');
321 $this->assertEquals($foo['id'], $foo2['id']);
323 // Override 'getrefcount' ==> The refcount is 0
324 $this->adhocProvider
->addAction('getrefcount', 'access CiviCRM', function ($apiRequest) {
325 return civicrm_api3_create_success([]);
328 // The entity definition disappeared and there's no reference; we decide to cleanup (per policy)
329 $this->managedEntities
= [];
330 $me = new CRM_Core_ManagedEntities($this->modules
);
332 $foo3 = $me->get('com.example.one', 'foo');
333 $this->assertDBQuery(0, 'SELECT count(*) FROM civicrm_option_value WHERE name = "CRM_Example_One_Foo"');
334 $this->assertNull($foo3);
338 * Setup an active module with a malformed entity declaration.
340 public function testInvalidDeclarationModule(): void
{
341 // create first managed entity ('foo')
342 $this->managedEntities
= [
345 'module' => 'com.example.unknown',
347 'entity' => 'CustomSearch',
350 'class_name' => 'CRM_Example_One_Foo',
355 $me = new CRM_Core_ManagedEntities($this->modules
);
358 $this->fail('Expected exception when using invalid declaration');
360 catch (Exception
$e) {
366 * Setup an active module with a malformed entity declaration.
368 public function testMissingName(): void
{
369 $this->managedEntities
= [
371 'module' => 'com.example.unknown',
374 'entity' => 'CustomSearch',
377 'class_name' => 'CRM_Example_One_Foo',
382 $me = new CRM_Core_ManagedEntities($this->modules
);
385 $this->fail('Expected exception when using invalid declaration');
387 catch (Exception
$e) {
393 * Setup an active module with a malformed entity declaration.
395 public function testMissingEntity(): void
{
396 $this->managedEntities
= [
398 'module' => 'com.example.unknown',
404 'class_name' => 'CRM_Example_One_Foo',
409 $me = new CRM_Core_ManagedEntities($this->modules
);
412 $this->fail('Expected exception when using invalid declaration');
414 catch (Exception
$e) {
420 * Setup an active module with an entity -- then disable and re-enable the
423 * @throws \CRM_Core_Exception
425 public function testDeactivateReactivateModule(): void
{
426 $manager = CRM_Extension_System
::singleton()->getManager();
427 // Register the hook so we can check there is no effort to de-activate contact.
428 $this->hookClass
->setHook('civicrm_pre', [$this, 'preHook']);
429 // create first managed entities ('foo' & Contact)
430 $this->managedEntities
= [$this->fixtures
['com.example.one-foo'], $this->fixtures
['com.example.one-Contact']];
431 // Mock the contextual process info that would be added by CRM_Extension_Manager::install
432 $manager->setProcessesForTesting(['com.example.one' => ['install']]);
433 $me = new CRM_Core_ManagedEntities($this->modules
);
435 $foo = $me->get('com.example.one', 'foo');
436 $this->assertEquals(1, $foo['is_active']);
437 $this->assertEquals('CRM_Example_One_Foo', $foo['name']);
438 $this->assertDBQuery(1, 'SELECT is_active FROM civicrm_option_value WHERE name = "CRM_Example_One_Foo"');
440 // now deactivate module, which has no declarations and which cascades to managed object
441 $this->modules
['one']->is_active
= FALSE;
442 // Mock the contextual process info that would be added by CRM_Extension_Manager::disable
443 $manager->setProcessesForTesting(['com.example.one' => ['disable']]);
444 $this->managedEntities
= [];
445 $me = new CRM_Core_ManagedEntities($this->modules
);
447 $foo = $me->get('com.example.one', 'foo');
448 $this->assertEquals(0, $foo['is_active']);
449 $this->assertEquals('CRM_Example_One_Foo', $foo['name']);
450 $this->assertDBQuery(0, 'SELECT is_active FROM civicrm_option_value WHERE name = "CRM_Example_One_Foo"');
452 // and reactivate module, which again provides decls and which cascades to managed object
453 $this->modules
['one']->is_active
= TRUE;
454 // Mock the contextual process info that would be added by CRM_Extension_Manager::enable
455 $manager->setProcessesForTesting(['com.example.one' => ['enable']]);
456 $this->managedEntities
= [$this->fixtures
['com.example.one-foo'], $this->fixtures
['com.example.one-Contact']];
458 $me = new CRM_Core_ManagedEntities($this->modules
);
460 $foo = $me->get('com.example.one', 'foo');
461 $this->assertEquals(1, $foo['is_active']);
462 $this->assertEquals('CRM_Example_One_Foo', $foo['name']);
463 $this->assertDBQuery(1, 'SELECT is_active FROM civicrm_option_value WHERE name = "CRM_Example_One_Foo"');
465 // Special case: Job entities.
467 // First we repeat the above steps, but adding the context that
468 // CRM_Extension_Manager adds when installing/enabling extensions.
470 // The behaviour should be as above.
471 $this->managedEntities
= [$this->fixtures
['com.example.one-Job']];
472 // Mock the contextual process info that would be added by CRM_Extension_Manager::install
473 $manager->setProcessesForTesting(['com.example.one' => ['install']]);
474 $me = new CRM_Core_ManagedEntities($this->modules
);
476 $job = $me->get('com.example.one', 'Job');
477 $this->assertEquals(1, $job['is_active']);
478 $this->assertEquals('test_job', $job['name']);
479 $this->assertDBQuery(1, 'SELECT is_active FROM civicrm_job WHERE name = "test_job"');
481 $manager->setProcessesForTesting([]);
483 // now deactivate module
484 $this->modules
['one']->is_active
= FALSE;
485 // Mock the contextual process info that would be added by CRM_Extension_Manager::disable
486 $manager->setProcessesForTesting(['com.example.one' => ['disable']]);
487 $this->managedEntities
= [];
488 $me = new CRM_Core_ManagedEntities($this->modules
);
490 $job = $me->get('com.example.one', 'Job');
491 $this->assertEquals(0, $job['is_active']);
492 $this->assertEquals('test_job', $job['name']);
493 $this->assertDBQuery(0, 'SELECT is_active FROM civicrm_job WHERE name = "test_job"');
495 // and reactivate module
496 $this->modules
['one']->is_active
= TRUE;
497 $this->managedEntities
= [$this->fixtures
['com.example.one-Job']];
498 $me = new CRM_Core_ManagedEntities($this->modules
);
499 // Mock the contextual process info that would be added by CRM_Extension_Manager::enable
500 $manager->setProcessesForTesting(['com.example.one' => ['enable']]);
502 $job = $me->get('com.example.one', 'Job');
503 $this->assertEquals(1, $job['is_active']);
504 $this->assertEquals('test_job', $job['name']);
505 $this->assertDBQuery(1, 'SELECT is_active FROM civicrm_job WHERE name = "test_job"');
507 // Currently: module enabled, job enabled.
508 // Test that if we now manually disable the job, calling reconcile in a
509 // normal flush situation does NOT re-enable it.
510 // ... manually disable job.
511 $this->callAPISuccess('Job', 'create', ['id' => $job['id'], 'is_active' => 0]);
513 // ... now call reconcile in the context of a normal flush operation.
514 // Mock the contextual process info - there would not be any
515 $manager->setProcessesForTesting([]);
516 $me = new CRM_Core_ManagedEntities($this->modules
);
518 $job = $me->get('com.example.one', 'Job');
519 $this->assertEquals(0, $job['is_active'], "Job that was manually set inactive should not have been set active again, but it was.");
520 $this->assertDBQuery(0, 'SELECT is_active FROM civicrm_job WHERE name = "test_job"');
522 // Now call reconcile again, but in the context of the job's extension being installed/enabled. This should re-enable the job.
523 foreach (['enable', 'install'] as $process) {
524 // Manually disable the job
525 $this->callAPISuccess('Job', 'create', ['id' => $job['id'], 'is_active' => 0]);
526 // Mock the contextual process info that would be added by CRM_Extension_Manager::enable
527 $manager->setProcessesForTesting(['com.example.one' => [$process]]);
528 $me = new CRM_Core_ManagedEntities($this->modules
);
530 $job = $me->get('com.example.one', 'Job');
531 $this->assertEquals(1, $job['is_active']);
532 $this->assertEquals('test_job', $job['name']);
533 $this->assertDBQuery(1, 'SELECT is_active FROM civicrm_job WHERE name = "test_job"');
537 $manager->setProcessesForTesting([]);
541 * Pre hook to test contact is not called on disable.
544 * @param string $objectName
545 * @param int|null $id
546 * @param array $params
548 public function preHook($op, $objectName, $id, $params): void
{
549 if ($op === 'edit' && $objectName === 'Individual') {
550 $this->assertArrayNotHasKey('is_active', $params);
555 * Setup an active module with an entity -- then entirely uninstall the
558 * @throws \CRM_Core_Exception
560 public function testUninstallModule(): void
{
561 $this->managedEntities
= [$this->fixtures
['com.example.one-foo']];
562 $me = new CRM_Core_ManagedEntities($this->modules
);
564 $foo = $me->get('com.example.one', 'foo');
565 $this->assertEquals('CRM_Example_One_Foo', $foo['name']);
566 $this->assertDBQuery(1, 'SELECT count(*) FROM civicrm_option_value WHERE name = "CRM_Example_One_Foo"');
568 // then destroy module
569 unset($this->modules
['one']);
570 $this->managedEntities
= [];
571 $me = new CRM_Core_ManagedEntities($this->modules
);
573 $fooNew = $me->get('com.example.one', 'foo');
574 $this->assertNull($fooNew);
575 $this->assertDBQuery(0, 'SELECT count(*) FROM civicrm_option_value WHERE name = "CRM_Example_One_Foo"');
579 * @throws \CRM_Core_Exception
581 public function testDependentEntitiesUninstallCleanly(): void
{
583 // Install a module with two dependent managed entities
584 $this->managedEntities
= [$this->fixtures
['com.example.one-CustomGroup']];
585 $this->managedEntities
[] = $this->fixtures
['com.example.one-CustomField'];
586 $me = new CRM_Core_ManagedEntities($this->modules
);
589 // Uninstall the module
590 unset($this->modules
['one']);
591 $this->managedEntities
= [];
592 $me = new CRM_Core_ManagedEntities($this->modules
);
595 // Ensure that no managed entities remain in the civicrm_managed
596 $this->assertDBQuery(0, 'SELECT count(*) FROM civicrm_managed');
598 // Ensure that com.example.one-CustomGroup is deleted
599 $this->assertDBQuery(0, 'SELECT count(*) FROM civicrm_custom_group WHERE name = "test_custom_group"');
601 // Ensure that com.example.one-CustomField is deleted
602 $this->assertDBQuery(0, 'SELECT count(*) FROM civicrm_custom_field WHERE name = "test_custom_field"');
607 * The hook_managed signature expanded slightly (adding the `$modules` filter).
608 * Pre-existing implementations may over-report (ie return entities despite the `$modules` filter).
609 * This test ensures that the framework respects the `$modules` filter (even if specific implementations don't).
611 public function testHookManaged_FilterModule() {
612 $this->managedEntities
= [
613 $this->fixtures
['com.example.one-bar'],
614 $this->fixtures
['com.example.two-CustomGroup'],
615 $this->fixtures
['com.example.three-CustomGroup'],
619 CRM_Utils_Hook
::managed($entitiesAll);
620 $this->assertEquals($this->managedEntities
, $entitiesAll);
621 $this->assertEquals(3, count($entitiesAll));
623 $entitiesTwoOnly = [];
624 CRM_Utils_Hook
::managed($entitiesTwoOnly, ['com.example.two']);
625 $this->assertEquals([$this->fixtures
['com.example.two-CustomGroup']], array_values($entitiesTwoOnly));
626 $this->assertEquals(1, count($entitiesTwoOnly));
628 $entitiesTwoExtra = [];
629 CRM_Utils_Hook
::managed($entitiesTwoExtra, ['com.example.two', 'com.example.extra']);
630 $this->assertEquals([$this->fixtures
['com.example.two-CustomGroup']], array_values($entitiesTwoExtra));
631 $this->assertEquals(1, count($entitiesTwoExtra));
633 $entitiesTwoThree = [];
634 CRM_Utils_Hook
::managed($entitiesTwoThree, ['com.example.two', 'com.example.three']);
635 $this->assertEquals([$this->fixtures
['com.example.two-CustomGroup'], $this->fixtures
['com.example.three-CustomGroup']], array_values($entitiesTwoThree));
636 $this->assertEquals(2, count($entitiesTwoThree));