3 require_once 'CiviTest/CiviUnitTestCase.php';
6 * Class CRM_Core_ManagedEntitiesTest
8 class CRM_Core_ManagedEntitiesTest
extends CiviUnitTestCase
{
10 * @var \Civi\API\Kernel
15 * @var \Civi\API\Provider\AdhocProvider
17 protected $adhocProvider;
20 * @var array(string $shortName => CRM_Core_Module $module)
26 public function setUp() {
27 $this->useTransaction(TRUE);
29 $this->modules
= array(
30 'one' => new CRM_Core_Module('com.example.one', TRUE),
31 'two' => new CRM_Core_Module('com.example.two', TRUE),
34 // Testing on drupal-demo fails because some extensions have mgd ents.
35 CRM_Core_DAO
::singleValueQuery('DELETE FROM civicrm_managed');
37 $this->fixtures
['com.example.one-foo'] = array(
38 'module' => 'com.example.one',
40 'entity' => 'CustomSearch',
43 'class_name' => 'CRM_Example_One_Foo',
47 $this->fixtures
['com.example.one-bar'] = array(
48 'module' => 'com.example.one',
50 'entity' => 'CustomSearch',
53 'class_name' => 'CRM_Example_One_Bar',
58 $this->apiKernel
= \Civi\Core\Container
::singleton()->get('civi_api_kernel');
59 $this->adhocProvider
= new \Civi\API\Provider\
AdhocProvider(3, 'CustomSearch');
60 $this->apiKernel
->registerApiProvider($this->adhocProvider
);
63 public function tearDown() {
65 \Civi\Core\Container
::singleton(TRUE);
69 * Set up an active module and, over time, the hook implementation changes
70 * to (1) create 'foo' entity, (2) create 'bar' entity', (3) remove 'foo'
73 public function testAddRemoveEntitiesModule_UpdateAlways_DeleteAlways() {
76 // create first managed entity ('foo')
77 $decls[] = $this->fixtures
['com.example.one-foo'];
78 $me = new CRM_Core_ManagedEntities($this->modules
, $decls);
80 $foo = $me->get('com.example.one', 'foo');
81 $this->assertEquals('CRM_Example_One_Foo', $foo['name']);
82 $this->assertDBQuery(1, 'SELECT count(*) FROM civicrm_option_value WHERE name = "CRM_Example_One_Foo"');
84 // later on, hook returns an extra managed entity ('bar')
85 $decls[] = $this->fixtures
['com.example.one-bar'];
86 $me = new CRM_Core_ManagedEntities($this->modules
, $decls);
88 $foo = $me->get('com.example.one', 'foo');
89 $this->assertEquals('CRM_Example_One_Foo', $foo['name']);
90 $this->assertDBQuery(1, 'SELECT count(*) FROM civicrm_option_value WHERE name = "CRM_Example_One_Foo"');
91 $bar = $me->get('com.example.one', 'bar');
92 $this->assertEquals('CRM_Example_One_Bar', $bar['name']);
93 $this->assertDBQuery(1, 'SELECT count(*) FROM civicrm_option_value WHERE name = "CRM_Example_One_Bar"');
95 // and then hook changes its mind, removing 'foo' (first of two entities)
97 $me = new CRM_Core_ManagedEntities($this->modules
, $decls);
99 $foo = $me->get('com.example.one', 'foo');
100 $this->assertTrue($foo === NULL);
101 $this->assertDBQuery(0, 'SELECT count(*) FROM civicrm_option_value WHERE name = "CRM_Example_One_Foo"');
102 $bar = $me->get('com.example.one', 'bar');
103 $this->assertEquals('CRM_Example_One_Bar', $bar['name']);
104 $this->assertDBQuery(1, 'SELECT count(*) FROM civicrm_option_value WHERE name = "CRM_Example_One_Bar"');
106 // and then hook changes its mind, removing 'bar' (the last remaining entity)
108 $me = new CRM_Core_ManagedEntities($this->modules
, $decls);
110 $foo = $me->get('com.example.one', 'foo');
111 $this->assertTrue($foo === NULL);
112 $this->assertDBQuery(0, 'SELECT count(*) FROM civicrm_option_value WHERE name = "CRM_Example_One_Foo"');
113 $bar = $me->get('com.example.one', 'bar');
114 $this->assertTrue($bar === NULL);
115 $this->assertDBQuery(0, 'SELECT count(*) FROM civicrm_option_value WHERE name = "CRM_Example_One_Bar"');
119 * Set up an active module with one managed-entity and, over
120 * time, the content of the entity changes
122 public function testModifyDeclaration_UpdateAlways() {
125 // create first managed entity ('foo')
126 $decls[] = $this->fixtures
['com.example.one-foo'];
127 $me = new CRM_Core_ManagedEntities($this->modules
, $decls);
129 $foo = $me->get('com.example.one', 'foo');
130 $this->assertEquals('CRM_Example_One_Foo', $foo['name']);
131 $this->assertDBQuery(1, 'SELECT count(*) FROM civicrm_option_value WHERE name = "CRM_Example_One_Foo"');
133 // later on, hook specification changes
134 $decls[0]['params']['class_name'] = 'CRM_Example_One_Foobar';
135 $me = new CRM_Core_ManagedEntities($this->modules
, $decls);
137 $foo2 = $me->get('com.example.one', 'foo');
138 $this->assertEquals('CRM_Example_One_Foobar', $foo2['name']);
139 $this->assertDBQuery(0, 'SELECT count(*) FROM civicrm_option_value WHERE name = "CRM_Example_One_Foo"');
140 $this->assertDBQuery(1, 'SELECT count(*) FROM civicrm_option_value WHERE name = "CRM_Example_One_FooBar"');
141 $this->assertEquals($foo['id'], $foo2['id']);
145 * Set up an active module with one managed-entity and, over
146 * time, the content of the entity changes
148 public function testModifyDeclaration_UpdateNever() {
151 // create first managed entity ('foo')
152 $decls[] = array_merge($this->fixtures
['com.example.one-foo'], array(
153 'update' => 'never', // Policy is to never update after initial creation
155 $me = new CRM_Core_ManagedEntities($this->modules
, $decls);
157 $foo = $me->get('com.example.one', 'foo');
158 $this->assertEquals('CRM_Example_One_Foo', $foo['name']);
159 $this->assertDBQuery(1, 'SELECT count(*) FROM civicrm_option_value WHERE name = "CRM_Example_One_Foo"');
161 // later on, hook specification changes
162 $decls[0]['params']['class_name'] = 'CRM_Example_One_Foobar';
163 $me = new CRM_Core_ManagedEntities($this->modules
, $decls);
165 $foo2 = $me->get('com.example.one', 'foo');
166 $this->assertEquals('CRM_Example_One_Foo', $foo2['name']);
167 $this->assertDBQuery(1, 'SELECT count(*) FROM civicrm_option_value WHERE name = "CRM_Example_One_Foo"');
168 $this->assertDBQuery(0, 'SELECT count(*) FROM civicrm_option_value WHERE name = "CRM_Example_One_FooBar"');
169 $this->assertEquals($foo['id'], $foo2['id']);
173 * Set up an active module with one managed-entity using the
174 * policy "cleanup=>never". When the managed-entity goes away,
175 * ensure that the policy is followed (ie the entity is not
178 public function testRemoveDeclaration_CleanupNever() {
181 // create first managed entity ('foo')
182 $decls[] = array_merge($this->fixtures
['com.example.one-foo'], array(
183 'cleanup' => 'never',
185 $me = new CRM_Core_ManagedEntities($this->modules
, $decls);
187 $foo = $me->get('com.example.one', 'foo');
188 $this->assertEquals('CRM_Example_One_Foo', $foo['name']);
189 $this->assertDBQuery(1, 'SELECT count(*) FROM civicrm_option_value WHERE name = "CRM_Example_One_Foo"');
191 // later on, entity definition disappears; but we decide not to do any cleanup (per policy)
193 $me = new CRM_Core_ManagedEntities($this->modules
, $decls);
195 $foo2 = $me->get('com.example.one', 'foo');
196 $this->assertEquals('CRM_Example_One_Foo', $foo2['name']);
197 $this->assertDBQuery(1, 'SELECT count(*) FROM civicrm_option_value WHERE name = "CRM_Example_One_Foo"');
198 $this->assertEquals($foo['id'], $foo2['id']);
202 * Set up an active module with one managed-entity using the
203 * policy "cleanup=>never". When the managed-entity goes away,
204 * ensure that the policy is followed (ie the entity is not
207 public function testRemoveDeclaration_CleanupUnused() {
210 // create first managed entity ('foo')
211 $decls[] = array_merge($this->fixtures
['com.example.one-foo'], array(
212 'cleanup' => 'unused',
214 $me = new CRM_Core_ManagedEntities($this->modules
, $decls);
216 $foo = $me->get('com.example.one', 'foo');
217 $this->assertEquals('CRM_Example_One_Foo', $foo['name']);
218 $this->assertDBQuery(1, 'SELECT count(*) FROM civicrm_option_value WHERE name = "CRM_Example_One_Foo"');
220 // Override 'getrefcount' ==> The refcount is 1
221 $this->adhocProvider
->addAction('getrefcount', 'access CiviCRM', function ($apiRequest) {
222 return civicrm_api3_create_success(array(
231 // Later on, entity definition disappears; but we decide not to do any cleanup (per policy)
233 $me = new CRM_Core_ManagedEntities($this->modules
, $decls);
235 $foo2 = $me->get('com.example.one', 'foo');
236 $this->assertEquals('CRM_Example_One_Foo', $foo2['name']);
237 $this->assertDBQuery(1, 'SELECT count(*) FROM civicrm_option_value WHERE name = "CRM_Example_One_Foo"');
238 $this->assertEquals($foo['id'], $foo2['id']);
240 // Override 'getrefcount' ==> The refcount is 0
241 $this->adhocProvider
->addAction('getrefcount', 'access CiviCRM', function ($apiRequest) {
242 return civicrm_api3_create_success(array());
245 // The entity definition disappeared and there's no reference; we decide to cleanup (per policy)
247 $me = new CRM_Core_ManagedEntities($this->modules
, $decls);
249 $foo3 = $me->get('com.example.one', 'foo');
250 $this->assertDBQuery(0, 'SELECT count(*) FROM civicrm_option_value WHERE name = "CRM_Example_One_Foo"');
251 $this->assertTrue($foo3 === NULL);
255 * Setup an active module with a malformed entity declaration.
257 public function testInvalidDeclarationModule() {
258 // create first managed entity ('foo')
261 'module' => 'com.example.unknown', // erroneous
263 'entity' => 'CustomSearch',
266 'class_name' => 'CRM_Example_One_Foo',
270 $me = new CRM_Core_ManagedEntities($this->modules
, $decls);
273 $this->fail('Expected exception when using invalid declaration');
275 catch (Exception
$e) {
281 * Setup an active module with a malformed entity declaration.
283 public function testMissingName() {
284 // create first managed entity ('foo')
287 'module' => 'com.example.unknown',
288 'name' => NULL, // erroneous
289 'entity' => 'CustomSearch',
292 'class_name' => 'CRM_Example_One_Foo',
296 $me = new CRM_Core_ManagedEntities($this->modules
, $decls);
299 $this->fail('Expected exception when using invalid declaration');
301 catch (Exception
$e) {
307 * Setup an active module with a malformed entity declaration.
309 public function testMissingEntity() {
310 // create first managed entity ('foo')
313 'module' => 'com.example.unknown',
315 'entity' => NULL, // erroneous
318 'class_name' => 'CRM_Example_One_Foo',
322 $me = new CRM_Core_ManagedEntities($this->modules
, $decls);
325 $this->fail('Expected exception when using invalid declaration');
327 catch (Exception
$e) {
333 * Setup an active module with an entity -- then disable and re-enable the
336 public function testDeactivateReactivateModule() {
337 // create first managed entity ('foo')
339 $decls[] = $this->fixtures
['com.example.one-foo'];
340 $me = new CRM_Core_ManagedEntities($this->modules
, $decls);
342 $foo = $me->get('com.example.one', 'foo');
343 $this->assertEquals(1, $foo['is_active']);
344 $this->assertEquals('CRM_Example_One_Foo', $foo['name']);
345 $this->assertDBQuery(1, 'SELECT is_active FROM civicrm_option_value WHERE name = "CRM_Example_One_Foo"');
347 // now deactivate module, which has empty decls and which cascades to managed object
348 $this->modules
['one']->is_active
= FALSE;
349 $me = new CRM_Core_ManagedEntities($this->modules
, array());
351 $foo = $me->get('com.example.one', 'foo');
352 $this->assertEquals(0, $foo['is_active']);
353 $this->assertEquals('CRM_Example_One_Foo', $foo['name']);
354 $this->assertDBQuery(0, 'SELECT is_active FROM civicrm_option_value WHERE name = "CRM_Example_One_Foo"');
356 // and reactivate module, which again provides decls and which cascades to managed object
357 $this->modules
['one']->is_active
= TRUE;
358 $me = new CRM_Core_ManagedEntities($this->modules
, $decls);
360 $foo = $me->get('com.example.one', 'foo');
361 $this->assertEquals(1, $foo['is_active']);
362 $this->assertEquals('CRM_Example_One_Foo', $foo['name']);
363 $this->assertDBQuery(1, 'SELECT is_active FROM civicrm_option_value WHERE name = "CRM_Example_One_Foo"');
367 * Setup an active module with an entity -- then entirely uninstall the
370 public function testUninstallModule() {
371 // create first managed entity ('foo')
373 $decls[] = $this->fixtures
['com.example.one-foo'];
374 $me = new CRM_Core_ManagedEntities($this->modules
, $decls);
376 $foo = $me->get('com.example.one', 'foo');
377 $this->assertEquals('CRM_Example_One_Foo', $foo['name']);
378 $this->assertDBQuery(1, 'SELECT count(*) FROM civicrm_option_value WHERE name = "CRM_Example_One_Foo"');
380 // then destroy module; note that decls go away
381 unset($this->modules
['one']);
382 $me = new CRM_Core_ManagedEntities($this->modules
, array());
384 $fooNew = $me->get('com.example.one', 'foo');
385 $this->assertTrue(NULL === $fooNew);
386 $this->assertDBQuery(0, 'SELECT count(*) FROM civicrm_option_value WHERE name = "CRM_Example_One_Foo"');