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
{
21 public function testGetTree() {
22 $customGroup = $this->CustomGroupCreate();
23 $customField = $this->customFieldCreate(['custom_group_id' => $customGroup['id']]);
24 $result = CRM_Core_BAO_CustomGroup
::getTree('Individual', NULL, $customGroup['id']);
25 $this->assertEquals('Custom Field', $result[$customGroup['id']]['fields'][$customField['id']]['label']);
26 $this->customGroupDelete($customGroup['id']);
30 * Test calling getTree with contact subtype data.
32 * Note that the function seems to support a range of formats so 3 are tested. Yay for
35 public function testGetTreeContactSubType() {
36 $contactType = $this->callAPISuccess('ContactType', 'create', ['name' => 'Big Bank', 'label' => 'biggee', 'parent_id' => 'Organization']);
37 $customGroup = $this->CustomGroupCreate(['extends' => 'Organization', 'extends_entity_column_value' => ['Big_Bank']]);
38 $customField = $this->customFieldCreate(['custom_group_id' => $customGroup['id']]);
39 $result1 = CRM_Core_BAO_CustomGroup
::getTree('Organization', NULL, NULL, NULL, ['Big_Bank']);
40 $this->assertEquals('Custom Field', $result1[$customGroup['id']]['fields'][$customField['id']]['label']);
41 $result = CRM_Core_BAO_CustomGroup
::getTree('Organization', NULL, NULL, NULL, CRM_Core_DAO
::VALUE_SEPARATOR
. 'Big_Bank' . CRM_Core_DAO
::VALUE_SEPARATOR
);
42 $this->assertEquals($result1, $result);
43 $result = CRM_Core_BAO_CustomGroup
::getTree('Organization', NULL, NULL, NULL, 'Big_Bank');
44 $this->assertEquals($result1, $result);
46 CRM_Core_BAO_CustomGroup
::getTree('Organization', NULL, NULL, NULL, ['Small Kind Bank']);
48 catch (CRM_Core_Exception
$e) {
49 $this->customGroupDelete($customGroup['id']);
50 $this->callAPISuccess('ContactType', 'delete', ['id' => $contactType['id']]);
53 $this->fail('There is no such thing as a small kind bank');
57 * Test calling getTree for a custom field extending a renamed contact type.
59 public function testGetTreeContactSubTypeForNameChangedContactType() {
60 $contactType = $this->callAPISuccess('ContactType', 'create', ['name' => 'Big Bank', 'label' => 'biggee', 'parent_id' => 'Organization']);
61 CRM_Core_DAO
::executeQuery('UPDATE civicrm_contact_type SET label = "boo" WHERE name = "Organization"');
62 $customGroup = $this->CustomGroupCreate(['extends' => 'Organization', 'extends_entity_column_value' => ['Big_Bank']]);
63 $customField = $this->customFieldCreate(['custom_group_id' => $customGroup['id']]);
64 $result1 = CRM_Core_BAO_CustomGroup
::getTree('Organization', NULL, NULL, NULL, ['Big_Bank']);
65 $this->assertEquals('Custom Field', $result1[$customGroup['id']]['fields'][$customField['id']]['label']);
66 $this->customGroupDelete($customGroup['id']);
67 $this->callAPISuccess('ContactType', 'delete', ['id' => $contactType['id']]);
71 * Test calling getTree for a custom field extending a disabled contact type.
73 public function testGetTreeContactSubTypeForDisabledChangedContactType() {
74 $contactType = $this->callAPISuccess('ContactType', 'create', ['name' => 'Big Bank', 'label' => 'biggee', 'parent_id' => 'Organization']);
75 $customGroup = $this->CustomGroupCreate(['extends' => 'Organization', 'extends_entity_column_value' => ['Big_Bank']]);
76 $customField = $this->customFieldCreate(['custom_group_id' => $customGroup['id']]);
77 $this->callAPISuccess('ContactType', 'create', ['id' => $contactType['id'], 'is_active' => 0]);
78 $result1 = CRM_Core_BAO_CustomGroup
::getTree('Organization', NULL, NULL, NULL, ['Big_Bank']);
79 $this->assertEquals('Custom Field', $result1[$customGroup['id']]['fields'][$customField['id']]['label']);
80 $this->customGroupDelete($customGroup['id']);
81 $this->callAPISuccess('ContactType', 'delete', ['id' => $contactType['id']]);
85 * Test calling GetTree for a custom field extending multiple subTypes.
87 public function testGetTreetContactSubTypeForMultipleSubTypes() {
88 $contactType1 = $this->callAPISuccess('ContactType', 'create', ['name' => 'Big Bank', 'label' => 'biggee', 'parent_id' => 'Organization']);
89 $contactType2 = $this->callAPISuccess('ContactType', 'create', ['name' => 'Small Bank', 'label' => 'smallee', 'parent_id' => 'Organization']);
90 $customGroup = $this->CustomGroupCreate(['extends' => 'Organization', 'extends_entity_column_value' => ['Big_Bank', 'Small_Bank']]);
91 $customField = $this->customFieldCreate(['custom_group_id' => $customGroup['id']]);
92 $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
);
93 $this->assertEquals('Custom Field', $result1[$customGroup['id']]['fields'][$customField['id']]['label']);
94 $this->customGroupDelete($customGroup['id']);
95 $this->callAPISuccess('ContactType', 'delete', ['id' => $contactType1['id']]);
96 $this->callAPISuccess('ContactType', 'delete', ['id' => $contactType2['id']]);
100 * Test calling GetTree for a custom field that extends a non numerical Event Type.
102 public function testGetTreeEventSubTypeAlphabetical() {
103 $eventType = $this->callAPISuccess('OptionValue', 'Create', ['option_group_id' => 'event_type', 'value' => 'meeting', 'label' => 'Meeting']);
104 $customGroup = $this->CustomGroupCreate(['extends' => 'Event', 'extends_entity_column_value' => ['Meeting']]);
105 $customField = $this->customFieldCreate(['custom_group_id' => $customGroup['id']]);
106 $result1 = CRM_Core_BAO_CustomGroup
::getTree('Event', NULL, NULL, NULL, CRM_Core_DAO
::VALUE_SEPARATOR
. 'meeting' . CRM_Core_DAO
::VALUE_SEPARATOR
);
107 $this->assertEquals('Custom Field', $result1[$customGroup['id']]['fields'][$customField['id']]['label']);
108 $this->customGroupDelete($customGroup['id']);
109 $this->callAPISuccess('OptionValue', 'delete', ['id' => $eventType['id']]);
113 * Test calling getTree with contact subtype data.
115 * Note that the function seems to support a range of formats so 3 are tested. Yay for
118 public function testGetTreeCampaignSubType() {
119 $sep = CRM_Core_DAO
::VALUE_SEPARATOR
;
120 $this->campaignCreate();
121 $this->campaignCreate();
122 $customGroup = $this->CustomGroupCreate([
123 'extends' => 'Campaign',
124 'extends_entity_column_value' => "{$sep}1{$sep}2{$sep}",
126 $customField = $this->customFieldCreate(['custom_group_id' => $customGroup['id']]);
127 $result1 = CRM_Core_BAO_CustomGroup
::getTree('Campaign', NULL, NULL, NULL, '\ 11\ 12\ 1');
128 $this->assertEquals('Custom Field', $result1[$customGroup['id']]['fields'][$customField['id']]['label']);
129 $this->customGroupDelete($customGroup['id']);
133 * Test calling getTree with contact subtype data.
135 public function testGetTreeActivitySubType() {
136 $customGroup = $this->CustomGroupCreate(['extends' => 'Activity', 'extends_entity_column_value' => 1]);
137 $customField = $this->customFieldCreate(['custom_group_id' => $customGroup['id']]);
138 $result = CRM_Core_BAO_CustomGroup
::getTree('Activity', NULL, NULL, NULL, 1);
139 $this->assertEquals('Custom Field', $result[$customGroup['id']]['fields'][$customField['id']]['label']);
140 $this->customGroupDelete($customGroup['id']);
144 * Test retrieve() with Empty Params.
146 public function testRetrieveEmptyParams() {
148 $customGroup = CRM_Core_BAO_CustomGroup
::retrieve($params, $dafaults);
149 $this->assertNull($customGroup, 'Check that no custom Group is retreived');
153 * Test retrieve() with Inalid Params
155 public function testRetrieveInvalidParams() {
156 $params = ['id' => 99];
157 $customGroup = CRM_Core_BAO_CustomGroup
::retrieve($params, $dafaults);
158 $this->assertNull($customGroup, 'Check that no custom Group is retreived');
164 public function testRetrieve() {
165 $customGroupTitle = 'Custom Group';
167 'title' => $customGroupTitle,
168 'name' => 'My_Custom_Group',
170 'extends' => 'Individual',
171 'help_pre' => 'Custom Group Help Pre',
172 'help_post' => 'Custom Group Help Post',
174 'collapse_display' => 1,
178 $customGroup = $this->customGroupCreate($groupParams);
180 $this->getAndCheck($groupParams, $customGroup['id'], 'CustomGroup');
186 public function testSetIsActive() {
187 $customGroupTitle = 'My Custom Group';
189 'title' => $customGroupTitle,
190 'name' => 'my_custom_group',
192 'extends' => 'Individual',
196 $customGroup = $this->customGroupCreate($groupParams);
197 $customGroupId = $customGroup['id'];
200 $result = CRM_Core_BAO_CustomGroup
::setIsActive($customGroupId, TRUE);
202 //check for object update
203 $this->assertEquals(TRUE, $result);
204 //check for is_active
205 $this->assertDBCompareValue('CRM_Core_DAO_CustomGroup', $customGroupId, 'is_active', 'id', 1,
206 'Database check for custom group is_active field.'
209 $this->customGroupDelete($customGroup['id']);
213 * Test getGroupDetail() with Empty Params
215 public function testGetGroupDetailEmptyParams() {
217 $customGroup = CRM_Core_BAO_CustomGroup
::getGroupDetail($customGroupId);
218 $this->assertTrue(empty($customGroup), 'Check that no custom Group details is retreived');
222 * Test getGroupDetail with Invalid Params.
224 public function testGetGroupDetailInvalidParams() {
226 $customGroup = CRM_Core_BAO_CustomGroup
::getGroupDetail($customGroupId);
227 $this->assertTrue(empty($customGroup), 'Check that no custom Group details is retreived');
231 * Test getGroupDetail().
233 public function testGetGroupDetail() {
234 $customGroupTitle = 'My Custom Group';
236 'title' => $customGroupTitle,
237 'name' => 'My_Custom_Group',
238 'extends' => 'Individual',
239 'help_pre' => 'Custom Group Help Pre',
240 'help_post' => 'Custom Group Help Post',
242 'collapse_display' => 1,
245 $customGroup = $this->customGroupCreate($groupParams);
246 $customGroupId = $customGroup['id'];
249 'custom_group_id' => $customGroupId,
250 'label' => 'Test Custom Field',
251 'html_type' => 'Text',
252 'data_type' => 'String',
254 'is_searchable' => 0,
258 $customField = $this->customFieldCreate($fieldParams);
259 $customFieldId = $customField['id'];
261 $groupTree = CRM_Core_BAO_CustomGroup
::getGroupDetail($customGroupId);
262 $this->assertDBNotNull('CRM_Core_DAO_CustomGroup', $customGroupId, 'title', 'id',
263 'Database check for custom group record.'
265 //check retieve values of custom group
266 unset($groupParams['is_active']);
267 unset($groupParams['title']);
268 unset($groupParams['version']);
269 $this->assertAttributesEquals($groupParams, $groupTree[$customGroupId]);
271 //check retieve values of custom field
272 unset($fieldParams['is_active']);
273 unset($fieldParams['custom_group_id']);
274 unset($fieldParams['version']);
275 $this->assertAttributesEquals($fieldParams, $groupTree[$customGroupId]['fields'][$customFieldId], " in line " . __LINE__
);
277 $this->customFieldDelete($customField['id']);
278 $this->customGroupDelete($customGroup['id']);
282 * Test getTitle() with Invalid Params()
284 public function testGetTitleWithInvalidParams() {
286 $customGroupTitle = CRM_Core_BAO_CustomGroup
::getTitle($params);
288 $this->assertNull($customGroupTitle, 'Check that no custom Group Title is retreived');
294 public function testGetTitle() {
295 $customGroupTitle = 'Custom Group';
297 'title' => $customGroupTitle,
298 'name' => 'my_custom_group',
300 'extends' => 'Individual',
304 $customGroup = $this->customGroupCreate($groupParams);
305 $customGroupId = $customGroup['id'];
307 //get the custom group title
308 $title = CRM_Core_BAO_CustomGroup
::getTitle($customGroupId);
310 //check for object update
311 $this->assertEquals($customGroupTitle, $title);
313 $this->customGroupDelete($customGroupId);
319 public function testDeleteGroup() {
320 $customGroupTitle = 'My Custom Group';
322 'title' => $customGroupTitle,
323 'name' => 'my_custom_group',
325 'extends' => 'Individual',
329 $customGroup = $this->customGroupCreate($groupParams);
330 $groupObject = new CRM_Core_BAO_CustomGroup();
331 $groupObject->id
= $customGroup['id'];
332 $groupObject->find(TRUE);
334 $isDelete = CRM_Core_BAO_CustomGroup
::deleteGroup($groupObject);
337 $this->assertEquals(TRUE, $isDelete);
338 $this->assertDBNull('CRM_Core_DAO_CustomGroup', $customGroup['id'], 'title', 'id',
339 'Database check for custom group record.'
346 public function testCreateTable() {
348 'title' => 'My Custom Group',
349 'name' => 'my_custom_group',
351 'extends' => 'Individual',
356 $customGroupBAO = new CRM_Core_BAO_CustomGroup();
357 $customGroupBAO->copyValues($groupParams);
358 $customGroup = $customGroupBAO->save();
359 $tableName = 'civicrm_value_test_group_' . $customGroup->id
;
360 $customGroup->table_name
= $tableName;
361 $customGroup = $customGroupBAO->save();
362 CRM_Core_BAO_CustomGroup
::createTable($customGroup);
363 $customGroupId = $customGroup->id
;
365 //check db for custom group.
366 $dbCustomGroupTitle = $this->assertDBNotNull('CRM_Core_DAO_CustomGroup', $customGroupId, 'title', 'id',
367 'Database check for custom group record.'
369 //check for custom group table name
370 $this->assertDBCompareValue('CRM_Core_DAO_CustomGroup', $customGroupId, 'table_name', 'id',
371 $tableName, 'Database check for custom group table name.'
374 $this->customGroupDelete($customGroup->id
);
378 * Test checkCustomField()
380 public function testCheckCustomField() {
382 'title' => 'My Custom Group',
383 'name' => 'my_custom_group',
384 'extends' => 'Individual',
385 'help_pre' => 'Custom Group Help Pre',
386 'help_post' => 'Custom Group Help Post',
388 'collapse_display' => 1,
391 $customGroup = $this->customGroupCreate($groupParams);
392 $this->assertNotNull($customGroup['id'], 'pre-requisite group not created successfully');
393 $customGroupId = $customGroup['id'];
395 $customFieldLabel = 'Test Custom Field';
397 'custom_group_id' => $customGroupId,
398 'label' => $customFieldLabel,
399 'html_type' => 'Text',
400 'data_type' => 'String',
402 'is_searchable' => 0,
406 $customField = $this->customFieldCreate($fieldParams);
407 $customField = $customField['values'][$customField['id']];
409 $customFieldId = $customField['id'];
411 //check db for custom field
412 $dbCustomFieldLabel = $this->assertDBNotNull('CRM_Core_DAO_CustomField', $customFieldId, 'label', 'id',
413 'Database check for custom field record.'
415 $this->assertEquals($customFieldLabel, $dbCustomFieldLabel);
417 //check the custom field type.
418 $params = ['Individual'];
419 $usedFor = CRM_Core_BAO_CustomGroup
::checkCustomField($customFieldId, $params);
420 $this->assertEquals(FALSE, $usedFor);
422 $params = ['Contribution', 'Membership', 'Participant'];
423 $usedFor = CRM_Core_BAO_CustomGroup
::checkCustomField($customFieldId, $params);
424 $this->assertEquals(TRUE, $usedFor);
426 $this->customFieldDelete($customField['id']);
427 $this->customGroupDelete($customGroup['id']);
431 * Test getActiveGroups() with Invalid Params()
433 public function testGetActiveGroupsWithInvalidParams() {
434 $contactId = $this->individualCreate();
435 $activeGroups = CRM_Core_BAO_CustomGroup
::getActiveGroups('ABC', 'civicrm/contact/view/cd', $contactId);
436 $this->assertEquals(empty($activeGroups), TRUE, 'Check that Emprt params are retreived');
439 public function testGetActiveGroups() {
440 $contactId = $this->individualCreate();
441 $customGroupTitle = 'Custom Group';
443 'title' => $customGroupTitle,
444 'name' => 'test_custom_group',
446 'extends' => 'Individual',
451 $customGroup = $this->customGroupCreate($groupParams);
452 $activeGroup = CRM_Core_BAO_CustomGroup
::getActiveGroups('Individual', 'civicrm/contact/view/cd', $contactId);
453 foreach ($activeGroup as $key => $value) {
454 if ($value['id'] == $customGroup['id']) {
455 $this->assertEquals($value['path'], 'civicrm/contact/view/cd');
456 $this->assertEquals($value['title'], $customGroupTitle);
457 $query = 'reset=1&gid=' . $customGroup['id'] . '&cid=' . $contactId;
458 $this->assertEquals($value['query'], $query);
462 $this->customGroupDelete($customGroup['id']);
463 $this->contactDelete($contactId);
469 public function testCreate() {
471 'title' => 'Test_Group_1',
472 'name' => 'test_group_1',
473 'extends' => [0 => 'Individual', 1 => []],
475 'collapse_display' => 1,
477 'help_pre' => 'This is Pre Help For Test Group 1',
478 'help_post' => 'This is Post Help For Test Group 1',
482 $customGroup = CRM_Core_BAO_CustomGroup
::create($params);
484 $dbCustomGroupTitle = $this->assertDBNotNull('CRM_Core_DAO_CustomGroup', $customGroup->id
, 'title', 'id',
485 'Database check for custom group record.'
487 $this->assertEquals($params['title'], $dbCustomGroupTitle);
489 $dbCustomGroupTableName = $this->assertDBNotNull('CRM_Core_DAO_CustomGroup', $customGroup->id
, 'table_name', 'id',
490 'Database check for custom group record.'
492 $this->assertEquals(strtolower("civicrm_value_{$params['name']}_{$customGroup->id}"), $dbCustomGroupTableName,
493 "The table name should be suffixed with '_ID' unless specified.");
495 $this->customGroupDelete($customGroup->id
);
499 * Test create() given a table_name
501 public function testCreateTableName() {
503 'title' => 'Test_Group_2',
504 'name' => 'test_group_2',
505 'table_name' => 'test_otherTableName',
506 'extends' => [0 => 'Individual', 1 => []],
508 'collapse_display' => 1,
510 'help_pre' => 'This is Pre Help For Test Group 1',
511 'help_post' => 'This is Post Help For Test Group 1',
514 $customGroup = CRM_Core_BAO_CustomGroup
::create($params);
516 $dbCustomGroupTitle = $this->assertDBNotNull('CRM_Core_DAO_CustomGroup', $customGroup->id
, 'title', 'id',
517 'Database check for custom group record.'
519 $this->assertEquals($params['title'], $dbCustomGroupTitle);
521 $dbCustomGroupTableName = $this->assertDBNotNull('CRM_Core_DAO_CustomGroup', $customGroup->id
, 'table_name', 'id',
522 'Database check for custom group record.'
524 $this->assertEquals($params['table_name'], $dbCustomGroupTableName);
526 $this->customGroupDelete($customGroup->id
);
530 * Test isGroupEmpty()
532 public function testIsGroupEmpty() {
533 $customGroupTitle = 'Test Custom Group';
535 'title' => $customGroupTitle,
536 'name' => 'test_custom_group',
538 'extends' => 'Individual',
543 $customGroup = $this->customGroupCreate($groupParams);
544 $customGroupId = $customGroup['id'];
545 $isEmptyGroup = CRM_Core_BAO_CustomGroup
::isGroupEmpty($customGroupId);
547 $this->assertEquals($isEmptyGroup, TRUE, 'Check that custom Group is Empty.');
548 $this->customGroupDelete($customGroup['id']);
552 * Test getGroupTitles() with Invalid Params()
554 public function testGetGroupTitlesWithInvalidParams() {
556 $groupTitles = CRM_Core_BAO_CustomGroup
::getGroupTitles($params);
557 $this->assertTrue(empty($groupTitles), 'Check that no titles are received');
561 * Test getGroupTitles()
563 public function testGetGroupTitles() {
565 'title' => 'Test Group',
566 'name' => 'test_custom_group',
568 'extends' => 'Individual',
573 $customGroup = $this->customGroupCreate($groupParams);
576 'label' => 'Custom Field',
577 'html_type' => 'Text',
578 'data_type' => 'String',
580 'is_searchable' => 0,
582 'custom_group_id' => $customGroup['id'],
585 $customField = $this->customFieldCreate($fieldParams);
586 $customFieldId = $customField['id'];
588 $params = [$customFieldId];
590 $groupTitles = CRM_Core_BAO_CustomGroup
::getGroupTitles($params);
592 $this->assertEquals($groupTitles[$customFieldId]['groupTitle'], 'Test Group', 'Check Group Title.');
593 $this->customGroupDelete($customGroup['id']);
597 * Test that passed dates are extracted from the url when processing custom data.
599 public function testExtractGetParamsReturnsDates() {
600 // Create a custom group to contain the custom field.
602 'title' => 'My Custom Group',
603 'name' => 'my_custom_group',
604 'extends' => 'Individual',
606 'collapse_display' => 1,
608 $customGroup = $this->customGroupCreate($groupParams);
609 $customGroupId = $customGroup['id'];
611 // Create teh custom field.
613 'custom_group_id' => $customGroupId,
614 'label' => 'My Custom Date Field',
615 'html_type' => 'Select Date',
616 'data_type' => 'Date',
618 'is_searchable' => 0,
620 'default_value' => '',
622 $customField = $this->customFieldCreate($fieldParams);
623 $customFieldId = $customField['id'];
625 // Create a form object. CRM_Core_BAO_CustomGroup::extractGetParams() will
626 // need this, along with the REQUEST_METHOD and controller too.
627 $form = new CRM_Contribute_Form_Contribution();
628 $_SERVER['REQUEST_METHOD'] = 'GET';
629 $form->controller
= new CRM_Core_Controller();
631 // Set the value in $_GET, then extract query string params with
632 $fieldName = 'custom_' . $customFieldId;
633 $_GET[$fieldName] = '2017-06-13';
634 $extractedGetParams = CRM_Core_BAO_CustomGroup
::extractGetParams($form, 'Individual');
636 $this->assertEquals($extractedGetParams[$fieldName], '2017-06-13');