CRM-18469, CRM-17984 - getTree regression on multiple integers separated by the cnrtl...
[civicrm-core.git] / tests / phpunit / CRM / Core / BAO / CustomGroupTest.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.7 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2016 |
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(array('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 $this->callAPISuccess('ContactType', 'create', array('name' => 'Big Bank', 'parent_id' => 'Organization'));
57 $customGroup = $this->CustomGroupCreate(array('extends' => 'Organization', 'extends_entity_column_value' => array('Big Bank')));
58 $customField = $this->customFieldCreate(array('custom_group_id' => $customGroup['id']));
59 $result1 = CRM_Core_BAO_CustomGroup::getTree('Organization', NULL, NULL, NULL, array('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, array('Small Kind Bank'));
67 }
68 catch (CRM_Core_Exception $e) {
69 $this->customGroupDelete($customGroup['id']);
70 return;
71 }
72 $this->fail('There is no such thing as a small kind bank');
73 }
74
75 /**
76 * Test calling getTree with contact subtype data.
77 *
78 * Note that the function seems to support a range of formats so 3 are tested. Yay for
79 * inconsistency.
80 */
81 public function testGetTreeCampaignSubType() {
82 $this->campaignCreate();
83 $this->campaignCreate();
84 $customGroup = $this->CustomGroupCreate(array(
85 'extends' => 'Campaign',
86 'extends_entity_column_value' => '\ 11\ 12\ 1'
87 ));
88 $customField = $this->customFieldCreate(array('custom_group_id' => $customGroup['id']));
89 $result1 = CRM_Core_BAO_CustomGroup::getTree('Campaign', NULL, NULL, NULL, '\ 11\ 12\ 1');
90 $this->assertEquals('Custom Field', $result1[$customGroup['id']]['fields'][$customField['id']]['label']);
91 $this->customGroupDelete($customGroup['id']);
92 }
93
94 /**
95 * Test calling getTree with contact subtype data.
96 */
97 public function testGetTreeActivitySubType() {
98 $customGroup = $this->CustomGroupCreate(array('extends' => 'Activity', 'extends_entity_column_value' => 1));
99 $customField = $this->customFieldCreate(array('custom_group_id' => $customGroup['id']));
100 $result = CRM_Core_BAO_CustomGroup::getTree('Activity', NULL, NULL, NULL, 1);
101 $this->assertEquals('Custom Field', $result[$customGroup['id']]['fields'][$customField['id']]['label']);
102 $this->customGroupDelete($customGroup['id']);
103 }
104
105 /**
106 * Test retrieve() with Empty Params.
107 */
108 public function testRetrieveEmptyParams() {
109 $params = array();
110 $customGroup = CRM_Core_BAO_CustomGroup::retrieve($params, $dafaults);
111 $this->assertNull($customGroup, 'Check that no custom Group is retreived');
112 }
113
114 /**
115 * Test retrieve() with Inalid Params
116 */
117 public function testRetrieveInvalidParams() {
118 $params = array('id' => 99);
119 $customGroup = CRM_Core_BAO_CustomGroup::retrieve($params, $dafaults);
120 $this->assertNull($customGroup, 'Check that no custom Group is retreived');
121 }
122
123 /**
124 * Test retrieve()
125 */
126 public function testRetrieve() {
127 $customGrouptitle = 'My Custom Group';
128 $groupParams = array(
129 'title' => $customGrouptitle,
130 'name' => 'My_Custom_Group',
131 'style' => 'Tab',
132 'extends' => 'Individual',
133 'help_pre' => 'Custom Group Help Pre',
134 'help_post' => 'Custom Group Help Post',
135 'is_active' => 1,
136 'collapse_display' => 1,
137 'weight' => 2,
138 'version' => 3,
139 );
140
141 $customGroup = Custom::createGroup($groupParams);
142 $customGroupId = $customGroup->id;
143
144 $params = array('id' => $customGroupId);
145 $customGroup = CRM_Core_BAO_CustomGroup::retrieve($params, $dafaults);
146 $dbCustomGroupTitle = $this->assertDBNotNull('CRM_Core_DAO_CustomGroup', $customGroupId, 'title', 'id',
147 'Database check for custom group record.'
148 );
149
150 $this->assertEquals($customGrouptitle, $dbCustomGroupTitle);
151 //check retieve values
152 unset($groupParams['version']);
153 $this->assertAttributesEquals($groupParams, $dafaults);
154
155 //cleanup DB by deleting customGroup
156 Custom::deleteGroup($customGroup);
157 }
158
159 /**
160 * Test setIsActive()
161 */
162 public function testSetIsActive() {
163 $customGrouptitle = 'My Custom Group';
164 $groupParams = array(
165 'title' => $customGrouptitle,
166 'name' => 'my_custom_group',
167 'style' => 'Tab',
168 'extends' => 'Individual',
169 'is_active' => 0,
170 'version' => 3,
171 );
172
173 $customGroup = Custom::createGroup($groupParams);
174 $customGroupId = $customGroup->id;
175
176 //update is_active
177 $result = CRM_Core_BAO_CustomGroup::setIsActive($customGroupId, TRUE);
178
179 //check for object update
180 $this->assertEquals(TRUE, $result);
181 //check for is_active
182 $this->assertDBCompareValue('CRM_Core_DAO_CustomGroup', $customGroupId, 'is_active', 'id', 1,
183 'Database check for custom group is_active field.'
184 );
185 //cleanup DB by deleting customGroup
186 Custom::deleteGroup($customGroup);
187 }
188
189 /**
190 * Test getGroupDetail() with Empty Params
191 */
192 public function testGetGroupDetailEmptyParams() {
193 $customGroupId = array();
194 $customGroup = CRM_Core_BAO_CustomGroup::getGroupDetail($customGroupId);
195 $this->assertTrue(empty($customGroup), 'Check that no custom Group details is retreived');
196 }
197
198 /**
199 * Test getGroupDetail() with Inalid Params
200 */
201 public function testGetGroupDetailInvalidParams() {
202 $customGroupId = 99;
203 $customGroup = CRM_Core_BAO_CustomGroup::getGroupDetail($customGroupId);
204 $this->assertTrue(empty($customGroup), 'Check that no custom Group details is retreived');
205 }
206
207 /**
208 * Test getGroupDetail()
209 */
210 public function testGetGroupDetail() {
211 $customGrouptitle = 'My Custom Group';
212 $groupParams = array(
213 'title' => $customGrouptitle,
214 'name' => 'My_Custom_Group',
215 'extends' => 'Individual',
216 'help_pre' => 'Custom Group Help Pre',
217 'help_post' => 'Custom Group Help Post',
218 'is_active' => 1,
219 'collapse_display' => 1,
220 'version' => 3,
221 );
222
223 $customGroup = Custom::createGroup($groupParams);
224 $customGroupId = $customGroup->id;
225
226 $fieldParams = array(
227 'custom_group_id' => $customGroupId,
228 'label' => 'Test Custom Field',
229 'html_type' => 'Text',
230 'data_type' => 'String',
231 'is_required' => 1,
232 'is_searchable' => 0,
233 'is_active' => 1,
234 'version' => 3,
235 );
236
237 $customField = Custom::createField($fieldParams);
238 $customFieldId = $customField->id;
239
240 $groupTree = CRM_Core_BAO_CustomGroup::getGroupDetail($customGroupId);
241 $dbCustomGroupTitle = $this->assertDBNotNull('CRM_Core_DAO_CustomGroup', $customGroupId, 'title', 'id',
242 'Database check for custom group record.'
243 );
244 //check retieve values of custom group
245 unset($groupParams['is_active']);
246 unset($groupParams['title']);
247 unset($groupParams['version']);
248 $this->assertAttributesEquals($groupParams, $groupTree[$customGroupId]);
249
250 //check retieve values of custom field
251 unset($fieldParams['is_active']);
252 unset($fieldParams['custom_group_id']);
253 unset($fieldParams['version']);
254 $this->assertAttributesEquals($fieldParams, $groupTree[$customGroupId]['fields'][$customFieldId], " in line " . __LINE__);
255
256 //cleanup DB by deleting customGroup
257 Custom::deleteField($customField);
258 Custom::deleteGroup($customGroup);
259 }
260
261 /**
262 * Test getTitle() with Invalid Params()
263 */
264 public function testGetTitleWithInvalidParams() {
265 $params = 99;
266 $customGroupTitle = CRM_Core_BAO_CustomGroup::getTitle($params);
267
268 $this->assertNull($customGroupTitle, 'Check that no custom Group Title is retreived');
269 }
270
271 /**
272 * Test getTitle()
273 */
274 public function testGetTitle() {
275 $customGrouptitle = 'My Custom Group';
276 $groupParams = array(
277 'title' => $customGrouptitle,
278 'name' => 'my_custom_group',
279 'style' => 'Tab',
280 'extends' => 'Individual',
281 'is_active' => 0,
282 'version' => 3,
283 );
284
285 $customGroup = Custom::createGroup($groupParams);
286 $customGroupId = $customGroup->id;
287
288 //get the custom group title
289 $title = CRM_Core_BAO_CustomGroup::getTitle($customGroupId);
290
291 //check for object update
292 $this->assertEquals($customGrouptitle, $title);
293
294 //cleanup DB by deleting customGroup
295 Custom::deleteGroup($customGroup);
296 }
297
298 /**
299 * Test deleteGroup()
300 */
301 public function testDeleteGroup() {
302 $customGrouptitle = 'My Custom Group';
303 $groupParams = array(
304 'title' => $customGrouptitle,
305 'name' => 'my_custom_group',
306 'style' => 'Tab',
307 'extends' => 'Individual',
308 'is_active' => 1,
309 'version' => 3,
310 );
311
312 $customGroup = Custom::createGroup($groupParams);
313
314 $customGroupId = $customGroup->id;
315
316 //get the custom group title
317 $dbCustomGroupTitle = $this->assertDBNotNull('CRM_Core_DAO_CustomGroup', $customGroupId, 'title', 'id',
318 'Database check for custom group record.'
319 );
320 //check for group title
321 $this->assertEquals($customGrouptitle, $dbCustomGroupTitle);
322
323 //delete the group
324 $isDelete = CRM_Core_BAO_CustomGroup::deleteGroup($customGroup);
325
326 //check for delete
327 $this->assertEquals(TRUE, $isDelete);
328
329 //check the DB
330 $this->assertDBNull('CRM_Core_DAO_CustomGroup', $customGroupId, 'title', 'id',
331 'Database check for custom group record.'
332 );
333 }
334
335 /**
336 * Test createTable()
337 */
338 public function testCreateTable() {
339 $customGrouptitle = 'My Custom Group';
340 $groupParams = array(
341 'title' => $customGrouptitle,
342 'name' => 'my_custom_group',
343 'style' => 'Tab',
344 'extends' => 'Individual',
345 'is_active' => 1,
346 'version' => 3,
347 );
348
349 $customGroupBAO = new CRM_Core_BAO_CustomGroup();
350 $customGroupBAO->copyValues($groupParams);
351 $customGroup = $customGroupBAO->save();
352 $tableName = 'civicrm_value_test_group_' . $customGroup->id;
353 $customGroup->table_name = $tableName;
354 $customGroup = $customGroupBAO->save();
355 $customTable = CRM_Core_BAO_CustomGroup::createTable($customGroup);
356 $customGroupId = $customGroup->id;
357
358 //check db for custom group.
359 $dbCustomGroupTitle = $this->assertDBNotNull('CRM_Core_DAO_CustomGroup', $customGroupId, 'title', 'id',
360 'Database check for custom group record.'
361 );
362 //check for custom group table name
363 $this->assertDBCompareValue('CRM_Core_DAO_CustomGroup', $customGroupId, 'table_name', 'id',
364 $tableName, 'Database check for custom group table name.'
365 );
366
367 //check for group title
368 $this->assertEquals($customGrouptitle, $dbCustomGroupTitle);
369
370 //cleanup DB by deleting customGroup
371 Custom::deleteGroup($customGroup);
372 }
373
374 /**
375 * Test checkCustomField()
376 */
377 public function testCheckCustomField() {
378 $customGroupTitle = 'My Custom Group';
379 $groupParams = array(
380 'title' => $customGroupTitle,
381 'name' => 'my_custom_group',
382 'extends' => 'Individual',
383 'help_pre' => 'Custom Group Help Pre',
384 'help_post' => 'Custom Group Help Post',
385 'is_active' => 1,
386 'collapse_display' => 1,
387 'version' => 3,
388 );
389
390 $customGroup = Custom::createGroup($groupParams);
391 $this->assertNotNull($customGroup->id, 'pre-requisite group not created successfully');
392 $customGroupId = $customGroup->id;
393
394 $customFieldLabel = 'Test Custom Field';
395 $fieldParams = array(
396 'custom_group_id' => $customGroupId,
397 'label' => $customFieldLabel,
398 'html_type' => 'Text',
399 'data_type' => 'String',
400 'is_required' => 1,
401 'is_searchable' => 0,
402 'is_active' => 1,
403 'version' => 3,
404 );
405
406 $customField = Custom::createField($fieldParams);
407 $this->assertNotNull($customField->id, 'pre-requisite field not created successfully');
408
409 $customFieldId = $customField->id;
410
411 //check db for custom group
412 $dbCustomGroupTitle = $this->assertDBNotNull('CRM_Core_DAO_CustomGroup', $customGroupId, 'title', 'id',
413 'Database check for custom group record.'
414 );
415 $this->assertEquals($customGroupTitle, $dbCustomGroupTitle);
416
417 //check db for custom field
418 $dbCustomFieldLabel = $this->assertDBNotNull('CRM_Core_DAO_CustomField', $customFieldId, 'label', 'id',
419 'Database check for custom field record.'
420 );
421 $this->assertEquals($customFieldLabel, $dbCustomFieldLabel);
422
423 //check the custom field type.
424 $params = array('Individual');
425 $usedFor = CRM_Core_BAO_CustomGroup::checkCustomField($customFieldId, $params);
426 $this->assertEquals(FALSE, $usedFor);
427
428 $params = array('Contribution', 'Membership', 'Participant');
429 $usedFor = CRM_Core_BAO_CustomGroup::checkCustomField($customFieldId, $params);
430 $this->assertEquals(TRUE, $usedFor);
431
432 //cleanup DB by deleting customGroup
433 Custom::deleteField($customField);
434 Custom::deleteGroup($customGroup);
435 }
436
437 /**
438 * Test getActiveGroups() with Invalid Params()
439 */
440 public function testGetActiveGroupsWithInvalidParams() {
441 $contactId = Contact::createIndividual();
442 $activeGroups = CRM_Core_BAO_CustomGroup::getActiveGroups('ABC', 'civicrm/contact/view/cd', $contactId);
443 $this->assertEquals(empty($activeGroups), TRUE, 'Check that Emprt params are retreived');
444 }
445
446 public function testGetActiveGroups() {
447 $contactId = Contact::createIndividual();
448 $customGrouptitle = 'Test Custom Group';
449 $groupParams = array(
450 'title' => $customGrouptitle,
451 'name' => 'test_custom_group',
452 'style' => 'Tab',
453 'extends' => 'Individual',
454 'weight' => 10,
455 'is_active' => 1,
456 'version' => 3,
457 );
458
459 $customGroup = Custom::createGroup($groupParams);
460 $activeGroup = CRM_Core_BAO_CustomGroup::getActiveGroups('Individual', 'civicrm/contact/view/cd', $contactId);
461 foreach ($activeGroup as $key => $value) {
462 if ($value['id'] == $customGroup->id) {
463 $this->assertEquals($value['path'], 'civicrm/contact/view/cd');
464 $this->assertEquals($value['title'], $customGrouptitle);
465 $query = 'reset=1&gid=' . $customGroup->id . '&cid=' . $contactId;
466 $this->assertEquals($value['query'], $query);
467 }
468 }
469
470 Custom::deleteGroup($customGroup);
471 Contact::delete($contactId);
472 }
473
474 /**
475 * Test create()
476 */
477 public function testCreate() {
478 $params = array(
479 'title' => 'Test_Group_1',
480 'name' => 'test_group_1',
481 'extends' => array(0 => 'Individual', 1 => array()),
482 'weight' => 4,
483 'collapse_display' => 1,
484 'style' => 'Inline',
485 'help_pre' => 'This is Pre Help For Test Group 1',
486 'help_post' => 'This is Post Help For Test Group 1',
487 'is_active' => 1,
488 'version' => 3,
489 );
490 $customGroup = CRM_Core_BAO_CustomGroup::create($params);
491
492 $dbCustomGroupTitle = $this->assertDBNotNull('CRM_Core_DAO_CustomGroup', $customGroup->id, 'title', 'id',
493 'Database check for custom group record.'
494 );
495 $this->assertEquals($params['title'], $dbCustomGroupTitle);
496
497 $dbCustomGroupTableName = $this->assertDBNotNull('CRM_Core_DAO_CustomGroup', $customGroup->id, 'table_name', 'id',
498 'Database check for custom group record.'
499 );
500 $this->assertEquals(strtolower("civicrm_value_{$params['name']}_{$customGroup->id}"), $dbCustomGroupTableName,
501 "The table name should be suffixed with '_ID' unless specified.");
502
503 Custom::deleteGroup($customGroup);
504 }
505
506 /**
507 * Test create() given a table_name
508 */
509 public function testCreateTableName() {
510 $params = array(
511 'title' => 'Test_Group_2',
512 'name' => 'test_group_2',
513 'table_name' => 'test_otherTableName',
514 'extends' => array(0 => 'Individual', 1 => array()),
515 'weight' => 4,
516 'collapse_display' => 1,
517 'style' => 'Inline',
518 'help_pre' => 'This is Pre Help For Test Group 1',
519 'help_post' => 'This is Post Help For Test Group 1',
520 'is_active' => 1,
521 'version' => 3,
522 );
523 $customGroup = CRM_Core_BAO_CustomGroup::create($params);
524
525 $dbCustomGroupTitle = $this->assertDBNotNull('CRM_Core_DAO_CustomGroup', $customGroup->id, 'title', 'id',
526 'Database check for custom group record.'
527 );
528 $this->assertEquals($params['title'], $dbCustomGroupTitle);
529
530 $dbCustomGroupTableName = $this->assertDBNotNull('CRM_Core_DAO_CustomGroup', $customGroup->id, 'table_name', 'id',
531 'Database check for custom group record.'
532 );
533 $this->assertEquals($params['table_name'], $dbCustomGroupTableName);
534
535 Custom::deleteGroup($customGroup);
536 }
537
538 /**
539 * Test isGroupEmpty()
540 */
541 public function testIsGroupEmpty() {
542 $customGrouptitle = 'Test Custom Group';
543 $groupParams = array(
544 'title' => $customGrouptitle,
545 'name' => 'test_custom_group',
546 'style' => 'Tab',
547 'extends' => 'Individual',
548 'weight' => 10,
549 'is_active' => 1,
550 'version' => 3,
551 );
552
553 $customGroup = Custom::createGroup($groupParams);
554 $customGroupId = $customGroup->id;
555 $isEmptyGroup = CRM_Core_BAO_CustomGroup::isGroupEmpty($customGroupId);
556
557 $this->assertEquals($isEmptyGroup, TRUE, 'Check that custom Group is Empty.');
558 Custom::deleteGroup($customGroup);
559 }
560
561 /**
562 * Test getGroupTitles() with Invalid Params()
563 */
564 public function testgetGroupTitlesWithInvalidParams() {
565 $params = array(99);
566 $groupTitles = CRM_Core_BAO_CustomGroup::getGroupTitles($params);
567 $this->assertTrue(empty($groupTitles), 'Check that no titles are recieved');
568 }
569
570 /**
571 * Test getGroupTitles()
572 */
573 public function testgetGroupTitles() {
574 $customGrouptitle = 'Test Custom Group';
575 $groupParams = array(
576 'title' => $customGrouptitle,
577 'name' => 'test_custom_group',
578 'style' => 'Tab',
579 'extends' => 'Individual',
580 'weight' => 10,
581 'is_active' => 1,
582 'version' => 3,
583 );
584
585 $customGroup = Custom::createGroup($groupParams);
586
587 $customGroupId = $customGroup->id;
588
589 $customFieldLabel = 'Test Custom Field';
590 $fieldParams = array(
591 'custom_group_id' => $customGroupId,
592 'label' => $customFieldLabel,
593 'html_type' => 'Text',
594 'data_type' => 'String',
595 'is_required' => 1,
596 'is_searchable' => 0,
597 'is_active' => 1,
598 'version' => 3,
599 );
600
601 $customField = Custom::createField($fieldParams);
602 $customFieldId = $customField->id;
603
604 $params = array($customFieldId);
605
606 $groupTitles = CRM_Core_BAO_CustomGroup::getGroupTitles($params);
607
608 $this->assertEquals($groupTitles[$customFieldId]['groupTitle'], 'Test Custom Group', 'Check Group Title.');
609 Custom::deleteGroup($customGroup);
610 }
611
612 }