Commit | Line | Data |
---|---|---|
6a488035 TO |
1 | <?php |
2 | /* | |
3 | +--------------------------------------------------------------------+ | |
81621fee | 4 | | CiviCRM version 4.7 | |
6a488035 | 5 | +--------------------------------------------------------------------+ |
15a4309a | 6 | | Copyright CiviCRM LLC (c) 2004-2017 | |
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 | 32 | class 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 E |
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 | } | |
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() { | |
4dcd2787 | 56 | $contactType = $this->callAPISuccess('ContactType', 'create', array('name' => 'Big Bank', 'label' => 'biggee', 'parent_id' => 'Organization')); |
325fa965 | 57 | $customGroup = $this->CustomGroupCreate(array('extends' => 'Organization', 'extends_entity_column_value' => array('Big_Bank'))); |
27dd6252 | 58 | $customField = $this->customFieldCreate(array('custom_group_id' => $customGroup['id'])); |
325fa965 | 59 | $result1 = CRM_Core_BAO_CustomGroup::getTree('Organization', NULL, NULL, NULL, array('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 { | |
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']); | |
4dcd2787 | 70 | $this->callAPISuccess('ContactType', 'delete', array('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() { | |
80 | $contactType = $this->callAPISuccess('ContactType', 'create', array('name' => 'Big Bank', 'label' => 'biggee', 'parent_id' => 'Organization')); | |
81 | CRM_Core_DAO::executeQuery('UPDATE civicrm_contact_type SET label = "boo" WHERE name = "Organization"'); | |
82 | $customGroup = $this->CustomGroupCreate(array('extends' => 'Organization', 'extends_entity_column_value' => array('Big_Bank'))); | |
83 | $customField = $this->customFieldCreate(array('custom_group_id' => $customGroup['id'])); | |
84 | $result1 = CRM_Core_BAO_CustomGroup::getTree('Organization', NULL, NULL, NULL, array('Big_Bank')); | |
85 | $this->assertEquals('Custom Field', $result1[$customGroup['id']]['fields'][$customField['id']]['label']); | |
86 | $this->customGroupDelete($customGroup['id']); | |
87 | $this->callAPISuccess('ContactType', 'delete', array('id' => $contactType['id'])); | |
88 | } | |
89 | ||
93ecb8e8 | 90 | /** |
91 | * Test calling getTree for a custom field extending a disabled contact type. | |
92 | */ | |
93 | public function testGetTreeContactSubTypeForDisabledChangedContactType() { | |
94 | $contactType = $this->callAPISuccess('ContactType', 'create', array('name' => 'Big Bank', 'label' => 'biggee', 'parent_id' => 'Organization')); | |
95 | $customGroup = $this->CustomGroupCreate(array('extends' => 'Organization', 'extends_entity_column_value' => array('Big_Bank'))); | |
96 | $customField = $this->customFieldCreate(array('custom_group_id' => $customGroup['id'])); | |
97 | $this->callAPISuccess('ContactType', 'create', array('id' => $contactType['id'], 'is_active' => 0)); | |
98 | $result1 = CRM_Core_BAO_CustomGroup::getTree('Organization', NULL, NULL, NULL, array('Big_Bank')); | |
99 | $this->assertEquals('Custom Field', $result1[$customGroup['id']]['fields'][$customField['id']]['label']); | |
100 | $this->customGroupDelete($customGroup['id']); | |
101 | $this->callAPISuccess('ContactType', 'delete', array('id' => $contactType['id'])); | |
102 | } | |
103 | ||
9364f696 SL |
104 | /** |
105 | * Test calling GetTree for a custom field extending multiple subTypes. | |
106 | */ | |
66b231c2 | 107 | public function testGetTreetContactSubTypeForMultipleSubTypes() { |
9364f696 | 108 | $contactType1 = $this->callAPISuccess('ContactType', 'create', array('name' => 'Big Bank', 'label' => 'biggee', 'parent_id' => 'Organization')); |
093974e2 | 109 | $contactType2 = $this->callAPISuccess('ContactType', 'create', array('name' => 'Small Bank', 'label' => 'smallee', 'parent_id' => 'Organization')); |
66b231c2 SL |
110 | $customGroup = $this->CustomGroupCreate(array('extends' => 'Organization', 'extends_entity_column_value' => array('Big_Bank', 'Small_Bank'))); |
111 | $customField = $this->customFieldCreate(array('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']); | |
093974e2 SL |
115 | $this->callAPISuccess('ContactType', 'delete', array('id' => $contactType1['id'])); |
116 | $this->callAPISuccess('ContactType', 'delete', array('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() { | |
123 | $eventType = $this->callAPISuccess('OptionValue', 'Create', array('option_group_id' => 'event_type', 'value' => 'meeting', 'label' => 'Meeting')); | |
2072f97d | 124 | $customGroup = $this->CustomGroupCreate(array('extends' => 'Event', 'extends_entity_column_value' => array('Meeting'))); |
9364f696 SL |
125 | $customField = $this->customFieldCreate(array('custom_group_id' => $customGroup['id'])); |
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']); | |
129 | $this->callAPISuccess('OptionValue', 'delete', array('id' => $eventType['id'])); | |
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(); | |
142 | $customGroup = $this->CustomGroupCreate(array( | |
143 | 'extends' => 'Campaign', | |
05e3bddf | 144 | 'extends_entity_column_value' => "{$sep}1{$sep}2{$sep}", |
35e83f39 | 145 | )); |
146 | $customField = $this->customFieldCreate(array('custom_group_id' => $customGroup['id'])); | |
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() { | |
156 | $customGroup = $this->CustomGroupCreate(array('extends' => 'Activity', 'extends_entity_column_value' => 1)); | |
157 | $customField = $this->customFieldCreate(array('custom_group_id' => $customGroup['id'])); | |
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() { |
6a488035 TO |
167 | $params = array(); |
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() { |
6a488035 TO |
176 | $params = array('id' => 99); |
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'; |
6a488035 | 186 | $groupParams = array( |
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, | |
6a488035 TO |
196 | ); |
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'; |
6a488035 | 208 | $groupParams = array( |
5c988f8a | 209 | 'title' => $customGroupTitle, |
6a488035 TO |
210 | 'name' => 'my_custom_group', |
211 | 'style' => 'Tab', | |
212 | 'extends' => 'Individual', | |
213 | 'is_active' => 0, | |
6a488035 TO |
214 | ); |
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() { |
6a488035 TO |
236 | $customGroupId = array(); |
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'; |
6a488035 | 255 | $groupParams = array( |
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, | |
6a488035 TO |
263 | ); |
264 | ||
5c988f8a | 265 | $customGroup = $this->customGroupCreate($groupParams); |
266 | $customGroupId = $customGroup['id']; | |
6a488035 TO |
267 | |
268 | $fieldParams = array( | |
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, | |
6a488035 TO |
276 | ); |
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'; |
6a488035 | 316 | $groupParams = array( |
5c988f8a | 317 | 'title' => $customGroupTitle, |
6a488035 TO |
318 | 'name' => 'my_custom_group', |
319 | 'style' => 'Tab', | |
320 | 'extends' => 'Individual', | |
321 | 'is_active' => 0, | |
6a488035 TO |
322 | ); |
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'; |
6a488035 | 341 | $groupParams = array( |
5c988f8a | 342 | 'title' => $customGroupTitle, |
6a488035 TO |
343 | 'name' => 'my_custom_group', |
344 | 'style' => 'Tab', | |
345 | 'extends' => 'Individual', | |
346 | 'is_active' => 1, | |
6a488035 TO |
347 | ); |
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() { |
6a488035 | 367 | $groupParams = array( |
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, | |
374 | ); | |
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() { |
6a488035 | 401 | $groupParams = array( |
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, | |
6a488035 TO |
409 | ); |
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'; | |
416 | $fieldParams = array( | |
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, | |
6a488035 TO |
424 | ); |
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. | |
438 | $params = array('Individual'); | |
439 | $usedFor = CRM_Core_BAO_CustomGroup::checkCustomField($customFieldId, $params); | |
440 | $this->assertEquals(FALSE, $usedFor); | |
441 | ||
442 | $params = array('Contribution', 'Membership', 'Participant'); | |
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'; |
92915c55 | 462 | $groupParams = array( |
5c988f8a | 463 | 'title' => $customGroupTitle, |
6a488035 TO |
464 | 'name' => 'test_custom_group', |
465 | 'style' => 'Tab', | |
466 | 'extends' => 'Individual', | |
467 | 'weight' => 10, | |
468 | 'is_active' => 1, | |
6a488035 TO |
469 | ); |
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() { |
6a488035 TO |
490 | $params = array( |
491 | 'title' => 'Test_Group_1', | |
492 | 'name' => 'test_group_1', | |
493 | 'extends' => array(0 => 'Individual', 1 => array()), | |
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, | |
501 | ); | |
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() { |
8b3b9a2e ARW |
522 | $params = array( |
523 | 'title' => 'Test_Group_2', | |
524 | 'name' => 'test_group_2', | |
525 | 'table_name' => 'test_otherTableName', | |
526 | 'extends' => array(0 => 'Individual', 1 => array()), | |
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, | |
8b3b9a2e ARW |
533 | ); |
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'; |
6a488035 | 554 | $groupParams = array( |
5c988f8a | 555 | 'title' => $customGroupTitle, |
6a488035 TO |
556 | 'name' => 'test_custom_group', |
557 | 'style' => 'Tab', | |
558 | 'extends' => 'Individual', | |
559 | 'weight' => 10, | |
560 | 'is_active' => 1, | |
6a488035 TO |
561 | ); |
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() { |
6a488035 TO |
575 | $params = array(99); |
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() { |
6a488035 | 584 | $groupParams = array( |
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, | |
6a488035 TO |
591 | ); |
592 | ||
5c988f8a | 593 | $customGroup = $this->customGroupCreate($groupParams); |
6a488035 | 594 | |
6a488035 | 595 | $fieldParams = array( |
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'], |
6a488035 TO |
603 | ); |
604 | ||
5c988f8a | 605 | $customField = $this->customFieldCreate($fieldParams); |
606 | $customFieldId = $customField['id']; | |
6a488035 TO |
607 | |
608 | $params = array($customFieldId); | |
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 | |
6a488035 | 616 | } |