3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.7 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2015 |
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
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. |
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. |
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 +--------------------------------------------------------------------+
29 require_once 'CiviTest/CiviUnitTestCase.php';
30 require_once 'CiviTest/Contact.php';
31 require_once 'CiviTest/Custom.php';
34 * Class CRM_Core_BAO_CustomGroupTest
36 class CRM_Core_BAO_CustomGroupTest
extends CiviUnitTestCase
{
38 public function setUp() {
45 public function testGetTree() {
47 $contactId = Contact
::createIndividual();
48 $customGrouptitle = 'My Custom Group';
50 'title' => $customGrouptitle,
51 'name' => 'my_custom_group',
53 'extends' => 'Individual',
58 $customGroup = Custom
::createGroup($groupParams);
60 $customGroupId = $customGroup->id
;
63 'groupId' => $customGroupId,
64 'dataType' => 'String',
68 $customField = Custom
::createField($params, $fields);
70 $getTree = CRM_Core_BAO_CustomGroup
::getTree('Individual', $formParams, $customGroupId);
72 $dbCustomGroupTitle = $this->assertDBNotNull('CRM_Core_DAO_CustomGroup', $customGroupId, 'title', 'id',
73 'Database check for custom group record.'
76 Custom
::deleteField($customField);
77 Custom
::deleteGroup($customGroup);
78 Contact
::delete($contactId);
83 * Test retrieve() with Empty Params
85 public function testRetrieveEmptyParams() {
87 $customGroup = CRM_Core_BAO_CustomGroup
::retrieve($params, $dafaults);
88 $this->assertNull($customGroup, 'Check that no custom Group is retreived');
92 * Test retrieve() with Inalid Params
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');
103 public function testRetrieve() {
104 $customGrouptitle = 'My Custom Group';
105 $groupParams = array(
106 'title' => $customGrouptitle,
107 'name' => 'My_Custom_Group',
109 'extends' => 'Individual',
110 'help_pre' => 'Custom Group Help Pre',
111 'help_post' => 'Custom Group Help Post',
113 'collapse_display' => 1,
118 $customGroup = Custom
::createGroup($groupParams);
119 $customGroupId = $customGroup->id
;
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.'
127 $this->assertEquals($customGrouptitle, $dbCustomGroupTitle);
128 //check retieve values
129 unset($groupParams['version']);
130 $this->assertAttributesEquals($groupParams, $dafaults);
132 //cleanup DB by deleting customGroup
133 Custom
::deleteGroup($customGroup);
139 public function testSetIsActive() {
140 $customGrouptitle = 'My Custom Group';
141 $groupParams = array(
142 'title' => $customGrouptitle,
143 'name' => 'my_custom_group',
145 'extends' => 'Individual',
150 $customGroup = Custom
::createGroup($groupParams);
151 $customGroupId = $customGroup->id
;
154 $result = CRM_Core_BAO_CustomGroup
::setIsActive($customGroupId, TRUE);
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.'
162 //cleanup DB by deleting customGroup
163 Custom
::deleteGroup($customGroup);
167 * Test getGroupDetail() with Empty Params
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');
176 * Test getGroupDetail() with Inalid Params
178 public function testGetGroupDetailInvalidParams() {
180 $customGroup = CRM_Core_BAO_CustomGroup
::getGroupDetail($customGroupId);
181 $this->assertTrue(empty($customGroup), 'Check that no custom Group details is retreived');
185 * Test getGroupDetail()
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',
196 'collapse_display' => 1,
200 $customGroup = Custom
::createGroup($groupParams);
201 $customGroupId = $customGroup->id
;
203 $fieldParams = array(
204 'custom_group_id' => $customGroupId,
205 'label' => 'Test Custom Field',
206 'html_type' => 'Text',
207 'data_type' => 'String',
209 'is_searchable' => 0,
214 $customField = Custom
::createField($fieldParams);
215 $customFieldId = $customField->id
;
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.'
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]);
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__
);
233 //cleanup DB by deleting customGroup
234 Custom
::deleteField($customField);
235 Custom
::deleteGroup($customGroup);
239 * Test getTitle() with Invalid Params()
241 public function testGetTitleWithInvalidParams() {
243 $customGroupTitle = CRM_Core_BAO_CustomGroup
::getTitle($params);
245 $this->assertNull($customGroupTitle, 'Check that no custom Group Title is retreived');
251 public function testGetTitle() {
252 $customGrouptitle = 'My Custom Group';
253 $groupParams = array(
254 'title' => $customGrouptitle,
255 'name' => 'my_custom_group',
257 'extends' => 'Individual',
262 $customGroup = Custom
::createGroup($groupParams);
263 $customGroupId = $customGroup->id
;
265 //get the custom group title
266 $title = CRM_Core_BAO_CustomGroup
::getTitle($customGroupId);
268 //check for object update
269 $this->assertEquals($customGrouptitle, $title);
271 //cleanup DB by deleting customGroup
272 Custom
::deleteGroup($customGroup);
278 public function testDeleteGroup() {
279 $customGrouptitle = 'My Custom Group';
280 $groupParams = array(
281 'title' => $customGrouptitle,
282 'name' => 'my_custom_group',
284 'extends' => 'Individual',
289 $customGroup = Custom
::createGroup($groupParams);
291 $customGroupId = $customGroup->id
;
293 //get the custom group title
294 $dbCustomGroupTitle = $this->assertDBNotNull('CRM_Core_DAO_CustomGroup', $customGroupId, 'title', 'id',
295 'Database check for custom group record.'
297 //check for group title
298 $this->assertEquals($customGrouptitle, $dbCustomGroupTitle);
301 $isDelete = CRM_Core_BAO_CustomGroup
::deleteGroup($customGroup);
304 $this->assertEquals(TRUE, $isDelete);
307 $this->assertDBNull('CRM_Core_DAO_CustomGroup', $customGroupId, 'title', 'id',
308 'Database check for custom group record.'
315 public function testCreateTable() {
316 $customGrouptitle = 'My Custom Group';
317 $groupParams = array(
318 'title' => $customGrouptitle,
319 'name' => 'my_custom_group',
321 'extends' => 'Individual',
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
;
335 //check db for custom group.
336 $dbCustomGroupTitle = $this->assertDBNotNull('CRM_Core_DAO_CustomGroup', $customGroupId, 'title', 'id',
337 'Database check for custom group record.'
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.'
344 //check for group title
345 $this->assertEquals($customGrouptitle, $dbCustomGroupTitle);
347 //cleanup DB by deleting customGroup
348 Custom
::deleteGroup($customGroup);
352 * Test checkCustomField()
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',
363 'collapse_display' => 1,
367 $customGroup = Custom
::createGroup($groupParams);
368 $this->assertNotNull($customGroup->id
, 'pre-requisite group not created successfully');
369 $customGroupId = $customGroup->id
;
371 $customFieldLabel = 'Test Custom Field';
372 $fieldParams = array(
373 'custom_group_id' => $customGroupId,
374 'label' => $customFieldLabel,
375 'html_type' => 'Text',
376 'data_type' => 'String',
378 'is_searchable' => 0,
383 $customField = Custom
::createField($fieldParams);
384 $this->assertNotNull($customField->id
, 'pre-requisite field not created successfully');
386 $customFieldId = $customField->id
;
388 //check db for custom group
389 $dbCustomGroupTitle = $this->assertDBNotNull('CRM_Core_DAO_CustomGroup', $customGroupId, 'title', 'id',
390 'Database check for custom group record.'
392 $this->assertEquals($customGroupTitle, $dbCustomGroupTitle);
394 //check db for custom field
395 $dbCustomFieldLabel = $this->assertDBNotNull('CRM_Core_DAO_CustomField', $customFieldId, 'label', 'id',
396 'Database check for custom field record.'
398 $this->assertEquals($customFieldLabel, $dbCustomFieldLabel);
400 //check the custom field type.
401 $params = array('Individual');
402 $usedFor = CRM_Core_BAO_CustomGroup
::checkCustomField($customFieldId, $params);
403 $this->assertEquals(FALSE, $usedFor);
405 $params = array('Contribution', 'Membership', 'Participant');
406 $usedFor = CRM_Core_BAO_CustomGroup
::checkCustomField($customFieldId, $params);
407 $this->assertEquals(TRUE, $usedFor);
409 //cleanup DB by deleting customGroup
410 Custom
::deleteField($customField);
411 Custom
::deleteGroup($customGroup);
415 * Test getActiveGroups() with Invalid Params()
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');
423 public function testGetActiveGroups() {
424 $contactId = Contact
::createIndividual();
425 $customGrouptitle = 'Test Custom Group';
426 $groupParams = array(
427 'title' => $customGrouptitle,
428 'name' => 'test_custom_group',
430 'extends' => 'Individual',
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);
447 Custom
::deleteGroup($customGroup);
448 Contact
::delete($contactId);
454 public function testCreate() {
456 'title' => 'Test_Group_1',
457 'name' => 'test_group_1',
458 'extends' => array(0 => 'Individual', 1 => array()),
460 'collapse_display' => 1,
462 'help_pre' => 'This is Pre Help For Test Group 1',
463 'help_post' => 'This is Post Help For Test Group 1',
467 $customGroup = CRM_Core_BAO_CustomGroup
::create($params);
469 $dbCustomGroupTitle = $this->assertDBNotNull('CRM_Core_DAO_CustomGroup', $customGroup->id
, 'title', 'id',
470 'Database check for custom group record.'
472 $this->assertEquals($params['title'], $dbCustomGroupTitle);
474 $dbCustomGroupTableName = $this->assertDBNotNull('CRM_Core_DAO_CustomGroup', $customGroup->id
, 'table_name', 'id',
475 'Database check for custom group record.'
477 $this->assertEquals(strtolower("civicrm_value_{$params['name']}_{$customGroup->id}"), $dbCustomGroupTableName,
478 "The table name should be suffixed with '_ID' unless specified.");
480 Custom
::deleteGroup($customGroup);
484 * Test create() given a table_name
486 public function testCreateTableName() {
488 'title' => 'Test_Group_2',
489 'name' => 'test_group_2',
490 'table_name' => 'test_otherTableName',
491 'extends' => array(0 => 'Individual', 1 => array()),
493 'collapse_display' => 1,
495 'help_pre' => 'This is Pre Help For Test Group 1',
496 'help_post' => 'This is Post Help For Test Group 1',
500 $customGroup = CRM_Core_BAO_CustomGroup
::create($params);
502 $dbCustomGroupTitle = $this->assertDBNotNull('CRM_Core_DAO_CustomGroup', $customGroup->id
, 'title', 'id',
503 'Database check for custom group record.'
505 $this->assertEquals($params['title'], $dbCustomGroupTitle);
507 $dbCustomGroupTableName = $this->assertDBNotNull('CRM_Core_DAO_CustomGroup', $customGroup->id
, 'table_name', 'id',
508 'Database check for custom group record.'
510 $this->assertEquals($params['table_name'], $dbCustomGroupTableName);
512 Custom
::deleteGroup($customGroup);
516 * Test isGroupEmpty()
518 public function testIsGroupEmpty() {
519 $customGrouptitle = 'Test Custom Group';
520 $groupParams = array(
521 'title' => $customGrouptitle,
522 'name' => 'test_custom_group',
524 'extends' => 'Individual',
530 $customGroup = Custom
::createGroup($groupParams);
531 $customGroupId = $customGroup->id
;
532 $isEmptyGroup = CRM_Core_BAO_CustomGroup
::isGroupEmpty($customGroupId);
534 $this->assertEquals($isEmptyGroup, TRUE, 'Check that custom Group is Empty.');
535 Custom
::deleteGroup($customGroup);
539 * Test getGroupTitles() with Invalid Params()
541 public function testgetGroupTitlesWithInvalidParams() {
543 $groupTitles = CRM_Core_BAO_CustomGroup
::getGroupTitles($params);
544 $this->assertTrue(empty($groupTitles), 'Check that no titles are recieved');
548 * Test getGroupTitles()
550 public function testgetGroupTitles() {
551 $customGrouptitle = 'Test Custom Group';
552 $groupParams = array(
553 'title' => $customGrouptitle,
554 'name' => 'test_custom_group',
556 'extends' => 'Individual',
562 $customGroup = Custom
::createGroup($groupParams);
564 $customGroupId = $customGroup->id
;
566 $customFieldLabel = 'Test Custom Field';
567 $fieldParams = array(
568 'custom_group_id' => $customGroupId,
569 'label' => $customFieldLabel,
570 'html_type' => 'Text',
571 'data_type' => 'String',
573 'is_searchable' => 0,
578 $customField = Custom
::createField($fieldParams);
579 $customFieldId = $customField->id
;
581 $params = array($customFieldId);
583 $groupTitles = CRM_Core_BAO_CustomGroup
::getGroupTitles($params);
585 $this->assertEquals($groupTitles[$customFieldId]['groupTitle'], 'Test Custom Group', 'Check Group Title.');
586 Custom
::deleteGroup($customGroup);