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