4 * Class CRM_Core_ManagedEntitiesTest
6 class CRM_Core_ManagedEntitiesTest
extends CiviUnitTestCase
{
8 * @var \Civi\API\Kernel
13 * @var \Civi\API\Provider\AdhocProvider
15 protected $adhocProvider;
18 * @var array(string $shortName => CRM_Core_Module $module)
24 public function setUp() {
25 $this->useTransaction(TRUE);
27 $this->modules
= array(
28 'one' => new CRM_Core_Module('com.example.one', TRUE),
29 'two' => new CRM_Core_Module('com.example.two', TRUE),
32 // Testing on drupal-demo fails because some extensions have mgd ents.
33 CRM_Core_DAO
::singleValueQuery('DELETE FROM civicrm_managed');
35 $this->fixtures
['com.example.one-foo'] = array(
36 'module' => 'com.example.one',
38 'entity' => 'CustomSearch',
41 'class_name' => 'CRM_Example_One_Foo',
45 $this->fixtures
['com.example.one-bar'] = array(
46 'module' => 'com.example.one',
48 'entity' => 'CustomSearch',
51 'class_name' => 'CRM_Example_One_Bar',
56 $this->apiKernel
= \Civi
::service('civi_api_kernel');
57 $this->adhocProvider
= new \Civi\API\Provider\
AdhocProvider(3, 'CustomSearch');
58 $this->apiKernel
->registerApiProvider($this->adhocProvider
);
61 public function tearDown() {
67 * Set up an active module and, over time, the hook implementation changes
68 * to (1) create 'foo' entity, (2) create 'bar' entity', (3) remove 'foo'
71 public function testAddRemoveEntitiesModule_UpdateAlways_DeleteAlways() {
74 // create first managed entity ('foo')
75 $decls[] = $this->fixtures
['com.example.one-foo'];
76 $me = new CRM_Core_ManagedEntities($this->modules
, $decls);
78 $foo = $me->get('com.example.one', 'foo');
79 $this->assertEquals('CRM_Example_One_Foo', $foo['name']);
80 $this->assertDBQuery(1, 'SELECT count(*) FROM civicrm_option_value WHERE name = "CRM_Example_One_Foo"');
82 // later on, hook returns an extra managed entity ('bar')
83 $decls[] = $this->fixtures
['com.example.one-bar'];
84 $me = new CRM_Core_ManagedEntities($this->modules
, $decls);
86 $foo = $me->get('com.example.one', 'foo');
87 $this->assertEquals('CRM_Example_One_Foo', $foo['name']);
88 $this->assertDBQuery(1, 'SELECT count(*) FROM civicrm_option_value WHERE name = "CRM_Example_One_Foo"');
89 $bar = $me->get('com.example.one', 'bar');
90 $this->assertEquals('CRM_Example_One_Bar', $bar['name']);
91 $this->assertDBQuery(1, 'SELECT count(*) FROM civicrm_option_value WHERE name = "CRM_Example_One_Bar"');
93 // and then hook changes its mind, removing 'foo' (first of two entities)
95 $me = new CRM_Core_ManagedEntities($this->modules
, $decls);
97 $foo = $me->get('com.example.one', 'foo');
98 $this->assertTrue($foo === NULL);
99 $this->assertDBQuery(0, 'SELECT count(*) FROM civicrm_option_value WHERE name = "CRM_Example_One_Foo"');
100 $bar = $me->get('com.example.one', 'bar');
101 $this->assertEquals('CRM_Example_One_Bar', $bar['name']);
102 $this->assertDBQuery(1, 'SELECT count(*) FROM civicrm_option_value WHERE name = "CRM_Example_One_Bar"');
104 // and then hook changes its mind, removing 'bar' (the last remaining entity)
106 $me = new CRM_Core_ManagedEntities($this->modules
, $decls);
108 $foo = $me->get('com.example.one', 'foo');
109 $this->assertTrue($foo === NULL);
110 $this->assertDBQuery(0, 'SELECT count(*) FROM civicrm_option_value WHERE name = "CRM_Example_One_Foo"');
111 $bar = $me->get('com.example.one', 'bar');
112 $this->assertTrue($bar === NULL);
113 $this->assertDBQuery(0, 'SELECT count(*) FROM civicrm_option_value WHERE name = "CRM_Example_One_Bar"');
117 * Set up an active module with one managed-entity and, over
118 * time, the content of the entity changes
120 public function testModifyDeclaration_UpdateAlways() {
123 // create first managed entity ('foo')
124 $decls[] = $this->fixtures
['com.example.one-foo'];
125 $me = new CRM_Core_ManagedEntities($this->modules
, $decls);
127 $foo = $me->get('com.example.one', 'foo');
128 $this->assertEquals('CRM_Example_One_Foo', $foo['name']);
129 $this->assertDBQuery(1, 'SELECT count(*) FROM civicrm_option_value WHERE name = "CRM_Example_One_Foo"');
131 // later on, hook specification changes
132 $decls[0]['params']['class_name'] = 'CRM_Example_One_Foobar';
133 $me = new CRM_Core_ManagedEntities($this->modules
, $decls);
135 $foo2 = $me->get('com.example.one', 'foo');
136 $this->assertEquals('CRM_Example_One_Foobar', $foo2['name']);
137 $this->assertDBQuery(0, 'SELECT count(*) FROM civicrm_option_value WHERE name = "CRM_Example_One_Foo"');
138 $this->assertDBQuery(1, 'SELECT count(*) FROM civicrm_option_value WHERE name = "CRM_Example_One_FooBar"');
139 $this->assertEquals($foo['id'], $foo2['id']);
143 * Set up an active module with one managed-entity and, over
144 * time, the content of the entity changes
146 public function testModifyDeclaration_UpdateNever() {
149 // create first managed entity ('foo')
150 $decls[] = array_merge($this->fixtures
['com.example.one-foo'], array(
151 'update' => 'never', // Policy is to never update after initial creation
153 $me = new CRM_Core_ManagedEntities($this->modules
, $decls);
155 $foo = $me->get('com.example.one', 'foo');
156 $this->assertEquals('CRM_Example_One_Foo', $foo['name']);
157 $this->assertDBQuery(1, 'SELECT count(*) FROM civicrm_option_value WHERE name = "CRM_Example_One_Foo"');
159 // later on, hook specification changes
160 $decls[0]['params']['class_name'] = 'CRM_Example_One_Foobar';
161 $me = new CRM_Core_ManagedEntities($this->modules
, $decls);
163 $foo2 = $me->get('com.example.one', 'foo');
164 $this->assertEquals('CRM_Example_One_Foo', $foo2['name']);
165 $this->assertDBQuery(1, 'SELECT count(*) FROM civicrm_option_value WHERE name = "CRM_Example_One_Foo"');
166 $this->assertDBQuery(0, 'SELECT count(*) FROM civicrm_option_value WHERE name = "CRM_Example_One_FooBar"');
167 $this->assertEquals($foo['id'], $foo2['id']);
171 * Set up an active module with one managed-entity using the
172 * policy "cleanup=>never". When the managed-entity goes away,
173 * ensure that the policy is followed (ie the entity is not
176 public function testRemoveDeclaration_CleanupNever() {
179 // create first managed entity ('foo')
180 $decls[] = array_merge($this->fixtures
['com.example.one-foo'], array(
181 'cleanup' => 'never',
183 $me = new CRM_Core_ManagedEntities($this->modules
, $decls);
185 $foo = $me->get('com.example.one', 'foo');
186 $this->assertEquals('CRM_Example_One_Foo', $foo['name']);
187 $this->assertDBQuery(1, 'SELECT count(*) FROM civicrm_option_value WHERE name = "CRM_Example_One_Foo"');
189 // later on, entity definition disappears; but we decide not to do any cleanup (per policy)
191 $me = new CRM_Core_ManagedEntities($this->modules
, $decls);
193 $foo2 = $me->get('com.example.one', 'foo');
194 $this->assertEquals('CRM_Example_One_Foo', $foo2['name']);
195 $this->assertDBQuery(1, 'SELECT count(*) FROM civicrm_option_value WHERE name = "CRM_Example_One_Foo"');
196 $this->assertEquals($foo['id'], $foo2['id']);
200 * Set up an active module with one managed-entity using the
201 * policy "cleanup=>never". When the managed-entity goes away,
202 * ensure that the policy is followed (ie the entity is not
205 public function testRemoveDeclaration_CleanupUnused() {
208 // create first managed entity ('foo')
209 $decls[] = array_merge($this->fixtures
['com.example.one-foo'], array(
210 'cleanup' => 'unused',
212 $me = new CRM_Core_ManagedEntities($this->modules
, $decls);
214 $foo = $me->get('com.example.one', 'foo');
215 $this->assertEquals('CRM_Example_One_Foo', $foo['name']);
216 $this->assertDBQuery(1, 'SELECT count(*) FROM civicrm_option_value WHERE name = "CRM_Example_One_Foo"');
218 // Override 'getrefcount' ==> The refcount is 1
219 $this->adhocProvider
->addAction('getrefcount', 'access CiviCRM', function ($apiRequest) {
220 return civicrm_api3_create_success(array(
229 // Later on, entity definition disappears; but we decide not to do any cleanup (per policy)
231 $me = new CRM_Core_ManagedEntities($this->modules
, $decls);
233 $foo2 = $me->get('com.example.one', 'foo');
234 $this->assertEquals('CRM_Example_One_Foo', $foo2['name']);
235 $this->assertDBQuery(1, 'SELECT count(*) FROM civicrm_option_value WHERE name = "CRM_Example_One_Foo"');
236 $this->assertEquals($foo['id'], $foo2['id']);
238 // Override 'getrefcount' ==> The refcount is 0
239 $this->adhocProvider
->addAction('getrefcount', 'access CiviCRM', function ($apiRequest) {
240 return civicrm_api3_create_success(array());
243 // The entity definition disappeared and there's no reference; we decide to cleanup (per policy)
245 $me = new CRM_Core_ManagedEntities($this->modules
, $decls);
247 $foo3 = $me->get('com.example.one', 'foo');
248 $this->assertDBQuery(0, 'SELECT count(*) FROM civicrm_option_value WHERE name = "CRM_Example_One_Foo"');
249 $this->assertTrue($foo3 === NULL);
253 * Setup an active module with a malformed entity declaration.
255 public function testInvalidDeclarationModule() {
256 // create first managed entity ('foo')
259 'module' => 'com.example.unknown', // erroneous
261 'entity' => 'CustomSearch',
264 'class_name' => 'CRM_Example_One_Foo',
268 $me = new CRM_Core_ManagedEntities($this->modules
, $decls);
271 $this->fail('Expected exception when using invalid declaration');
273 catch (Exception
$e) {
279 * Setup an active module with a malformed entity declaration.
281 public function testMissingName() {
282 // create first managed entity ('foo')
285 'module' => 'com.example.unknown',
286 'name' => NULL, // erroneous
287 'entity' => 'CustomSearch',
290 'class_name' => 'CRM_Example_One_Foo',
294 $me = new CRM_Core_ManagedEntities($this->modules
, $decls);
297 $this->fail('Expected exception when using invalid declaration');
299 catch (Exception
$e) {
305 * Setup an active module with a malformed entity declaration.
307 public function testMissingEntity() {
308 // create first managed entity ('foo')
311 'module' => 'com.example.unknown',
313 'entity' => NULL, // erroneous
316 'class_name' => 'CRM_Example_One_Foo',
320 $me = new CRM_Core_ManagedEntities($this->modules
, $decls);
323 $this->fail('Expected exception when using invalid declaration');
325 catch (Exception
$e) {
331 * Setup an active module with an entity -- then disable and re-enable the
334 public function testDeactivateReactivateModule() {
335 // create first managed entity ('foo')
337 $decls[] = $this->fixtures
['com.example.one-foo'];
338 $me = new CRM_Core_ManagedEntities($this->modules
, $decls);
340 $foo = $me->get('com.example.one', 'foo');
341 $this->assertEquals(1, $foo['is_active']);
342 $this->assertEquals('CRM_Example_One_Foo', $foo['name']);
343 $this->assertDBQuery(1, 'SELECT is_active FROM civicrm_option_value WHERE name = "CRM_Example_One_Foo"');
345 // now deactivate module, which has empty decls and which cascades to managed object
346 $this->modules
['one']->is_active
= FALSE;
347 $me = new CRM_Core_ManagedEntities($this->modules
, array());
349 $foo = $me->get('com.example.one', 'foo');
350 $this->assertEquals(0, $foo['is_active']);
351 $this->assertEquals('CRM_Example_One_Foo', $foo['name']);
352 $this->assertDBQuery(0, 'SELECT is_active FROM civicrm_option_value WHERE name = "CRM_Example_One_Foo"');
354 // and reactivate module, which again provides decls and which cascades to managed object
355 $this->modules
['one']->is_active
= TRUE;
356 $me = new CRM_Core_ManagedEntities($this->modules
, $decls);
358 $foo = $me->get('com.example.one', 'foo');
359 $this->assertEquals(1, $foo['is_active']);
360 $this->assertEquals('CRM_Example_One_Foo', $foo['name']);
361 $this->assertDBQuery(1, 'SELECT is_active FROM civicrm_option_value WHERE name = "CRM_Example_One_Foo"');
365 * Setup an active module with an entity -- then entirely uninstall the
368 public function testUninstallModule() {
369 // create first managed entity ('foo')
371 $decls[] = $this->fixtures
['com.example.one-foo'];
372 $me = new CRM_Core_ManagedEntities($this->modules
, $decls);
374 $foo = $me->get('com.example.one', 'foo');
375 $this->assertEquals('CRM_Example_One_Foo', $foo['name']);
376 $this->assertDBQuery(1, 'SELECT count(*) FROM civicrm_option_value WHERE name = "CRM_Example_One_Foo"');
378 // then destroy module; note that decls go away
379 unset($this->modules
['one']);
380 $me = new CRM_Core_ManagedEntities($this->modules
, array());
382 $fooNew = $me->get('com.example.one', 'foo');
383 $this->assertTrue(NULL === $fooNew);
384 $this->assertDBQuery(0, 'SELECT count(*) FROM civicrm_option_value WHERE name = "CRM_Example_One_Foo"');