Merge pull request #15503 from magnolia61/Generalize_Thank_You_in_Message_Templates
[civicrm-core.git] / tests / phpunit / CRM / Core / BAO / CustomGroupTest.php
CommitLineData
6a488035
TO
1<?php
2/*
3 +--------------------------------------------------------------------+
2fe49090 4 | CiviCRM version 5 |
6a488035 5 +--------------------------------------------------------------------+
6b83d5bd 6 | Copyright CiviCRM LLC (c) 2004-2019 |
6a488035
TO
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 +--------------------------------------------------------------------+
d25dd0ee 26 */
6a488035 27
4cbe18b8
EM
28/**
29 * Class CRM_Core_BAO_CustomGroupTest
acb109b7 30 * @group headless
4cbe18b8 31 */
6a488035 32class CRM_Core_BAO_CustomGroupTest extends CiviUnitTestCase {
6a488035 33
00be9182 34 public function setUp() {
6a488035
TO
35 parent::setUp();
36 }
37
38 /**
fe482240 39 * Test getTree().
6a488035 40 */
00be9182 41 public function testGetTree() {
27dd6252 42 $customGroup = $this->CustomGroupCreate();
9099cab3 43 $customField = $this->customFieldCreate(['custom_group_id' => $customGroup['id']]);
27dd6252
E
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 }
6a488035 48
27dd6252
E
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() {
9099cab3
CW
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']);
27dd6252 60 $this->assertEquals('Custom Field', $result1[$customGroup['id']]['fields'][$customField['id']]['label']);
325fa965 61 $result = CRM_Core_BAO_CustomGroup::getTree('Organization', NULL, NULL, NULL, CRM_Core_DAO::VALUE_SEPARATOR . 'Big_Bank' . CRM_Core_DAO::VALUE_SEPARATOR);
27dd6252 62 $this->assertEquals($result1, $result);
325fa965 63 $result = CRM_Core_BAO_CustomGroup::getTree('Organization', NULL, NULL, NULL, 'Big_Bank');
27dd6252
E
64 $this->assertEquals($result1, $result);
65 try {
9099cab3 66 CRM_Core_BAO_CustomGroup::getTree('Organization', NULL, NULL, NULL, ['Small Kind Bank']);
27dd6252
E
67 }
68 catch (CRM_Core_Exception $e) {
69 $this->customGroupDelete($customGroup['id']);
9099cab3 70 $this->callAPISuccess('ContactType', 'delete', ['id' => $contactType['id']]);
27dd6252
E
71 return;
72 }
73 $this->fail('There is no such thing as a small kind bank');
74 }
6a488035 75
4dcd2787
J
76 /**
77 * Test calling getTree for a custom field extending a renamed contact type.
78 */
79 public function testGetTreeContactSubTypeForNameChangedContactType() {
9099cab3 80 $contactType = $this->callAPISuccess('ContactType', 'create', ['name' => 'Big Bank', 'label' => 'biggee', 'parent_id' => 'Organization']);
4dcd2787 81 CRM_Core_DAO::executeQuery('UPDATE civicrm_contact_type SET label = "boo" WHERE name = "Organization"');
9099cab3
CW
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']);
4dcd2787
J
85 $this->assertEquals('Custom Field', $result1[$customGroup['id']]['fields'][$customField['id']]['label']);
86 $this->customGroupDelete($customGroup['id']);
9099cab3 87 $this->callAPISuccess('ContactType', 'delete', ['id' => $contactType['id']]);
4dcd2787
J
88 }
89
93ecb8e8 90 /**
91 * Test calling getTree for a custom field extending a disabled contact type.
92 */
93 public function testGetTreeContactSubTypeForDisabledChangedContactType() {
9099cab3
CW
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']);
93ecb8e8 99 $this->assertEquals('Custom Field', $result1[$customGroup['id']]['fields'][$customField['id']]['label']);
100 $this->customGroupDelete($customGroup['id']);
9099cab3 101 $this->callAPISuccess('ContactType', 'delete', ['id' => $contactType['id']]);
93ecb8e8 102 }
103
9364f696
SL
104 /**
105 * Test calling GetTree for a custom field extending multiple subTypes.
106 */
66b231c2 107 public function testGetTreetContactSubTypeForMultipleSubTypes() {
9099cab3
CW
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']]);
72054bb5 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);
66b231c2
SL
113 $this->assertEquals('Custom Field', $result1[$customGroup['id']]['fields'][$customField['id']]['label']);
114 $this->customGroupDelete($customGroup['id']);
9099cab3
CW
115 $this->callAPISuccess('ContactType', 'delete', ['id' => $contactType1['id']]);
116 $this->callAPISuccess('ContactType', 'delete', ['id' => $contactType2['id']]);
66b231c2
SL
117 }
118
9364f696
SL
119 /**
120 * Test calling GetTree for a custom field that extends a non numerical Event Type.
121 */
122 public function testGetTreeEventSubTypeAlphabetical() {
9099cab3
CW
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']]);
9364f696
SL
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']);
9099cab3 129 $this->callAPISuccess('OptionValue', 'delete', ['id' => $eventType['id']]);
9364f696
SL
130 }
131
35e83f39 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() {
05e3bddf 139 $sep = CRM_Core_DAO::VALUE_SEPARATOR;
35e83f39 140 $this->campaignCreate();
141 $this->campaignCreate();
9099cab3 142 $customGroup = $this->CustomGroupCreate([
35e83f39 143 'extends' => 'Campaign',
05e3bddf 144 'extends_entity_column_value' => "{$sep}1{$sep}2{$sep}",
9099cab3
CW
145 ]);
146 $customField = $this->customFieldCreate(['custom_group_id' => $customGroup['id']]);
35e83f39 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
27dd6252
E
152 /**
153 * Test calling getTree with contact subtype data.
154 */
155 public function testGetTreeActivitySubType() {
9099cab3
CW
156 $customGroup = $this->CustomGroupCreate(['extends' => 'Activity', 'extends_entity_column_value' => 1]);
157 $customField = $this->customFieldCreate(['custom_group_id' => $customGroup['id']]);
27dd6252
E
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']);
6a488035
TO
161 }
162
163 /**
35e83f39 164 * Test retrieve() with Empty Params.
6a488035 165 */
00be9182 166 public function testRetrieveEmptyParams() {
9099cab3 167 $params = [];
6a488035
TO
168 $customGroup = CRM_Core_BAO_CustomGroup::retrieve($params, $dafaults);
169 $this->assertNull($customGroup, 'Check that no custom Group is retreived');
170 }
171
172 /**
100fef9d 173 * Test retrieve() with Inalid Params
6a488035 174 */
00be9182 175 public function testRetrieveInvalidParams() {
9099cab3 176 $params = ['id' => 99];
6a488035
TO
177 $customGroup = CRM_Core_BAO_CustomGroup::retrieve($params, $dafaults);
178 $this->assertNull($customGroup, 'Check that no custom Group is retreived');
179 }
180
181 /**
100fef9d 182 * Test retrieve()
6a488035 183 */
00be9182 184 public function testRetrieve() {
5c988f8a 185 $customGroupTitle = 'Custom Group';
9099cab3 186 $groupParams = [
5c988f8a 187 'title' => $customGroupTitle,
6a488035
TO
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,
9099cab3 196 ];
6a488035 197
5c988f8a 198 $customGroup = $this->customGroupCreate($groupParams);
6a488035 199
5c988f8a 200 $this->getAndCheck($groupParams, $customGroup['id'], 'CustomGroup');
6a488035
TO
201 }
202
203 /**
100fef9d 204 * Test setIsActive()
6a488035 205 */
00be9182 206 public function testSetIsActive() {
5c988f8a 207 $customGroupTitle = 'My Custom Group';
9099cab3 208 $groupParams = [
5c988f8a 209 'title' => $customGroupTitle,
6a488035
TO
210 'name' => 'my_custom_group',
211 'style' => 'Tab',
212 'extends' => 'Individual',
213 'is_active' => 0,
9099cab3 214 ];
6a488035 215
5c988f8a 216 $customGroup = $this->customGroupCreate($groupParams);
217 $customGroupId = $customGroup['id'];
6a488035
TO
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 );
5c988f8a 228
229 $this->customGroupDelete($customGroup['id']);
6a488035
TO
230 }
231
232 /**
100fef9d 233 * Test getGroupDetail() with Empty Params
6a488035 234 */
00be9182 235 public function testGetGroupDetailEmptyParams() {
9099cab3 236 $customGroupId = [];
6a488035
TO
237 $customGroup = CRM_Core_BAO_CustomGroup::getGroupDetail($customGroupId);
238 $this->assertTrue(empty($customGroup), 'Check that no custom Group details is retreived');
239 }
240
241 /**
5c988f8a 242 * Test getGroupDetail with Invalid Params.
6a488035 243 */
00be9182 244 public function testGetGroupDetailInvalidParams() {
6a488035
TO
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 /**
5c988f8a 251 * Test getGroupDetail().
6a488035 252 */
00be9182 253 public function testGetGroupDetail() {
5c988f8a 254 $customGroupTitle = 'My Custom Group';
9099cab3 255 $groupParams = [
5c988f8a 256 'title' => $customGroupTitle,
6a488035
TO
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,
9099cab3 263 ];
6a488035 264
5c988f8a 265 $customGroup = $this->customGroupCreate($groupParams);
266 $customGroupId = $customGroup['id'];
6a488035 267
9099cab3 268 $fieldParams = [
6a488035
TO
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,
9099cab3 276 ];
6a488035 277
5c988f8a 278 $customField = $this->customFieldCreate($fieldParams);
279 $customFieldId = $customField['id'];
6a488035
TO
280
281 $groupTree = CRM_Core_BAO_CustomGroup::getGroupDetail($customGroupId);
5c988f8a 282 $this->assertDBNotNull('CRM_Core_DAO_CustomGroup', $customGroupId, 'title', 'id',
6a488035
TO
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
5c988f8a 297 $this->customFieldDelete($customField['id']);
298 $this->customGroupDelete($customGroup['id']);
6a488035
TO
299 }
300
301 /**
100fef9d 302 * Test getTitle() with Invalid Params()
6a488035 303 */
00be9182 304 public function testGetTitleWithInvalidParams() {
6a488035
TO
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 /**
100fef9d 312 * Test getTitle()
6a488035 313 */
00be9182 314 public function testGetTitle() {
5c988f8a 315 $customGroupTitle = 'Custom Group';
9099cab3 316 $groupParams = [
5c988f8a 317 'title' => $customGroupTitle,
6a488035
TO
318 'name' => 'my_custom_group',
319 'style' => 'Tab',
320 'extends' => 'Individual',
321 'is_active' => 0,
9099cab3 322 ];
6a488035 323
5c988f8a 324 $customGroup = $this->customGroupCreate($groupParams);
325 $customGroupId = $customGroup['id'];
6a488035
TO
326
327 //get the custom group title
328 $title = CRM_Core_BAO_CustomGroup::getTitle($customGroupId);
329
330 //check for object update
5c988f8a 331 $this->assertEquals($customGroupTitle, $title);
6a488035 332
5c988f8a 333 $this->customGroupDelete($customGroupId);
6a488035
TO
334 }
335
336 /**
5c988f8a 337 * Test deleteGroup.
6a488035 338 */
00be9182 339 public function testDeleteGroup() {
5c988f8a 340 $customGroupTitle = 'My Custom Group';
9099cab3 341 $groupParams = [
5c988f8a 342 'title' => $customGroupTitle,
6a488035
TO
343 'name' => 'my_custom_group',
344 'style' => 'Tab',
345 'extends' => 'Individual',
346 'is_active' => 1,
9099cab3 347 ];
6a488035 348
5c988f8a 349 $customGroup = $this->customGroupCreate($groupParams);
350 $groupObject = new CRM_Core_BAO_CustomGroup();
351 $groupObject->id = $customGroup['id'];
352 $groupObject->find(TRUE);
6a488035 353
5c988f8a 354 $isDelete = CRM_Core_BAO_CustomGroup::deleteGroup($groupObject);
6a488035 355
5c988f8a 356 // Check it worked!
6a488035 357 $this->assertEquals(TRUE, $isDelete);
5c988f8a 358 $this->assertDBNull('CRM_Core_DAO_CustomGroup', $customGroup['id'], 'title', 'id',
6a488035
TO
359 'Database check for custom group record.'
360 );
361 }
362
363 /**
100fef9d 364 * Test createTable()
6a488035 365 */
00be9182 366 public function testCreateTable() {
9099cab3 367 $groupParams = [
5c988f8a 368 'title' => 'My Custom Group',
6a488035
TO
369 'name' => 'my_custom_group',
370 'style' => 'Tab',
371 'extends' => 'Individual',
372 'is_active' => 1,
373 'version' => 3,
9099cab3 374 ];
6a488035
TO
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();
5c988f8a 382 CRM_Core_BAO_CustomGroup::createTable($customGroup);
6a488035
TO
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
5c988f8a 394 $this->customGroupDelete($customGroup->id);
6a488035
TO
395 }
396
397 /**
100fef9d 398 * Test checkCustomField()
6a488035 399 */
00be9182 400 public function testCheckCustomField() {
9099cab3 401 $groupParams = [
5c988f8a 402 'title' => 'My Custom Group',
6a488035
TO
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,
9099cab3 409 ];
6a488035 410
5c988f8a 411 $customGroup = $this->customGroupCreate($groupParams);
412 $this->assertNotNull($customGroup['id'], 'pre-requisite group not created successfully');
413 $customGroupId = $customGroup['id'];
6a488035
TO
414
415 $customFieldLabel = 'Test Custom Field';
9099cab3 416 $fieldParams = [
6a488035
TO
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,
9099cab3 424 ];
6a488035 425
5c988f8a 426 $customField = $this->customFieldCreate($fieldParams);
427 $customField = $customField['values'][$customField['id']];
6a488035 428
5c988f8a 429 $customFieldId = $customField['id'];
6a488035
TO
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.
9099cab3 438 $params = ['Individual'];
6a488035
TO
439 $usedFor = CRM_Core_BAO_CustomGroup::checkCustomField($customFieldId, $params);
440 $this->assertEquals(FALSE, $usedFor);
441
9099cab3 442 $params = ['Contribution', 'Membership', 'Participant'];
6a488035
TO
443 $usedFor = CRM_Core_BAO_CustomGroup::checkCustomField($customFieldId, $params);
444 $this->assertEquals(TRUE, $usedFor);
445
5c988f8a 446 $this->customFieldDelete($customField['id']);
447 $this->customGroupDelete($customGroup['id']);
6a488035
TO
448 }
449
450 /**
100fef9d 451 * Test getActiveGroups() with Invalid Params()
6a488035 452 */
00be9182 453 public function testGetActiveGroupsWithInvalidParams() {
f2040bc6 454 $contactId = $this->individualCreate();
6a488035
TO
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
00be9182 459 public function testGetActiveGroups() {
f2040bc6 460 $contactId = $this->individualCreate();
5c988f8a 461 $customGroupTitle = 'Custom Group';
9099cab3 462 $groupParams = [
5c988f8a 463 'title' => $customGroupTitle,
6a488035
TO
464 'name' => 'test_custom_group',
465 'style' => 'Tab',
466 'extends' => 'Individual',
467 'weight' => 10,
468 'is_active' => 1,
9099cab3 469 ];
6a488035 470
5c988f8a 471 $customGroup = $this->customGroupCreate($groupParams);
6a488035
TO
472 $activeGroup = CRM_Core_BAO_CustomGroup::getActiveGroups('Individual', 'civicrm/contact/view/cd', $contactId);
473 foreach ($activeGroup as $key => $value) {
5c988f8a 474 if ($value['id'] == $customGroup['id']) {
6a488035 475 $this->assertEquals($value['path'], 'civicrm/contact/view/cd');
5c988f8a 476 $this->assertEquals($value['title'], $customGroupTitle);
477 $query = 'reset=1&gid=' . $customGroup['id'] . '&cid=' . $contactId;
6a488035
TO
478 $this->assertEquals($value['query'], $query);
479 }
480 }
481
5c988f8a 482 $this->customGroupDelete($customGroup['id']);
93ac19cd 483 $this->contactDelete($contactId);
6a488035
TO
484 }
485
486 /**
100fef9d 487 * Test create()
6a488035 488 */
00be9182 489 public function testCreate() {
9099cab3 490 $params = [
6a488035
TO
491 'title' => 'Test_Group_1',
492 'name' => 'test_group_1',
9099cab3 493 'extends' => [0 => 'Individual', 1 => []],
6a488035
TO
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,
9099cab3 501 ];
6a488035
TO
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);
8b3b9a2e
ARW
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,
92915c55 513 "The table name should be suffixed with '_ID' unless specified.");
8b3b9a2e 514
5c988f8a 515 $this->customGroupDelete($customGroup->id);
8b3b9a2e
ARW
516 }
517
518 /**
100fef9d 519 * Test create() given a table_name
8b3b9a2e 520 */
00be9182 521 public function testCreateTableName() {
9099cab3 522 $params = [
8b3b9a2e
ARW
523 'title' => 'Test_Group_2',
524 'name' => 'test_group_2',
525 'table_name' => 'test_otherTableName',
9099cab3 526 'extends' => [0 => 'Individual', 1 => []],
8b3b9a2e
ARW
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,
9099cab3 533 ];
8b3b9a2e
ARW
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
5c988f8a 546 $this->customGroupDelete($customGroup->id);
6a488035
TO
547 }
548
549 /**
100fef9d 550 * Test isGroupEmpty()
6a488035 551 */
00be9182 552 public function testIsGroupEmpty() {
5c988f8a 553 $customGroupTitle = 'Test Custom Group';
9099cab3 554 $groupParams = [
5c988f8a 555 'title' => $customGroupTitle,
6a488035
TO
556 'name' => 'test_custom_group',
557 'style' => 'Tab',
558 'extends' => 'Individual',
559 'weight' => 10,
560 'is_active' => 1,
9099cab3 561 ];
6a488035 562
5c988f8a 563 $customGroup = $this->customGroupCreate($groupParams);
564 $customGroupId = $customGroup['id'];
92915c55 565 $isEmptyGroup = CRM_Core_BAO_CustomGroup::isGroupEmpty($customGroupId);
6a488035
TO
566
567 $this->assertEquals($isEmptyGroup, TRUE, 'Check that custom Group is Empty.');
5c988f8a 568 $this->customGroupDelete($customGroup['id']);
6a488035
TO
569 }
570
571 /**
100fef9d 572 * Test getGroupTitles() with Invalid Params()
6a488035 573 */
5c988f8a 574 public function testGetGroupTitlesWithInvalidParams() {
9099cab3 575 $params = [99];
6a488035 576 $groupTitles = CRM_Core_BAO_CustomGroup::getGroupTitles($params);
5c988f8a 577 $this->assertTrue(empty($groupTitles), 'Check that no titles are received');
6a488035
TO
578 }
579
580 /**
100fef9d 581 * Test getGroupTitles()
6a488035 582 */
5c988f8a 583 public function testGetGroupTitles() {
9099cab3 584 $groupParams = [
5c988f8a 585 'title' => 'Test Group',
6a488035
TO
586 'name' => 'test_custom_group',
587 'style' => 'Tab',
588 'extends' => 'Individual',
589 'weight' => 10,
590 'is_active' => 1,
9099cab3 591 ];
6a488035 592
5c988f8a 593 $customGroup = $this->customGroupCreate($groupParams);
6a488035 594
9099cab3 595 $fieldParams = [
5c988f8a 596 'label' => 'Custom Field',
6a488035
TO
597 'html_type' => 'Text',
598 'data_type' => 'String',
599 'is_required' => 1,
600 'is_searchable' => 0,
601 'is_active' => 1,
5c988f8a 602 'custom_group_id' => $customGroup['id'],
9099cab3 603 ];
6a488035 604
5c988f8a 605 $customField = $this->customFieldCreate($fieldParams);
606 $customFieldId = $customField['id'];
6a488035 607
9099cab3 608 $params = [$customFieldId];
6a488035
TO
609
610 $groupTitles = CRM_Core_BAO_CustomGroup::getGroupTitles($params);
611
5c988f8a 612 $this->assertEquals($groupTitles[$customFieldId]['groupTitle'], 'Test Group', 'Check Group Title.');
613 $this->customGroupDelete($customGroup['id']);
6a488035 614 }
96025800 615
31349d27
AS
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.
9099cab3 621 $groupParams = [
31349d27
AS
622 'title' => 'My Custom Group',
623 'name' => 'my_custom_group',
624 'extends' => 'Individual',
625 'is_active' => 1,
626 'collapse_display' => 1,
9099cab3 627 ];
31349d27
AS
628 $customGroup = $this->customGroupCreate($groupParams);
629 $customGroupId = $customGroup['id'];
630
631 // Create teh custom field.
9099cab3 632 $fieldParams = [
31349d27
AS
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' => '',
9099cab3 641 ];
31349d27
AS
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
6a488035 659}