3 +--------------------------------------------------------------------+
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2020 |
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(['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', ['name' => 'Big Bank', 'label' => 'biggee', 'parent_id' => 'Organization']);
57 $customGroup = $this->CustomGroupCreate(['extends' => 'Organization', 'extends_entity_column_value' => ['Big_Bank']]);
58 $customField = $this->customFieldCreate(['custom_group_id' => $customGroup['id']]);
59 $result1 = CRM_Core_BAO_CustomGroup
::getTree('Organization', NULL, NULL, NULL, ['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, ['Small Kind Bank']);
68 catch (CRM_Core_Exception
$e) {
69 $this->customGroupDelete($customGroup['id']);
70 $this->callAPISuccess('ContactType', 'delete', ['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', ['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(['extends' => 'Organization', 'extends_entity_column_value' => ['Big_Bank']]);
83 $customField = $this->customFieldCreate(['custom_group_id' => $customGroup['id']]);
84 $result1 = CRM_Core_BAO_CustomGroup
::getTree('Organization', NULL, NULL, NULL, ['Big_Bank']);
85 $this->assertEquals('Custom Field', $result1[$customGroup['id']]['fields'][$customField['id']]['label']);
86 $this->customGroupDelete($customGroup['id']);
87 $this->callAPISuccess('ContactType', 'delete', ['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', ['name' => 'Big Bank', 'label' => 'biggee', 'parent_id' => 'Organization']);
95 $customGroup = $this->CustomGroupCreate(['extends' => 'Organization', 'extends_entity_column_value' => ['Big_Bank']]);
96 $customField = $this->customFieldCreate(['custom_group_id' => $customGroup['id']]);
97 $this->callAPISuccess('ContactType', 'create', ['id' => $contactType['id'], 'is_active' => 0]);
98 $result1 = CRM_Core_BAO_CustomGroup
::getTree('Organization', NULL, NULL, NULL, ['Big_Bank']);
99 $this->assertEquals('Custom Field', $result1[$customGroup['id']]['fields'][$customField['id']]['label']);
100 $this->customGroupDelete($customGroup['id']);
101 $this->callAPISuccess('ContactType', 'delete', ['id' => $contactType['id']]);
105 * Test calling GetTree for a custom field extending multiple subTypes.
107 public function testGetTreetContactSubTypeForMultipleSubTypes() {
108 $contactType1 = $this->callAPISuccess('ContactType', 'create', ['name' => 'Big Bank', 'label' => 'biggee', 'parent_id' => 'Organization']);
109 $contactType2 = $this->callAPISuccess('ContactType', 'create', ['name' => 'Small Bank', 'label' => 'smallee', 'parent_id' => 'Organization']);
110 $customGroup = $this->CustomGroupCreate(['extends' => 'Organization', 'extends_entity_column_value' => ['Big_Bank', 'Small_Bank']]);
111 $customField = $this->customFieldCreate(['custom_group_id' => $customGroup['id']]);
112 $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
);
113 $this->assertEquals('Custom Field', $result1[$customGroup['id']]['fields'][$customField['id']]['label']);
114 $this->customGroupDelete($customGroup['id']);
115 $this->callAPISuccess('ContactType', 'delete', ['id' => $contactType1['id']]);
116 $this->callAPISuccess('ContactType', 'delete', ['id' => $contactType2['id']]);
120 * Test calling GetTree for a custom field that extends a non numerical Event Type.
122 public function testGetTreeEventSubTypeAlphabetical() {
123 $eventType = $this->callAPISuccess('OptionValue', 'Create', ['option_group_id' => 'event_type', 'value' => 'meeting', 'label' => 'Meeting']);
124 $customGroup = $this->CustomGroupCreate(['extends' => 'Event', 'extends_entity_column_value' => ['Meeting']]);
125 $customField = $this->customFieldCreate(['custom_group_id' => $customGroup['id']]);
126 $result1 = CRM_Core_BAO_CustomGroup
::getTree('Event', NULL, NULL, NULL, CRM_Core_DAO
::VALUE_SEPARATOR
. 'meeting' . CRM_Core_DAO
::VALUE_SEPARATOR
);
127 $this->assertEquals('Custom Field', $result1[$customGroup['id']]['fields'][$customField['id']]['label']);
128 $this->customGroupDelete($customGroup['id']);
129 $this->callAPISuccess('OptionValue', 'delete', ['id' => $eventType['id']]);
133 * Test calling getTree with contact subtype data.
135 * Note that the function seems to support a range of formats so 3 are tested. Yay for
138 public function testGetTreeCampaignSubType() {
139 $sep = CRM_Core_DAO
::VALUE_SEPARATOR
;
140 $this->campaignCreate();
141 $this->campaignCreate();
142 $customGroup = $this->CustomGroupCreate([
143 'extends' => 'Campaign',
144 'extends_entity_column_value' => "{$sep}1{$sep}2{$sep}",
146 $customField = $this->customFieldCreate(['custom_group_id' => $customGroup['id']]);
147 $result1 = CRM_Core_BAO_CustomGroup
::getTree('Campaign', NULL, NULL, NULL, '\ 11\ 12\ 1');
148 $this->assertEquals('Custom Field', $result1[$customGroup['id']]['fields'][$customField['id']]['label']);
149 $this->customGroupDelete($customGroup['id']);
153 * Test calling getTree with contact subtype data.
155 public function testGetTreeActivitySubType() {
156 $customGroup = $this->CustomGroupCreate(['extends' => 'Activity', 'extends_entity_column_value' => 1]);
157 $customField = $this->customFieldCreate(['custom_group_id' => $customGroup['id']]);
158 $result = CRM_Core_BAO_CustomGroup
::getTree('Activity', NULL, NULL, NULL, 1);
159 $this->assertEquals('Custom Field', $result[$customGroup['id']]['fields'][$customField['id']]['label']);
160 $this->customGroupDelete($customGroup['id']);
164 * Test retrieve() with Empty Params.
166 public function testRetrieveEmptyParams() {
168 $customGroup = CRM_Core_BAO_CustomGroup
::retrieve($params, $dafaults);
169 $this->assertNull($customGroup, 'Check that no custom Group is retreived');
173 * Test retrieve() with Inalid Params
175 public function testRetrieveInvalidParams() {
176 $params = ['id' => 99];
177 $customGroup = CRM_Core_BAO_CustomGroup
::retrieve($params, $dafaults);
178 $this->assertNull($customGroup, 'Check that no custom Group is retreived');
184 public function testRetrieve() {
185 $customGroupTitle = 'Custom Group';
187 'title' => $customGroupTitle,
188 'name' => 'My_Custom_Group',
190 'extends' => 'Individual',
191 'help_pre' => 'Custom Group Help Pre',
192 'help_post' => 'Custom Group Help Post',
194 'collapse_display' => 1,
198 $customGroup = $this->customGroupCreate($groupParams);
200 $this->getAndCheck($groupParams, $customGroup['id'], 'CustomGroup');
206 public function testSetIsActive() {
207 $customGroupTitle = 'My Custom Group';
209 'title' => $customGroupTitle,
210 'name' => 'my_custom_group',
212 'extends' => 'Individual',
216 $customGroup = $this->customGroupCreate($groupParams);
217 $customGroupId = $customGroup['id'];
220 $result = CRM_Core_BAO_CustomGroup
::setIsActive($customGroupId, TRUE);
222 //check for object update
223 $this->assertEquals(TRUE, $result);
224 //check for is_active
225 $this->assertDBCompareValue('CRM_Core_DAO_CustomGroup', $customGroupId, 'is_active', 'id', 1,
226 'Database check for custom group is_active field.'
229 $this->customGroupDelete($customGroup['id']);
233 * Test getGroupDetail() with Empty Params
235 public function testGetGroupDetailEmptyParams() {
237 $customGroup = CRM_Core_BAO_CustomGroup
::getGroupDetail($customGroupId);
238 $this->assertTrue(empty($customGroup), 'Check that no custom Group details is retreived');
242 * Test getGroupDetail with Invalid Params.
244 public function testGetGroupDetailInvalidParams() {
246 $customGroup = CRM_Core_BAO_CustomGroup
::getGroupDetail($customGroupId);
247 $this->assertTrue(empty($customGroup), 'Check that no custom Group details is retreived');
251 * Test getGroupDetail().
253 public function testGetGroupDetail() {
254 $customGroupTitle = 'My Custom Group';
256 'title' => $customGroupTitle,
257 'name' => 'My_Custom_Group',
258 'extends' => 'Individual',
259 'help_pre' => 'Custom Group Help Pre',
260 'help_post' => 'Custom Group Help Post',
262 'collapse_display' => 1,
265 $customGroup = $this->customGroupCreate($groupParams);
266 $customGroupId = $customGroup['id'];
269 'custom_group_id' => $customGroupId,
270 'label' => 'Test Custom Field',
271 'html_type' => 'Text',
272 'data_type' => 'String',
274 'is_searchable' => 0,
278 $customField = $this->customFieldCreate($fieldParams);
279 $customFieldId = $customField['id'];
281 $groupTree = CRM_Core_BAO_CustomGroup
::getGroupDetail($customGroupId);
282 $this->assertDBNotNull('CRM_Core_DAO_CustomGroup', $customGroupId, 'title', 'id',
283 'Database check for custom group record.'
285 //check retieve values of custom group
286 unset($groupParams['is_active']);
287 unset($groupParams['title']);
288 unset($groupParams['version']);
289 $this->assertAttributesEquals($groupParams, $groupTree[$customGroupId]);
291 //check retieve values of custom field
292 unset($fieldParams['is_active']);
293 unset($fieldParams['custom_group_id']);
294 unset($fieldParams['version']);
295 $this->assertAttributesEquals($fieldParams, $groupTree[$customGroupId]['fields'][$customFieldId], " in line " . __LINE__
);
297 $this->customFieldDelete($customField['id']);
298 $this->customGroupDelete($customGroup['id']);
302 * Test getTitle() with Invalid Params()
304 public function testGetTitleWithInvalidParams() {
306 $customGroupTitle = CRM_Core_BAO_CustomGroup
::getTitle($params);
308 $this->assertNull($customGroupTitle, 'Check that no custom Group Title is retreived');
314 public function testGetTitle() {
315 $customGroupTitle = 'Custom Group';
317 'title' => $customGroupTitle,
318 'name' => 'my_custom_group',
320 'extends' => 'Individual',
324 $customGroup = $this->customGroupCreate($groupParams);
325 $customGroupId = $customGroup['id'];
327 //get the custom group title
328 $title = CRM_Core_BAO_CustomGroup
::getTitle($customGroupId);
330 //check for object update
331 $this->assertEquals($customGroupTitle, $title);
333 $this->customGroupDelete($customGroupId);
339 public function testDeleteGroup() {
340 $customGroupTitle = 'My Custom Group';
342 'title' => $customGroupTitle,
343 'name' => 'my_custom_group',
345 'extends' => 'Individual',
349 $customGroup = $this->customGroupCreate($groupParams);
350 $groupObject = new CRM_Core_BAO_CustomGroup();
351 $groupObject->id
= $customGroup['id'];
352 $groupObject->find(TRUE);
354 $isDelete = CRM_Core_BAO_CustomGroup
::deleteGroup($groupObject);
357 $this->assertEquals(TRUE, $isDelete);
358 $this->assertDBNull('CRM_Core_DAO_CustomGroup', $customGroup['id'], 'title', 'id',
359 'Database check for custom group record.'
366 public function testCreateTable() {
368 'title' => 'My Custom Group',
369 'name' => 'my_custom_group',
371 'extends' => 'Individual',
376 $customGroupBAO = new CRM_Core_BAO_CustomGroup();
377 $customGroupBAO->copyValues($groupParams);
378 $customGroup = $customGroupBAO->save();
379 $tableName = 'civicrm_value_test_group_' . $customGroup->id
;
380 $customGroup->table_name
= $tableName;
381 $customGroup = $customGroupBAO->save();
382 CRM_Core_BAO_CustomGroup
::createTable($customGroup);
383 $customGroupId = $customGroup->id
;
385 //check db for custom group.
386 $dbCustomGroupTitle = $this->assertDBNotNull('CRM_Core_DAO_CustomGroup', $customGroupId, 'title', 'id',
387 'Database check for custom group record.'
389 //check for custom group table name
390 $this->assertDBCompareValue('CRM_Core_DAO_CustomGroup', $customGroupId, 'table_name', 'id',
391 $tableName, 'Database check for custom group table name.'
394 $this->customGroupDelete($customGroup->id
);
398 * Test checkCustomField()
400 public function testCheckCustomField() {
402 'title' => 'My Custom Group',
403 'name' => 'my_custom_group',
404 'extends' => 'Individual',
405 'help_pre' => 'Custom Group Help Pre',
406 'help_post' => 'Custom Group Help Post',
408 'collapse_display' => 1,
411 $customGroup = $this->customGroupCreate($groupParams);
412 $this->assertNotNull($customGroup['id'], 'pre-requisite group not created successfully');
413 $customGroupId = $customGroup['id'];
415 $customFieldLabel = 'Test Custom Field';
417 'custom_group_id' => $customGroupId,
418 'label' => $customFieldLabel,
419 'html_type' => 'Text',
420 'data_type' => 'String',
422 'is_searchable' => 0,
426 $customField = $this->customFieldCreate($fieldParams);
427 $customField = $customField['values'][$customField['id']];
429 $customFieldId = $customField['id'];
431 //check db for custom field
432 $dbCustomFieldLabel = $this->assertDBNotNull('CRM_Core_DAO_CustomField', $customFieldId, 'label', 'id',
433 'Database check for custom field record.'
435 $this->assertEquals($customFieldLabel, $dbCustomFieldLabel);
437 //check the custom field type.
438 $params = ['Individual'];
439 $usedFor = CRM_Core_BAO_CustomGroup
::checkCustomField($customFieldId, $params);
440 $this->assertEquals(FALSE, $usedFor);
442 $params = ['Contribution', 'Membership', 'Participant'];
443 $usedFor = CRM_Core_BAO_CustomGroup
::checkCustomField($customFieldId, $params);
444 $this->assertEquals(TRUE, $usedFor);
446 $this->customFieldDelete($customField['id']);
447 $this->customGroupDelete($customGroup['id']);
451 * Test getActiveGroups() with Invalid Params()
453 public function testGetActiveGroupsWithInvalidParams() {
454 $contactId = $this->individualCreate();
455 $activeGroups = CRM_Core_BAO_CustomGroup
::getActiveGroups('ABC', 'civicrm/contact/view/cd', $contactId);
456 $this->assertEquals(empty($activeGroups), TRUE, 'Check that Emprt params are retreived');
459 public function testGetActiveGroups() {
460 $contactId = $this->individualCreate();
461 $customGroupTitle = 'Custom Group';
463 'title' => $customGroupTitle,
464 'name' => 'test_custom_group',
466 'extends' => 'Individual',
471 $customGroup = $this->customGroupCreate($groupParams);
472 $activeGroup = CRM_Core_BAO_CustomGroup
::getActiveGroups('Individual', 'civicrm/contact/view/cd', $contactId);
473 foreach ($activeGroup as $key => $value) {
474 if ($value['id'] == $customGroup['id']) {
475 $this->assertEquals($value['path'], 'civicrm/contact/view/cd');
476 $this->assertEquals($value['title'], $customGroupTitle);
477 $query = 'reset=1&gid=' . $customGroup['id'] . '&cid=' . $contactId;
478 $this->assertEquals($value['query'], $query);
482 $this->customGroupDelete($customGroup['id']);
483 $this->contactDelete($contactId);
489 public function testCreate() {
491 'title' => 'Test_Group_1',
492 'name' => 'test_group_1',
493 'extends' => [0 => 'Individual', 1 => []],
495 'collapse_display' => 1,
497 'help_pre' => 'This is Pre Help For Test Group 1',
498 'help_post' => 'This is Post Help For Test Group 1',
502 $customGroup = CRM_Core_BAO_CustomGroup
::create($params);
504 $dbCustomGroupTitle = $this->assertDBNotNull('CRM_Core_DAO_CustomGroup', $customGroup->id
, 'title', 'id',
505 'Database check for custom group record.'
507 $this->assertEquals($params['title'], $dbCustomGroupTitle);
509 $dbCustomGroupTableName = $this->assertDBNotNull('CRM_Core_DAO_CustomGroup', $customGroup->id
, 'table_name', 'id',
510 'Database check for custom group record.'
512 $this->assertEquals(strtolower("civicrm_value_{$params['name']}_{$customGroup->id}"), $dbCustomGroupTableName,
513 "The table name should be suffixed with '_ID' unless specified.");
515 $this->customGroupDelete($customGroup->id
);
519 * Test create() given a table_name
521 public function testCreateTableName() {
523 'title' => 'Test_Group_2',
524 'name' => 'test_group_2',
525 'table_name' => 'test_otherTableName',
526 'extends' => [0 => 'Individual', 1 => []],
528 'collapse_display' => 1,
530 'help_pre' => 'This is Pre Help For Test Group 1',
531 'help_post' => 'This is Post Help For Test Group 1',
534 $customGroup = CRM_Core_BAO_CustomGroup
::create($params);
536 $dbCustomGroupTitle = $this->assertDBNotNull('CRM_Core_DAO_CustomGroup', $customGroup->id
, 'title', 'id',
537 'Database check for custom group record.'
539 $this->assertEquals($params['title'], $dbCustomGroupTitle);
541 $dbCustomGroupTableName = $this->assertDBNotNull('CRM_Core_DAO_CustomGroup', $customGroup->id
, 'table_name', 'id',
542 'Database check for custom group record.'
544 $this->assertEquals($params['table_name'], $dbCustomGroupTableName);
546 $this->customGroupDelete($customGroup->id
);
550 * Test isGroupEmpty()
552 public function testIsGroupEmpty() {
553 $customGroupTitle = 'Test Custom Group';
555 'title' => $customGroupTitle,
556 'name' => 'test_custom_group',
558 'extends' => 'Individual',
563 $customGroup = $this->customGroupCreate($groupParams);
564 $customGroupId = $customGroup['id'];
565 $isEmptyGroup = CRM_Core_BAO_CustomGroup
::isGroupEmpty($customGroupId);
567 $this->assertEquals($isEmptyGroup, TRUE, 'Check that custom Group is Empty.');
568 $this->customGroupDelete($customGroup['id']);
572 * Test getGroupTitles() with Invalid Params()
574 public function testGetGroupTitlesWithInvalidParams() {
576 $groupTitles = CRM_Core_BAO_CustomGroup
::getGroupTitles($params);
577 $this->assertTrue(empty($groupTitles), 'Check that no titles are received');
581 * Test getGroupTitles()
583 public function testGetGroupTitles() {
585 'title' => 'Test Group',
586 'name' => 'test_custom_group',
588 'extends' => 'Individual',
593 $customGroup = $this->customGroupCreate($groupParams);
596 'label' => 'Custom Field',
597 'html_type' => 'Text',
598 'data_type' => 'String',
600 'is_searchable' => 0,
602 'custom_group_id' => $customGroup['id'],
605 $customField = $this->customFieldCreate($fieldParams);
606 $customFieldId = $customField['id'];
608 $params = [$customFieldId];
610 $groupTitles = CRM_Core_BAO_CustomGroup
::getGroupTitles($params);
612 $this->assertEquals($groupTitles[$customFieldId]['groupTitle'], 'Test Group', 'Check Group Title.');
613 $this->customGroupDelete($customGroup['id']);
617 * Test that passed dates are extracted from the url when processing custom data.
619 public function testExtractGetParamsReturnsDates() {
620 // Create a custom group to contain the custom field.
622 'title' => 'My Custom Group',
623 'name' => 'my_custom_group',
624 'extends' => 'Individual',
626 'collapse_display' => 1,
628 $customGroup = $this->customGroupCreate($groupParams);
629 $customGroupId = $customGroup['id'];
631 // Create teh custom field.
633 'custom_group_id' => $customGroupId,
634 'label' => 'My Custom Date Field',
635 'html_type' => 'Select Date',
636 'data_type' => 'Date',
638 'is_searchable' => 0,
640 'default_value' => '',
642 $customField = $this->customFieldCreate($fieldParams);
643 $customFieldId = $customField['id'];
645 // Create a form object. CRM_Core_BAO_CustomGroup::extractGetParams() will
646 // need this, along with the REQUEST_METHOD and controller too.
647 $form = new CRM_Contribute_Form_Contribution();
648 $_SERVER['REQUEST_METHOD'] = 'GET';
649 $form->controller
= new CRM_Core_Controller();
651 // Set the value in $_GET, then extract query string params with
652 $fieldName = 'custom_' . $customFieldId;
653 $_GET[$fieldName] = '2017-06-13';
654 $extractedGetParams = CRM_Core_BAO_CustomGroup
::extractGetParams($form, 'Individual');
656 $this->assertEquals($extractedGetParams[$fieldName], '2017-06-13');