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',
57 $this->apiKernel
= \Civi
::service('civi_api_kernel');
58 $this->adhocProvider
= new \Civi\API\Provider\
AdhocProvider(3, 'CustomSearch');
59 $this->apiKernel
->registerApiProvider($this->adhocProvider
);
62 public function tearDown() {
68 * Set up an active module and, over time, the hook implementation changes
69 * to (1) create 'foo' entity, (2) create 'bar' entity', (3) remove 'foo'
72 public function testAddRemoveEntitiesModule_UpdateAlways_DeleteAlways() {
75 // create first managed entity ('foo')
76 $decls[] = $this->fixtures
['com.example.one-foo'];
77 $me = new CRM_Core_ManagedEntities($this->modules
, $decls);
79 $foo = $me->get('com.example.one', 'foo');
80 $this->assertEquals('CRM_Example_One_Foo', $foo['name']);
81 $this->assertDBQuery(1, 'SELECT count(*) FROM civicrm_option_value WHERE name = "CRM_Example_One_Foo"');
83 // later on, hook returns an extra managed entity ('bar')
84 $decls[] = $this->fixtures
['com.example.one-bar'];
85 $me = new CRM_Core_ManagedEntities($this->modules
, $decls);
87 $foo = $me->get('com.example.one', 'foo');
88 $this->assertEquals('CRM_Example_One_Foo', $foo['name']);
89 $this->assertDBQuery(1, 'SELECT count(*) FROM civicrm_option_value WHERE name = "CRM_Example_One_Foo"');
90 $bar = $me->get('com.example.one', 'bar');
91 $this->assertEquals('CRM_Example_One_Bar', $bar['name']);
92 $this->assertDBQuery(1, 'SELECT count(*) FROM civicrm_option_value WHERE name = "CRM_Example_One_Bar"');
94 // and then hook changes its mind, removing 'foo' (first of two entities)
96 $me = new CRM_Core_ManagedEntities($this->modules
, $decls);
98 $foo = $me->get('com.example.one', 'foo');
99 $this->assertTrue($foo === NULL);
100 $this->assertDBQuery(0, 'SELECT count(*) FROM civicrm_option_value WHERE name = "CRM_Example_One_Foo"');
101 $bar = $me->get('com.example.one', 'bar');
102 $this->assertEquals('CRM_Example_One_Bar', $bar['name']);
103 $this->assertDBQuery(1, 'SELECT count(*) FROM civicrm_option_value WHERE name = "CRM_Example_One_Bar"');
105 // and then hook changes its mind, removing 'bar' (the last remaining entity)
107 $me = new CRM_Core_ManagedEntities($this->modules
, $decls);
109 $foo = $me->get('com.example.one', 'foo');
110 $this->assertTrue($foo === NULL);
111 $this->assertDBQuery(0, 'SELECT count(*) FROM civicrm_option_value WHERE name = "CRM_Example_One_Foo"');
112 $bar = $me->get('com.example.one', 'bar');
113 $this->assertTrue($bar === NULL);
114 $this->assertDBQuery(0, 'SELECT count(*) FROM civicrm_option_value WHERE name = "CRM_Example_One_Bar"');
118 * Set up an active module with one managed-entity and, over
119 * time, the content of the entity changes
121 public function testModifyDeclaration_UpdateAlways() {
124 // create first managed entity ('foo')
125 $decls[] = $this->fixtures
['com.example.one-foo'];
126 $me = new CRM_Core_ManagedEntities($this->modules
, $decls);
128 $foo = $me->get('com.example.one', 'foo');
129 $this->assertEquals('CRM_Example_One_Foo', $foo['name']);
130 $this->assertDBQuery(1, 'SELECT count(*) FROM civicrm_option_value WHERE name = "CRM_Example_One_Foo"');
132 // later on, hook specification changes
133 $decls[0]['params']['class_name'] = 'CRM_Example_One_Foobar';
134 $me = new CRM_Core_ManagedEntities($this->modules
, $decls);
136 $foo2 = $me->get('com.example.one', 'foo');
137 $this->assertEquals('CRM_Example_One_Foobar', $foo2['name']);
138 $this->assertDBQuery(0, 'SELECT count(*) FROM civicrm_option_value WHERE name = "CRM_Example_One_Foo"');
139 $this->assertDBQuery(1, 'SELECT count(*) FROM civicrm_option_value WHERE name = "CRM_Example_One_FooBar"');
140 $this->assertEquals($foo['id'], $foo2['id']);
144 * Set up an active module with one managed-entity and, over
145 * time, the content of the entity changes
147 public function testModifyDeclaration_UpdateNever() {
150 // create first managed entity ('foo')
151 $decls[] = array_merge($this->fixtures
['com.example.one-foo'], array(
152 'update' => 'never', // Policy is to never update after initial creation
154 $me = new CRM_Core_ManagedEntities($this->modules
, $decls);
156 $foo = $me->get('com.example.one', 'foo');
157 $this->assertEquals('CRM_Example_One_Foo', $foo['name']);
158 $this->assertDBQuery(1, 'SELECT count(*) FROM civicrm_option_value WHERE name = "CRM_Example_One_Foo"');
160 // later on, hook specification changes
161 $decls[0]['params']['class_name'] = 'CRM_Example_One_Foobar';
162 $me = new CRM_Core_ManagedEntities($this->modules
, $decls);
164 $foo2 = $me->get('com.example.one', 'foo');
165 $this->assertEquals('CRM_Example_One_Foo', $foo2['name']);
166 $this->assertDBQuery(1, 'SELECT count(*) FROM civicrm_option_value WHERE name = "CRM_Example_One_Foo"');
167 $this->assertDBQuery(0, 'SELECT count(*) FROM civicrm_option_value WHERE name = "CRM_Example_One_FooBar"');
168 $this->assertEquals($foo['id'], $foo2['id']);
172 * Set up an active module with one managed-entity using the
173 * policy "cleanup=>never". When the managed-entity goes away,
174 * ensure that the policy is followed (ie the entity is not
177 public function testRemoveDeclaration_CleanupNever() {
180 // create first managed entity ('foo')
181 $decls[] = array_merge($this->fixtures
['com.example.one-foo'], array(
182 'cleanup' => 'never',
184 $me = new CRM_Core_ManagedEntities($this->modules
, $decls);
186 $foo = $me->get('com.example.one', 'foo');
187 $this->assertEquals('CRM_Example_One_Foo', $foo['name']);
188 $this->assertDBQuery(1, 'SELECT count(*) FROM civicrm_option_value WHERE name = "CRM_Example_One_Foo"');
190 // later on, entity definition disappears; but we decide not to do any cleanup (per policy)
192 $me = new CRM_Core_ManagedEntities($this->modules
, $decls);
194 $foo2 = $me->get('com.example.one', 'foo');
195 $this->assertEquals('CRM_Example_One_Foo', $foo2['name']);
196 $this->assertDBQuery(1, 'SELECT count(*) FROM civicrm_option_value WHERE name = "CRM_Example_One_Foo"');
197 $this->assertEquals($foo['id'], $foo2['id']);
201 * Set up an active module with one managed-entity using the
202 * policy "cleanup=>never". When the managed-entity goes away,
203 * ensure that the policy is followed (ie the entity is not
206 public function testRemoveDeclaration_CleanupUnused() {
209 // create first managed entity ('foo')
210 $decls[] = array_merge($this->fixtures
['com.example.one-foo'], array(
211 'cleanup' => 'unused',
213 $me = new CRM_Core_ManagedEntities($this->modules
, $decls);
215 $foo = $me->get('com.example.one', 'foo');
216 $this->assertEquals('CRM_Example_One_Foo', $foo['name']);
217 $this->assertDBQuery(1, 'SELECT count(*) FROM civicrm_option_value WHERE name = "CRM_Example_One_Foo"');
219 // Override 'getrefcount' ==> The refcount is 1
220 $this->adhocProvider
->addAction('getrefcount', 'access CiviCRM', function ($apiRequest) {
221 return civicrm_api3_create_success(array(
230 // Later on, entity definition disappears; but we decide not to do any cleanup (per policy)
232 $me = new CRM_Core_ManagedEntities($this->modules
, $decls);
234 $foo2 = $me->get('com.example.one', 'foo');
235 $this->assertEquals('CRM_Example_One_Foo', $foo2['name']);
236 $this->assertDBQuery(1, 'SELECT count(*) FROM civicrm_option_value WHERE name = "CRM_Example_One_Foo"');
237 $this->assertEquals($foo['id'], $foo2['id']);
239 // Override 'getrefcount' ==> The refcount is 0
240 $this->adhocProvider
->addAction('getrefcount', 'access CiviCRM', function ($apiRequest) {
241 return civicrm_api3_create_success(array());
244 // The entity definition disappeared and there's no reference; we decide to cleanup (per policy)
246 $me = new CRM_Core_ManagedEntities($this->modules
, $decls);
248 $foo3 = $me->get('com.example.one', 'foo');
249 $this->assertDBQuery(0, 'SELECT count(*) FROM civicrm_option_value WHERE name = "CRM_Example_One_Foo"');
250 $this->assertTrue($foo3 === NULL);
254 * Setup an active module with a malformed entity declaration.
256 public function testInvalidDeclarationModule() {
257 // create first managed entity ('foo')
260 'module' => 'com.example.unknown', // erroneous
262 'entity' => 'CustomSearch',
265 'class_name' => 'CRM_Example_One_Foo',
269 $me = new CRM_Core_ManagedEntities($this->modules
, $decls);
272 $this->fail('Expected exception when using invalid declaration');
274 catch (Exception
$e) {
280 * Setup an active module with a malformed entity declaration.
282 public function testMissingName() {
283 // create first managed entity ('foo')
286 'module' => 'com.example.unknown',
287 'name' => NULL, // erroneous
288 'entity' => 'CustomSearch',
291 'class_name' => 'CRM_Example_One_Foo',
295 $me = new CRM_Core_ManagedEntities($this->modules
, $decls);
298 $this->fail('Expected exception when using invalid declaration');
300 catch (Exception
$e) {
306 * Setup an active module with a malformed entity declaration.
308 public function testMissingEntity() {
309 // create first managed entity ('foo')
312 'module' => 'com.example.unknown',
314 'entity' => NULL, // erroneous
317 'class_name' => 'CRM_Example_One_Foo',
321 $me = new CRM_Core_ManagedEntities($this->modules
, $decls);
324 $this->fail('Expected exception when using invalid declaration');
326 catch (Exception
$e) {
332 * Setup an active module with an entity -- then disable and re-enable the
335 public function testDeactivateReactivateModule() {
336 // create first managed entity ('foo')
338 $decls[] = $this->fixtures
['com.example.one-foo'];
339 $me = new CRM_Core_ManagedEntities($this->modules
, $decls);
341 $foo = $me->get('com.example.one', 'foo');
342 $this->assertEquals(1, $foo['is_active']);
343 $this->assertEquals('CRM_Example_One_Foo', $foo['name']);
344 $this->assertDBQuery(1, 'SELECT is_active FROM civicrm_option_value WHERE name = "CRM_Example_One_Foo"');
346 // now deactivate module, which has empty decls and which cascades to managed object
347 $this->modules
['one']->is_active
= FALSE;
348 $me = new CRM_Core_ManagedEntities($this->modules
, array());
350 $foo = $me->get('com.example.one', 'foo');
351 $this->assertEquals(0, $foo['is_active']);
352 $this->assertEquals('CRM_Example_One_Foo', $foo['name']);
353 $this->assertDBQuery(0, 'SELECT is_active FROM civicrm_option_value WHERE name = "CRM_Example_One_Foo"');
355 // and reactivate module, which again provides decls and which cascades to managed object
356 $this->modules
['one']->is_active
= TRUE;
357 $me = new CRM_Core_ManagedEntities($this->modules
, $decls);
359 $foo = $me->get('com.example.one', 'foo');
360 $this->assertEquals(1, $foo['is_active']);
361 $this->assertEquals('CRM_Example_One_Foo', $foo['name']);
362 $this->assertDBQuery(1, 'SELECT is_active FROM civicrm_option_value WHERE name = "CRM_Example_One_Foo"');
366 * Setup an active module with an entity -- then entirely uninstall the
369 public function testUninstallModule() {
370 // create first managed entity ('foo')
372 $decls[] = $this->fixtures
['com.example.one-foo'];
373 $me = new CRM_Core_ManagedEntities($this->modules
, $decls);
375 $foo = $me->get('com.example.one', 'foo');
376 $this->assertEquals('CRM_Example_One_Foo', $foo['name']);
377 $this->assertDBQuery(1, 'SELECT count(*) FROM civicrm_option_value WHERE name = "CRM_Example_One_Foo"');
379 // then destroy module; note that decls go away
380 unset($this->modules
['one']);
381 $me = new CRM_Core_ManagedEntities($this->modules
, array());
383 $fooNew = $me->get('com.example.one', 'foo');
384 $this->assertTrue(NULL === $fooNew);
385 $this->assertDBQuery(0, 'SELECT count(*) FROM civicrm_option_value WHERE name = "CRM_Example_One_Foo"');