Get contact fields from the schema
[civicrm-core.git] / tests / phpunit / CRM / Core / ManagedEntitiesTest.php
1 <?php
2
3 require_once 'CiviTest/CiviUnitTestCase.php';
4
5 class CRM_Core_ManagedEntitiesTest extends CiviUnitTestCase {
6 function get_info() {
7 return array(
8 'name' => 'ManagedEntities',
9 'description' => 'Test automatic creation/deletion of entities',
10 'group' => 'Core',
11 );
12 }
13
14 function setUp() {
15 parent::setUp();
16 $this->modules = array(
17 'one' => new CRM_Core_Module('com.example.one', TRUE),
18 'two' => new CRM_Core_Module('com.example.two', TRUE),
19 );
20 $this->assertDBQuery(0, 'SELECT count(*) FROM civicrm_managed');
21 $this->assertDBQuery(0, 'SELECT count(*) FROM civicrm_option_value WHERE name like "CRM_Example_%"');
22 }
23
24 function tearDown() {
25 parent::tearDown();
26 CRM_Core_DAO::singleValueQuery('DELETE FROM civicrm_managed');
27 CRM_Core_DAO::singleValueQuery('DELETE FROM civicrm_option_value WHERE name like "CRM_Example_%"');
28 }
29
30 /**
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'
33 * entity
34 */
35 function testAddRemoveEntitiesModule() {
36 $decls = array();
37
38 // create first managed entity ('foo')
39 $decls[] = array(
40 'module' => 'com.example.one',
41 'name' => 'foo',
42 'entity' => 'CustomSearch',
43 'params' => array(
44 'version' => 3,
45 'class_name' => 'CRM_Example_One_Foo',
46 'is_reserved' => 1,
47 ),
48 );
49 $me = new CRM_Core_ManagedEntities($this->modules, $decls);
50 $me->reconcile();
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"');
54
55 // later on, hook returns an extra managed entity ('bar')
56 $decls[] = array(
57 'module' => 'com.example.one',
58 'name' => 'bar',
59 'entity' => 'CustomSearch',
60 'params' => array(
61 'version' => 3,
62 'class_name' => 'CRM_Example_One_Bar',
63 'is_reserved' => 1,
64 ),
65 );
66 $me = new CRM_Core_ManagedEntities($this->modules, $decls);
67 $me->reconcile();
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"');
74
75 // and then hook changes its mind, removing 'foo' (first of two entities)
76 unset($decls[0]);
77 $me = new CRM_Core_ManagedEntities($this->modules, $decls);
78 $me->reconcile();
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"');
85
86 // and then hook changes its mind, removing 'bar' (the last remaining entity)
87 unset($decls[1]);
88 $me = new CRM_Core_ManagedEntities($this->modules, $decls);
89 $me->reconcile();
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"');
96 }
97
98 /**
99 * Set up an active module with one managed-entity and, over
100 * time, the content of the entity changes
101 */
102 function testModifyDeclaration() {
103 $decls = array();
104
105 // create first managed entity ('foo')
106 $decls[] = array(
107 'module' => 'com.example.one',
108 'name' => 'foo',
109 'entity' => 'CustomSearch',
110 'params' => array(
111 'version' => 3,
112 'class_name' => 'CRM_Example_One_Foo',
113 'is_reserved' => 1,
114 ),
115 );
116 $me = new CRM_Core_ManagedEntities($this->modules, $decls);
117 $me->reconcile();
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"');
121
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);
125 $me->reconcile();
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']);
131 }
132
133 /**
134 * Setup an active module with a malformed entity declaration
135 */
136 function testInvalidDeclarationModule() {
137 // create first managed entity ('foo')
138 $decls = array();
139 $decls[] = array(
140 'module' => 'com.example.unknown', // erroneous
141 'name' => 'foo',
142 'entity' => 'CustomSearch',
143 'params' => array(
144 'version' => 3,
145 'class_name' => 'CRM_Example_One_Foo',
146 'is_reserved' => 1,
147 ),
148 );
149 $me = new CRM_Core_ManagedEntities($this->modules, $decls);
150 try {
151 $me->reconcile();
152 $this->fail('Expected exception when using invalid declaration');
153 } catch (Exception $e) {
154 // good
155 }
156 }
157
158 /**
159 * Setup an active module with a malformed entity declaration
160 */
161 function testMissingName() {
162 // create first managed entity ('foo')
163 $decls = array();
164 $decls[] = array(
165 'module' => 'com.example.unknown',
166 'name' => NULL, // erroneous
167 'entity' => 'CustomSearch',
168 'params' => array(
169 'version' => 3,
170 'class_name' => 'CRM_Example_One_Foo',
171 'is_reserved' => 1,
172 ),
173 );
174 $me = new CRM_Core_ManagedEntities($this->modules, $decls);
175 try {
176 $me->reconcile();
177 $this->fail('Expected exception when using invalid declaration');
178 } catch (Exception $e) {
179 // good
180 }
181 }
182
183 /**
184 * Setup an active module with a malformed entity declaration
185 */
186 function testMissingEntity() {
187 // create first managed entity ('foo')
188 $decls = array();
189 $decls[] = array(
190 'module' => 'com.example.unknown',
191 'name' => 'foo',
192 'entity' => NULL, // erroneous
193 'params' => array(
194 'version' => 3,
195 'class_name' => 'CRM_Example_One_Foo',
196 'is_reserved' => 1,
197 ),
198 );
199 $me = new CRM_Core_ManagedEntities($this->modules, $decls);
200 try {
201 $me->reconcile();
202 $this->fail('Expected exception when using invalid declaration');
203 } catch (Exception $e) {
204 // good
205 }
206 }
207
208 /**
209 * Setup an active module with an entity -- then disable and re-enable the
210 * module
211 */
212 function testDeactivateReactivateModule() {
213 // create first managed entity ('foo')
214 $decls = array();
215 $decls[] = array(
216 'module' => 'com.example.one',
217 'name' => 'foo',
218 'entity' => 'CustomSearch',
219 'params' => array(
220 'version' => 3,
221 'class_name' => 'CRM_Example_One_Foo',
222 'is_reserved' => 1,
223 ),
224 );
225 $me = new CRM_Core_ManagedEntities($this->modules, $decls);
226 $me->reconcile();
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"');
231
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());
235 $me->reconcile();
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"');
240
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);
244 $me->reconcile();
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"');
249 }
250
251 /**
252 * Setup an active module with an entity -- then entirely uninstall the
253 * module
254 */
255 function testUninstallModule() {
256 // create first managed entity ('foo')
257 $decls = array();
258 $decls[] = array(
259 'module' => 'com.example.one',
260 'name' => 'foo',
261 'entity' => 'CustomSearch',
262 'params' => array(
263 'version' => 3,
264 'class_name' => 'CRM_Example_One_Foo',
265 'is_reserved' => 1,
266 ),
267 );
268 $me = new CRM_Core_ManagedEntities($this->modules, $decls);
269 $me->reconcile();
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"');
273
274 // then destory module; note that decls go away
275 unset($this->modules['one']);
276 $me = new CRM_Core_ManagedEntities($this->modules, array());
277 $me->reconcile();
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"');
281 }
282 }