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;
19 * @var array(string $shortName => CRM_Core_Module $module)
25 public function setUp() {
26 $this->useTransaction(TRUE);
28 $this->modules
= array(
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'] = array(
37 'module' => 'com.example.one',
39 'entity' => 'CustomSearch',
42 'class_name' => 'CRM_Example_One_Foo',
46 $this->fixtures
['com.example.one-bar'] = array(
47 'module' => 'com.example.one',
49 'entity' => 'CustomSearch',
52 'class_name' => 'CRM_Example_One_Bar',
56 $this->fixtures
['com.example.one-CustomGroup'] = array(
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'] = array(
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->apiKernel
= \Civi
::service('civi_api_kernel');
82 $this->adhocProvider
= new \Civi\API\Provider\
AdhocProvider(3, 'CustomSearch');
83 $this->apiKernel
->registerApiProvider($this->adhocProvider
);
86 public function tearDown() {
92 * Set up an active module and, over time, the hook implementation changes
93 * to (1) create 'foo' entity, (2) create 'bar' entity', (3) remove 'foo'
96 public function testAddRemoveEntitiesModule_UpdateAlways_DeleteAlways() {
99 // create first managed entity ('foo')
100 $decls[] = $this->fixtures
['com.example.one-foo'];
101 $me = new CRM_Core_ManagedEntities($this->modules
, $decls);
103 $foo = $me->get('com.example.one', 'foo');
104 $this->assertEquals('CRM_Example_One_Foo', $foo['name']);
105 $this->assertDBQuery(1, 'SELECT count(*) FROM civicrm_option_value WHERE name = "CRM_Example_One_Foo"');
107 // later on, hook returns an extra managed entity ('bar')
108 $decls[] = $this->fixtures
['com.example.one-bar'];
109 $me = new CRM_Core_ManagedEntities($this->modules
, $decls);
111 $foo = $me->get('com.example.one', 'foo');
112 $this->assertEquals('CRM_Example_One_Foo', $foo['name']);
113 $this->assertDBQuery(1, 'SELECT count(*) FROM civicrm_option_value WHERE name = "CRM_Example_One_Foo"');
114 $bar = $me->get('com.example.one', 'bar');
115 $this->assertEquals('CRM_Example_One_Bar', $bar['name']);
116 $this->assertDBQuery(1, 'SELECT count(*) FROM civicrm_option_value WHERE name = "CRM_Example_One_Bar"');
118 // and then hook changes its mind, removing 'foo' (first of two entities)
120 $me = new CRM_Core_ManagedEntities($this->modules
, $decls);
122 $foo = $me->get('com.example.one', 'foo');
123 $this->assertTrue($foo === NULL);
124 $this->assertDBQuery(0, 'SELECT count(*) FROM civicrm_option_value WHERE name = "CRM_Example_One_Foo"');
125 $bar = $me->get('com.example.one', 'bar');
126 $this->assertEquals('CRM_Example_One_Bar', $bar['name']);
127 $this->assertDBQuery(1, 'SELECT count(*) FROM civicrm_option_value WHERE name = "CRM_Example_One_Bar"');
129 // and then hook changes its mind, removing 'bar' (the last remaining entity)
131 $me = new CRM_Core_ManagedEntities($this->modules
, $decls);
133 $foo = $me->get('com.example.one', 'foo');
134 $this->assertTrue($foo === NULL);
135 $this->assertDBQuery(0, 'SELECT count(*) FROM civicrm_option_value WHERE name = "CRM_Example_One_Foo"');
136 $bar = $me->get('com.example.one', 'bar');
137 $this->assertTrue($bar === NULL);
138 $this->assertDBQuery(0, 'SELECT count(*) FROM civicrm_option_value WHERE name = "CRM_Example_One_Bar"');
142 * Set up an active module with one managed-entity and, over
143 * time, the content of the entity changes
145 public function testModifyDeclaration_UpdateAlways() {
148 // create first managed entity ('foo')
149 $decls[] = $this->fixtures
['com.example.one-foo'];
150 $me = new CRM_Core_ManagedEntities($this->modules
, $decls);
152 $foo = $me->get('com.example.one', 'foo');
153 $this->assertEquals('CRM_Example_One_Foo', $foo['name']);
154 $this->assertDBQuery(1, 'SELECT count(*) FROM civicrm_option_value WHERE name = "CRM_Example_One_Foo"');
156 // later on, hook specification changes
157 $decls[0]['params']['class_name'] = 'CRM_Example_One_Foobar';
158 $me = new CRM_Core_ManagedEntities($this->modules
, $decls);
160 $foo2 = $me->get('com.example.one', 'foo');
161 $this->assertEquals('CRM_Example_One_Foobar', $foo2['name']);
162 $this->assertDBQuery(0, 'SELECT count(*) FROM civicrm_option_value WHERE name = "CRM_Example_One_Foo"');
163 $this->assertDBQuery(1, 'SELECT count(*) FROM civicrm_option_value WHERE name = "CRM_Example_One_FooBar"');
164 $this->assertEquals($foo['id'], $foo2['id']);
168 * Set up an active module with one managed-entity and, over
169 * time, the content of the entity changes
171 public function testModifyDeclaration_UpdateNever() {
174 // create first managed entity ('foo')
175 $decls[] = array_merge($this->fixtures
['com.example.one-foo'], array(
176 'update' => 'never', // Policy is to never update after initial creation
178 $me = new CRM_Core_ManagedEntities($this->modules
, $decls);
180 $foo = $me->get('com.example.one', 'foo');
181 $this->assertEquals('CRM_Example_One_Foo', $foo['name']);
182 $this->assertDBQuery(1, 'SELECT count(*) FROM civicrm_option_value WHERE name = "CRM_Example_One_Foo"');
184 // later on, hook specification changes
185 $decls[0]['params']['class_name'] = 'CRM_Example_One_Foobar';
186 $me = new CRM_Core_ManagedEntities($this->modules
, $decls);
188 $foo2 = $me->get('com.example.one', 'foo');
189 $this->assertEquals('CRM_Example_One_Foo', $foo2['name']);
190 $this->assertDBQuery(1, 'SELECT count(*) FROM civicrm_option_value WHERE name = "CRM_Example_One_Foo"');
191 $this->assertDBQuery(0, 'SELECT count(*) FROM civicrm_option_value WHERE name = "CRM_Example_One_FooBar"');
192 $this->assertEquals($foo['id'], $foo2['id']);
196 * Set up an active module with one managed-entity using the
197 * policy "cleanup=>never". When the managed-entity goes away,
198 * ensure that the policy is followed (ie the entity is not
201 public function testRemoveDeclaration_CleanupNever() {
204 // create first managed entity ('foo')
205 $decls[] = array_merge($this->fixtures
['com.example.one-foo'], array(
206 'cleanup' => 'never',
208 $me = new CRM_Core_ManagedEntities($this->modules
, $decls);
210 $foo = $me->get('com.example.one', 'foo');
211 $this->assertEquals('CRM_Example_One_Foo', $foo['name']);
212 $this->assertDBQuery(1, 'SELECT count(*) FROM civicrm_option_value WHERE name = "CRM_Example_One_Foo"');
214 // later on, entity definition disappears; but we decide not to do any cleanup (per policy)
216 $me = new CRM_Core_ManagedEntities($this->modules
, $decls);
218 $foo2 = $me->get('com.example.one', 'foo');
219 $this->assertEquals('CRM_Example_One_Foo', $foo2['name']);
220 $this->assertDBQuery(1, 'SELECT count(*) FROM civicrm_option_value WHERE name = "CRM_Example_One_Foo"');
221 $this->assertEquals($foo['id'], $foo2['id']);
225 * Set up an active module with one managed-entity using the
226 * policy "cleanup=>never". When the managed-entity goes away,
227 * ensure that the policy is followed (ie the entity is not
230 public function testRemoveDeclaration_CleanupUnused() {
233 // create first managed entity ('foo')
234 $decls[] = array_merge($this->fixtures
['com.example.one-foo'], array(
235 'cleanup' => 'unused',
237 $me = new CRM_Core_ManagedEntities($this->modules
, $decls);
239 $foo = $me->get('com.example.one', 'foo');
240 $this->assertEquals('CRM_Example_One_Foo', $foo['name']);
241 $this->assertDBQuery(1, 'SELECT count(*) FROM civicrm_option_value WHERE name = "CRM_Example_One_Foo"');
243 // Override 'getrefcount' ==> The refcount is 1
244 $this->adhocProvider
->addAction('getrefcount', 'access CiviCRM', function ($apiRequest) {
245 return civicrm_api3_create_success(array(
254 // Later on, entity definition disappears; but we decide not to do any cleanup (per policy)
256 $me = new CRM_Core_ManagedEntities($this->modules
, $decls);
258 $foo2 = $me->get('com.example.one', 'foo');
259 $this->assertEquals('CRM_Example_One_Foo', $foo2['name']);
260 $this->assertDBQuery(1, 'SELECT count(*) FROM civicrm_option_value WHERE name = "CRM_Example_One_Foo"');
261 $this->assertEquals($foo['id'], $foo2['id']);
263 // Override 'getrefcount' ==> The refcount is 0
264 $this->adhocProvider
->addAction('getrefcount', 'access CiviCRM', function ($apiRequest) {
265 return civicrm_api3_create_success(array());
268 // The entity definition disappeared and there's no reference; we decide to cleanup (per policy)
270 $me = new CRM_Core_ManagedEntities($this->modules
, $decls);
272 $foo3 = $me->get('com.example.one', 'foo');
273 $this->assertDBQuery(0, 'SELECT count(*) FROM civicrm_option_value WHERE name = "CRM_Example_One_Foo"');
274 $this->assertTrue($foo3 === NULL);
278 * Setup an active module with a malformed entity declaration.
280 public function testInvalidDeclarationModule() {
281 // create first managed entity ('foo')
284 'module' => 'com.example.unknown', // erroneous
286 'entity' => 'CustomSearch',
289 'class_name' => 'CRM_Example_One_Foo',
293 $me = new CRM_Core_ManagedEntities($this->modules
, $decls);
296 $this->fail('Expected exception when using invalid declaration');
298 catch (Exception
$e) {
304 * Setup an active module with a malformed entity declaration.
306 public function testMissingName() {
307 // create first managed entity ('foo')
310 'module' => 'com.example.unknown',
311 'name' => NULL, // erroneous
312 'entity' => 'CustomSearch',
315 'class_name' => 'CRM_Example_One_Foo',
319 $me = new CRM_Core_ManagedEntities($this->modules
, $decls);
322 $this->fail('Expected exception when using invalid declaration');
324 catch (Exception
$e) {
330 * Setup an active module with a malformed entity declaration.
332 public function testMissingEntity() {
333 // create first managed entity ('foo')
336 'module' => 'com.example.unknown',
338 'entity' => NULL, // erroneous
341 'class_name' => 'CRM_Example_One_Foo',
345 $me = new CRM_Core_ManagedEntities($this->modules
, $decls);
348 $this->fail('Expected exception when using invalid declaration');
350 catch (Exception
$e) {
356 * Setup an active module with an entity -- then disable and re-enable the
359 public function testDeactivateReactivateModule() {
360 // create first managed entity ('foo')
362 $decls[] = $this->fixtures
['com.example.one-foo'];
363 $me = new CRM_Core_ManagedEntities($this->modules
, $decls);
365 $foo = $me->get('com.example.one', 'foo');
366 $this->assertEquals(1, $foo['is_active']);
367 $this->assertEquals('CRM_Example_One_Foo', $foo['name']);
368 $this->assertDBQuery(1, 'SELECT is_active FROM civicrm_option_value WHERE name = "CRM_Example_One_Foo"');
370 // now deactivate module, which has empty decls and which cascades to managed object
371 $this->modules
['one']->is_active
= FALSE;
372 $me = new CRM_Core_ManagedEntities($this->modules
, array());
374 $foo = $me->get('com.example.one', 'foo');
375 $this->assertEquals(0, $foo['is_active']);
376 $this->assertEquals('CRM_Example_One_Foo', $foo['name']);
377 $this->assertDBQuery(0, 'SELECT is_active FROM civicrm_option_value WHERE name = "CRM_Example_One_Foo"');
379 // and reactivate module, which again provides decls and which cascades to managed object
380 $this->modules
['one']->is_active
= TRUE;
381 $me = new CRM_Core_ManagedEntities($this->modules
, $decls);
383 $foo = $me->get('com.example.one', 'foo');
384 $this->assertEquals(1, $foo['is_active']);
385 $this->assertEquals('CRM_Example_One_Foo', $foo['name']);
386 $this->assertDBQuery(1, 'SELECT is_active FROM civicrm_option_value WHERE name = "CRM_Example_One_Foo"');
390 * Setup an active module with an entity -- then entirely uninstall the
393 public function testUninstallModule() {
394 // create first managed entity ('foo')
396 $decls[] = $this->fixtures
['com.example.one-foo'];
397 $me = new CRM_Core_ManagedEntities($this->modules
, $decls);
399 $foo = $me->get('com.example.one', 'foo');
400 $this->assertEquals('CRM_Example_One_Foo', $foo['name']);
401 $this->assertDBQuery(1, 'SELECT count(*) FROM civicrm_option_value WHERE name = "CRM_Example_One_Foo"');
403 // then destroy module; note that decls go away
404 unset($this->modules
['one']);
405 $me = new CRM_Core_ManagedEntities($this->modules
, array());
407 $fooNew = $me->get('com.example.one', 'foo');
408 $this->assertTrue(NULL === $fooNew);
409 $this->assertDBQuery(0, 'SELECT count(*) FROM civicrm_option_value WHERE name = "CRM_Example_One_Foo"');
412 public function testDependentEntitiesUninstallCleanly() {
414 // Install a module with two dependent managed entities
416 $decls[] = $this->fixtures
['com.example.one-CustomGroup'];
417 $decls[] = $this->fixtures
['com.example.one-CustomField'];
418 $me = new CRM_Core_ManagedEntities($this->modules
, $decls);
421 // Uninstall the module
422 unset($this->modules
['one']);
423 $me = new CRM_Core_ManagedEntities($this->modules
, []);
426 // Ensure that no managed entities remain in the civicrm_managed
427 $this->assertDBQuery(0, 'SELECT count(*) FROM civicrm_managed');
429 // Ensure that com.example.one-CustomGroup is deleted
430 $this->assertDBQuery(0, 'SELECT count(*) FROM civicrm_custom_group WHERE name = "test_custom_group"');
432 // Ensure that com.example.one-CustomField is deleted
433 $this->assertDBQuery(0, 'SELECT count(*) FROM civicrm_custom_field WHERE name = "test_custom_field"');