Merge pull request #19943 from civicrm/5.36
[civicrm-core.git] / tests / phpunit / CRM / Core / BAO / CustomGroupTest.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | Copyright CiviCRM LLC. All rights reserved. |
5 | |
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 +--------------------------------------------------------------------+
10 */
11
12 /**
13 * Class CRM_Core_BAO_CustomGroupTest
14 * @group headless
15 */
16 class CRM_Core_BAO_CustomGroupTest extends CiviUnitTestCase {
17
18 /**
19 * Test getTree().
20 */
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']);
27 }
28
29 /**
30 * Test calling getTree with contact subtype data.
31 *
32 * Note that the function seems to support a range of formats so 3 are tested. Yay for
33 * inconsistency.
34 */
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);
45 try {
46 CRM_Core_BAO_CustomGroup::getTree('Organization', NULL, NULL, NULL, ['Small Kind Bank']);
47 }
48 catch (CRM_Core_Exception $e) {
49 $this->customGroupDelete($customGroup['id']);
50 $this->callAPISuccess('ContactType', 'delete', ['id' => $contactType['id']]);
51 return;
52 }
53 $this->fail('There is no such thing as a small kind bank');
54 }
55
56 /**
57 * Test calling getTree for a custom field extending a renamed contact type.
58 */
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']]);
68 }
69
70 /**
71 * Test calling getTree for a custom field extending a disabled contact type.
72 */
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']]);
82 }
83
84 /**
85 * Test calling GetTree for a custom field extending multiple subTypes.
86 */
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']]);
97 }
98
99 /**
100 * Test calling GetTree for a custom field that extends a non numerical Event Type.
101 */
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']]);
110 }
111
112 /**
113 * Test calling getTree with contact subtype data.
114 *
115 * Note that the function seems to support a range of formats so 3 are tested. Yay for
116 * inconsistency.
117 */
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}",
125 ]);
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']);
130 }
131
132 /**
133 * Test calling getTree with contact subtype data.
134 */
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']);
141 }
142
143 /**
144 * Test retrieve() with Empty Params.
145 */
146 public function testRetrieveEmptyParams() {
147 $params = [];
148 $customGroup = CRM_Core_BAO_CustomGroup::retrieve($params, $dafaults);
149 $this->assertNull($customGroup, 'Check that no custom Group is retreived');
150 }
151
152 /**
153 * Test retrieve() with Inalid Params
154 */
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');
159 }
160
161 /**
162 * Test retrieve()
163 */
164 public function testRetrieve() {
165 $customGroupTitle = 'Custom Group';
166 $groupParams = [
167 'title' => $customGroupTitle,
168 'name' => 'My_Custom_Group',
169 'style' => 'Tab',
170 'extends' => 'Individual',
171 'help_pre' => 'Custom Group Help Pre',
172 'help_post' => 'Custom Group Help Post',
173 'is_active' => 1,
174 'collapse_display' => 1,
175 'weight' => 2,
176 ];
177
178 $customGroup = $this->customGroupCreate($groupParams);
179
180 $this->getAndCheck($groupParams, $customGroup['id'], 'CustomGroup');
181 }
182
183 /**
184 * Test setIsActive()
185 */
186 public function testSetIsActive() {
187 $customGroupTitle = 'My Custom Group';
188 $groupParams = [
189 'title' => $customGroupTitle,
190 'name' => 'my_custom_group',
191 'style' => 'Tab',
192 'extends' => 'Individual',
193 'is_active' => 0,
194 ];
195
196 $customGroup = $this->customGroupCreate($groupParams);
197 $customGroupId = $customGroup['id'];
198
199 //update is_active
200 $result = CRM_Core_BAO_CustomGroup::setIsActive($customGroupId, TRUE);
201
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.'
207 );
208
209 $this->customGroupDelete($customGroup['id']);
210 }
211
212 /**
213 * Test getGroupDetail() with Empty Params
214 */
215 public function testGetGroupDetailEmptyParams() {
216 $customGroupId = [];
217 $customGroup = CRM_Core_BAO_CustomGroup::getGroupDetail($customGroupId);
218 $this->assertTrue(empty($customGroup), 'Check that no custom Group details is retreived');
219 }
220
221 /**
222 * Test getGroupDetail with Invalid Params.
223 */
224 public function testGetGroupDetailInvalidParams() {
225 $customGroupId = 99;
226 $customGroup = CRM_Core_BAO_CustomGroup::getGroupDetail($customGroupId);
227 $this->assertTrue(empty($customGroup), 'Check that no custom Group details is retreived');
228 }
229
230 /**
231 * Test getGroupDetail().
232 */
233 public function testGetGroupDetail() {
234 $customGroupTitle = 'My Custom Group';
235 $groupParams = [
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',
241 'is_active' => 1,
242 'collapse_display' => 1,
243 ];
244
245 $customGroup = $this->customGroupCreate($groupParams);
246 $customGroupId = $customGroup['id'];
247
248 $fieldParams = [
249 'custom_group_id' => $customGroupId,
250 'label' => 'Test Custom Field',
251 'html_type' => 'Text',
252 'data_type' => 'String',
253 'is_required' => 1,
254 'is_searchable' => 0,
255 'is_active' => 1,
256 ];
257
258 $customField = $this->customFieldCreate($fieldParams);
259 $customFieldId = $customField['id'];
260
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.'
264 );
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]);
270
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__);
276
277 $this->customFieldDelete($customField['id']);
278 $this->customGroupDelete($customGroup['id']);
279 }
280
281 /**
282 * Test getTitle() with Invalid Params()
283 */
284 public function testGetTitleWithInvalidParams() {
285 $params = 99;
286 $customGroupTitle = CRM_Core_BAO_CustomGroup::getTitle($params);
287
288 $this->assertNull($customGroupTitle, 'Check that no custom Group Title is retreived');
289 }
290
291 /**
292 * Test getTitle()
293 */
294 public function testGetTitle() {
295 $customGroupTitle = 'Custom Group';
296 $groupParams = [
297 'title' => $customGroupTitle,
298 'name' => 'my_custom_group',
299 'style' => 'Tab',
300 'extends' => 'Individual',
301 'is_active' => 0,
302 ];
303
304 $customGroup = $this->customGroupCreate($groupParams);
305 $customGroupId = $customGroup['id'];
306
307 //get the custom group title
308 $title = CRM_Core_BAO_CustomGroup::getTitle($customGroupId);
309
310 //check for object update
311 $this->assertEquals($customGroupTitle, $title);
312
313 $this->customGroupDelete($customGroupId);
314 }
315
316 /**
317 * Test deleteGroup.
318 */
319 public function testDeleteGroup() {
320 $customGroupTitle = 'My Custom Group';
321 $groupParams = [
322 'title' => $customGroupTitle,
323 'name' => 'my_custom_group',
324 'style' => 'Tab',
325 'extends' => 'Individual',
326 'is_active' => 1,
327 ];
328
329 $customGroup = $this->customGroupCreate($groupParams);
330 $groupObject = new CRM_Core_BAO_CustomGroup();
331 $groupObject->id = $customGroup['id'];
332 $groupObject->find(TRUE);
333
334 $isDelete = CRM_Core_BAO_CustomGroup::deleteGroup($groupObject);
335
336 // Check it worked!
337 $this->assertEquals(TRUE, $isDelete);
338 $this->assertDBNull('CRM_Core_DAO_CustomGroup', $customGroup['id'], 'title', 'id',
339 'Database check for custom group record.'
340 );
341 }
342
343 /**
344 * Test createTable()
345 */
346 public function testCreateTable() {
347 $groupParams = [
348 'title' => 'My Custom Group',
349 'name' => 'my_custom_group',
350 'style' => 'Tab',
351 'extends' => 'Individual',
352 'is_active' => 1,
353 'version' => 3,
354 ];
355
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;
364
365 //check db for custom group.
366 $dbCustomGroupTitle = $this->assertDBNotNull('CRM_Core_DAO_CustomGroup', $customGroupId, 'title', 'id',
367 'Database check for custom group record.'
368 );
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.'
372 );
373
374 $this->customGroupDelete($customGroup->id);
375 }
376
377 /**
378 * Test checkCustomField()
379 */
380 public function testCheckCustomField() {
381 $groupParams = [
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',
387 'is_active' => 1,
388 'collapse_display' => 1,
389 ];
390
391 $customGroup = $this->customGroupCreate($groupParams);
392 $this->assertNotNull($customGroup['id'], 'pre-requisite group not created successfully');
393 $customGroupId = $customGroup['id'];
394
395 $customFieldLabel = 'Test Custom Field';
396 $fieldParams = [
397 'custom_group_id' => $customGroupId,
398 'label' => $customFieldLabel,
399 'html_type' => 'Text',
400 'data_type' => 'String',
401 'is_required' => 1,
402 'is_searchable' => 0,
403 'is_active' => 1,
404 ];
405
406 $customField = $this->customFieldCreate($fieldParams);
407 $customField = $customField['values'][$customField['id']];
408
409 $customFieldId = $customField['id'];
410
411 //check db for custom field
412 $dbCustomFieldLabel = $this->assertDBNotNull('CRM_Core_DAO_CustomField', $customFieldId, 'label', 'id',
413 'Database check for custom field record.'
414 );
415 $this->assertEquals($customFieldLabel, $dbCustomFieldLabel);
416
417 //check the custom field type.
418 $params = ['Individual'];
419 $usedFor = CRM_Core_BAO_CustomGroup::checkCustomField($customFieldId, $params);
420 $this->assertEquals(FALSE, $usedFor);
421
422 $params = ['Contribution', 'Membership', 'Participant'];
423 $usedFor = CRM_Core_BAO_CustomGroup::checkCustomField($customFieldId, $params);
424 $this->assertEquals(TRUE, $usedFor);
425
426 $this->customFieldDelete($customField['id']);
427 $this->customGroupDelete($customGroup['id']);
428 }
429
430 /**
431 * Test getActiveGroups() with Invalid Params()
432 */
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');
437 }
438
439 public function testGetActiveGroups() {
440 $contactId = $this->individualCreate();
441 $customGroupTitle = 'Custom Group';
442 $groupParams = [
443 'title' => $customGroupTitle,
444 'name' => 'test_custom_group',
445 'style' => 'Tab',
446 'extends' => 'Individual',
447 'weight' => 10,
448 'is_active' => 1,
449 ];
450
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);
459 }
460 }
461
462 $this->customGroupDelete($customGroup['id']);
463 $this->contactDelete($contactId);
464 }
465
466 /**
467 * Test create()
468 */
469 public function testCreate() {
470 $params = [
471 'title' => 'Test_Group_1',
472 'name' => 'test_group_1',
473 'extends' => [0 => 'Individual', 1 => []],
474 'weight' => 4,
475 'collapse_display' => 1,
476 'style' => 'Inline',
477 'help_pre' => 'This is Pre Help For Test Group 1',
478 'help_post' => 'This is Post Help For Test Group 1',
479 'is_active' => 1,
480 'version' => 3,
481 ];
482 $customGroup = CRM_Core_BAO_CustomGroup::create($params);
483
484 $dbCustomGroupTitle = $this->assertDBNotNull('CRM_Core_DAO_CustomGroup', $customGroup->id, 'title', 'id',
485 'Database check for custom group record.'
486 );
487 $this->assertEquals($params['title'], $dbCustomGroupTitle);
488
489 $dbCustomGroupTableName = $this->assertDBNotNull('CRM_Core_DAO_CustomGroup', $customGroup->id, 'table_name', 'id',
490 'Database check for custom group record.'
491 );
492 $this->assertEquals(strtolower("civicrm_value_{$params['name']}_{$customGroup->id}"), $dbCustomGroupTableName,
493 "The table name should be suffixed with '_ID' unless specified.");
494
495 $this->customGroupDelete($customGroup->id);
496 }
497
498 /**
499 * Test create() given a table_name
500 */
501 public function testCreateTableName() {
502 $params = [
503 'title' => 'Test_Group_2',
504 'name' => 'test_group_2',
505 'table_name' => 'test_otherTableName',
506 'extends' => [0 => 'Individual', 1 => []],
507 'weight' => 4,
508 'collapse_display' => 1,
509 'style' => 'Inline',
510 'help_pre' => 'This is Pre Help For Test Group 1',
511 'help_post' => 'This is Post Help For Test Group 1',
512 'is_active' => 1,
513 ];
514 $customGroup = CRM_Core_BAO_CustomGroup::create($params);
515
516 $dbCustomGroupTitle = $this->assertDBNotNull('CRM_Core_DAO_CustomGroup', $customGroup->id, 'title', 'id',
517 'Database check for custom group record.'
518 );
519 $this->assertEquals($params['title'], $dbCustomGroupTitle);
520
521 $dbCustomGroupTableName = $this->assertDBNotNull('CRM_Core_DAO_CustomGroup', $customGroup->id, 'table_name', 'id',
522 'Database check for custom group record.'
523 );
524 $this->assertEquals($params['table_name'], $dbCustomGroupTableName);
525
526 $this->customGroupDelete($customGroup->id);
527 }
528
529 /**
530 * Test isGroupEmpty()
531 */
532 public function testIsGroupEmpty() {
533 $customGroupTitle = 'Test Custom Group';
534 $groupParams = [
535 'title' => $customGroupTitle,
536 'name' => 'test_custom_group',
537 'style' => 'Tab',
538 'extends' => 'Individual',
539 'weight' => 10,
540 'is_active' => 1,
541 ];
542
543 $customGroup = $this->customGroupCreate($groupParams);
544 $customGroupId = $customGroup['id'];
545 $isEmptyGroup = CRM_Core_BAO_CustomGroup::isGroupEmpty($customGroupId);
546
547 $this->assertEquals($isEmptyGroup, TRUE, 'Check that custom Group is Empty.');
548 $this->customGroupDelete($customGroup['id']);
549 }
550
551 /**
552 * Test getGroupTitles() with Invalid Params()
553 */
554 public function testGetGroupTitlesWithInvalidParams() {
555 $params = [99];
556 $groupTitles = CRM_Core_BAO_CustomGroup::getGroupTitles($params);
557 $this->assertTrue(empty($groupTitles), 'Check that no titles are received');
558 }
559
560 /**
561 * Test getGroupTitles()
562 */
563 public function testGetGroupTitles() {
564 $groupParams = [
565 'title' => 'Test Group',
566 'name' => 'test_custom_group',
567 'style' => 'Tab',
568 'extends' => 'Individual',
569 'weight' => 10,
570 'is_active' => 1,
571 ];
572
573 $customGroup = $this->customGroupCreate($groupParams);
574
575 $fieldParams = [
576 'label' => 'Custom Field',
577 'html_type' => 'Text',
578 'data_type' => 'String',
579 'is_required' => 1,
580 'is_searchable' => 0,
581 'is_active' => 1,
582 'custom_group_id' => $customGroup['id'],
583 ];
584
585 $customField = $this->customFieldCreate($fieldParams);
586 $customFieldId = $customField['id'];
587
588 $params = [$customFieldId];
589
590 $groupTitles = CRM_Core_BAO_CustomGroup::getGroupTitles($params);
591
592 $this->assertEquals($groupTitles[$customFieldId]['groupTitle'], 'Test Group', 'Check Group Title.');
593 $this->customGroupDelete($customGroup['id']);
594 }
595
596 /**
597 * Test that passed dates are extracted from the url when processing custom data.
598 */
599 public function testExtractGetParamsReturnsDates() {
600 // Create a custom group to contain the custom field.
601 $groupParams = [
602 'title' => 'My Custom Group',
603 'name' => 'my_custom_group',
604 'extends' => 'Individual',
605 'is_active' => 1,
606 'collapse_display' => 1,
607 ];
608 $customGroup = $this->customGroupCreate($groupParams);
609 $customGroupId = $customGroup['id'];
610
611 // Create teh custom field.
612 $fieldParams = [
613 'custom_group_id' => $customGroupId,
614 'label' => 'My Custom Date Field',
615 'html_type' => 'Select Date',
616 'data_type' => 'Date',
617 'is_required' => 1,
618 'is_searchable' => 0,
619 'is_active' => 1,
620 'default_value' => '',
621 ];
622 $customField = $this->customFieldCreate($fieldParams);
623 $customFieldId = $customField['id'];
624
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();
630
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');
635
636 $this->assertEquals($extractedGetParams[$fieldName], '2017-06-13');
637 }
638
639 }