3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.7 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2016 |
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 * Class CRM_Core_BAO_CustomGroupTest
32 class CRM_Core_BAO_CustomGroupTest
extends CiviUnitTestCase
{
34 public function setUp() {
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']);
50 * Test calling getTree with contact subtype data.
52 * Note that the function seems to support a range of formats so 3 are tested. Yay for
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);
66 CRM_Core_BAO_CustomGroup
::getTree('Organization', NULL, NULL, NULL, array('Small Kind Bank'));
68 catch (CRM_Core_Exception
$e) {
69 $this->customGroupDelete($customGroup['id']);
72 $this->fail('There is no such thing as a small kind bank');
76 * Test calling getTree with contact subtype data.
78 * Note that the function seems to support a range of formats so 3 are tested. Yay for
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'
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']);
95 * Test calling getTree with contact subtype data.
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']);
106 * Test retrieve() with Empty Params.
108 public function testRetrieveEmptyParams() {
110 $customGroup = CRM_Core_BAO_CustomGroup
::retrieve($params, $dafaults);
111 $this->assertNull($customGroup, 'Check that no custom Group is retreived');
115 * Test retrieve() with Inalid Params
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');
126 public function testRetrieve() {
127 $customGrouptitle = 'My Custom Group';
128 $groupParams = array(
129 'title' => $customGrouptitle,
130 'name' => 'My_Custom_Group',
132 'extends' => 'Individual',
133 'help_pre' => 'Custom Group Help Pre',
134 'help_post' => 'Custom Group Help Post',
136 'collapse_display' => 1,
141 $customGroup = Custom
::createGroup($groupParams);
142 $customGroupId = $customGroup->id
;
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.'
150 $this->assertEquals($customGrouptitle, $dbCustomGroupTitle);
151 //check retieve values
152 unset($groupParams['version']);
153 $this->assertAttributesEquals($groupParams, $dafaults);
155 //cleanup DB by deleting customGroup
156 Custom
::deleteGroup($customGroup);
162 public function testSetIsActive() {
163 $customGrouptitle = 'My Custom Group';
164 $groupParams = array(
165 'title' => $customGrouptitle,
166 'name' => 'my_custom_group',
168 'extends' => 'Individual',
173 $customGroup = Custom
::createGroup($groupParams);
174 $customGroupId = $customGroup->id
;
177 $result = CRM_Core_BAO_CustomGroup
::setIsActive($customGroupId, TRUE);
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.'
185 //cleanup DB by deleting customGroup
186 Custom
::deleteGroup($customGroup);
190 * Test getGroupDetail() with Empty Params
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');
199 * Test getGroupDetail() with Inalid Params
201 public function testGetGroupDetailInvalidParams() {
203 $customGroup = CRM_Core_BAO_CustomGroup
::getGroupDetail($customGroupId);
204 $this->assertTrue(empty($customGroup), 'Check that no custom Group details is retreived');
208 * Test getGroupDetail()
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',
219 'collapse_display' => 1,
223 $customGroup = Custom
::createGroup($groupParams);
224 $customGroupId = $customGroup->id
;
226 $fieldParams = array(
227 'custom_group_id' => $customGroupId,
228 'label' => 'Test Custom Field',
229 'html_type' => 'Text',
230 'data_type' => 'String',
232 'is_searchable' => 0,
237 $customField = Custom
::createField($fieldParams);
238 $customFieldId = $customField->id
;
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.'
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]);
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__
);
256 //cleanup DB by deleting customGroup
257 Custom
::deleteField($customField);
258 Custom
::deleteGroup($customGroup);
262 * Test getTitle() with Invalid Params()
264 public function testGetTitleWithInvalidParams() {
266 $customGroupTitle = CRM_Core_BAO_CustomGroup
::getTitle($params);
268 $this->assertNull($customGroupTitle, 'Check that no custom Group Title is retreived');
274 public function testGetTitle() {
275 $customGrouptitle = 'My Custom Group';
276 $groupParams = array(
277 'title' => $customGrouptitle,
278 'name' => 'my_custom_group',
280 'extends' => 'Individual',
285 $customGroup = Custom
::createGroup($groupParams);
286 $customGroupId = $customGroup->id
;
288 //get the custom group title
289 $title = CRM_Core_BAO_CustomGroup
::getTitle($customGroupId);
291 //check for object update
292 $this->assertEquals($customGrouptitle, $title);
294 //cleanup DB by deleting customGroup
295 Custom
::deleteGroup($customGroup);
301 public function testDeleteGroup() {
302 $customGrouptitle = 'My Custom Group';
303 $groupParams = array(
304 'title' => $customGrouptitle,
305 'name' => 'my_custom_group',
307 'extends' => 'Individual',
312 $customGroup = Custom
::createGroup($groupParams);
314 $customGroupId = $customGroup->id
;
316 //get the custom group title
317 $dbCustomGroupTitle = $this->assertDBNotNull('CRM_Core_DAO_CustomGroup', $customGroupId, 'title', 'id',
318 'Database check for custom group record.'
320 //check for group title
321 $this->assertEquals($customGrouptitle, $dbCustomGroupTitle);
324 $isDelete = CRM_Core_BAO_CustomGroup
::deleteGroup($customGroup);
327 $this->assertEquals(TRUE, $isDelete);
330 $this->assertDBNull('CRM_Core_DAO_CustomGroup', $customGroupId, 'title', 'id',
331 'Database check for custom group record.'
338 public function testCreateTable() {
339 $customGrouptitle = 'My Custom Group';
340 $groupParams = array(
341 'title' => $customGrouptitle,
342 'name' => 'my_custom_group',
344 'extends' => 'Individual',
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
;
358 //check db for custom group.
359 $dbCustomGroupTitle = $this->assertDBNotNull('CRM_Core_DAO_CustomGroup', $customGroupId, 'title', 'id',
360 'Database check for custom group record.'
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.'
367 //check for group title
368 $this->assertEquals($customGrouptitle, $dbCustomGroupTitle);
370 //cleanup DB by deleting customGroup
371 Custom
::deleteGroup($customGroup);
375 * Test checkCustomField()
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',
386 'collapse_display' => 1,
390 $customGroup = Custom
::createGroup($groupParams);
391 $this->assertNotNull($customGroup->id
, 'pre-requisite group not created successfully');
392 $customGroupId = $customGroup->id
;
394 $customFieldLabel = 'Test Custom Field';
395 $fieldParams = array(
396 'custom_group_id' => $customGroupId,
397 'label' => $customFieldLabel,
398 'html_type' => 'Text',
399 'data_type' => 'String',
401 'is_searchable' => 0,
406 $customField = Custom
::createField($fieldParams);
407 $this->assertNotNull($customField->id
, 'pre-requisite field not created successfully');
409 $customFieldId = $customField->id
;
411 //check db for custom group
412 $dbCustomGroupTitle = $this->assertDBNotNull('CRM_Core_DAO_CustomGroup', $customGroupId, 'title', 'id',
413 'Database check for custom group record.'
415 $this->assertEquals($customGroupTitle, $dbCustomGroupTitle);
417 //check db for custom field
418 $dbCustomFieldLabel = $this->assertDBNotNull('CRM_Core_DAO_CustomField', $customFieldId, 'label', 'id',
419 'Database check for custom field record.'
421 $this->assertEquals($customFieldLabel, $dbCustomFieldLabel);
423 //check the custom field type.
424 $params = array('Individual');
425 $usedFor = CRM_Core_BAO_CustomGroup
::checkCustomField($customFieldId, $params);
426 $this->assertEquals(FALSE, $usedFor);
428 $params = array('Contribution', 'Membership', 'Participant');
429 $usedFor = CRM_Core_BAO_CustomGroup
::checkCustomField($customFieldId, $params);
430 $this->assertEquals(TRUE, $usedFor);
432 //cleanup DB by deleting customGroup
433 Custom
::deleteField($customField);
434 Custom
::deleteGroup($customGroup);
438 * Test getActiveGroups() with Invalid Params()
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');
446 public function testGetActiveGroups() {
447 $contactId = Contact
::createIndividual();
448 $customGrouptitle = 'Test Custom Group';
449 $groupParams = array(
450 'title' => $customGrouptitle,
451 'name' => 'test_custom_group',
453 'extends' => 'Individual',
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);
470 Custom
::deleteGroup($customGroup);
471 Contact
::delete($contactId);
477 public function testCreate() {
479 'title' => 'Test_Group_1',
480 'name' => 'test_group_1',
481 'extends' => array(0 => 'Individual', 1 => array()),
483 'collapse_display' => 1,
485 'help_pre' => 'This is Pre Help For Test Group 1',
486 'help_post' => 'This is Post Help For Test Group 1',
490 $customGroup = CRM_Core_BAO_CustomGroup
::create($params);
492 $dbCustomGroupTitle = $this->assertDBNotNull('CRM_Core_DAO_CustomGroup', $customGroup->id
, 'title', 'id',
493 'Database check for custom group record.'
495 $this->assertEquals($params['title'], $dbCustomGroupTitle);
497 $dbCustomGroupTableName = $this->assertDBNotNull('CRM_Core_DAO_CustomGroup', $customGroup->id
, 'table_name', 'id',
498 'Database check for custom group record.'
500 $this->assertEquals(strtolower("civicrm_value_{$params['name']}_{$customGroup->id}"), $dbCustomGroupTableName,
501 "The table name should be suffixed with '_ID' unless specified.");
503 Custom
::deleteGroup($customGroup);
507 * Test create() given a table_name
509 public function testCreateTableName() {
511 'title' => 'Test_Group_2',
512 'name' => 'test_group_2',
513 'table_name' => 'test_otherTableName',
514 'extends' => array(0 => 'Individual', 1 => array()),
516 'collapse_display' => 1,
518 'help_pre' => 'This is Pre Help For Test Group 1',
519 'help_post' => 'This is Post Help For Test Group 1',
523 $customGroup = CRM_Core_BAO_CustomGroup
::create($params);
525 $dbCustomGroupTitle = $this->assertDBNotNull('CRM_Core_DAO_CustomGroup', $customGroup->id
, 'title', 'id',
526 'Database check for custom group record.'
528 $this->assertEquals($params['title'], $dbCustomGroupTitle);
530 $dbCustomGroupTableName = $this->assertDBNotNull('CRM_Core_DAO_CustomGroup', $customGroup->id
, 'table_name', 'id',
531 'Database check for custom group record.'
533 $this->assertEquals($params['table_name'], $dbCustomGroupTableName);
535 Custom
::deleteGroup($customGroup);
539 * Test isGroupEmpty()
541 public function testIsGroupEmpty() {
542 $customGrouptitle = 'Test Custom Group';
543 $groupParams = array(
544 'title' => $customGrouptitle,
545 'name' => 'test_custom_group',
547 'extends' => 'Individual',
553 $customGroup = Custom
::createGroup($groupParams);
554 $customGroupId = $customGroup->id
;
555 $isEmptyGroup = CRM_Core_BAO_CustomGroup
::isGroupEmpty($customGroupId);
557 $this->assertEquals($isEmptyGroup, TRUE, 'Check that custom Group is Empty.');
558 Custom
::deleteGroup($customGroup);
562 * Test getGroupTitles() with Invalid Params()
564 public function testgetGroupTitlesWithInvalidParams() {
566 $groupTitles = CRM_Core_BAO_CustomGroup
::getGroupTitles($params);
567 $this->assertTrue(empty($groupTitles), 'Check that no titles are recieved');
571 * Test getGroupTitles()
573 public function testgetGroupTitles() {
574 $customGrouptitle = 'Test Custom Group';
575 $groupParams = array(
576 'title' => $customGrouptitle,
577 'name' => 'test_custom_group',
579 'extends' => 'Individual',
585 $customGroup = Custom
::createGroup($groupParams);
587 $customGroupId = $customGroup->id
;
589 $customFieldLabel = 'Test Custom Field';
590 $fieldParams = array(
591 'custom_group_id' => $customGroupId,
592 'label' => $customFieldLabel,
593 'html_type' => 'Text',
594 'data_type' => 'String',
596 'is_searchable' => 0,
601 $customField = Custom
::createField($fieldParams);
602 $customFieldId = $customField->id
;
604 $params = array($customFieldId);
606 $groupTitles = CRM_Core_BAO_CustomGroup
::getGroupTitles($params);
608 $this->assertEquals($groupTitles[$customFieldId]['groupTitle'], 'Test Custom Group', 'Check Group Title.');
609 Custom
::deleteGroup($customGroup);