3 require_once 'CiviTest/CiviUnitTestCase.php';
5 class CRM_Core_ManagedEntitiesTest
extends CiviUnitTestCase
{
8 'name' => 'ManagedEntities',
9 'description' => 'Test automatic creation/deletion of entities',
16 $this->modules
= array(
17 'one' => new CRM_Core_Module('com.example.one', TRUE),
18 'two' => new CRM_Core_Module('com.example.two', TRUE),
20 $this->assertDBQuery(0, 'SELECT count(*) FROM civicrm_managed');
21 $this->assertDBQuery(0, 'SELECT count(*) FROM civicrm_option_value WHERE name like "CRM_Example_%"');
26 CRM_Core_DAO
::singleValueQuery('DELETE FROM civicrm_managed');
27 CRM_Core_DAO
::singleValueQuery('DELETE FROM civicrm_option_value WHERE name like "CRM_Example_%"');
31 * Set up an active module and, over time, the hook implementation changes
32 * to (1) create 'foo' entity, (2) create 'bar' entity', (3) remove 'foo'
35 function testAddRemoveEntitiesModule() {
38 // create first managed entity ('foo')
40 'module' => 'com.example.one',
42 'entity' => 'CustomSearch',
45 'class_name' => 'CRM_Example_One_Foo',
49 $me = new CRM_Core_ManagedEntities($this->modules
, $decls);
51 $foo = $me->get('com.example.one', 'foo');
52 $this->assertEquals('CRM_Example_One_Foo', $foo['name']);
53 $this->assertDBQuery(1, 'SELECT count(*) FROM civicrm_option_value WHERE name = "CRM_Example_One_Foo"');
55 // later on, hook returns an extra managed entity ('bar')
57 'module' => 'com.example.one',
59 'entity' => 'CustomSearch',
62 'class_name' => 'CRM_Example_One_Bar',
66 $me = new CRM_Core_ManagedEntities($this->modules
, $decls);
68 $foo = $me->get('com.example.one', 'foo');
69 $this->assertEquals('CRM_Example_One_Foo', $foo['name']);
70 $this->assertDBQuery(1, 'SELECT count(*) FROM civicrm_option_value WHERE name = "CRM_Example_One_Foo"');
71 $bar = $me->get('com.example.one', 'bar');
72 $this->assertEquals('CRM_Example_One_Bar', $bar['name']);
73 $this->assertDBQuery(1, 'SELECT count(*) FROM civicrm_option_value WHERE name = "CRM_Example_One_Bar"');
75 // and then hook changes its mind, removing 'foo' (first of two entities)
77 $me = new CRM_Core_ManagedEntities($this->modules
, $decls);
79 $foo = $me->get('com.example.one', 'foo');
80 $this->assertTrue($foo === NULL);
81 $this->assertDBQuery(0, 'SELECT count(*) FROM civicrm_option_value WHERE name = "CRM_Example_One_Foo"');
82 $bar = $me->get('com.example.one', 'bar');
83 $this->assertEquals('CRM_Example_One_Bar', $bar['name']);
84 $this->assertDBQuery(1, 'SELECT count(*) FROM civicrm_option_value WHERE name = "CRM_Example_One_Bar"');
86 // and then hook changes its mind, removing 'bar' (the last remaining entity)
88 $me = new CRM_Core_ManagedEntities($this->modules
, $decls);
90 $foo = $me->get('com.example.one', 'foo');
91 $this->assertTrue($foo === NULL);
92 $this->assertDBQuery(0, 'SELECT count(*) FROM civicrm_option_value WHERE name = "CRM_Example_One_Foo"');
93 $bar = $me->get('com.example.one', 'bar');
94 $this->assertTrue($bar === NULL);
95 $this->assertDBQuery(0, 'SELECT count(*) FROM civicrm_option_value WHERE name = "CRM_Example_One_Bar"');
99 * Set up an active module with one managed-entity and, over
100 * time, the content of the entity changes
102 function testModifyDeclaration() {
105 // create first managed entity ('foo')
107 'module' => 'com.example.one',
109 'entity' => 'CustomSearch',
112 'class_name' => 'CRM_Example_One_Foo',
116 $me = new CRM_Core_ManagedEntities($this->modules
, $decls);
118 $foo = $me->get('com.example.one', 'foo');
119 $this->assertEquals('CRM_Example_One_Foo', $foo['name']);
120 $this->assertDBQuery(1, 'SELECT count(*) FROM civicrm_option_value WHERE name = "CRM_Example_One_Foo"');
122 // later on, hook specification changes
123 $decls[0]['params']['class_name'] = 'CRM_Example_One_Foobar';
124 $me = new CRM_Core_ManagedEntities($this->modules
, $decls);
126 $foo2 = $me->get('com.example.one', 'foo');
127 $this->assertEquals('CRM_Example_One_Foobar', $foo2['name']);
128 $this->assertDBQuery(0, 'SELECT count(*) FROM civicrm_option_value WHERE name = "CRM_Example_One_Foo"');
129 $this->assertDBQuery(1, 'SELECT count(*) FROM civicrm_option_value WHERE name = "CRM_Example_One_FooBar"');
130 $this->assertEquals($foo['id'], $foo2['id']);
134 * Setup an active module with a malformed entity declaration
136 function testInvalidDeclarationModule() {
137 // create first managed entity ('foo')
140 'module' => 'com.example.unknown', // erroneous
142 'entity' => 'CustomSearch',
145 'class_name' => 'CRM_Example_One_Foo',
149 $me = new CRM_Core_ManagedEntities($this->modules
, $decls);
152 $this->fail('Expected exception when using invalid declaration');
153 } catch (Exception
$e) {
159 * Setup an active module with a malformed entity declaration
161 function testMissingName() {
162 // create first managed entity ('foo')
165 'module' => 'com.example.unknown',
166 'name' => NULL, // erroneous
167 'entity' => 'CustomSearch',
170 'class_name' => 'CRM_Example_One_Foo',
174 $me = new CRM_Core_ManagedEntities($this->modules
, $decls);
177 $this->fail('Expected exception when using invalid declaration');
178 } catch (Exception
$e) {
184 * Setup an active module with a malformed entity declaration
186 function testMissingEntity() {
187 // create first managed entity ('foo')
190 'module' => 'com.example.unknown',
192 'entity' => NULL, // erroneous
195 'class_name' => 'CRM_Example_One_Foo',
199 $me = new CRM_Core_ManagedEntities($this->modules
, $decls);
202 $this->fail('Expected exception when using invalid declaration');
203 } catch (Exception
$e) {
209 * Setup an active module with an entity -- then disable and re-enable the
212 function testDeactivateReactivateModule() {
213 // create first managed entity ('foo')
216 'module' => 'com.example.one',
218 'entity' => 'CustomSearch',
221 'class_name' => 'CRM_Example_One_Foo',
225 $me = new CRM_Core_ManagedEntities($this->modules
, $decls);
227 $foo = $me->get('com.example.one', 'foo');
228 $this->assertEquals(1, $foo['is_active']);
229 $this->assertEquals('CRM_Example_One_Foo', $foo['name']);
230 $this->assertDBQuery(1, 'SELECT is_active FROM civicrm_option_value WHERE name = "CRM_Example_One_Foo"');
232 // now deactivate module, which has empty decls and which cascades to managed object
233 $this->modules
['one']->is_active
= FALSE;
234 $me = new CRM_Core_ManagedEntities($this->modules
, array());
236 $foo = $me->get('com.example.one', 'foo');
237 $this->assertEquals(0, $foo['is_active']);
238 $this->assertEquals('CRM_Example_One_Foo', $foo['name']);
239 $this->assertDBQuery(0, 'SELECT is_active FROM civicrm_option_value WHERE name = "CRM_Example_One_Foo"');
241 // and reactivate module, which again provides decls and which cascades to managed object
242 $this->modules
['one']->is_active
= TRUE;
243 $me = new CRM_Core_ManagedEntities($this->modules
, $decls);
245 $foo = $me->get('com.example.one', 'foo');
246 $this->assertEquals(1, $foo['is_active']);
247 $this->assertEquals('CRM_Example_One_Foo', $foo['name']);
248 $this->assertDBQuery(1, 'SELECT is_active FROM civicrm_option_value WHERE name = "CRM_Example_One_Foo"');
252 * Setup an active module with an entity -- then entirely uninstall the
255 function testUninstallModule() {
256 // create first managed entity ('foo')
259 'module' => 'com.example.one',
261 'entity' => 'CustomSearch',
264 'class_name' => 'CRM_Example_One_Foo',
268 $me = new CRM_Core_ManagedEntities($this->modules
, $decls);
270 $foo = $me->get('com.example.one', 'foo');
271 $this->assertEquals('CRM_Example_One_Foo', $foo['name']);
272 $this->assertDBQuery(1, 'SELECT count(*) FROM civicrm_option_value WHERE name = "CRM_Example_One_Foo"');
274 // then destory module; note that decls go away
275 unset($this->modules
['one']);
276 $me = new CRM_Core_ManagedEntities($this->modules
, array());
278 $fooNew = $me->get('com.example.one', 'foo');
279 $this->assertTrue(NULL === $fooNew);
280 $this->assertDBQuery(0, 'SELECT count(*) FROM civicrm_option_value WHERE name = "CRM_Example_One_Foo"');