Merge pull request #15790 from civicrm/5.20
[civicrm-core.git] / tests / phpunit / CRM / Core / BAO / CustomGroupTest.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 5 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2020 |
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
9 | |
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. |
13 | |
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. |
18 | |
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 +--------------------------------------------------------------------+
26 */
27
28 /**
29 * Class CRM_Core_BAO_CustomGroupTest
30 * @group headless
31 */
32 class CRM_Core_BAO_CustomGroupTest extends CiviUnitTestCase {
33
34 public function setUp() {
35 parent::setUp();
36 }
37
38 /**
39 * Test getTree().
40 */
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']);
47 }
48
49 /**
50 * Test calling getTree with contact subtype data.
51 *
52 * Note that the function seems to support a range of formats so 3 are tested. Yay for
53 * inconsistency.
54 */
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);
65 try {
66 CRM_Core_BAO_CustomGroup::getTree('Organization', NULL, NULL, NULL, ['Small Kind Bank']);
67 }
68 catch (CRM_Core_Exception $e) {
69 $this->customGroupDelete($customGroup['id']);
70 $this->callAPISuccess('ContactType', 'delete', ['id' => $contactType['id']]);
71 return;
72 }
73 $this->fail('There is no such thing as a small kind bank');
74 }
75
76 /**
77 * Test calling getTree for a custom field extending a renamed contact type.
78 */
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']]);
88 }
89
90 /**
91 * Test calling getTree for a custom field extending a disabled contact type.
92 */
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']]);
102 }
103
104 /**
105 * Test calling GetTree for a custom field extending multiple subTypes.
106 */
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']]);
117 }
118
119 /**
120 * Test calling GetTree for a custom field that extends a non numerical Event Type.
121 */
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']]);
130 }
131
132 /**
133 * Test calling getTree with contact subtype data.
134 *
135 * Note that the function seems to support a range of formats so 3 are tested. Yay for
136 * inconsistency.
137 */
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}",
145 ]);
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']);
150 }
151
152 /**
153 * Test calling getTree with contact subtype data.
154 */
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']);
161 }
162
163 /**
164 * Test retrieve() with Empty Params.
165 */
166 public function testRetrieveEmptyParams() {
167 $params = [];
168 $customGroup = CRM_Core_BAO_CustomGroup::retrieve($params, $dafaults);
169 $this->assertNull($customGroup, 'Check that no custom Group is retreived');
170 }
171
172 /**
173 * Test retrieve() with Inalid Params
174 */
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');
179 }
180
181 /**
182 * Test retrieve()
183 */
184 public function testRetrieve() {
185 $customGroupTitle = 'Custom Group';
186 $groupParams = [
187 'title' => $customGroupTitle,
188 'name' => 'My_Custom_Group',
189 'style' => 'Tab',
190 'extends' => 'Individual',
191 'help_pre' => 'Custom Group Help Pre',
192 'help_post' => 'Custom Group Help Post',
193 'is_active' => 1,
194 'collapse_display' => 1,
195 'weight' => 2,
196 ];
197
198 $customGroup = $this->customGroupCreate($groupParams);
199
200 $this->getAndCheck($groupParams, $customGroup['id'], 'CustomGroup');
201 }
202
203 /**
204 * Test setIsActive()
205 */
206 public function testSetIsActive() {
207 $customGroupTitle = 'My Custom Group';
208 $groupParams = [
209 'title' => $customGroupTitle,
210 'name' => 'my_custom_group',
211 'style' => 'Tab',
212 'extends' => 'Individual',
213 'is_active' => 0,
214 ];
215
216 $customGroup = $this->customGroupCreate($groupParams);
217 $customGroupId = $customGroup['id'];
218
219 //update is_active
220 $result = CRM_Core_BAO_CustomGroup::setIsActive($customGroupId, TRUE);
221
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.'
227 );
228
229 $this->customGroupDelete($customGroup['id']);
230 }
231
232 /**
233 * Test getGroupDetail() with Empty Params
234 */
235 public function testGetGroupDetailEmptyParams() {
236 $customGroupId = [];
237 $customGroup = CRM_Core_BAO_CustomGroup::getGroupDetail($customGroupId);
238 $this->assertTrue(empty($customGroup), 'Check that no custom Group details is retreived');
239 }
240
241 /**
242 * Test getGroupDetail with Invalid Params.
243 */
244 public function testGetGroupDetailInvalidParams() {
245 $customGroupId = 99;
246 $customGroup = CRM_Core_BAO_CustomGroup::getGroupDetail($customGroupId);
247 $this->assertTrue(empty($customGroup), 'Check that no custom Group details is retreived');
248 }
249
250 /**
251 * Test getGroupDetail().
252 */
253 public function testGetGroupDetail() {
254 $customGroupTitle = 'My Custom Group';
255 $groupParams = [
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',
261 'is_active' => 1,
262 'collapse_display' => 1,
263 ];
264
265 $customGroup = $this->customGroupCreate($groupParams);
266 $customGroupId = $customGroup['id'];
267
268 $fieldParams = [
269 'custom_group_id' => $customGroupId,
270 'label' => 'Test Custom Field',
271 'html_type' => 'Text',
272 'data_type' => 'String',
273 'is_required' => 1,
274 'is_searchable' => 0,
275 'is_active' => 1,
276 ];
277
278 $customField = $this->customFieldCreate($fieldParams);
279 $customFieldId = $customField['id'];
280
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.'
284 );
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]);
290
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__);
296
297 $this->customFieldDelete($customField['id']);
298 $this->customGroupDelete($customGroup['id']);
299 }
300
301 /**
302 * Test getTitle() with Invalid Params()
303 */
304 public function testGetTitleWithInvalidParams() {
305 $params = 99;
306 $customGroupTitle = CRM_Core_BAO_CustomGroup::getTitle($params);
307
308 $this->assertNull($customGroupTitle, 'Check that no custom Group Title is retreived');
309 }
310
311 /**
312 * Test getTitle()
313 */
314 public function testGetTitle() {
315 $customGroupTitle = 'Custom Group';
316 $groupParams = [
317 'title' => $customGroupTitle,
318 'name' => 'my_custom_group',
319 'style' => 'Tab',
320 'extends' => 'Individual',
321 'is_active' => 0,
322 ];
323
324 $customGroup = $this->customGroupCreate($groupParams);
325 $customGroupId = $customGroup['id'];
326
327 //get the custom group title
328 $title = CRM_Core_BAO_CustomGroup::getTitle($customGroupId);
329
330 //check for object update
331 $this->assertEquals($customGroupTitle, $title);
332
333 $this->customGroupDelete($customGroupId);
334 }
335
336 /**
337 * Test deleteGroup.
338 */
339 public function testDeleteGroup() {
340 $customGroupTitle = 'My Custom Group';
341 $groupParams = [
342 'title' => $customGroupTitle,
343 'name' => 'my_custom_group',
344 'style' => 'Tab',
345 'extends' => 'Individual',
346 'is_active' => 1,
347 ];
348
349 $customGroup = $this->customGroupCreate($groupParams);
350 $groupObject = new CRM_Core_BAO_CustomGroup();
351 $groupObject->id = $customGroup['id'];
352 $groupObject->find(TRUE);
353
354 $isDelete = CRM_Core_BAO_CustomGroup::deleteGroup($groupObject);
355
356 // Check it worked!
357 $this->assertEquals(TRUE, $isDelete);
358 $this->assertDBNull('CRM_Core_DAO_CustomGroup', $customGroup['id'], 'title', 'id',
359 'Database check for custom group record.'
360 );
361 }
362
363 /**
364 * Test createTable()
365 */
366 public function testCreateTable() {
367 $groupParams = [
368 'title' => 'My Custom Group',
369 'name' => 'my_custom_group',
370 'style' => 'Tab',
371 'extends' => 'Individual',
372 'is_active' => 1,
373 'version' => 3,
374 ];
375
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;
384
385 //check db for custom group.
386 $dbCustomGroupTitle = $this->assertDBNotNull('CRM_Core_DAO_CustomGroup', $customGroupId, 'title', 'id',
387 'Database check for custom group record.'
388 );
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.'
392 );
393
394 $this->customGroupDelete($customGroup->id);
395 }
396
397 /**
398 * Test checkCustomField()
399 */
400 public function testCheckCustomField() {
401 $groupParams = [
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',
407 'is_active' => 1,
408 'collapse_display' => 1,
409 ];
410
411 $customGroup = $this->customGroupCreate($groupParams);
412 $this->assertNotNull($customGroup['id'], 'pre-requisite group not created successfully');
413 $customGroupId = $customGroup['id'];
414
415 $customFieldLabel = 'Test Custom Field';
416 $fieldParams = [
417 'custom_group_id' => $customGroupId,
418 'label' => $customFieldLabel,
419 'html_type' => 'Text',
420 'data_type' => 'String',
421 'is_required' => 1,
422 'is_searchable' => 0,
423 'is_active' => 1,
424 ];
425
426 $customField = $this->customFieldCreate($fieldParams);
427 $customField = $customField['values'][$customField['id']];
428
429 $customFieldId = $customField['id'];
430
431 //check db for custom field
432 $dbCustomFieldLabel = $this->assertDBNotNull('CRM_Core_DAO_CustomField', $customFieldId, 'label', 'id',
433 'Database check for custom field record.'
434 );
435 $this->assertEquals($customFieldLabel, $dbCustomFieldLabel);
436
437 //check the custom field type.
438 $params = ['Individual'];
439 $usedFor = CRM_Core_BAO_CustomGroup::checkCustomField($customFieldId, $params);
440 $this->assertEquals(FALSE, $usedFor);
441
442 $params = ['Contribution', 'Membership', 'Participant'];
443 $usedFor = CRM_Core_BAO_CustomGroup::checkCustomField($customFieldId, $params);
444 $this->assertEquals(TRUE, $usedFor);
445
446 $this->customFieldDelete($customField['id']);
447 $this->customGroupDelete($customGroup['id']);
448 }
449
450 /**
451 * Test getActiveGroups() with Invalid Params()
452 */
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');
457 }
458
459 public function testGetActiveGroups() {
460 $contactId = $this->individualCreate();
461 $customGroupTitle = 'Custom Group';
462 $groupParams = [
463 'title' => $customGroupTitle,
464 'name' => 'test_custom_group',
465 'style' => 'Tab',
466 'extends' => 'Individual',
467 'weight' => 10,
468 'is_active' => 1,
469 ];
470
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);
479 }
480 }
481
482 $this->customGroupDelete($customGroup['id']);
483 $this->contactDelete($contactId);
484 }
485
486 /**
487 * Test create()
488 */
489 public function testCreate() {
490 $params = [
491 'title' => 'Test_Group_1',
492 'name' => 'test_group_1',
493 'extends' => [0 => 'Individual', 1 => []],
494 'weight' => 4,
495 'collapse_display' => 1,
496 'style' => 'Inline',
497 'help_pre' => 'This is Pre Help For Test Group 1',
498 'help_post' => 'This is Post Help For Test Group 1',
499 'is_active' => 1,
500 'version' => 3,
501 ];
502 $customGroup = CRM_Core_BAO_CustomGroup::create($params);
503
504 $dbCustomGroupTitle = $this->assertDBNotNull('CRM_Core_DAO_CustomGroup', $customGroup->id, 'title', 'id',
505 'Database check for custom group record.'
506 );
507 $this->assertEquals($params['title'], $dbCustomGroupTitle);
508
509 $dbCustomGroupTableName = $this->assertDBNotNull('CRM_Core_DAO_CustomGroup', $customGroup->id, 'table_name', 'id',
510 'Database check for custom group record.'
511 );
512 $this->assertEquals(strtolower("civicrm_value_{$params['name']}_{$customGroup->id}"), $dbCustomGroupTableName,
513 "The table name should be suffixed with '_ID' unless specified.");
514
515 $this->customGroupDelete($customGroup->id);
516 }
517
518 /**
519 * Test create() given a table_name
520 */
521 public function testCreateTableName() {
522 $params = [
523 'title' => 'Test_Group_2',
524 'name' => 'test_group_2',
525 'table_name' => 'test_otherTableName',
526 'extends' => [0 => 'Individual', 1 => []],
527 'weight' => 4,
528 'collapse_display' => 1,
529 'style' => 'Inline',
530 'help_pre' => 'This is Pre Help For Test Group 1',
531 'help_post' => 'This is Post Help For Test Group 1',
532 'is_active' => 1,
533 ];
534 $customGroup = CRM_Core_BAO_CustomGroup::create($params);
535
536 $dbCustomGroupTitle = $this->assertDBNotNull('CRM_Core_DAO_CustomGroup', $customGroup->id, 'title', 'id',
537 'Database check for custom group record.'
538 );
539 $this->assertEquals($params['title'], $dbCustomGroupTitle);
540
541 $dbCustomGroupTableName = $this->assertDBNotNull('CRM_Core_DAO_CustomGroup', $customGroup->id, 'table_name', 'id',
542 'Database check for custom group record.'
543 );
544 $this->assertEquals($params['table_name'], $dbCustomGroupTableName);
545
546 $this->customGroupDelete($customGroup->id);
547 }
548
549 /**
550 * Test isGroupEmpty()
551 */
552 public function testIsGroupEmpty() {
553 $customGroupTitle = 'Test Custom Group';
554 $groupParams = [
555 'title' => $customGroupTitle,
556 'name' => 'test_custom_group',
557 'style' => 'Tab',
558 'extends' => 'Individual',
559 'weight' => 10,
560 'is_active' => 1,
561 ];
562
563 $customGroup = $this->customGroupCreate($groupParams);
564 $customGroupId = $customGroup['id'];
565 $isEmptyGroup = CRM_Core_BAO_CustomGroup::isGroupEmpty($customGroupId);
566
567 $this->assertEquals($isEmptyGroup, TRUE, 'Check that custom Group is Empty.');
568 $this->customGroupDelete($customGroup['id']);
569 }
570
571 /**
572 * Test getGroupTitles() with Invalid Params()
573 */
574 public function testGetGroupTitlesWithInvalidParams() {
575 $params = [99];
576 $groupTitles = CRM_Core_BAO_CustomGroup::getGroupTitles($params);
577 $this->assertTrue(empty($groupTitles), 'Check that no titles are received');
578 }
579
580 /**
581 * Test getGroupTitles()
582 */
583 public function testGetGroupTitles() {
584 $groupParams = [
585 'title' => 'Test Group',
586 'name' => 'test_custom_group',
587 'style' => 'Tab',
588 'extends' => 'Individual',
589 'weight' => 10,
590 'is_active' => 1,
591 ];
592
593 $customGroup = $this->customGroupCreate($groupParams);
594
595 $fieldParams = [
596 'label' => 'Custom Field',
597 'html_type' => 'Text',
598 'data_type' => 'String',
599 'is_required' => 1,
600 'is_searchable' => 0,
601 'is_active' => 1,
602 'custom_group_id' => $customGroup['id'],
603 ];
604
605 $customField = $this->customFieldCreate($fieldParams);
606 $customFieldId = $customField['id'];
607
608 $params = [$customFieldId];
609
610 $groupTitles = CRM_Core_BAO_CustomGroup::getGroupTitles($params);
611
612 $this->assertEquals($groupTitles[$customFieldId]['groupTitle'], 'Test Group', 'Check Group Title.');
613 $this->customGroupDelete($customGroup['id']);
614 }
615
616 /**
617 * Test that passed dates are extracted from the url when processing custom data.
618 */
619 public function testExtractGetParamsReturnsDates() {
620 // Create a custom group to contain the custom field.
621 $groupParams = [
622 'title' => 'My Custom Group',
623 'name' => 'my_custom_group',
624 'extends' => 'Individual',
625 'is_active' => 1,
626 'collapse_display' => 1,
627 ];
628 $customGroup = $this->customGroupCreate($groupParams);
629 $customGroupId = $customGroup['id'];
630
631 // Create teh custom field.
632 $fieldParams = [
633 'custom_group_id' => $customGroupId,
634 'label' => 'My Custom Date Field',
635 'html_type' => 'Select Date',
636 'data_type' => 'Date',
637 'is_required' => 1,
638 'is_searchable' => 0,
639 'is_active' => 1,
640 'default_value' => '',
641 ];
642 $customField = $this->customFieldCreate($fieldParams);
643 $customFieldId = $customField['id'];
644
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();
650
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');
655
656 $this->assertEquals($extractedGetParams[$fieldName], '2017-06-13');
657 }
658
659 }