3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.5 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2014 |
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
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. |
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. |
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 +--------------------------------------------------------------------+
30 require_once 'CiviTest/CiviUnitTestCase.php';
31 require_once 'CiviTest/Contact.php';
32 require_once 'CiviTest/Custom.php';
35 * Class CRM_Core_BAO_CustomGroupTest
37 class CRM_Core_BAO_CustomGroupTest
extends CiviUnitTestCase
{
43 'name' => 'CustomGroup BAOs',
44 'description' => 'Test all Core_BAO_CustomGroup methods.',
45 'group' => 'CiviCRM BAO Tests',
54 * Function to test getTree()
56 function testGetTree() {
58 $contactId = Contact
::createIndividual();
59 $customGrouptitle = 'My Custom Group';
61 'title' => $customGrouptitle,
62 'name' => 'my_custom_group',
64 'extends' => 'Individual',
69 $customGroup = Custom
::createGroup($groupParams);
71 $customGroupId = $customGroup->id
;
74 'groupId' => $customGroupId,
75 'dataType' => 'String',
79 $customField = Custom
::createField($params, $fields);
81 $getTree = CRM_Core_BAO_CustomGroup
::getTree('Individual', $formParams, $customGroupId);
83 $dbCustomGroupTitle = $this->assertDBNotNull('CRM_Core_DAO_CustomGroup', $customGroupId, 'title', 'id',
84 'Database check for custom group record.'
87 Custom
::deleteField($customField);
88 Custom
::deleteGroup($customGroup);
89 Contact
::delete($contactId);
94 * Function to test retrieve() with Empty Params
96 function testRetrieveEmptyParams() {
98 $customGroup = CRM_Core_BAO_CustomGroup
::retrieve($params, $dafaults);
99 $this->assertNull($customGroup, 'Check that no custom Group is retreived');
103 * Function to test retrieve() with Inalid Params
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');
112 * Function to test retrieve()
114 function testRetrieve() {
115 $customGrouptitle = 'My Custom Group';
116 $groupParams = array(
117 'title' => $customGrouptitle,
118 'name' => 'My_Custom_Group',
120 'extends' => 'Individual',
121 'help_pre' => 'Custom Group Help Pre',
122 'help_post' => 'Custom Group Help Post',
124 'collapse_display' => 1,
129 $customGroup = Custom
::createGroup($groupParams);
130 $customGroupId = $customGroup->id
;
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.'
138 $this->assertEquals($customGrouptitle, $dbCustomGroupTitle);
139 //check retieve values
140 unset($groupParams['version']);
141 $this->assertAttributesEquals($groupParams, $dafaults);
143 //cleanup DB by deleting customGroup
144 Custom
::deleteGroup($customGroup);
148 * Function to test setIsActive()
150 function testSetIsActive() {
151 $customGrouptitle = 'My Custom Group';
152 $groupParams = array(
153 'title' => $customGrouptitle,
154 'name' => 'my_custom_group',
156 'extends' => 'Individual',
161 $customGroup = Custom
::createGroup($groupParams);
162 $customGroupId = $customGroup->id
;
165 $result = CRM_Core_BAO_CustomGroup
::setIsActive($customGroupId, TRUE);
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.'
173 //cleanup DB by deleting customGroup
174 Custom
::deleteGroup($customGroup);
178 * Function to test getGroupDetail() with Empty Params
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');
187 * Function to test getGroupDetail() with Inalid Params
189 function testGetGroupDetailInvalidParams() {
191 $customGroup = CRM_Core_BAO_CustomGroup
::getGroupDetail($customGroupId);
192 $this->assertTrue(empty($customGroup), 'Check that no custom Group details is retreived');
196 * Function to test getGroupDetail()
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',
207 'collapse_display' => 1,
211 $customGroup = Custom
::createGroup($groupParams);
212 $customGroupId = $customGroup->id
;
214 $fieldParams = array(
215 'custom_group_id' => $customGroupId,
216 'label' => 'Test Custom Field',
217 'html_type' => 'Text',
218 'data_type' => 'String',
220 'is_searchable' => 0,
225 $customField = Custom
::createField($fieldParams);
226 $customFieldId = $customField->id
;
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.'
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]);
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__
);
244 //cleanup DB by deleting customGroup
245 Custom
::deleteField($customField);
246 Custom
::deleteGroup($customGroup);
250 * Function to test getTitle() with Invalid Params()
252 function testGetTitleWithInvalidParams() {
254 $customGroupTitle = CRM_Core_BAO_CustomGroup
::getTitle($params);
256 $this->assertNull($customGroupTitle, 'Check that no custom Group Title is retreived');
260 * Function to test getTitle()
262 function testGetTitle() {
263 $customGrouptitle = 'My Custom Group';
264 $groupParams = array(
265 'title' => $customGrouptitle,
266 'name' => 'my_custom_group',
268 'extends' => 'Individual',
273 $customGroup = Custom
::createGroup($groupParams);
274 $customGroupId = $customGroup->id
;
276 //get the custom group title
277 $title = CRM_Core_BAO_CustomGroup
::getTitle($customGroupId);
279 //check for object update
280 $this->assertEquals($customGrouptitle, $title);
282 //cleanup DB by deleting customGroup
283 Custom
::deleteGroup($customGroup);
287 * Function to test deleteGroup()
289 function testDeleteGroup() {
290 $customGrouptitle = 'My Custom Group';
291 $groupParams = array(
292 'title' => $customGrouptitle,
293 'name' => 'my_custom_group',
295 'extends' => 'Individual',
300 $customGroup = Custom
::createGroup($groupParams);
302 $customGroupId = $customGroup->id
;
304 //get the custom group title
305 $dbCustomGroupTitle = $this->assertDBNotNull('CRM_Core_DAO_CustomGroup', $customGroupId, 'title', 'id',
306 'Database check for custom group record.'
308 //check for group title
309 $this->assertEquals($customGrouptitle, $dbCustomGroupTitle);
312 $isDelete = CRM_Core_BAO_CustomGroup
::deleteGroup($customGroup);
315 $this->assertEquals(TRUE, $isDelete);
318 $this->assertDBNull('CRM_Core_DAO_CustomGroup', $customGroupId, 'title', 'id',
319 'Database check for custom group record.'
324 * Function to test createTable()
326 function testCreateTable() {
327 $customGrouptitle = 'My Custom Group';
328 $groupParams = array(
329 'title' => $customGrouptitle,
330 'name' => 'my_custom_group',
332 'extends' => 'Individual',
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
;
346 //check db for custom group.
347 $dbCustomGroupTitle = $this->assertDBNotNull('CRM_Core_DAO_CustomGroup', $customGroupId, 'title', 'id',
348 'Database check for custom group record.'
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.'
355 //check for group title
356 $this->assertEquals($customGrouptitle, $dbCustomGroupTitle);
358 //cleanup DB by deleting customGroup
359 Custom
::deleteGroup($customGroup);
363 * Function to test checkCustomField()
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',
374 'collapse_display' => 1,
378 $customGroup = Custom
::createGroup($groupParams);
379 $this->assertNotNull($customGroup->id
, 'pre-requisite group not created successfully');
380 $customGroupId = $customGroup->id
;
382 $customFieldLabel = 'Test Custom Field';
383 $fieldParams = array(
384 'custom_group_id' => $customGroupId,
385 'label' => $customFieldLabel,
386 'html_type' => 'Text',
387 'data_type' => 'String',
389 'is_searchable' => 0,
394 $customField = Custom
::createField($fieldParams);
395 $this->assertNotNull($customField->id
, 'pre-requisite field not created successfully');
397 $customFieldId = $customField->id
;
399 //check db for custom group
400 $dbCustomGroupTitle = $this->assertDBNotNull('CRM_Core_DAO_CustomGroup', $customGroupId, 'title', 'id',
401 'Database check for custom group record.'
403 $this->assertEquals($customGroupTitle, $dbCustomGroupTitle);
405 //check db for custom field
406 $dbCustomFieldLabel = $this->assertDBNotNull('CRM_Core_DAO_CustomField', $customFieldId, 'label', 'id',
407 'Database check for custom field record.'
409 $this->assertEquals($customFieldLabel, $dbCustomFieldLabel);
411 //check the custom field type.
412 $params = array('Individual');
413 $usedFor = CRM_Core_BAO_CustomGroup
::checkCustomField($customFieldId, $params);
414 $this->assertEquals(FALSE, $usedFor);
416 $params = array('Contribution', 'Membership', 'Participant');
417 $usedFor = CRM_Core_BAO_CustomGroup
::checkCustomField($customFieldId, $params);
418 $this->assertEquals(TRUE, $usedFor);
420 //cleanup DB by deleting customGroup
421 Custom
::deleteField($customField);
422 Custom
::deleteGroup($customGroup);
426 * Function to test getActiveGroups() with Invalid Params()
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');
434 function testGetActiveGroups() {
435 $contactId = Contact
::createIndividual();
436 $customGrouptitle = 'Test Custom Group';
437 $groupParams = array(
438 'title' => $customGrouptitle,
439 'name' => 'test_custom_group',
441 'extends' => 'Individual',
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);
459 Custom
::deleteGroup($customGroup);
460 Contact
::delete($contactId);
464 * Function to test create()
466 function testCreate() {
468 'title' => 'Test_Group_1',
469 'name' => 'test_group_1',
470 'extends' => array(0 => 'Individual', 1 => array()),
472 'collapse_display' => 1,
474 'help_pre' => 'This is Pre Help For Test Group 1',
475 'help_post' => 'This is Post Help For Test Group 1',
479 $customGroup = CRM_Core_BAO_CustomGroup
::create($params);
481 $dbCustomGroupTitle = $this->assertDBNotNull('CRM_Core_DAO_CustomGroup', $customGroup->id
, 'title', 'id',
482 'Database check for custom group record.'
484 $this->assertEquals($params['title'], $dbCustomGroupTitle);
486 $dbCustomGroupTableName = $this->assertDBNotNull('CRM_Core_DAO_CustomGroup', $customGroup->id
, 'table_name', 'id',
487 'Database check for custom group record.'
489 $this->assertEquals(strtolower("civicrm_value_{$params['name']}_{$customGroup->id}"), $dbCustomGroupTableName,
490 "The table name should be suffixed with '_ID' unless specified.");
492 Custom
::deleteGroup($customGroup);
496 * Function to test create() given a table_name
498 function testCreateTableName() {
500 'title' => 'Test_Group_2',
501 'name' => 'test_group_2',
502 'table_name' => 'test_otherTableName',
503 'extends' => array(0 => 'Individual', 1 => array()),
505 'collapse_display' => 1,
507 'help_pre' => 'This is Pre Help For Test Group 1',
508 'help_post' => 'This is Post Help For Test Group 1',
512 $customGroup = CRM_Core_BAO_CustomGroup
::create($params);
514 $dbCustomGroupTitle = $this->assertDBNotNull('CRM_Core_DAO_CustomGroup', $customGroup->id
, 'title', 'id',
515 'Database check for custom group record.'
517 $this->assertEquals($params['title'], $dbCustomGroupTitle);
519 $dbCustomGroupTableName = $this->assertDBNotNull('CRM_Core_DAO_CustomGroup', $customGroup->id
, 'table_name', 'id',
520 'Database check for custom group record.'
522 $this->assertEquals($params['table_name'], $dbCustomGroupTableName);
524 Custom
::deleteGroup($customGroup);
528 * Function to test isGroupEmpty()
530 function testIsGroupEmpty() {
531 $customGrouptitle = 'Test Custom Group';
532 $groupParams = array(
533 'title' => $customGrouptitle,
534 'name' => 'test_custom_group',
536 'extends' => 'Individual',
542 $customGroup = Custom
::createGroup($groupParams);
543 $customGroupId = $customGroup->id
;
544 $isEmptyGroup = CRM_Core_BAO_CustomGroup
::isGroupEmpty($customGroupId);
546 $this->assertEquals($isEmptyGroup, TRUE, 'Check that custom Group is Empty.');
547 Custom
::deleteGroup($customGroup);
551 * Function to test getGroupTitles() with Invalid Params()
553 function testgetGroupTitlesWithInvalidParams() {
555 $groupTitles = CRM_Core_BAO_CustomGroup
::getGroupTitles($params);
556 $this->assertTrue(empty($groupTitles), 'Check that no titles are recieved');
560 * Function to test getGroupTitles()
562 function testgetGroupTitles() {
563 $customGrouptitle = 'Test Custom Group';
564 $groupParams = array(
565 'title' => $customGrouptitle,
566 'name' => 'test_custom_group',
568 'extends' => 'Individual',
574 $customGroup = Custom
::createGroup($groupParams);
576 $customGroupId = $customGroup->id
;
578 $customFieldLabel = 'Test Custom Field';
579 $fieldParams = array(
580 'custom_group_id' => $customGroupId,
581 'label' => $customFieldLabel,
582 'html_type' => 'Text',
583 'data_type' => 'String',
585 'is_searchable' => 0,
590 $customField = Custom
::createField($fieldParams);
591 $customFieldId = $customField->id
;
593 $params = array($customFieldId);
595 $groupTitles = CRM_Core_BAO_CustomGroup
::getGroupTitles($params);
597 $this->assertEquals($groupTitles[$customFieldId]['groupTitle'], 'Test Custom Group', 'Check Group Title.');
598 Custom
::deleteGroup($customGroup);