3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.7 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2016 |
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 +--------------------------------------------------------------------+
29 * Class CRM_Core_BAO_CustomGroupTest
32 class CRM_Core_BAO_CustomGroupTest
extends CiviUnitTestCase
{
34 public function setUp() {
41 public function testGetTree() {
42 $customGroup = $this->CustomGroupCreate();
43 $customField = $this->customFieldCreate(array('custom_group_id' => $customGroup['id']));
44 $result = CRM_Core_BAO_CustomGroup
::getTree('Individual', NULL, $customGroup['id']);
45 $this->assertEquals('Custom Field', $result[$customGroup['id']]['fields'][$customField['id']]['label']);
46 $this->customGroupDelete($customGroup['id']);
50 * Test calling getTree with contact subtype data.
52 * Note that the function seems to support a range of formats so 3 are tested. Yay for
55 public function testGetTreeContactSubType() {
56 $contactType = $this->callAPISuccess('ContactType', 'create', array('name' => 'Big Bank', 'label' => 'biggee', 'parent_id' => 'Organization'));
57 $customGroup = $this->CustomGroupCreate(array('extends' => 'Organization', 'extends_entity_column_value' => array('Big_Bank')));
58 $customField = $this->customFieldCreate(array('custom_group_id' => $customGroup['id']));
59 $result1 = CRM_Core_BAO_CustomGroup
::getTree('Organization', NULL, NULL, NULL, array('Big_Bank'));
60 $this->assertEquals('Custom Field', $result1[$customGroup['id']]['fields'][$customField['id']]['label']);
61 $result = CRM_Core_BAO_CustomGroup
::getTree('Organization', NULL, NULL, NULL, CRM_Core_DAO
::VALUE_SEPARATOR
. 'Big_Bank' . CRM_Core_DAO
::VALUE_SEPARATOR
);
62 $this->assertEquals($result1, $result);
63 $result = CRM_Core_BAO_CustomGroup
::getTree('Organization', NULL, NULL, NULL, 'Big_Bank');
64 $this->assertEquals($result1, $result);
66 CRM_Core_BAO_CustomGroup
::getTree('Organization', NULL, NULL, NULL, array('Small Kind Bank'));
68 catch (CRM_Core_Exception
$e) {
69 $this->customGroupDelete($customGroup['id']);
70 $this->callAPISuccess('ContactType', 'delete', array('id' => $contactType['id']));
73 $this->fail('There is no such thing as a small kind bank');
77 * Test calling getTree for a custom field extending a renamed contact type.
79 public function testGetTreeContactSubTypeForNameChangedContactType() {
80 $contactType = $this->callAPISuccess('ContactType', 'create', array('name' => 'Big Bank', 'label' => 'biggee', 'parent_id' => 'Organization'));
81 CRM_Core_DAO
::executeQuery('UPDATE civicrm_contact_type SET label = "boo" WHERE name = "Organization"');
82 $customGroup = $this->CustomGroupCreate(array('extends' => 'Organization', 'extends_entity_column_value' => array('Big_Bank')));
83 $customField = $this->customFieldCreate(array('custom_group_id' => $customGroup['id']));
84 $result1 = CRM_Core_BAO_CustomGroup
::getTree('Organization', NULL, NULL, NULL, array('Big_Bank'));
85 $this->assertEquals('Custom Field', $result1[$customGroup['id']]['fields'][$customField['id']]['label']);
86 $this->customGroupDelete($customGroup['id']);
87 $this->callAPISuccess('ContactType', 'delete', array('id' => $contactType['id']));
91 * Test calling getTree for a custom field extending a disabled contact type.
93 public function testGetTreeContactSubTypeForDisabledChangedContactType() {
94 $contactType = $this->callAPISuccess('ContactType', 'create', array('name' => 'Big Bank', 'label' => 'biggee', 'parent_id' => 'Organization'));
95 $customGroup = $this->CustomGroupCreate(array('extends' => 'Organization', 'extends_entity_column_value' => array('Big_Bank')));
96 $customField = $this->customFieldCreate(array('custom_group_id' => $customGroup['id']));
97 $this->callAPISuccess('ContactType', 'create', array('id' => $contactType['id'], 'is_active' => 0));
98 $result1 = CRM_Core_BAO_CustomGroup
::getTree('Organization', NULL, NULL, NULL, array('Big_Bank'));
99 $this->assertEquals('Custom Field', $result1[$customGroup['id']]['fields'][$customField['id']]['label']);
100 $this->customGroupDelete($customGroup['id']);
101 $this->callAPISuccess('ContactType', 'delete', array('id' => $contactType['id']));
104 public function testGetTreetContactSubTypeForMultipleSubTypes() {
105 $contactType1 = $this->callApiSuccess('ContactType', 'create', array('name' => 'Big Bank', 'label' => 'biggee', 'parent_id' => 'Organization'));
106 $contactType2 = $this->callAPISuccess('ContactType', 'create', array('name' => 'Small Bank', 'label' => 'smallee', 'parent_id' => 'Organization'));
107 $customGroup = $this->CustomGroupCreate(array('extends' => 'Organization', 'extends_entity_column_value' => array('Big_Bank', 'Small_Bank')));
108 $customField = $this->customFieldCreate(array('custom_group_id' => $customGroup['id']));
109 $result1 = CRM_Core_BAO_CustomGroup
::getTree('Organization', NULL, NULL, NULL, CRM_Core_DAO
::VALUE_SEPARATOR
. 'Big_Bank' . CRM_Core_DAO
::VALUE_SEPARATOR
. 'Small_Bank' . CRM_Core_DAO
::VALUE_SEPARATOR
);
110 $this->assertEquals('Custom Field', $result1[$customGroup['id']]['fields'][$customField['id']]['label']);
111 $this->customGroupDelete($customGroup['id']);
112 $this->callAPISuccess('ContactType', 'delete', array('id' => $contactType1['id']));
113 $this->callAPISuccess('ContactType', 'delete', array('id' => $contactType2['id']));
117 * Test calling getTree with contact subtype data.
119 * Note that the function seems to support a range of formats so 3 are tested. Yay for
122 public function testGetTreeCampaignSubType() {
123 $sep = CRM_Core_DAO
::VALUE_SEPARATOR
;
124 $this->campaignCreate();
125 $this->campaignCreate();
126 $customGroup = $this->CustomGroupCreate(array(
127 'extends' => 'Campaign',
128 'extends_entity_column_value' => "{$sep}1{$sep}2{$sep}",
130 $customField = $this->customFieldCreate(array('custom_group_id' => $customGroup['id']));
131 $result1 = CRM_Core_BAO_CustomGroup
::getTree('Campaign', NULL, NULL, NULL, '\ 11\ 12\ 1');
132 $this->assertEquals('Custom Field', $result1[$customGroup['id']]['fields'][$customField['id']]['label']);
133 $this->customGroupDelete($customGroup['id']);
137 * Test calling getTree with contact subtype data.
139 public function testGetTreeActivitySubType() {
140 $customGroup = $this->CustomGroupCreate(array('extends' => 'Activity', 'extends_entity_column_value' => 1));
141 $customField = $this->customFieldCreate(array('custom_group_id' => $customGroup['id']));
142 $result = CRM_Core_BAO_CustomGroup
::getTree('Activity', NULL, NULL, NULL, 1);
143 $this->assertEquals('Custom Field', $result[$customGroup['id']]['fields'][$customField['id']]['label']);
144 $this->customGroupDelete($customGroup['id']);
148 * Test retrieve() with Empty Params.
150 public function testRetrieveEmptyParams() {
152 $customGroup = CRM_Core_BAO_CustomGroup
::retrieve($params, $dafaults);
153 $this->assertNull($customGroup, 'Check that no custom Group is retreived');
157 * Test retrieve() with Inalid Params
159 public function testRetrieveInvalidParams() {
160 $params = array('id' => 99);
161 $customGroup = CRM_Core_BAO_CustomGroup
::retrieve($params, $dafaults);
162 $this->assertNull($customGroup, 'Check that no custom Group is retreived');
168 public function testRetrieve() {
169 $customGrouptitle = 'My Custom Group';
170 $groupParams = array(
171 'title' => $customGrouptitle,
172 'name' => 'My_Custom_Group',
174 'extends' => 'Individual',
175 'help_pre' => 'Custom Group Help Pre',
176 'help_post' => 'Custom Group Help Post',
178 'collapse_display' => 1,
183 $customGroup = Custom
::createGroup($groupParams);
184 $customGroupId = $customGroup->id
;
186 $params = array('id' => $customGroupId);
187 $customGroup = CRM_Core_BAO_CustomGroup
::retrieve($params, $dafaults);
188 $dbCustomGroupTitle = $this->assertDBNotNull('CRM_Core_DAO_CustomGroup', $customGroupId, 'title', 'id',
189 'Database check for custom group record.'
192 $this->assertEquals($customGrouptitle, $dbCustomGroupTitle);
193 //check retieve values
194 unset($groupParams['version']);
195 $this->assertAttributesEquals($groupParams, $dafaults);
197 //cleanup DB by deleting customGroup
198 Custom
::deleteGroup($customGroup);
204 public function testSetIsActive() {
205 $customGrouptitle = 'My Custom Group';
206 $groupParams = array(
207 'title' => $customGrouptitle,
208 'name' => 'my_custom_group',
210 'extends' => 'Individual',
215 $customGroup = Custom
::createGroup($groupParams);
216 $customGroupId = $customGroup->id
;
219 $result = CRM_Core_BAO_CustomGroup
::setIsActive($customGroupId, TRUE);
221 //check for object update
222 $this->assertEquals(TRUE, $result);
223 //check for is_active
224 $this->assertDBCompareValue('CRM_Core_DAO_CustomGroup', $customGroupId, 'is_active', 'id', 1,
225 'Database check for custom group is_active field.'
227 //cleanup DB by deleting customGroup
228 Custom
::deleteGroup($customGroup);
232 * Test getGroupDetail() with Empty Params
234 public function testGetGroupDetailEmptyParams() {
235 $customGroupId = array();
236 $customGroup = CRM_Core_BAO_CustomGroup
::getGroupDetail($customGroupId);
237 $this->assertTrue(empty($customGroup), 'Check that no custom Group details is retreived');
241 * Test getGroupDetail() with Inalid Params
243 public function testGetGroupDetailInvalidParams() {
245 $customGroup = CRM_Core_BAO_CustomGroup
::getGroupDetail($customGroupId);
246 $this->assertTrue(empty($customGroup), 'Check that no custom Group details is retreived');
250 * Test getGroupDetail()
252 public function testGetGroupDetail() {
253 $customGrouptitle = 'My Custom Group';
254 $groupParams = array(
255 'title' => $customGrouptitle,
256 'name' => 'My_Custom_Group',
257 'extends' => 'Individual',
258 'help_pre' => 'Custom Group Help Pre',
259 'help_post' => 'Custom Group Help Post',
261 'collapse_display' => 1,
265 $customGroup = Custom
::createGroup($groupParams);
266 $customGroupId = $customGroup->id
;
268 $fieldParams = array(
269 'custom_group_id' => $customGroupId,
270 'label' => 'Test Custom Field',
271 'html_type' => 'Text',
272 'data_type' => 'String',
274 'is_searchable' => 0,
279 $customField = Custom
::createField($fieldParams);
280 $customFieldId = $customField->id
;
282 $groupTree = CRM_Core_BAO_CustomGroup
::getGroupDetail($customGroupId);
283 $dbCustomGroupTitle = $this->assertDBNotNull('CRM_Core_DAO_CustomGroup', $customGroupId, 'title', 'id',
284 'Database check for custom group record.'
286 //check retieve values of custom group
287 unset($groupParams['is_active']);
288 unset($groupParams['title']);
289 unset($groupParams['version']);
290 $this->assertAttributesEquals($groupParams, $groupTree[$customGroupId]);
292 //check retieve values of custom field
293 unset($fieldParams['is_active']);
294 unset($fieldParams['custom_group_id']);
295 unset($fieldParams['version']);
296 $this->assertAttributesEquals($fieldParams, $groupTree[$customGroupId]['fields'][$customFieldId], " in line " . __LINE__
);
298 //cleanup DB by deleting customGroup
299 Custom
::deleteField($customField);
300 Custom
::deleteGroup($customGroup);
304 * Test getTitle() with Invalid Params()
306 public function testGetTitleWithInvalidParams() {
308 $customGroupTitle = CRM_Core_BAO_CustomGroup
::getTitle($params);
310 $this->assertNull($customGroupTitle, 'Check that no custom Group Title is retreived');
316 public function testGetTitle() {
317 $customGrouptitle = 'My Custom Group';
318 $groupParams = array(
319 'title' => $customGrouptitle,
320 'name' => 'my_custom_group',
322 'extends' => 'Individual',
327 $customGroup = Custom
::createGroup($groupParams);
328 $customGroupId = $customGroup->id
;
330 //get the custom group title
331 $title = CRM_Core_BAO_CustomGroup
::getTitle($customGroupId);
333 //check for object update
334 $this->assertEquals($customGrouptitle, $title);
336 //cleanup DB by deleting customGroup
337 Custom
::deleteGroup($customGroup);
343 public function testDeleteGroup() {
344 $customGrouptitle = 'My Custom Group';
345 $groupParams = array(
346 'title' => $customGrouptitle,
347 'name' => 'my_custom_group',
349 'extends' => 'Individual',
354 $customGroup = Custom
::createGroup($groupParams);
356 $customGroupId = $customGroup->id
;
358 //get the custom group title
359 $dbCustomGroupTitle = $this->assertDBNotNull('CRM_Core_DAO_CustomGroup', $customGroupId, 'title', 'id',
360 'Database check for custom group record.'
362 //check for group title
363 $this->assertEquals($customGrouptitle, $dbCustomGroupTitle);
366 $isDelete = CRM_Core_BAO_CustomGroup
::deleteGroup($customGroup);
369 $this->assertEquals(TRUE, $isDelete);
372 $this->assertDBNull('CRM_Core_DAO_CustomGroup', $customGroupId, 'title', 'id',
373 'Database check for custom group record.'
380 public function testCreateTable() {
381 $customGrouptitle = 'My Custom Group';
382 $groupParams = array(
383 'title' => $customGrouptitle,
384 'name' => 'my_custom_group',
386 'extends' => 'Individual',
391 $customGroupBAO = new CRM_Core_BAO_CustomGroup();
392 $customGroupBAO->copyValues($groupParams);
393 $customGroup = $customGroupBAO->save();
394 $tableName = 'civicrm_value_test_group_' . $customGroup->id
;
395 $customGroup->table_name
= $tableName;
396 $customGroup = $customGroupBAO->save();
397 $customTable = CRM_Core_BAO_CustomGroup
::createTable($customGroup);
398 $customGroupId = $customGroup->id
;
400 //check db for custom group.
401 $dbCustomGroupTitle = $this->assertDBNotNull('CRM_Core_DAO_CustomGroup', $customGroupId, 'title', 'id',
402 'Database check for custom group record.'
404 //check for custom group table name
405 $this->assertDBCompareValue('CRM_Core_DAO_CustomGroup', $customGroupId, 'table_name', 'id',
406 $tableName, 'Database check for custom group table name.'
409 //check for group title
410 $this->assertEquals($customGrouptitle, $dbCustomGroupTitle);
412 //cleanup DB by deleting customGroup
413 Custom
::deleteGroup($customGroup);
417 * Test checkCustomField()
419 public function testCheckCustomField() {
420 $customGroupTitle = 'My Custom Group';
421 $groupParams = array(
422 'title' => $customGroupTitle,
423 'name' => 'my_custom_group',
424 'extends' => 'Individual',
425 'help_pre' => 'Custom Group Help Pre',
426 'help_post' => 'Custom Group Help Post',
428 'collapse_display' => 1,
432 $customGroup = Custom
::createGroup($groupParams);
433 $this->assertNotNull($customGroup->id
, 'pre-requisite group not created successfully');
434 $customGroupId = $customGroup->id
;
436 $customFieldLabel = 'Test Custom Field';
437 $fieldParams = array(
438 'custom_group_id' => $customGroupId,
439 'label' => $customFieldLabel,
440 'html_type' => 'Text',
441 'data_type' => 'String',
443 'is_searchable' => 0,
448 $customField = Custom
::createField($fieldParams);
449 $this->assertNotNull($customField->id
, 'pre-requisite field not created successfully');
451 $customFieldId = $customField->id
;
453 //check db for custom group
454 $dbCustomGroupTitle = $this->assertDBNotNull('CRM_Core_DAO_CustomGroup', $customGroupId, 'title', 'id',
455 'Database check for custom group record.'
457 $this->assertEquals($customGroupTitle, $dbCustomGroupTitle);
459 //check db for custom field
460 $dbCustomFieldLabel = $this->assertDBNotNull('CRM_Core_DAO_CustomField', $customFieldId, 'label', 'id',
461 'Database check for custom field record.'
463 $this->assertEquals($customFieldLabel, $dbCustomFieldLabel);
465 //check the custom field type.
466 $params = array('Individual');
467 $usedFor = CRM_Core_BAO_CustomGroup
::checkCustomField($customFieldId, $params);
468 $this->assertEquals(FALSE, $usedFor);
470 $params = array('Contribution', 'Membership', 'Participant');
471 $usedFor = CRM_Core_BAO_CustomGroup
::checkCustomField($customFieldId, $params);
472 $this->assertEquals(TRUE, $usedFor);
474 //cleanup DB by deleting customGroup
475 Custom
::deleteField($customField);
476 Custom
::deleteGroup($customGroup);
480 * Test getActiveGroups() with Invalid Params()
482 public function testGetActiveGroupsWithInvalidParams() {
483 $contactId = $this->individualCreate();
484 $activeGroups = CRM_Core_BAO_CustomGroup
::getActiveGroups('ABC', 'civicrm/contact/view/cd', $contactId);
485 $this->assertEquals(empty($activeGroups), TRUE, 'Check that Emprt params are retreived');
488 public function testGetActiveGroups() {
489 $contactId = $this->individualCreate();
490 $customGrouptitle = 'Test Custom Group';
491 $groupParams = array(
492 'title' => $customGrouptitle,
493 'name' => 'test_custom_group',
495 'extends' => 'Individual',
501 $customGroup = Custom
::createGroup($groupParams);
502 $activeGroup = CRM_Core_BAO_CustomGroup
::getActiveGroups('Individual', 'civicrm/contact/view/cd', $contactId);
503 foreach ($activeGroup as $key => $value) {
504 if ($value['id'] == $customGroup->id
) {
505 $this->assertEquals($value['path'], 'civicrm/contact/view/cd');
506 $this->assertEquals($value['title'], $customGrouptitle);
507 $query = 'reset=1&gid=' . $customGroup->id
. '&cid=' . $contactId;
508 $this->assertEquals($value['query'], $query);
512 Custom
::deleteGroup($customGroup);
513 $this->contactDelete($contactId);
519 public function testCreate() {
521 'title' => 'Test_Group_1',
522 'name' => 'test_group_1',
523 'extends' => array(0 => 'Individual', 1 => array()),
525 'collapse_display' => 1,
527 'help_pre' => 'This is Pre Help For Test Group 1',
528 'help_post' => 'This is Post Help For Test Group 1',
532 $customGroup = CRM_Core_BAO_CustomGroup
::create($params);
534 $dbCustomGroupTitle = $this->assertDBNotNull('CRM_Core_DAO_CustomGroup', $customGroup->id
, 'title', 'id',
535 'Database check for custom group record.'
537 $this->assertEquals($params['title'], $dbCustomGroupTitle);
539 $dbCustomGroupTableName = $this->assertDBNotNull('CRM_Core_DAO_CustomGroup', $customGroup->id
, 'table_name', 'id',
540 'Database check for custom group record.'
542 $this->assertEquals(strtolower("civicrm_value_{$params['name']}_{$customGroup->id}"), $dbCustomGroupTableName,
543 "The table name should be suffixed with '_ID' unless specified.");
545 Custom
::deleteGroup($customGroup);
549 * Test create() given a table_name
551 public function testCreateTableName() {
553 'title' => 'Test_Group_2',
554 'name' => 'test_group_2',
555 'table_name' => 'test_otherTableName',
556 'extends' => array(0 => 'Individual', 1 => array()),
558 'collapse_display' => 1,
560 'help_pre' => 'This is Pre Help For Test Group 1',
561 'help_post' => 'This is Post Help For Test Group 1',
565 $customGroup = CRM_Core_BAO_CustomGroup
::create($params);
567 $dbCustomGroupTitle = $this->assertDBNotNull('CRM_Core_DAO_CustomGroup', $customGroup->id
, 'title', 'id',
568 'Database check for custom group record.'
570 $this->assertEquals($params['title'], $dbCustomGroupTitle);
572 $dbCustomGroupTableName = $this->assertDBNotNull('CRM_Core_DAO_CustomGroup', $customGroup->id
, 'table_name', 'id',
573 'Database check for custom group record.'
575 $this->assertEquals($params['table_name'], $dbCustomGroupTableName);
577 Custom
::deleteGroup($customGroup);
581 * Test isGroupEmpty()
583 public function testIsGroupEmpty() {
584 $customGrouptitle = 'Test Custom Group';
585 $groupParams = array(
586 'title' => $customGrouptitle,
587 'name' => 'test_custom_group',
589 'extends' => 'Individual',
595 $customGroup = Custom
::createGroup($groupParams);
596 $customGroupId = $customGroup->id
;
597 $isEmptyGroup = CRM_Core_BAO_CustomGroup
::isGroupEmpty($customGroupId);
599 $this->assertEquals($isEmptyGroup, TRUE, 'Check that custom Group is Empty.');
600 Custom
::deleteGroup($customGroup);
604 * Test getGroupTitles() with Invalid Params()
606 public function testgetGroupTitlesWithInvalidParams() {
608 $groupTitles = CRM_Core_BAO_CustomGroup
::getGroupTitles($params);
609 $this->assertTrue(empty($groupTitles), 'Check that no titles are recieved');
613 * Test getGroupTitles()
615 public function testgetGroupTitles() {
616 $customGrouptitle = 'Test Custom Group';
617 $groupParams = array(
618 'title' => $customGrouptitle,
619 'name' => 'test_custom_group',
621 'extends' => 'Individual',
627 $customGroup = Custom
::createGroup($groupParams);
629 $customGroupId = $customGroup->id
;
631 $customFieldLabel = 'Test Custom Field';
632 $fieldParams = array(
633 'custom_group_id' => $customGroupId,
634 'label' => $customFieldLabel,
635 'html_type' => 'Text',
636 'data_type' => 'String',
638 'is_searchable' => 0,
643 $customField = Custom
::createField($fieldParams);
644 $customFieldId = $customField->id
;
646 $params = array($customFieldId);
648 $groupTitles = CRM_Core_BAO_CustomGroup
::getGroupTitles($params);
650 $this->assertEquals($groupTitles[$customFieldId]['groupTitle'], 'Test Custom Group', 'Check Group Title.');
651 Custom
::deleteGroup($customGroup);