Merge pull request #17253 from mattwire/utf8convertblocksize
[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 public function setUp() {
19 parent::setUp();
20 }
21
22 /**
23 * Test getTree().
24 */
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']);
31 }
32
33 /**
34 * Test calling getTree with contact subtype data.
35 *
36 * Note that the function seems to support a range of formats so 3 are tested. Yay for
37 * inconsistency.
38 */
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);
49 try {
50 CRM_Core_BAO_CustomGroup::getTree('Organization', NULL, NULL, NULL, ['Small Kind Bank']);
51 }
52 catch (CRM_Core_Exception $e) {
53 $this->customGroupDelete($customGroup['id']);
54 $this->callAPISuccess('ContactType', 'delete', ['id' => $contactType['id']]);
55 return;
56 }
57 $this->fail('There is no such thing as a small kind bank');
58 }
59
60 /**
61 * Test calling getTree for a custom field extending a renamed contact type.
62 */
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']]);
72 }
73
74 /**
75 * Test calling getTree for a custom field extending a disabled contact type.
76 */
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']]);
86 }
87
88 /**
89 * Test calling GetTree for a custom field extending multiple subTypes.
90 */
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']]);
101 }
102
103 /**
104 * Test calling GetTree for a custom field that extends a non numerical Event Type.
105 */
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']]);
114 }
115
116 /**
117 * Test calling getTree with contact subtype data.
118 *
119 * Note that the function seems to support a range of formats so 3 are tested. Yay for
120 * inconsistency.
121 */
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}",
129 ]);
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']);
134 }
135
136 /**
137 * Test calling getTree with contact subtype data.
138 */
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']);
145 }
146
147 /**
148 * Test retrieve() with Empty Params.
149 */
150 public function testRetrieveEmptyParams() {
151 $params = [];
152 $customGroup = CRM_Core_BAO_CustomGroup::retrieve($params, $dafaults);
153 $this->assertNull($customGroup, 'Check that no custom Group is retreived');
154 }
155
156 /**
157 * Test retrieve() with Inalid Params
158 */
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');
163 }
164
165 /**
166 * Test retrieve()
167 */
168 public function testRetrieve() {
169 $customGroupTitle = 'Custom Group';
170 $groupParams = [
171 'title' => $customGroupTitle,
172 'name' => 'My_Custom_Group',
173 'style' => 'Tab',
174 'extends' => 'Individual',
175 'help_pre' => 'Custom Group Help Pre',
176 'help_post' => 'Custom Group Help Post',
177 'is_active' => 1,
178 'collapse_display' => 1,
179 'weight' => 2,
180 ];
181
182 $customGroup = $this->customGroupCreate($groupParams);
183
184 $this->getAndCheck($groupParams, $customGroup['id'], 'CustomGroup');
185 }
186
187 /**
188 * Test setIsActive()
189 */
190 public function testSetIsActive() {
191 $customGroupTitle = 'My Custom Group';
192 $groupParams = [
193 'title' => $customGroupTitle,
194 'name' => 'my_custom_group',
195 'style' => 'Tab',
196 'extends' => 'Individual',
197 'is_active' => 0,
198 ];
199
200 $customGroup = $this->customGroupCreate($groupParams);
201 $customGroupId = $customGroup['id'];
202
203 //update is_active
204 $result = CRM_Core_BAO_CustomGroup::setIsActive($customGroupId, TRUE);
205
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.'
211 );
212
213 $this->customGroupDelete($customGroup['id']);
214 }
215
216 /**
217 * Test getGroupDetail() with Empty Params
218 */
219 public function testGetGroupDetailEmptyParams() {
220 $customGroupId = [];
221 $customGroup = CRM_Core_BAO_CustomGroup::getGroupDetail($customGroupId);
222 $this->assertTrue(empty($customGroup), 'Check that no custom Group details is retreived');
223 }
224
225 /**
226 * Test getGroupDetail with Invalid Params.
227 */
228 public function testGetGroupDetailInvalidParams() {
229 $customGroupId = 99;
230 $customGroup = CRM_Core_BAO_CustomGroup::getGroupDetail($customGroupId);
231 $this->assertTrue(empty($customGroup), 'Check that no custom Group details is retreived');
232 }
233
234 /**
235 * Test getGroupDetail().
236 */
237 public function testGetGroupDetail() {
238 $customGroupTitle = 'My Custom Group';
239 $groupParams = [
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',
245 'is_active' => 1,
246 'collapse_display' => 1,
247 ];
248
249 $customGroup = $this->customGroupCreate($groupParams);
250 $customGroupId = $customGroup['id'];
251
252 $fieldParams = [
253 'custom_group_id' => $customGroupId,
254 'label' => 'Test Custom Field',
255 'html_type' => 'Text',
256 'data_type' => 'String',
257 'is_required' => 1,
258 'is_searchable' => 0,
259 'is_active' => 1,
260 ];
261
262 $customField = $this->customFieldCreate($fieldParams);
263 $customFieldId = $customField['id'];
264
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.'
268 );
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]);
274
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__);
280
281 $this->customFieldDelete($customField['id']);
282 $this->customGroupDelete($customGroup['id']);
283 }
284
285 /**
286 * Test getTitle() with Invalid Params()
287 */
288 public function testGetTitleWithInvalidParams() {
289 $params = 99;
290 $customGroupTitle = CRM_Core_BAO_CustomGroup::getTitle($params);
291
292 $this->assertNull($customGroupTitle, 'Check that no custom Group Title is retreived');
293 }
294
295 /**
296 * Test getTitle()
297 */
298 public function testGetTitle() {
299 $customGroupTitle = 'Custom Group';
300 $groupParams = [
301 'title' => $customGroupTitle,
302 'name' => 'my_custom_group',
303 'style' => 'Tab',
304 'extends' => 'Individual',
305 'is_active' => 0,
306 ];
307
308 $customGroup = $this->customGroupCreate($groupParams);
309 $customGroupId = $customGroup['id'];
310
311 //get the custom group title
312 $title = CRM_Core_BAO_CustomGroup::getTitle($customGroupId);
313
314 //check for object update
315 $this->assertEquals($customGroupTitle, $title);
316
317 $this->customGroupDelete($customGroupId);
318 }
319
320 /**
321 * Test deleteGroup.
322 */
323 public function testDeleteGroup() {
324 $customGroupTitle = 'My Custom Group';
325 $groupParams = [
326 'title' => $customGroupTitle,
327 'name' => 'my_custom_group',
328 'style' => 'Tab',
329 'extends' => 'Individual',
330 'is_active' => 1,
331 ];
332
333 $customGroup = $this->customGroupCreate($groupParams);
334 $groupObject = new CRM_Core_BAO_CustomGroup();
335 $groupObject->id = $customGroup['id'];
336 $groupObject->find(TRUE);
337
338 $isDelete = CRM_Core_BAO_CustomGroup::deleteGroup($groupObject);
339
340 // Check it worked!
341 $this->assertEquals(TRUE, $isDelete);
342 $this->assertDBNull('CRM_Core_DAO_CustomGroup', $customGroup['id'], 'title', 'id',
343 'Database check for custom group record.'
344 );
345 }
346
347 /**
348 * Test createTable()
349 */
350 public function testCreateTable() {
351 $groupParams = [
352 'title' => 'My Custom Group',
353 'name' => 'my_custom_group',
354 'style' => 'Tab',
355 'extends' => 'Individual',
356 'is_active' => 1,
357 'version' => 3,
358 ];
359
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;
368
369 //check db for custom group.
370 $dbCustomGroupTitle = $this->assertDBNotNull('CRM_Core_DAO_CustomGroup', $customGroupId, 'title', 'id',
371 'Database check for custom group record.'
372 );
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.'
376 );
377
378 $this->customGroupDelete($customGroup->id);
379 }
380
381 /**
382 * Test checkCustomField()
383 */
384 public function testCheckCustomField() {
385 $groupParams = [
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',
391 'is_active' => 1,
392 'collapse_display' => 1,
393 ];
394
395 $customGroup = $this->customGroupCreate($groupParams);
396 $this->assertNotNull($customGroup['id'], 'pre-requisite group not created successfully');
397 $customGroupId = $customGroup['id'];
398
399 $customFieldLabel = 'Test Custom Field';
400 $fieldParams = [
401 'custom_group_id' => $customGroupId,
402 'label' => $customFieldLabel,
403 'html_type' => 'Text',
404 'data_type' => 'String',
405 'is_required' => 1,
406 'is_searchable' => 0,
407 'is_active' => 1,
408 ];
409
410 $customField = $this->customFieldCreate($fieldParams);
411 $customField = $customField['values'][$customField['id']];
412
413 $customFieldId = $customField['id'];
414
415 //check db for custom field
416 $dbCustomFieldLabel = $this->assertDBNotNull('CRM_Core_DAO_CustomField', $customFieldId, 'label', 'id',
417 'Database check for custom field record.'
418 );
419 $this->assertEquals($customFieldLabel, $dbCustomFieldLabel);
420
421 //check the custom field type.
422 $params = ['Individual'];
423 $usedFor = CRM_Core_BAO_CustomGroup::checkCustomField($customFieldId, $params);
424 $this->assertEquals(FALSE, $usedFor);
425
426 $params = ['Contribution', 'Membership', 'Participant'];
427 $usedFor = CRM_Core_BAO_CustomGroup::checkCustomField($customFieldId, $params);
428 $this->assertEquals(TRUE, $usedFor);
429
430 $this->customFieldDelete($customField['id']);
431 $this->customGroupDelete($customGroup['id']);
432 }
433
434 /**
435 * Test getActiveGroups() with Invalid Params()
436 */
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');
441 }
442
443 public function testGetActiveGroups() {
444 $contactId = $this->individualCreate();
445 $customGroupTitle = 'Custom Group';
446 $groupParams = [
447 'title' => $customGroupTitle,
448 'name' => 'test_custom_group',
449 'style' => 'Tab',
450 'extends' => 'Individual',
451 'weight' => 10,
452 'is_active' => 1,
453 ];
454
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);
463 }
464 }
465
466 $this->customGroupDelete($customGroup['id']);
467 $this->contactDelete($contactId);
468 }
469
470 /**
471 * Test create()
472 */
473 public function testCreate() {
474 $params = [
475 'title' => 'Test_Group_1',
476 'name' => 'test_group_1',
477 'extends' => [0 => 'Individual', 1 => []],
478 'weight' => 4,
479 'collapse_display' => 1,
480 'style' => 'Inline',
481 'help_pre' => 'This is Pre Help For Test Group 1',
482 'help_post' => 'This is Post Help For Test Group 1',
483 'is_active' => 1,
484 'version' => 3,
485 ];
486 $customGroup = CRM_Core_BAO_CustomGroup::create($params);
487
488 $dbCustomGroupTitle = $this->assertDBNotNull('CRM_Core_DAO_CustomGroup', $customGroup->id, 'title', 'id',
489 'Database check for custom group record.'
490 );
491 $this->assertEquals($params['title'], $dbCustomGroupTitle);
492
493 $dbCustomGroupTableName = $this->assertDBNotNull('CRM_Core_DAO_CustomGroup', $customGroup->id, 'table_name', 'id',
494 'Database check for custom group record.'
495 );
496 $this->assertEquals(strtolower("civicrm_value_{$params['name']}_{$customGroup->id}"), $dbCustomGroupTableName,
497 "The table name should be suffixed with '_ID' unless specified.");
498
499 $this->customGroupDelete($customGroup->id);
500 }
501
502 /**
503 * Test create() given a table_name
504 */
505 public function testCreateTableName() {
506 $params = [
507 'title' => 'Test_Group_2',
508 'name' => 'test_group_2',
509 'table_name' => 'test_otherTableName',
510 'extends' => [0 => 'Individual', 1 => []],
511 'weight' => 4,
512 'collapse_display' => 1,
513 'style' => 'Inline',
514 'help_pre' => 'This is Pre Help For Test Group 1',
515 'help_post' => 'This is Post Help For Test Group 1',
516 'is_active' => 1,
517 ];
518 $customGroup = CRM_Core_BAO_CustomGroup::create($params);
519
520 $dbCustomGroupTitle = $this->assertDBNotNull('CRM_Core_DAO_CustomGroup', $customGroup->id, 'title', 'id',
521 'Database check for custom group record.'
522 );
523 $this->assertEquals($params['title'], $dbCustomGroupTitle);
524
525 $dbCustomGroupTableName = $this->assertDBNotNull('CRM_Core_DAO_CustomGroup', $customGroup->id, 'table_name', 'id',
526 'Database check for custom group record.'
527 );
528 $this->assertEquals($params['table_name'], $dbCustomGroupTableName);
529
530 $this->customGroupDelete($customGroup->id);
531 }
532
533 /**
534 * Test isGroupEmpty()
535 */
536 public function testIsGroupEmpty() {
537 $customGroupTitle = 'Test Custom Group';
538 $groupParams = [
539 'title' => $customGroupTitle,
540 'name' => 'test_custom_group',
541 'style' => 'Tab',
542 'extends' => 'Individual',
543 'weight' => 10,
544 'is_active' => 1,
545 ];
546
547 $customGroup = $this->customGroupCreate($groupParams);
548 $customGroupId = $customGroup['id'];
549 $isEmptyGroup = CRM_Core_BAO_CustomGroup::isGroupEmpty($customGroupId);
550
551 $this->assertEquals($isEmptyGroup, TRUE, 'Check that custom Group is Empty.');
552 $this->customGroupDelete($customGroup['id']);
553 }
554
555 /**
556 * Test getGroupTitles() with Invalid Params()
557 */
558 public function testGetGroupTitlesWithInvalidParams() {
559 $params = [99];
560 $groupTitles = CRM_Core_BAO_CustomGroup::getGroupTitles($params);
561 $this->assertTrue(empty($groupTitles), 'Check that no titles are received');
562 }
563
564 /**
565 * Test getGroupTitles()
566 */
567 public function testGetGroupTitles() {
568 $groupParams = [
569 'title' => 'Test Group',
570 'name' => 'test_custom_group',
571 'style' => 'Tab',
572 'extends' => 'Individual',
573 'weight' => 10,
574 'is_active' => 1,
575 ];
576
577 $customGroup = $this->customGroupCreate($groupParams);
578
579 $fieldParams = [
580 'label' => 'Custom Field',
581 'html_type' => 'Text',
582 'data_type' => 'String',
583 'is_required' => 1,
584 'is_searchable' => 0,
585 'is_active' => 1,
586 'custom_group_id' => $customGroup['id'],
587 ];
588
589 $customField = $this->customFieldCreate($fieldParams);
590 $customFieldId = $customField['id'];
591
592 $params = [$customFieldId];
593
594 $groupTitles = CRM_Core_BAO_CustomGroup::getGroupTitles($params);
595
596 $this->assertEquals($groupTitles[$customFieldId]['groupTitle'], 'Test Group', 'Check Group Title.');
597 $this->customGroupDelete($customGroup['id']);
598 }
599
600 /**
601 * Test that passed dates are extracted from the url when processing custom data.
602 */
603 public function testExtractGetParamsReturnsDates() {
604 // Create a custom group to contain the custom field.
605 $groupParams = [
606 'title' => 'My Custom Group',
607 'name' => 'my_custom_group',
608 'extends' => 'Individual',
609 'is_active' => 1,
610 'collapse_display' => 1,
611 ];
612 $customGroup = $this->customGroupCreate($groupParams);
613 $customGroupId = $customGroup['id'];
614
615 // Create teh custom field.
616 $fieldParams = [
617 'custom_group_id' => $customGroupId,
618 'label' => 'My Custom Date Field',
619 'html_type' => 'Select Date',
620 'data_type' => 'Date',
621 'is_required' => 1,
622 'is_searchable' => 0,
623 'is_active' => 1,
624 'default_value' => '',
625 ];
626 $customField = $this->customFieldCreate($fieldParams);
627 $customFieldId = $customField['id'];
628
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();
634
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');
639
640 $this->assertEquals($extractedGetParams[$fieldName], '2017-06-13');
641 }
642
643 }