3 +--------------------------------------------------------------------+
4 | Copyright CiviCRM LLC. All rights reserved. |
6 | This work is published under the GNU AGPLv3 license with some |
7 | permitted exceptions and without any warranty. For full license |
8 | and copyright information, see https://civicrm.org/licensing |
9 +--------------------------------------------------------------------+
13 * Class CRM_Core_BAO_CustomGroupTest
16 class CRM_Core_BAO_CustomGroupTest
extends CiviUnitTestCase
{
18 public function setUp() {
25 public function testGetTree() {
26 $customGroup = $this->CustomGroupCreate();
27 $customField = $this->customFieldCreate(['custom_group_id' => $customGroup['id']]);
28 $result = CRM_Core_BAO_CustomGroup
::getTree('Individual', NULL, $customGroup['id']);
29 $this->assertEquals('Custom Field', $result[$customGroup['id']]['fields'][$customField['id']]['label']);
30 $this->customGroupDelete($customGroup['id']);
34 * Test calling getTree with contact subtype data.
36 * Note that the function seems to support a range of formats so 3 are tested. Yay for
39 public function testGetTreeContactSubType() {
40 $contactType = $this->callAPISuccess('ContactType', 'create', ['name' => 'Big Bank', 'label' => 'biggee', 'parent_id' => 'Organization']);
41 $customGroup = $this->CustomGroupCreate(['extends' => 'Organization', 'extends_entity_column_value' => ['Big_Bank']]);
42 $customField = $this->customFieldCreate(['custom_group_id' => $customGroup['id']]);
43 $result1 = CRM_Core_BAO_CustomGroup
::getTree('Organization', NULL, NULL, NULL, ['Big_Bank']);
44 $this->assertEquals('Custom Field', $result1[$customGroup['id']]['fields'][$customField['id']]['label']);
45 $result = CRM_Core_BAO_CustomGroup
::getTree('Organization', NULL, NULL, NULL, CRM_Core_DAO
::VALUE_SEPARATOR
. 'Big_Bank' . CRM_Core_DAO
::VALUE_SEPARATOR
);
46 $this->assertEquals($result1, $result);
47 $result = CRM_Core_BAO_CustomGroup
::getTree('Organization', NULL, NULL, NULL, 'Big_Bank');
48 $this->assertEquals($result1, $result);
50 CRM_Core_BAO_CustomGroup
::getTree('Organization', NULL, NULL, NULL, ['Small Kind Bank']);
52 catch (CRM_Core_Exception
$e) {
53 $this->customGroupDelete($customGroup['id']);
54 $this->callAPISuccess('ContactType', 'delete', ['id' => $contactType['id']]);
57 $this->fail('There is no such thing as a small kind bank');
61 * Test calling getTree for a custom field extending a renamed contact type.
63 public function testGetTreeContactSubTypeForNameChangedContactType() {
64 $contactType = $this->callAPISuccess('ContactType', 'create', ['name' => 'Big Bank', 'label' => 'biggee', 'parent_id' => 'Organization']);
65 CRM_Core_DAO
::executeQuery('UPDATE civicrm_contact_type SET label = "boo" WHERE name = "Organization"');
66 $customGroup = $this->CustomGroupCreate(['extends' => 'Organization', 'extends_entity_column_value' => ['Big_Bank']]);
67 $customField = $this->customFieldCreate(['custom_group_id' => $customGroup['id']]);
68 $result1 = CRM_Core_BAO_CustomGroup
::getTree('Organization', NULL, NULL, NULL, ['Big_Bank']);
69 $this->assertEquals('Custom Field', $result1[$customGroup['id']]['fields'][$customField['id']]['label']);
70 $this->customGroupDelete($customGroup['id']);
71 $this->callAPISuccess('ContactType', 'delete', ['id' => $contactType['id']]);
75 * Test calling getTree for a custom field extending a disabled contact type.
77 public function testGetTreeContactSubTypeForDisabledChangedContactType() {
78 $contactType = $this->callAPISuccess('ContactType', 'create', ['name' => 'Big Bank', 'label' => 'biggee', 'parent_id' => 'Organization']);
79 $customGroup = $this->CustomGroupCreate(['extends' => 'Organization', 'extends_entity_column_value' => ['Big_Bank']]);
80 $customField = $this->customFieldCreate(['custom_group_id' => $customGroup['id']]);
81 $this->callAPISuccess('ContactType', 'create', ['id' => $contactType['id'], 'is_active' => 0]);
82 $result1 = CRM_Core_BAO_CustomGroup
::getTree('Organization', NULL, NULL, NULL, ['Big_Bank']);
83 $this->assertEquals('Custom Field', $result1[$customGroup['id']]['fields'][$customField['id']]['label']);
84 $this->customGroupDelete($customGroup['id']);
85 $this->callAPISuccess('ContactType', 'delete', ['id' => $contactType['id']]);
89 * Test calling GetTree for a custom field extending multiple subTypes.
91 public function testGetTreetContactSubTypeForMultipleSubTypes() {
92 $contactType1 = $this->callAPISuccess('ContactType', 'create', ['name' => 'Big Bank', 'label' => 'biggee', 'parent_id' => 'Organization']);
93 $contactType2 = $this->callAPISuccess('ContactType', 'create', ['name' => 'Small Bank', 'label' => 'smallee', 'parent_id' => 'Organization']);
94 $customGroup = $this->CustomGroupCreate(['extends' => 'Organization', 'extends_entity_column_value' => ['Big_Bank', 'Small_Bank']]);
95 $customField = $this->customFieldCreate(['custom_group_id' => $customGroup['id']]);
96 $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
);
97 $this->assertEquals('Custom Field', $result1[$customGroup['id']]['fields'][$customField['id']]['label']);
98 $this->customGroupDelete($customGroup['id']);
99 $this->callAPISuccess('ContactType', 'delete', ['id' => $contactType1['id']]);
100 $this->callAPISuccess('ContactType', 'delete', ['id' => $contactType2['id']]);
104 * Test calling GetTree for a custom field that extends a non numerical Event Type.
106 public function testGetTreeEventSubTypeAlphabetical() {
107 $eventType = $this->callAPISuccess('OptionValue', 'Create', ['option_group_id' => 'event_type', 'value' => 'meeting', 'label' => 'Meeting']);
108 $customGroup = $this->CustomGroupCreate(['extends' => 'Event', 'extends_entity_column_value' => ['Meeting']]);
109 $customField = $this->customFieldCreate(['custom_group_id' => $customGroup['id']]);
110 $result1 = CRM_Core_BAO_CustomGroup
::getTree('Event', NULL, NULL, NULL, CRM_Core_DAO
::VALUE_SEPARATOR
. 'meeting' . CRM_Core_DAO
::VALUE_SEPARATOR
);
111 $this->assertEquals('Custom Field', $result1[$customGroup['id']]['fields'][$customField['id']]['label']);
112 $this->customGroupDelete($customGroup['id']);
113 $this->callAPISuccess('OptionValue', 'delete', ['id' => $eventType['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([
127 'extends' => 'Campaign',
128 'extends_entity_column_value' => "{$sep}1{$sep}2{$sep}",
130 $customField = $this->customFieldCreate(['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(['extends' => 'Activity', 'extends_entity_column_value' => 1]);
141 $customField = $this->customFieldCreate(['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 = ['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 = 'Custom Group';
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,
182 $customGroup = $this->customGroupCreate($groupParams);
184 $this->getAndCheck($groupParams, $customGroup['id'], 'CustomGroup');
190 public function testSetIsActive() {
191 $customGroupTitle = 'My Custom Group';
193 'title' => $customGroupTitle,
194 'name' => 'my_custom_group',
196 'extends' => 'Individual',
200 $customGroup = $this->customGroupCreate($groupParams);
201 $customGroupId = $customGroup['id'];
204 $result = CRM_Core_BAO_CustomGroup
::setIsActive($customGroupId, TRUE);
206 //check for object update
207 $this->assertEquals(TRUE, $result);
208 //check for is_active
209 $this->assertDBCompareValue('CRM_Core_DAO_CustomGroup', $customGroupId, 'is_active', 'id', 1,
210 'Database check for custom group is_active field.'
213 $this->customGroupDelete($customGroup['id']);
217 * Test getGroupDetail() with Empty Params
219 public function testGetGroupDetailEmptyParams() {
221 $customGroup = CRM_Core_BAO_CustomGroup
::getGroupDetail($customGroupId);
222 $this->assertTrue(empty($customGroup), 'Check that no custom Group details is retreived');
226 * Test getGroupDetail with Invalid Params.
228 public function testGetGroupDetailInvalidParams() {
230 $customGroup = CRM_Core_BAO_CustomGroup
::getGroupDetail($customGroupId);
231 $this->assertTrue(empty($customGroup), 'Check that no custom Group details is retreived');
235 * Test getGroupDetail().
237 public function testGetGroupDetail() {
238 $customGroupTitle = 'My Custom Group';
240 'title' => $customGroupTitle,
241 'name' => 'My_Custom_Group',
242 'extends' => 'Individual',
243 'help_pre' => 'Custom Group Help Pre',
244 'help_post' => 'Custom Group Help Post',
246 'collapse_display' => 1,
249 $customGroup = $this->customGroupCreate($groupParams);
250 $customGroupId = $customGroup['id'];
253 'custom_group_id' => $customGroupId,
254 'label' => 'Test Custom Field',
255 'html_type' => 'Text',
256 'data_type' => 'String',
258 'is_searchable' => 0,
262 $customField = $this->customFieldCreate($fieldParams);
263 $customFieldId = $customField['id'];
265 $groupTree = CRM_Core_BAO_CustomGroup
::getGroupDetail($customGroupId);
266 $this->assertDBNotNull('CRM_Core_DAO_CustomGroup', $customGroupId, 'title', 'id',
267 'Database check for custom group record.'
269 //check retieve values of custom group
270 unset($groupParams['is_active']);
271 unset($groupParams['title']);
272 unset($groupParams['version']);
273 $this->assertAttributesEquals($groupParams, $groupTree[$customGroupId]);
275 //check retieve values of custom field
276 unset($fieldParams['is_active']);
277 unset($fieldParams['custom_group_id']);
278 unset($fieldParams['version']);
279 $this->assertAttributesEquals($fieldParams, $groupTree[$customGroupId]['fields'][$customFieldId], " in line " . __LINE__
);
281 $this->customFieldDelete($customField['id']);
282 $this->customGroupDelete($customGroup['id']);
286 * Test getTitle() with Invalid Params()
288 public function testGetTitleWithInvalidParams() {
290 $customGroupTitle = CRM_Core_BAO_CustomGroup
::getTitle($params);
292 $this->assertNull($customGroupTitle, 'Check that no custom Group Title is retreived');
298 public function testGetTitle() {
299 $customGroupTitle = 'Custom Group';
301 'title' => $customGroupTitle,
302 'name' => 'my_custom_group',
304 'extends' => 'Individual',
308 $customGroup = $this->customGroupCreate($groupParams);
309 $customGroupId = $customGroup['id'];
311 //get the custom group title
312 $title = CRM_Core_BAO_CustomGroup
::getTitle($customGroupId);
314 //check for object update
315 $this->assertEquals($customGroupTitle, $title);
317 $this->customGroupDelete($customGroupId);
323 public function testDeleteGroup() {
324 $customGroupTitle = 'My Custom Group';
326 'title' => $customGroupTitle,
327 'name' => 'my_custom_group',
329 'extends' => 'Individual',
333 $customGroup = $this->customGroupCreate($groupParams);
334 $groupObject = new CRM_Core_BAO_CustomGroup();
335 $groupObject->id
= $customGroup['id'];
336 $groupObject->find(TRUE);
338 $isDelete = CRM_Core_BAO_CustomGroup
::deleteGroup($groupObject);
341 $this->assertEquals(TRUE, $isDelete);
342 $this->assertDBNull('CRM_Core_DAO_CustomGroup', $customGroup['id'], 'title', 'id',
343 'Database check for custom group record.'
350 public function testCreateTable() {
352 'title' => 'My Custom Group',
353 'name' => 'my_custom_group',
355 'extends' => 'Individual',
360 $customGroupBAO = new CRM_Core_BAO_CustomGroup();
361 $customGroupBAO->copyValues($groupParams);
362 $customGroup = $customGroupBAO->save();
363 $tableName = 'civicrm_value_test_group_' . $customGroup->id
;
364 $customGroup->table_name
= $tableName;
365 $customGroup = $customGroupBAO->save();
366 CRM_Core_BAO_CustomGroup
::createTable($customGroup);
367 $customGroupId = $customGroup->id
;
369 //check db for custom group.
370 $dbCustomGroupTitle = $this->assertDBNotNull('CRM_Core_DAO_CustomGroup', $customGroupId, 'title', 'id',
371 'Database check for custom group record.'
373 //check for custom group table name
374 $this->assertDBCompareValue('CRM_Core_DAO_CustomGroup', $customGroupId, 'table_name', 'id',
375 $tableName, 'Database check for custom group table name.'
378 $this->customGroupDelete($customGroup->id
);
382 * Test checkCustomField()
384 public function testCheckCustomField() {
386 'title' => 'My Custom Group',
387 'name' => 'my_custom_group',
388 'extends' => 'Individual',
389 'help_pre' => 'Custom Group Help Pre',
390 'help_post' => 'Custom Group Help Post',
392 'collapse_display' => 1,
395 $customGroup = $this->customGroupCreate($groupParams);
396 $this->assertNotNull($customGroup['id'], 'pre-requisite group not created successfully');
397 $customGroupId = $customGroup['id'];
399 $customFieldLabel = 'Test Custom Field';
401 'custom_group_id' => $customGroupId,
402 'label' => $customFieldLabel,
403 'html_type' => 'Text',
404 'data_type' => 'String',
406 'is_searchable' => 0,
410 $customField = $this->customFieldCreate($fieldParams);
411 $customField = $customField['values'][$customField['id']];
413 $customFieldId = $customField['id'];
415 //check db for custom field
416 $dbCustomFieldLabel = $this->assertDBNotNull('CRM_Core_DAO_CustomField', $customFieldId, 'label', 'id',
417 'Database check for custom field record.'
419 $this->assertEquals($customFieldLabel, $dbCustomFieldLabel);
421 //check the custom field type.
422 $params = ['Individual'];
423 $usedFor = CRM_Core_BAO_CustomGroup
::checkCustomField($customFieldId, $params);
424 $this->assertEquals(FALSE, $usedFor);
426 $params = ['Contribution', 'Membership', 'Participant'];
427 $usedFor = CRM_Core_BAO_CustomGroup
::checkCustomField($customFieldId, $params);
428 $this->assertEquals(TRUE, $usedFor);
430 $this->customFieldDelete($customField['id']);
431 $this->customGroupDelete($customGroup['id']);
435 * Test getActiveGroups() with Invalid Params()
437 public function testGetActiveGroupsWithInvalidParams() {
438 $contactId = $this->individualCreate();
439 $activeGroups = CRM_Core_BAO_CustomGroup
::getActiveGroups('ABC', 'civicrm/contact/view/cd', $contactId);
440 $this->assertEquals(empty($activeGroups), TRUE, 'Check that Emprt params are retreived');
443 public function testGetActiveGroups() {
444 $contactId = $this->individualCreate();
445 $customGroupTitle = 'Custom Group';
447 'title' => $customGroupTitle,
448 'name' => 'test_custom_group',
450 'extends' => 'Individual',
455 $customGroup = $this->customGroupCreate($groupParams);
456 $activeGroup = CRM_Core_BAO_CustomGroup
::getActiveGroups('Individual', 'civicrm/contact/view/cd', $contactId);
457 foreach ($activeGroup as $key => $value) {
458 if ($value['id'] == $customGroup['id']) {
459 $this->assertEquals($value['path'], 'civicrm/contact/view/cd');
460 $this->assertEquals($value['title'], $customGroupTitle);
461 $query = 'reset=1&gid=' . $customGroup['id'] . '&cid=' . $contactId;
462 $this->assertEquals($value['query'], $query);
466 $this->customGroupDelete($customGroup['id']);
467 $this->contactDelete($contactId);
473 public function testCreate() {
475 'title' => 'Test_Group_1',
476 'name' => 'test_group_1',
477 'extends' => [0 => 'Individual', 1 => []],
479 'collapse_display' => 1,
481 'help_pre' => 'This is Pre Help For Test Group 1',
482 'help_post' => 'This is Post Help For Test Group 1',
486 $customGroup = CRM_Core_BAO_CustomGroup
::create($params);
488 $dbCustomGroupTitle = $this->assertDBNotNull('CRM_Core_DAO_CustomGroup', $customGroup->id
, 'title', 'id',
489 'Database check for custom group record.'
491 $this->assertEquals($params['title'], $dbCustomGroupTitle);
493 $dbCustomGroupTableName = $this->assertDBNotNull('CRM_Core_DAO_CustomGroup', $customGroup->id
, 'table_name', 'id',
494 'Database check for custom group record.'
496 $this->assertEquals(strtolower("civicrm_value_{$params['name']}_{$customGroup->id}"), $dbCustomGroupTableName,
497 "The table name should be suffixed with '_ID' unless specified.");
499 $this->customGroupDelete($customGroup->id
);
503 * Test create() given a table_name
505 public function testCreateTableName() {
507 'title' => 'Test_Group_2',
508 'name' => 'test_group_2',
509 'table_name' => 'test_otherTableName',
510 'extends' => [0 => 'Individual', 1 => []],
512 'collapse_display' => 1,
514 'help_pre' => 'This is Pre Help For Test Group 1',
515 'help_post' => 'This is Post Help For Test Group 1',
518 $customGroup = CRM_Core_BAO_CustomGroup
::create($params);
520 $dbCustomGroupTitle = $this->assertDBNotNull('CRM_Core_DAO_CustomGroup', $customGroup->id
, 'title', 'id',
521 'Database check for custom group record.'
523 $this->assertEquals($params['title'], $dbCustomGroupTitle);
525 $dbCustomGroupTableName = $this->assertDBNotNull('CRM_Core_DAO_CustomGroup', $customGroup->id
, 'table_name', 'id',
526 'Database check for custom group record.'
528 $this->assertEquals($params['table_name'], $dbCustomGroupTableName);
530 $this->customGroupDelete($customGroup->id
);
534 * Test isGroupEmpty()
536 public function testIsGroupEmpty() {
537 $customGroupTitle = 'Test Custom Group';
539 'title' => $customGroupTitle,
540 'name' => 'test_custom_group',
542 'extends' => 'Individual',
547 $customGroup = $this->customGroupCreate($groupParams);
548 $customGroupId = $customGroup['id'];
549 $isEmptyGroup = CRM_Core_BAO_CustomGroup
::isGroupEmpty($customGroupId);
551 $this->assertEquals($isEmptyGroup, TRUE, 'Check that custom Group is Empty.');
552 $this->customGroupDelete($customGroup['id']);
556 * Test getGroupTitles() with Invalid Params()
558 public function testGetGroupTitlesWithInvalidParams() {
560 $groupTitles = CRM_Core_BAO_CustomGroup
::getGroupTitles($params);
561 $this->assertTrue(empty($groupTitles), 'Check that no titles are received');
565 * Test getGroupTitles()
567 public function testGetGroupTitles() {
569 'title' => 'Test Group',
570 'name' => 'test_custom_group',
572 'extends' => 'Individual',
577 $customGroup = $this->customGroupCreate($groupParams);
580 'label' => 'Custom Field',
581 'html_type' => 'Text',
582 'data_type' => 'String',
584 'is_searchable' => 0,
586 'custom_group_id' => $customGroup['id'],
589 $customField = $this->customFieldCreate($fieldParams);
590 $customFieldId = $customField['id'];
592 $params = [$customFieldId];
594 $groupTitles = CRM_Core_BAO_CustomGroup
::getGroupTitles($params);
596 $this->assertEquals($groupTitles[$customFieldId]['groupTitle'], 'Test Group', 'Check Group Title.');
597 $this->customGroupDelete($customGroup['id']);
601 * Test that passed dates are extracted from the url when processing custom data.
603 public function testExtractGetParamsReturnsDates() {
604 // Create a custom group to contain the custom field.
606 'title' => 'My Custom Group',
607 'name' => 'my_custom_group',
608 'extends' => 'Individual',
610 'collapse_display' => 1,
612 $customGroup = $this->customGroupCreate($groupParams);
613 $customGroupId = $customGroup['id'];
615 // Create teh custom field.
617 'custom_group_id' => $customGroupId,
618 'label' => 'My Custom Date Field',
619 'html_type' => 'Select Date',
620 'data_type' => 'Date',
622 'is_searchable' => 0,
624 'default_value' => '',
626 $customField = $this->customFieldCreate($fieldParams);
627 $customFieldId = $customField['id'];
629 // Create a form object. CRM_Core_BAO_CustomGroup::extractGetParams() will
630 // need this, along with the REQUEST_METHOD and controller too.
631 $form = new CRM_Contribute_Form_Contribution();
632 $_SERVER['REQUEST_METHOD'] = 'GET';
633 $form->controller
= new CRM_Core_Controller();
635 // Set the value in $_GET, then extract query string params with
636 $fieldName = 'custom_' . $customFieldId;
637 $_GET[$fieldName] = '2017-06-13';
638 $extractedGetParams = CRM_Core_BAO_CustomGroup
::extractGetParams($form, 'Individual');
640 $this->assertEquals($extractedGetParams[$fieldName], '2017-06-13');