Merge pull request #17253 from mattwire/utf8convertblocksize
[civicrm-core.git] / tests / phpunit / CRM / Core / BAO / CustomGroupTest.php
CommitLineData
6a488035
TO
1<?php
2/*
3 +--------------------------------------------------------------------+
7d61e75f 4 | Copyright CiviCRM LLC. All rights reserved. |
6a488035 5 | |
7d61e75f
TO
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 |
6a488035 9 +--------------------------------------------------------------------+
d25dd0ee 10 */
6a488035 11
4cbe18b8
EM
12/**
13 * Class CRM_Core_BAO_CustomGroupTest
acb109b7 14 * @group headless
4cbe18b8 15 */
6a488035 16class CRM_Core_BAO_CustomGroupTest extends CiviUnitTestCase {
6a488035 17
00be9182 18 public function setUp() {
6a488035
TO
19 parent::setUp();
20 }
21
22 /**
fe482240 23 * Test getTree().
6a488035 24 */
00be9182 25 public function testGetTree() {
27dd6252 26 $customGroup = $this->CustomGroupCreate();
9099cab3 27 $customField = $this->customFieldCreate(['custom_group_id' => $customGroup['id']]);
27dd6252
E
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 }
6a488035 32
27dd6252
E
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() {
9099cab3
CW
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']);
27dd6252 44 $this->assertEquals('Custom Field', $result1[$customGroup['id']]['fields'][$customField['id']]['label']);
325fa965 45 $result = CRM_Core_BAO_CustomGroup::getTree('Organization', NULL, NULL, NULL, CRM_Core_DAO::VALUE_SEPARATOR . 'Big_Bank' . CRM_Core_DAO::VALUE_SEPARATOR);
27dd6252 46 $this->assertEquals($result1, $result);
325fa965 47 $result = CRM_Core_BAO_CustomGroup::getTree('Organization', NULL, NULL, NULL, 'Big_Bank');
27dd6252
E
48 $this->assertEquals($result1, $result);
49 try {
9099cab3 50 CRM_Core_BAO_CustomGroup::getTree('Organization', NULL, NULL, NULL, ['Small Kind Bank']);
27dd6252
E
51 }
52 catch (CRM_Core_Exception $e) {
53 $this->customGroupDelete($customGroup['id']);
9099cab3 54 $this->callAPISuccess('ContactType', 'delete', ['id' => $contactType['id']]);
27dd6252
E
55 return;
56 }
57 $this->fail('There is no such thing as a small kind bank');
58 }
6a488035 59
4dcd2787
J
60 /**
61 * Test calling getTree for a custom field extending a renamed contact type.
62 */
63 public function testGetTreeContactSubTypeForNameChangedContactType() {
9099cab3 64 $contactType = $this->callAPISuccess('ContactType', 'create', ['name' => 'Big Bank', 'label' => 'biggee', 'parent_id' => 'Organization']);
4dcd2787 65 CRM_Core_DAO::executeQuery('UPDATE civicrm_contact_type SET label = "boo" WHERE name = "Organization"');
9099cab3
CW
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']);
4dcd2787
J
69 $this->assertEquals('Custom Field', $result1[$customGroup['id']]['fields'][$customField['id']]['label']);
70 $this->customGroupDelete($customGroup['id']);
9099cab3 71 $this->callAPISuccess('ContactType', 'delete', ['id' => $contactType['id']]);
4dcd2787
J
72 }
73
93ecb8e8 74 /**
75 * Test calling getTree for a custom field extending a disabled contact type.
76 */
77 public function testGetTreeContactSubTypeForDisabledChangedContactType() {
9099cab3
CW
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']);
93ecb8e8 83 $this->assertEquals('Custom Field', $result1[$customGroup['id']]['fields'][$customField['id']]['label']);
84 $this->customGroupDelete($customGroup['id']);
9099cab3 85 $this->callAPISuccess('ContactType', 'delete', ['id' => $contactType['id']]);
93ecb8e8 86 }
87
9364f696
SL
88 /**
89 * Test calling GetTree for a custom field extending multiple subTypes.
90 */
66b231c2 91 public function testGetTreetContactSubTypeForMultipleSubTypes() {
9099cab3
CW
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']]);
72054bb5 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);
66b231c2
SL
97 $this->assertEquals('Custom Field', $result1[$customGroup['id']]['fields'][$customField['id']]['label']);
98 $this->customGroupDelete($customGroup['id']);
9099cab3
CW
99 $this->callAPISuccess('ContactType', 'delete', ['id' => $contactType1['id']]);
100 $this->callAPISuccess('ContactType', 'delete', ['id' => $contactType2['id']]);
66b231c2
SL
101 }
102
9364f696
SL
103 /**
104 * Test calling GetTree for a custom field that extends a non numerical Event Type.
105 */
106 public function testGetTreeEventSubTypeAlphabetical() {
9099cab3
CW
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']]);
9364f696
SL
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']);
9099cab3 113 $this->callAPISuccess('OptionValue', 'delete', ['id' => $eventType['id']]);
9364f696
SL
114 }
115
35e83f39 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() {
05e3bddf 123 $sep = CRM_Core_DAO::VALUE_SEPARATOR;
35e83f39 124 $this->campaignCreate();
125 $this->campaignCreate();
9099cab3 126 $customGroup = $this->CustomGroupCreate([
35e83f39 127 'extends' => 'Campaign',
05e3bddf 128 'extends_entity_column_value' => "{$sep}1{$sep}2{$sep}",
9099cab3
CW
129 ]);
130 $customField = $this->customFieldCreate(['custom_group_id' => $customGroup['id']]);
35e83f39 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
27dd6252
E
136 /**
137 * Test calling getTree with contact subtype data.
138 */
139 public function testGetTreeActivitySubType() {
9099cab3
CW
140 $customGroup = $this->CustomGroupCreate(['extends' => 'Activity', 'extends_entity_column_value' => 1]);
141 $customField = $this->customFieldCreate(['custom_group_id' => $customGroup['id']]);
27dd6252
E
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']);
6a488035
TO
145 }
146
147 /**
35e83f39 148 * Test retrieve() with Empty Params.
6a488035 149 */
00be9182 150 public function testRetrieveEmptyParams() {
9099cab3 151 $params = [];
6a488035
TO
152 $customGroup = CRM_Core_BAO_CustomGroup::retrieve($params, $dafaults);
153 $this->assertNull($customGroup, 'Check that no custom Group is retreived');
154 }
155
156 /**
100fef9d 157 * Test retrieve() with Inalid Params
6a488035 158 */
00be9182 159 public function testRetrieveInvalidParams() {
9099cab3 160 $params = ['id' => 99];
6a488035
TO
161 $customGroup = CRM_Core_BAO_CustomGroup::retrieve($params, $dafaults);
162 $this->assertNull($customGroup, 'Check that no custom Group is retreived');
163 }
164
165 /**
100fef9d 166 * Test retrieve()
6a488035 167 */
00be9182 168 public function testRetrieve() {
5c988f8a 169 $customGroupTitle = 'Custom Group';
9099cab3 170 $groupParams = [
5c988f8a 171 'title' => $customGroupTitle,
6a488035
TO
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,
9099cab3 180 ];
6a488035 181
5c988f8a 182 $customGroup = $this->customGroupCreate($groupParams);
6a488035 183
5c988f8a 184 $this->getAndCheck($groupParams, $customGroup['id'], 'CustomGroup');
6a488035
TO
185 }
186
187 /**
100fef9d 188 * Test setIsActive()
6a488035 189 */
00be9182 190 public function testSetIsActive() {
5c988f8a 191 $customGroupTitle = 'My Custom Group';
9099cab3 192 $groupParams = [
5c988f8a 193 'title' => $customGroupTitle,
6a488035
TO
194 'name' => 'my_custom_group',
195 'style' => 'Tab',
196 'extends' => 'Individual',
197 'is_active' => 0,
9099cab3 198 ];
6a488035 199
5c988f8a 200 $customGroup = $this->customGroupCreate($groupParams);
201 $customGroupId = $customGroup['id'];
6a488035
TO
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 );
5c988f8a 212
213 $this->customGroupDelete($customGroup['id']);
6a488035
TO
214 }
215
216 /**
100fef9d 217 * Test getGroupDetail() with Empty Params
6a488035 218 */
00be9182 219 public function testGetGroupDetailEmptyParams() {
9099cab3 220 $customGroupId = [];
6a488035
TO
221 $customGroup = CRM_Core_BAO_CustomGroup::getGroupDetail($customGroupId);
222 $this->assertTrue(empty($customGroup), 'Check that no custom Group details is retreived');
223 }
224
225 /**
5c988f8a 226 * Test getGroupDetail with Invalid Params.
6a488035 227 */
00be9182 228 public function testGetGroupDetailInvalidParams() {
6a488035
TO
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 /**
5c988f8a 235 * Test getGroupDetail().
6a488035 236 */
00be9182 237 public function testGetGroupDetail() {
5c988f8a 238 $customGroupTitle = 'My Custom Group';
9099cab3 239 $groupParams = [
5c988f8a 240 'title' => $customGroupTitle,
6a488035
TO
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,
9099cab3 247 ];
6a488035 248
5c988f8a 249 $customGroup = $this->customGroupCreate($groupParams);
250 $customGroupId = $customGroup['id'];
6a488035 251
9099cab3 252 $fieldParams = [
6a488035
TO
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,
9099cab3 260 ];
6a488035 261
5c988f8a 262 $customField = $this->customFieldCreate($fieldParams);
263 $customFieldId = $customField['id'];
6a488035
TO
264
265 $groupTree = CRM_Core_BAO_CustomGroup::getGroupDetail($customGroupId);
5c988f8a 266 $this->assertDBNotNull('CRM_Core_DAO_CustomGroup', $customGroupId, 'title', 'id',
6a488035
TO
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
5c988f8a 281 $this->customFieldDelete($customField['id']);
282 $this->customGroupDelete($customGroup['id']);
6a488035
TO
283 }
284
285 /**
100fef9d 286 * Test getTitle() with Invalid Params()
6a488035 287 */
00be9182 288 public function testGetTitleWithInvalidParams() {
6a488035
TO
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 /**
100fef9d 296 * Test getTitle()
6a488035 297 */
00be9182 298 public function testGetTitle() {
5c988f8a 299 $customGroupTitle = 'Custom Group';
9099cab3 300 $groupParams = [
5c988f8a 301 'title' => $customGroupTitle,
6a488035
TO
302 'name' => 'my_custom_group',
303 'style' => 'Tab',
304 'extends' => 'Individual',
305 'is_active' => 0,
9099cab3 306 ];
6a488035 307
5c988f8a 308 $customGroup = $this->customGroupCreate($groupParams);
309 $customGroupId = $customGroup['id'];
6a488035
TO
310
311 //get the custom group title
312 $title = CRM_Core_BAO_CustomGroup::getTitle($customGroupId);
313
314 //check for object update
5c988f8a 315 $this->assertEquals($customGroupTitle, $title);
6a488035 316
5c988f8a 317 $this->customGroupDelete($customGroupId);
6a488035
TO
318 }
319
320 /**
5c988f8a 321 * Test deleteGroup.
6a488035 322 */
00be9182 323 public function testDeleteGroup() {
5c988f8a 324 $customGroupTitle = 'My Custom Group';
9099cab3 325 $groupParams = [
5c988f8a 326 'title' => $customGroupTitle,
6a488035
TO
327 'name' => 'my_custom_group',
328 'style' => 'Tab',
329 'extends' => 'Individual',
330 'is_active' => 1,
9099cab3 331 ];
6a488035 332
5c988f8a 333 $customGroup = $this->customGroupCreate($groupParams);
334 $groupObject = new CRM_Core_BAO_CustomGroup();
335 $groupObject->id = $customGroup['id'];
336 $groupObject->find(TRUE);
6a488035 337
5c988f8a 338 $isDelete = CRM_Core_BAO_CustomGroup::deleteGroup($groupObject);
6a488035 339
5c988f8a 340 // Check it worked!
6a488035 341 $this->assertEquals(TRUE, $isDelete);
5c988f8a 342 $this->assertDBNull('CRM_Core_DAO_CustomGroup', $customGroup['id'], 'title', 'id',
6a488035
TO
343 'Database check for custom group record.'
344 );
345 }
346
347 /**
100fef9d 348 * Test createTable()
6a488035 349 */
00be9182 350 public function testCreateTable() {
9099cab3 351 $groupParams = [
5c988f8a 352 'title' => 'My Custom Group',
6a488035
TO
353 'name' => 'my_custom_group',
354 'style' => 'Tab',
355 'extends' => 'Individual',
356 'is_active' => 1,
357 'version' => 3,
9099cab3 358 ];
6a488035
TO
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();
5c988f8a 366 CRM_Core_BAO_CustomGroup::createTable($customGroup);
6a488035
TO
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
5c988f8a 378 $this->customGroupDelete($customGroup->id);
6a488035
TO
379 }
380
381 /**
100fef9d 382 * Test checkCustomField()
6a488035 383 */
00be9182 384 public function testCheckCustomField() {
9099cab3 385 $groupParams = [
5c988f8a 386 'title' => 'My Custom Group',
6a488035
TO
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,
9099cab3 393 ];
6a488035 394
5c988f8a 395 $customGroup = $this->customGroupCreate($groupParams);
396 $this->assertNotNull($customGroup['id'], 'pre-requisite group not created successfully');
397 $customGroupId = $customGroup['id'];
6a488035
TO
398
399 $customFieldLabel = 'Test Custom Field';
9099cab3 400 $fieldParams = [
6a488035
TO
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,
9099cab3 408 ];
6a488035 409
5c988f8a 410 $customField = $this->customFieldCreate($fieldParams);
411 $customField = $customField['values'][$customField['id']];
6a488035 412
5c988f8a 413 $customFieldId = $customField['id'];
6a488035
TO
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.
9099cab3 422 $params = ['Individual'];
6a488035
TO
423 $usedFor = CRM_Core_BAO_CustomGroup::checkCustomField($customFieldId, $params);
424 $this->assertEquals(FALSE, $usedFor);
425
9099cab3 426 $params = ['Contribution', 'Membership', 'Participant'];
6a488035
TO
427 $usedFor = CRM_Core_BAO_CustomGroup::checkCustomField($customFieldId, $params);
428 $this->assertEquals(TRUE, $usedFor);
429
5c988f8a 430 $this->customFieldDelete($customField['id']);
431 $this->customGroupDelete($customGroup['id']);
6a488035
TO
432 }
433
434 /**
100fef9d 435 * Test getActiveGroups() with Invalid Params()
6a488035 436 */
00be9182 437 public function testGetActiveGroupsWithInvalidParams() {
f2040bc6 438 $contactId = $this->individualCreate();
6a488035
TO
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
00be9182 443 public function testGetActiveGroups() {
f2040bc6 444 $contactId = $this->individualCreate();
5c988f8a 445 $customGroupTitle = 'Custom Group';
9099cab3 446 $groupParams = [
5c988f8a 447 'title' => $customGroupTitle,
6a488035
TO
448 'name' => 'test_custom_group',
449 'style' => 'Tab',
450 'extends' => 'Individual',
451 'weight' => 10,
452 'is_active' => 1,
9099cab3 453 ];
6a488035 454
5c988f8a 455 $customGroup = $this->customGroupCreate($groupParams);
6a488035
TO
456 $activeGroup = CRM_Core_BAO_CustomGroup::getActiveGroups('Individual', 'civicrm/contact/view/cd', $contactId);
457 foreach ($activeGroup as $key => $value) {
5c988f8a 458 if ($value['id'] == $customGroup['id']) {
6a488035 459 $this->assertEquals($value['path'], 'civicrm/contact/view/cd');
5c988f8a 460 $this->assertEquals($value['title'], $customGroupTitle);
461 $query = 'reset=1&gid=' . $customGroup['id'] . '&cid=' . $contactId;
6a488035
TO
462 $this->assertEquals($value['query'], $query);
463 }
464 }
465
5c988f8a 466 $this->customGroupDelete($customGroup['id']);
93ac19cd 467 $this->contactDelete($contactId);
6a488035
TO
468 }
469
470 /**
100fef9d 471 * Test create()
6a488035 472 */
00be9182 473 public function testCreate() {
9099cab3 474 $params = [
6a488035
TO
475 'title' => 'Test_Group_1',
476 'name' => 'test_group_1',
9099cab3 477 'extends' => [0 => 'Individual', 1 => []],
6a488035
TO
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,
9099cab3 485 ];
6a488035
TO
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);
8b3b9a2e
ARW
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,
92915c55 497 "The table name should be suffixed with '_ID' unless specified.");
8b3b9a2e 498
5c988f8a 499 $this->customGroupDelete($customGroup->id);
8b3b9a2e
ARW
500 }
501
502 /**
100fef9d 503 * Test create() given a table_name
8b3b9a2e 504 */
00be9182 505 public function testCreateTableName() {
9099cab3 506 $params = [
8b3b9a2e
ARW
507 'title' => 'Test_Group_2',
508 'name' => 'test_group_2',
509 'table_name' => 'test_otherTableName',
9099cab3 510 'extends' => [0 => 'Individual', 1 => []],
8b3b9a2e
ARW
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,
9099cab3 517 ];
8b3b9a2e
ARW
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
5c988f8a 530 $this->customGroupDelete($customGroup->id);
6a488035
TO
531 }
532
533 /**
100fef9d 534 * Test isGroupEmpty()
6a488035 535 */
00be9182 536 public function testIsGroupEmpty() {
5c988f8a 537 $customGroupTitle = 'Test Custom Group';
9099cab3 538 $groupParams = [
5c988f8a 539 'title' => $customGroupTitle,
6a488035
TO
540 'name' => 'test_custom_group',
541 'style' => 'Tab',
542 'extends' => 'Individual',
543 'weight' => 10,
544 'is_active' => 1,
9099cab3 545 ];
6a488035 546
5c988f8a 547 $customGroup = $this->customGroupCreate($groupParams);
548 $customGroupId = $customGroup['id'];
92915c55 549 $isEmptyGroup = CRM_Core_BAO_CustomGroup::isGroupEmpty($customGroupId);
6a488035
TO
550
551 $this->assertEquals($isEmptyGroup, TRUE, 'Check that custom Group is Empty.');
5c988f8a 552 $this->customGroupDelete($customGroup['id']);
6a488035
TO
553 }
554
555 /**
100fef9d 556 * Test getGroupTitles() with Invalid Params()
6a488035 557 */
5c988f8a 558 public function testGetGroupTitlesWithInvalidParams() {
9099cab3 559 $params = [99];
6a488035 560 $groupTitles = CRM_Core_BAO_CustomGroup::getGroupTitles($params);
5c988f8a 561 $this->assertTrue(empty($groupTitles), 'Check that no titles are received');
6a488035
TO
562 }
563
564 /**
100fef9d 565 * Test getGroupTitles()
6a488035 566 */
5c988f8a 567 public function testGetGroupTitles() {
9099cab3 568 $groupParams = [
5c988f8a 569 'title' => 'Test Group',
6a488035
TO
570 'name' => 'test_custom_group',
571 'style' => 'Tab',
572 'extends' => 'Individual',
573 'weight' => 10,
574 'is_active' => 1,
9099cab3 575 ];
6a488035 576
5c988f8a 577 $customGroup = $this->customGroupCreate($groupParams);
6a488035 578
9099cab3 579 $fieldParams = [
5c988f8a 580 'label' => 'Custom Field',
6a488035
TO
581 'html_type' => 'Text',
582 'data_type' => 'String',
583 'is_required' => 1,
584 'is_searchable' => 0,
585 'is_active' => 1,
5c988f8a 586 'custom_group_id' => $customGroup['id'],
9099cab3 587 ];
6a488035 588
5c988f8a 589 $customField = $this->customFieldCreate($fieldParams);
590 $customFieldId = $customField['id'];
6a488035 591
9099cab3 592 $params = [$customFieldId];
6a488035
TO
593
594 $groupTitles = CRM_Core_BAO_CustomGroup::getGroupTitles($params);
595
5c988f8a 596 $this->assertEquals($groupTitles[$customFieldId]['groupTitle'], 'Test Group', 'Check Group Title.');
597 $this->customGroupDelete($customGroup['id']);
6a488035 598 }
96025800 599
31349d27
AS
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.
9099cab3 605 $groupParams = [
31349d27
AS
606 'title' => 'My Custom Group',
607 'name' => 'my_custom_group',
608 'extends' => 'Individual',
609 'is_active' => 1,
610 'collapse_display' => 1,
9099cab3 611 ];
31349d27
AS
612 $customGroup = $this->customGroupCreate($groupParams);
613 $customGroupId = $customGroup['id'];
614
615 // Create teh custom field.
9099cab3 616 $fieldParams = [
31349d27
AS
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' => '',
9099cab3 625 ];
31349d27
AS
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
6a488035 643}