CRM-15011 - tests - Fix dbunit references
[civicrm-core.git] / tests / phpunit / CRM / Core / BAO / CustomGroupTest.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.5 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2014 |
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
9 | |
10 | CiviCRM is free software; you can copy, modify, and distribute it |
11 | under the terms of the GNU Affero General Public License |
12 | Version 3, 19 November 2007 and the CiviCRM Licensing Exception. |
13 | |
14 | CiviCRM is distributed in the hope that it will be useful, but |
15 | WITHOUT ANY WARRANTY; without even the implied warranty of |
16 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
17 | See the GNU Affero General Public License for more details. |
18 | |
19 | You should have received a copy of the GNU Affero General Public |
20 | License and the CiviCRM Licensing Exception along |
21 | with this program; if not, contact CiviCRM LLC |
22 | at info[AT]civicrm[DOT]org. If you have questions about the |
23 | GNU Affero General Public License or the licensing of CiviCRM, |
24 | see the CiviCRM license FAQ at http://civicrm.org/licensing |
25 +--------------------------------------------------------------------+
26 */
27
28
29
30 require_once 'CiviTest/CiviUnitTestCase.php';
31 require_once 'CiviTest/Contact.php';
32 require_once 'CiviTest/Custom.php';
33
34 /**
35 * Class CRM_Core_BAO_CustomGroupTest
36 */
37 class CRM_Core_BAO_CustomGroupTest extends CiviUnitTestCase {
38 /**
39 * @return array
40 */
41 function get_info() {
42 return array(
43 'name' => 'CustomGroup BAOs',
44 'description' => 'Test all Core_BAO_CustomGroup methods.',
45 'group' => 'CiviCRM BAO Tests',
46 );
47 }
48
49 function setUp() {
50 parent::setUp();
51 }
52
53 /**
54 * Function to test getTree()
55 */
56 function testGetTree() {
57 $params = array();
58 $contactId = Contact::createIndividual();
59 $customGrouptitle = 'My Custom Group';
60 $groupParams = array(
61 'title' => $customGrouptitle,
62 'name' => 'my_custom_group',
63 'style' => 'Tab',
64 'extends' => 'Individual',
65 'is_active' => 1,
66 'version' => 3,
67 );
68
69 $customGroup = Custom::createGroup($groupParams);
70
71 $customGroupId = $customGroup->id;
72
73 $fields = array(
74 'groupId' => $customGroupId,
75 'dataType' => 'String',
76 'htmlType' => 'Text',
77 );
78
79 $customField = Custom::createField($params, $fields);
80 $formParams = NULL;
81 $getTree = CRM_Core_BAO_CustomGroup::getTree('Individual', $formParams, $customGroupId);
82
83 $dbCustomGroupTitle = $this->assertDBNotNull('CRM_Core_DAO_CustomGroup', $customGroupId, 'title', 'id',
84 'Database check for custom group record.'
85 );
86
87 Custom::deleteField($customField);
88 Custom::deleteGroup($customGroup);
89 Contact::delete($contactId);
90 $customGroup->free();
91 }
92
93 /**
94 * Function to test retrieve() with Empty Params
95 */
96 function testRetrieveEmptyParams() {
97 $params = array();
98 $customGroup = CRM_Core_BAO_CustomGroup::retrieve($params, $dafaults);
99 $this->assertNull($customGroup, 'Check that no custom Group is retreived');
100 }
101
102 /**
103 * Function to test retrieve() with Inalid Params
104 */
105 function testRetrieveInvalidParams() {
106 $params = array('id' => 99);
107 $customGroup = CRM_Core_BAO_CustomGroup::retrieve($params, $dafaults);
108 $this->assertNull($customGroup, 'Check that no custom Group is retreived');
109 }
110
111 /**
112 * Function to test retrieve()
113 */
114 function testRetrieve() {
115 $customGrouptitle = 'My Custom Group';
116 $groupParams = array(
117 'title' => $customGrouptitle,
118 'name' => 'My_Custom_Group',
119 'style' => 'Tab',
120 'extends' => 'Individual',
121 'help_pre' => 'Custom Group Help Pre',
122 'help_post' => 'Custom Group Help Post',
123 'is_active' => 1,
124 'collapse_display' => 1,
125 'weight' => 2,
126 'version' => 3,
127 );
128
129 $customGroup = Custom::createGroup($groupParams);
130 $customGroupId = $customGroup->id;
131
132 $params = array('id' => $customGroupId);
133 $customGroup = CRM_Core_BAO_CustomGroup::retrieve($params, $dafaults);
134 $dbCustomGroupTitle = $this->assertDBNotNull('CRM_Core_DAO_CustomGroup', $customGroupId, 'title', 'id',
135 'Database check for custom group record.'
136 );
137
138 $this->assertEquals($customGrouptitle, $dbCustomGroupTitle);
139 //check retieve values
140 unset($groupParams['version']);
141 $this->assertAttributesEquals($groupParams, $dafaults);
142
143 //cleanup DB by deleting customGroup
144 Custom::deleteGroup($customGroup);
145 }
146
147 /**
148 * Function to test setIsActive()
149 */
150 function testSetIsActive() {
151 $customGrouptitle = 'My Custom Group';
152 $groupParams = array(
153 'title' => $customGrouptitle,
154 'name' => 'my_custom_group',
155 'style' => 'Tab',
156 'extends' => 'Individual',
157 'is_active' => 0,
158 'version' => 3,
159 );
160
161 $customGroup = Custom::createGroup($groupParams);
162 $customGroupId = $customGroup->id;
163
164 //update is_active
165 $result = CRM_Core_BAO_CustomGroup::setIsActive($customGroupId, TRUE);
166
167 //check for object update
168 $this->assertEquals(TRUE, $result);
169 //check for is_active
170 $this->assertDBCompareValue('CRM_Core_DAO_CustomGroup', $customGroupId, 'is_active', 'id', 1,
171 'Database check for custom group is_active field.'
172 );
173 //cleanup DB by deleting customGroup
174 Custom::deleteGroup($customGroup);
175 }
176
177 /**
178 * Function to test getGroupDetail() with Empty Params
179 */
180 function testGetGroupDetailEmptyParams() {
181 $customGroupId = array();
182 $customGroup = CRM_Core_BAO_CustomGroup::getGroupDetail($customGroupId);
183 $this->assertTrue(empty($customGroup), 'Check that no custom Group details is retreived');
184 }
185
186 /**
187 * Function to test getGroupDetail() with Inalid Params
188 */
189 function testGetGroupDetailInvalidParams() {
190 $customGroupId = 99;
191 $customGroup = CRM_Core_BAO_CustomGroup::getGroupDetail($customGroupId);
192 $this->assertTrue(empty($customGroup), 'Check that no custom Group details is retreived');
193 }
194
195 /**
196 * Function to test getGroupDetail()
197 */
198 function testGetGroupDetail() {
199 $customGrouptitle = 'My Custom Group';
200 $groupParams = array(
201 'title' => $customGrouptitle,
202 'name' => 'My_Custom_Group',
203 'extends' => 'Individual',
204 'help_pre' => 'Custom Group Help Pre',
205 'help_post' => 'Custom Group Help Post',
206 'is_active' => 1,
207 'collapse_display' => 1,
208 'version' => 3,
209 );
210
211 $customGroup = Custom::createGroup($groupParams);
212 $customGroupId = $customGroup->id;
213
214 $fieldParams = array(
215 'custom_group_id' => $customGroupId,
216 'label' => 'Test Custom Field',
217 'html_type' => 'Text',
218 'data_type' => 'String',
219 'is_required' => 1,
220 'is_searchable' => 0,
221 'is_active' => 1,
222 'version' => 3,
223 );
224
225 $customField = Custom::createField($fieldParams);
226 $customFieldId = $customField->id;
227
228 $groupTree = CRM_Core_BAO_CustomGroup::getGroupDetail($customGroupId);
229 $dbCustomGroupTitle = $this->assertDBNotNull('CRM_Core_DAO_CustomGroup', $customGroupId, 'title', 'id',
230 'Database check for custom group record.'
231 );
232 //check retieve values of custom group
233 unset($groupParams['is_active']);
234 unset($groupParams['title']);
235 unset($groupParams['version']);
236 $this->assertAttributesEquals($groupParams, $groupTree[$customGroupId]);
237
238 //check retieve values of custom field
239 unset($fieldParams['is_active']);
240 unset($fieldParams['custom_group_id']);
241 unset($fieldParams['version']);
242 $this->assertAttributesEquals($fieldParams, $groupTree[$customGroupId]['fields'][$customFieldId], " in line " . __LINE__);
243
244 //cleanup DB by deleting customGroup
245 Custom::deleteField($customField);
246 Custom::deleteGroup($customGroup);
247 }
248
249 /**
250 * Function to test getTitle() with Invalid Params()
251 */
252 function testGetTitleWithInvalidParams() {
253 $params = 99;
254 $customGroupTitle = CRM_Core_BAO_CustomGroup::getTitle($params);
255
256 $this->assertNull($customGroupTitle, 'Check that no custom Group Title is retreived');
257 }
258
259 /**
260 * Function to test getTitle()
261 */
262 function testGetTitle() {
263 $customGrouptitle = 'My Custom Group';
264 $groupParams = array(
265 'title' => $customGrouptitle,
266 'name' => 'my_custom_group',
267 'style' => 'Tab',
268 'extends' => 'Individual',
269 'is_active' => 0,
270 'version' => 3,
271 );
272
273 $customGroup = Custom::createGroup($groupParams);
274 $customGroupId = $customGroup->id;
275
276 //get the custom group title
277 $title = CRM_Core_BAO_CustomGroup::getTitle($customGroupId);
278
279 //check for object update
280 $this->assertEquals($customGrouptitle, $title);
281
282 //cleanup DB by deleting customGroup
283 Custom::deleteGroup($customGroup);
284 }
285
286 /**
287 * Function to test deleteGroup()
288 */
289 function testDeleteGroup() {
290 $customGrouptitle = 'My Custom Group';
291 $groupParams = array(
292 'title' => $customGrouptitle,
293 'name' => 'my_custom_group',
294 'style' => 'Tab',
295 'extends' => 'Individual',
296 'is_active' => 1,
297 'version' => 3,
298 );
299
300 $customGroup = Custom::createGroup($groupParams);
301
302 $customGroupId = $customGroup->id;
303
304 //get the custom group title
305 $dbCustomGroupTitle = $this->assertDBNotNull('CRM_Core_DAO_CustomGroup', $customGroupId, 'title', 'id',
306 'Database check for custom group record.'
307 );
308 //check for group title
309 $this->assertEquals($customGrouptitle, $dbCustomGroupTitle);
310
311 //delete the group
312 $isDelete = CRM_Core_BAO_CustomGroup::deleteGroup($customGroup);
313
314 //check for delete
315 $this->assertEquals(TRUE, $isDelete);
316
317 //check the DB
318 $this->assertDBNull('CRM_Core_DAO_CustomGroup', $customGroupId, 'title', 'id',
319 'Database check for custom group record.'
320 );
321 }
322
323 /**
324 * Function to test createTable()
325 */
326 function testCreateTable() {
327 $customGrouptitle = 'My Custom Group';
328 $groupParams = array(
329 'title' => $customGrouptitle,
330 'name' => 'my_custom_group',
331 'style' => 'Tab',
332 'extends' => 'Individual',
333 'is_active' => 1,
334 'version' => 3,
335 );
336
337 $customGroupBAO = new CRM_Core_BAO_CustomGroup();
338 $customGroupBAO->copyValues($groupParams);
339 $customGroup = $customGroupBAO->save();
340 $tableName = 'civicrm_value_test_group_' . $customGroup->id;
341 $customGroup->table_name = $tableName;
342 $customGroup = $customGroupBAO->save();
343 $customTable = CRM_Core_BAO_CustomGroup::createTable($customGroup);
344 $customGroupId = $customGroup->id;
345
346 //check db for custom group.
347 $dbCustomGroupTitle = $this->assertDBNotNull('CRM_Core_DAO_CustomGroup', $customGroupId, 'title', 'id',
348 'Database check for custom group record.'
349 );
350 //check for custom group table name
351 $this->assertDBCompareValue('CRM_Core_DAO_CustomGroup', $customGroupId, 'table_name', 'id',
352 $tableName, 'Database check for custom group table name.'
353 );
354
355 //check for group title
356 $this->assertEquals($customGrouptitle, $dbCustomGroupTitle);
357
358 //cleanup DB by deleting customGroup
359 Custom::deleteGroup($customGroup);
360 }
361
362 /**
363 * Function to test checkCustomField()
364 */
365 function testCheckCustomField() {
366 $customGroupTitle = 'My Custom Group';
367 $groupParams = array(
368 'title' => $customGroupTitle,
369 'name' => 'my_custom_group',
370 'extends' => 'Individual',
371 'help_pre' => 'Custom Group Help Pre',
372 'help_post' => 'Custom Group Help Post',
373 'is_active' => 1,
374 'collapse_display' => 1,
375 'version' => 3,
376 );
377
378 $customGroup = Custom::createGroup($groupParams);
379 $this->assertNotNull($customGroup->id, 'pre-requisite group not created successfully');
380 $customGroupId = $customGroup->id;
381
382 $customFieldLabel = 'Test Custom Field';
383 $fieldParams = array(
384 'custom_group_id' => $customGroupId,
385 'label' => $customFieldLabel,
386 'html_type' => 'Text',
387 'data_type' => 'String',
388 'is_required' => 1,
389 'is_searchable' => 0,
390 'is_active' => 1,
391 'version' => 3,
392 );
393
394 $customField = Custom::createField($fieldParams);
395 $this->assertNotNull($customField->id, 'pre-requisite field not created successfully');
396
397 $customFieldId = $customField->id;
398
399 //check db for custom group
400 $dbCustomGroupTitle = $this->assertDBNotNull('CRM_Core_DAO_CustomGroup', $customGroupId, 'title', 'id',
401 'Database check for custom group record.'
402 );
403 $this->assertEquals($customGroupTitle, $dbCustomGroupTitle);
404
405 //check db for custom field
406 $dbCustomFieldLabel = $this->assertDBNotNull('CRM_Core_DAO_CustomField', $customFieldId, 'label', 'id',
407 'Database check for custom field record.'
408 );
409 $this->assertEquals($customFieldLabel, $dbCustomFieldLabel);
410
411 //check the custom field type.
412 $params = array('Individual');
413 $usedFor = CRM_Core_BAO_CustomGroup::checkCustomField($customFieldId, $params);
414 $this->assertEquals(FALSE, $usedFor);
415
416 $params = array('Contribution', 'Membership', 'Participant');
417 $usedFor = CRM_Core_BAO_CustomGroup::checkCustomField($customFieldId, $params);
418 $this->assertEquals(TRUE, $usedFor);
419
420 //cleanup DB by deleting customGroup
421 Custom::deleteField($customField);
422 Custom::deleteGroup($customGroup);
423 }
424
425 /**
426 * Function to test getActiveGroups() with Invalid Params()
427 */
428 function testGetActiveGroupsWithInvalidParams() {
429 $contactId = Contact::createIndividual();
430 $activeGroups = CRM_Core_BAO_CustomGroup::getActiveGroups('ABC', 'civicrm/contact/view/cd', $contactId);
431 $this->assertEquals(empty($activeGroups), TRUE, 'Check that Emprt params are retreived');
432 }
433
434 function testGetActiveGroups() {
435 $contactId = Contact::createIndividual();
436 $customGrouptitle = 'Test Custom Group';
437 $groupParams = array(
438 'title' => $customGrouptitle,
439 'name' => 'test_custom_group',
440 'style' => 'Tab',
441 'extends' => 'Individual',
442 'weight' => 10,
443 'is_active' => 1,
444 'version' => 3,
445 );
446
447
448 $customGroup = Custom::createGroup($groupParams);
449 $activeGroup = CRM_Core_BAO_CustomGroup::getActiveGroups('Individual', 'civicrm/contact/view/cd', $contactId);
450 foreach ($activeGroup as $key => $value) {
451 if ($value['id'] == $customGroup->id) {
452 $this->assertEquals($value['path'], 'civicrm/contact/view/cd');
453 $this->assertEquals($value['title'], $customGrouptitle);
454 $query = 'reset=1&gid=' . $customGroup->id . '&cid=' . $contactId;
455 $this->assertEquals($value['query'], $query);
456 }
457 }
458
459 Custom::deleteGroup($customGroup);
460 Contact::delete($contactId);
461 }
462
463 /**
464 * Function to test create()
465 */
466 function testCreate() {
467 $params = array(
468 'title' => 'Test_Group_1',
469 'name' => 'test_group_1',
470 'extends' => array(0 => 'Individual', 1 => array()),
471 'weight' => 4,
472 'collapse_display' => 1,
473 'style' => 'Inline',
474 'help_pre' => 'This is Pre Help For Test Group 1',
475 'help_post' => 'This is Post Help For Test Group 1',
476 'is_active' => 1,
477 'version' => 3,
478 );
479 $customGroup = CRM_Core_BAO_CustomGroup::create($params);
480
481 $dbCustomGroupTitle = $this->assertDBNotNull('CRM_Core_DAO_CustomGroup', $customGroup->id, 'title', 'id',
482 'Database check for custom group record.'
483 );
484 $this->assertEquals($params['title'], $dbCustomGroupTitle);
485
486 $dbCustomGroupTableName = $this->assertDBNotNull('CRM_Core_DAO_CustomGroup', $customGroup->id, 'table_name', 'id',
487 'Database check for custom group record.'
488 );
489 $this->assertEquals(strtolower("civicrm_value_{$params['name']}_{$customGroup->id}"), $dbCustomGroupTableName,
490 "The table name should be suffixed with '_ID' unless specified.");
491
492 Custom::deleteGroup($customGroup);
493 }
494
495 /**
496 * Function to test create() given a table_name
497 */
498 function testCreateTableName() {
499 $params = array(
500 'title' => 'Test_Group_2',
501 'name' => 'test_group_2',
502 'table_name' => 'test_otherTableName',
503 'extends' => array(0 => 'Individual', 1 => array()),
504 'weight' => 4,
505 'collapse_display' => 1,
506 'style' => 'Inline',
507 'help_pre' => 'This is Pre Help For Test Group 1',
508 'help_post' => 'This is Post Help For Test Group 1',
509 'is_active' => 1,
510 'version' => 3,
511 );
512 $customGroup = CRM_Core_BAO_CustomGroup::create($params);
513
514 $dbCustomGroupTitle = $this->assertDBNotNull('CRM_Core_DAO_CustomGroup', $customGroup->id, 'title', 'id',
515 'Database check for custom group record.'
516 );
517 $this->assertEquals($params['title'], $dbCustomGroupTitle);
518
519 $dbCustomGroupTableName = $this->assertDBNotNull('CRM_Core_DAO_CustomGroup', $customGroup->id, 'table_name', 'id',
520 'Database check for custom group record.'
521 );
522 $this->assertEquals($params['table_name'], $dbCustomGroupTableName);
523
524 Custom::deleteGroup($customGroup);
525 }
526
527 /**
528 * Function to test isGroupEmpty()
529 */
530 function testIsGroupEmpty() {
531 $customGrouptitle = 'Test Custom Group';
532 $groupParams = array(
533 'title' => $customGrouptitle,
534 'name' => 'test_custom_group',
535 'style' => 'Tab',
536 'extends' => 'Individual',
537 'weight' => 10,
538 'is_active' => 1,
539 'version' => 3,
540 );
541
542 $customGroup = Custom::createGroup($groupParams);
543 $customGroupId = $customGroup->id;
544 $isEmptyGroup = CRM_Core_BAO_CustomGroup::isGroupEmpty($customGroupId);
545
546 $this->assertEquals($isEmptyGroup, TRUE, 'Check that custom Group is Empty.');
547 Custom::deleteGroup($customGroup);
548 }
549
550 /**
551 * Function to test getGroupTitles() with Invalid Params()
552 */
553 function testgetGroupTitlesWithInvalidParams() {
554 $params = array(99);
555 $groupTitles = CRM_Core_BAO_CustomGroup::getGroupTitles($params);
556 $this->assertTrue(empty($groupTitles), 'Check that no titles are recieved');
557 }
558
559 /**
560 * Function to test getGroupTitles()
561 */
562 function testgetGroupTitles() {
563 $customGrouptitle = 'Test Custom Group';
564 $groupParams = array(
565 'title' => $customGrouptitle,
566 'name' => 'test_custom_group',
567 'style' => 'Tab',
568 'extends' => 'Individual',
569 'weight' => 10,
570 'is_active' => 1,
571 'version' => 3,
572 );
573
574 $customGroup = Custom::createGroup($groupParams);
575
576 $customGroupId = $customGroup->id;
577
578 $customFieldLabel = 'Test Custom Field';
579 $fieldParams = array(
580 'custom_group_id' => $customGroupId,
581 'label' => $customFieldLabel,
582 'html_type' => 'Text',
583 'data_type' => 'String',
584 'is_required' => 1,
585 'is_searchable' => 0,
586 'is_active' => 1,
587 'version' => 3,
588 );
589
590 $customField = Custom::createField($fieldParams);
591 $customFieldId = $customField->id;
592
593 $params = array($customFieldId);
594
595 $groupTitles = CRM_Core_BAO_CustomGroup::getGroupTitles($params);
596
597 $this->assertEquals($groupTitles[$customFieldId]['groupTitle'], 'Test Custom Group', 'Check Group Title.');
598 Custom::deleteGroup($customGroup);
599 }
600 }
601