Commit | Line | Data |
---|---|---|
6a488035 TO |
1 | <?php |
2 | /* | |
3 | +--------------------------------------------------------------------+ | |
7d61e75f | 4 | | Copyright CiviCRM LLC. All rights reserved. | |
6a488035 | 5 | | | |
7d61e75f TO |
6 | | This work is published under the GNU AGPLv3 license with some | |
7 | | permitted exceptions and without any warranty. For full license | | |
8 | | and copyright information, see https://civicrm.org/licensing | | |
6a488035 | 9 | +--------------------------------------------------------------------+ |
d25dd0ee | 10 | */ |
6a488035 | 11 | |
4cbe18b8 EM |
12 | /** |
13 | * Class CRM_Core_BAO_CustomGroupTest | |
acb109b7 | 14 | * @group headless |
4cbe18b8 | 15 | */ |
6a488035 | 16 | class CRM_Core_BAO_CustomGroupTest extends CiviUnitTestCase { |
6a488035 | 17 | |
6a488035 | 18 | /** |
fe482240 | 19 | * Test getTree(). |
6a488035 | 20 | */ |
00be9182 | 21 | public function testGetTree() { |
27dd6252 | 22 | $customGroup = $this->CustomGroupCreate(); |
9099cab3 | 23 | $customField = $this->customFieldCreate(['custom_group_id' => $customGroup['id']]); |
27dd6252 E |
24 | $result = CRM_Core_BAO_CustomGroup::getTree('Individual', NULL, $customGroup['id']); |
25 | $this->assertEquals('Custom Field', $result[$customGroup['id']]['fields'][$customField['id']]['label']); | |
26 | $this->customGroupDelete($customGroup['id']); | |
27 | } | |
6a488035 | 28 | |
27dd6252 E |
29 | /** |
30 | * Test calling getTree with contact subtype data. | |
31 | * | |
32 | * Note that the function seems to support a range of formats so 3 are tested. Yay for | |
33 | * inconsistency. | |
34 | */ | |
35 | public function testGetTreeContactSubType() { | |
9099cab3 CW |
36 | $contactType = $this->callAPISuccess('ContactType', 'create', ['name' => 'Big Bank', 'label' => 'biggee', 'parent_id' => 'Organization']); |
37 | $customGroup = $this->CustomGroupCreate(['extends' => 'Organization', 'extends_entity_column_value' => ['Big_Bank']]); | |
38 | $customField = $this->customFieldCreate(['custom_group_id' => $customGroup['id']]); | |
39 | $result1 = CRM_Core_BAO_CustomGroup::getTree('Organization', NULL, NULL, NULL, ['Big_Bank']); | |
27dd6252 | 40 | $this->assertEquals('Custom Field', $result1[$customGroup['id']]['fields'][$customField['id']]['label']); |
325fa965 | 41 | $result = CRM_Core_BAO_CustomGroup::getTree('Organization', NULL, NULL, NULL, CRM_Core_DAO::VALUE_SEPARATOR . 'Big_Bank' . CRM_Core_DAO::VALUE_SEPARATOR); |
27dd6252 | 42 | $this->assertEquals($result1, $result); |
325fa965 | 43 | $result = CRM_Core_BAO_CustomGroup::getTree('Organization', NULL, NULL, NULL, 'Big_Bank'); |
27dd6252 E |
44 | $this->assertEquals($result1, $result); |
45 | try { | |
9099cab3 | 46 | CRM_Core_BAO_CustomGroup::getTree('Organization', NULL, NULL, NULL, ['Small Kind Bank']); |
27dd6252 E |
47 | } |
48 | catch (CRM_Core_Exception $e) { | |
49 | $this->customGroupDelete($customGroup['id']); | |
9099cab3 | 50 | $this->callAPISuccess('ContactType', 'delete', ['id' => $contactType['id']]); |
27dd6252 E |
51 | return; |
52 | } | |
53 | $this->fail('There is no such thing as a small kind bank'); | |
54 | } | |
6a488035 | 55 | |
4dcd2787 J |
56 | /** |
57 | * Test calling getTree for a custom field extending a renamed contact type. | |
58 | */ | |
59 | public function testGetTreeContactSubTypeForNameChangedContactType() { | |
9099cab3 | 60 | $contactType = $this->callAPISuccess('ContactType', 'create', ['name' => 'Big Bank', 'label' => 'biggee', 'parent_id' => 'Organization']); |
4dcd2787 | 61 | CRM_Core_DAO::executeQuery('UPDATE civicrm_contact_type SET label = "boo" WHERE name = "Organization"'); |
9099cab3 CW |
62 | $customGroup = $this->CustomGroupCreate(['extends' => 'Organization', 'extends_entity_column_value' => ['Big_Bank']]); |
63 | $customField = $this->customFieldCreate(['custom_group_id' => $customGroup['id']]); | |
64 | $result1 = CRM_Core_BAO_CustomGroup::getTree('Organization', NULL, NULL, NULL, ['Big_Bank']); | |
4dcd2787 J |
65 | $this->assertEquals('Custom Field', $result1[$customGroup['id']]['fields'][$customField['id']]['label']); |
66 | $this->customGroupDelete($customGroup['id']); | |
9099cab3 | 67 | $this->callAPISuccess('ContactType', 'delete', ['id' => $contactType['id']]); |
4dcd2787 J |
68 | } |
69 | ||
93ecb8e8 | 70 | /** |
71 | * Test calling getTree for a custom field extending a disabled contact type. | |
72 | */ | |
73 | public function testGetTreeContactSubTypeForDisabledChangedContactType() { | |
9099cab3 CW |
74 | $contactType = $this->callAPISuccess('ContactType', 'create', ['name' => 'Big Bank', 'label' => 'biggee', 'parent_id' => 'Organization']); |
75 | $customGroup = $this->CustomGroupCreate(['extends' => 'Organization', 'extends_entity_column_value' => ['Big_Bank']]); | |
76 | $customField = $this->customFieldCreate(['custom_group_id' => $customGroup['id']]); | |
77 | $this->callAPISuccess('ContactType', 'create', ['id' => $contactType['id'], 'is_active' => 0]); | |
78 | $result1 = CRM_Core_BAO_CustomGroup::getTree('Organization', NULL, NULL, NULL, ['Big_Bank']); | |
93ecb8e8 | 79 | $this->assertEquals('Custom Field', $result1[$customGroup['id']]['fields'][$customField['id']]['label']); |
80 | $this->customGroupDelete($customGroup['id']); | |
9099cab3 | 81 | $this->callAPISuccess('ContactType', 'delete', ['id' => $contactType['id']]); |
93ecb8e8 | 82 | } |
83 | ||
9364f696 SL |
84 | /** |
85 | * Test calling GetTree for a custom field extending multiple subTypes. | |
86 | */ | |
66b231c2 | 87 | public function testGetTreetContactSubTypeForMultipleSubTypes() { |
9099cab3 CW |
88 | $contactType1 = $this->callAPISuccess('ContactType', 'create', ['name' => 'Big Bank', 'label' => 'biggee', 'parent_id' => 'Organization']); |
89 | $contactType2 = $this->callAPISuccess('ContactType', 'create', ['name' => 'Small Bank', 'label' => 'smallee', 'parent_id' => 'Organization']); | |
90 | $customGroup = $this->CustomGroupCreate(['extends' => 'Organization', 'extends_entity_column_value' => ['Big_Bank', 'Small_Bank']]); | |
91 | $customField = $this->customFieldCreate(['custom_group_id' => $customGroup['id']]); | |
72054bb5 | 92 | $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 |
93 | $this->assertEquals('Custom Field', $result1[$customGroup['id']]['fields'][$customField['id']]['label']); |
94 | $this->customGroupDelete($customGroup['id']); | |
9099cab3 CW |
95 | $this->callAPISuccess('ContactType', 'delete', ['id' => $contactType1['id']]); |
96 | $this->callAPISuccess('ContactType', 'delete', ['id' => $contactType2['id']]); | |
66b231c2 SL |
97 | } |
98 | ||
9364f696 SL |
99 | /** |
100 | * Test calling GetTree for a custom field that extends a non numerical Event Type. | |
101 | */ | |
102 | public function testGetTreeEventSubTypeAlphabetical() { | |
9099cab3 CW |
103 | $eventType = $this->callAPISuccess('OptionValue', 'Create', ['option_group_id' => 'event_type', 'value' => 'meeting', 'label' => 'Meeting']); |
104 | $customGroup = $this->CustomGroupCreate(['extends' => 'Event', 'extends_entity_column_value' => ['Meeting']]); | |
105 | $customField = $this->customFieldCreate(['custom_group_id' => $customGroup['id']]); | |
9364f696 SL |
106 | $result1 = CRM_Core_BAO_CustomGroup::getTree('Event', NULL, NULL, NULL, CRM_Core_DAO::VALUE_SEPARATOR . 'meeting' . CRM_Core_DAO::VALUE_SEPARATOR); |
107 | $this->assertEquals('Custom Field', $result1[$customGroup['id']]['fields'][$customField['id']]['label']); | |
108 | $this->customGroupDelete($customGroup['id']); | |
9099cab3 | 109 | $this->callAPISuccess('OptionValue', 'delete', ['id' => $eventType['id']]); |
9364f696 SL |
110 | } |
111 | ||
35e83f39 | 112 | /** |
113 | * Test calling getTree with contact subtype data. | |
114 | * | |
115 | * Note that the function seems to support a range of formats so 3 are tested. Yay for | |
116 | * inconsistency. | |
117 | */ | |
118 | public function testGetTreeCampaignSubType() { | |
05e3bddf | 119 | $sep = CRM_Core_DAO::VALUE_SEPARATOR; |
35e83f39 | 120 | $this->campaignCreate(); |
121 | $this->campaignCreate(); | |
9099cab3 | 122 | $customGroup = $this->CustomGroupCreate([ |
35e83f39 | 123 | 'extends' => 'Campaign', |
05e3bddf | 124 | 'extends_entity_column_value' => "{$sep}1{$sep}2{$sep}", |
9099cab3 CW |
125 | ]); |
126 | $customField = $this->customFieldCreate(['custom_group_id' => $customGroup['id']]); | |
35e83f39 | 127 | $result1 = CRM_Core_BAO_CustomGroup::getTree('Campaign', NULL, NULL, NULL, '\ 11\ 12\ 1'); |
128 | $this->assertEquals('Custom Field', $result1[$customGroup['id']]['fields'][$customField['id']]['label']); | |
129 | $this->customGroupDelete($customGroup['id']); | |
130 | } | |
131 | ||
27dd6252 E |
132 | /** |
133 | * Test calling getTree with contact subtype data. | |
134 | */ | |
135 | public function testGetTreeActivitySubType() { | |
9099cab3 CW |
136 | $customGroup = $this->CustomGroupCreate(['extends' => 'Activity', 'extends_entity_column_value' => 1]); |
137 | $customField = $this->customFieldCreate(['custom_group_id' => $customGroup['id']]); | |
27dd6252 E |
138 | $result = CRM_Core_BAO_CustomGroup::getTree('Activity', NULL, NULL, NULL, 1); |
139 | $this->assertEquals('Custom Field', $result[$customGroup['id']]['fields'][$customField['id']]['label']); | |
140 | $this->customGroupDelete($customGroup['id']); | |
6a488035 TO |
141 | } |
142 | ||
143 | /** | |
35e83f39 | 144 | * Test retrieve() with Empty Params. |
6a488035 | 145 | */ |
00be9182 | 146 | public function testRetrieveEmptyParams() { |
9099cab3 | 147 | $params = []; |
6a488035 TO |
148 | $customGroup = CRM_Core_BAO_CustomGroup::retrieve($params, $dafaults); |
149 | $this->assertNull($customGroup, 'Check that no custom Group is retreived'); | |
150 | } | |
151 | ||
152 | /** | |
100fef9d | 153 | * Test retrieve() with Inalid Params |
6a488035 | 154 | */ |
00be9182 | 155 | public function testRetrieveInvalidParams() { |
9099cab3 | 156 | $params = ['id' => 99]; |
6a488035 TO |
157 | $customGroup = CRM_Core_BAO_CustomGroup::retrieve($params, $dafaults); |
158 | $this->assertNull($customGroup, 'Check that no custom Group is retreived'); | |
159 | } | |
160 | ||
161 | /** | |
100fef9d | 162 | * Test retrieve() |
6a488035 | 163 | */ |
00be9182 | 164 | public function testRetrieve() { |
5c988f8a | 165 | $customGroupTitle = 'Custom Group'; |
9099cab3 | 166 | $groupParams = [ |
5c988f8a | 167 | 'title' => $customGroupTitle, |
6a488035 TO |
168 | 'name' => 'My_Custom_Group', |
169 | 'style' => 'Tab', | |
170 | 'extends' => 'Individual', | |
171 | 'help_pre' => 'Custom Group Help Pre', | |
172 | 'help_post' => 'Custom Group Help Post', | |
173 | 'is_active' => 1, | |
174 | 'collapse_display' => 1, | |
175 | 'weight' => 2, | |
9099cab3 | 176 | ]; |
6a488035 | 177 | |
5c988f8a | 178 | $customGroup = $this->customGroupCreate($groupParams); |
6a488035 | 179 | |
5c988f8a | 180 | $this->getAndCheck($groupParams, $customGroup['id'], 'CustomGroup'); |
6a488035 TO |
181 | } |
182 | ||
183 | /** | |
100fef9d | 184 | * Test setIsActive() |
6a488035 | 185 | */ |
00be9182 | 186 | public function testSetIsActive() { |
5c988f8a | 187 | $customGroupTitle = 'My Custom Group'; |
9099cab3 | 188 | $groupParams = [ |
5c988f8a | 189 | 'title' => $customGroupTitle, |
6a488035 TO |
190 | 'name' => 'my_custom_group', |
191 | 'style' => 'Tab', | |
192 | 'extends' => 'Individual', | |
193 | 'is_active' => 0, | |
9099cab3 | 194 | ]; |
6a488035 | 195 | |
5c988f8a | 196 | $customGroup = $this->customGroupCreate($groupParams); |
197 | $customGroupId = $customGroup['id']; | |
6a488035 TO |
198 | |
199 | //update is_active | |
200 | $result = CRM_Core_BAO_CustomGroup::setIsActive($customGroupId, TRUE); | |
201 | ||
202 | //check for object update | |
203 | $this->assertEquals(TRUE, $result); | |
204 | //check for is_active | |
205 | $this->assertDBCompareValue('CRM_Core_DAO_CustomGroup', $customGroupId, 'is_active', 'id', 1, | |
206 | 'Database check for custom group is_active field.' | |
207 | ); | |
5c988f8a | 208 | |
209 | $this->customGroupDelete($customGroup['id']); | |
6a488035 TO |
210 | } |
211 | ||
212 | /** | |
100fef9d | 213 | * Test getGroupDetail() with Empty Params |
6a488035 | 214 | */ |
00be9182 | 215 | public function testGetGroupDetailEmptyParams() { |
9099cab3 | 216 | $customGroupId = []; |
6a488035 TO |
217 | $customGroup = CRM_Core_BAO_CustomGroup::getGroupDetail($customGroupId); |
218 | $this->assertTrue(empty($customGroup), 'Check that no custom Group details is retreived'); | |
219 | } | |
220 | ||
221 | /** | |
5c988f8a | 222 | * Test getGroupDetail with Invalid Params. |
6a488035 | 223 | */ |
00be9182 | 224 | public function testGetGroupDetailInvalidParams() { |
6a488035 TO |
225 | $customGroupId = 99; |
226 | $customGroup = CRM_Core_BAO_CustomGroup::getGroupDetail($customGroupId); | |
227 | $this->assertTrue(empty($customGroup), 'Check that no custom Group details is retreived'); | |
228 | } | |
229 | ||
230 | /** | |
5c988f8a | 231 | * Test getGroupDetail(). |
6a488035 | 232 | */ |
00be9182 | 233 | public function testGetGroupDetail() { |
5c988f8a | 234 | $customGroupTitle = 'My Custom Group'; |
9099cab3 | 235 | $groupParams = [ |
5c988f8a | 236 | 'title' => $customGroupTitle, |
6a488035 TO |
237 | 'name' => 'My_Custom_Group', |
238 | 'extends' => 'Individual', | |
239 | 'help_pre' => 'Custom Group Help Pre', | |
240 | 'help_post' => 'Custom Group Help Post', | |
241 | 'is_active' => 1, | |
242 | 'collapse_display' => 1, | |
9099cab3 | 243 | ]; |
6a488035 | 244 | |
5c988f8a | 245 | $customGroup = $this->customGroupCreate($groupParams); |
246 | $customGroupId = $customGroup['id']; | |
6a488035 | 247 | |
9099cab3 | 248 | $fieldParams = [ |
6a488035 TO |
249 | 'custom_group_id' => $customGroupId, |
250 | 'label' => 'Test Custom Field', | |
251 | 'html_type' => 'Text', | |
252 | 'data_type' => 'String', | |
253 | 'is_required' => 1, | |
254 | 'is_searchable' => 0, | |
255 | 'is_active' => 1, | |
9099cab3 | 256 | ]; |
6a488035 | 257 | |
5c988f8a | 258 | $customField = $this->customFieldCreate($fieldParams); |
259 | $customFieldId = $customField['id']; | |
6a488035 TO |
260 | |
261 | $groupTree = CRM_Core_BAO_CustomGroup::getGroupDetail($customGroupId); | |
5c988f8a | 262 | $this->assertDBNotNull('CRM_Core_DAO_CustomGroup', $customGroupId, 'title', 'id', |
6a488035 TO |
263 | 'Database check for custom group record.' |
264 | ); | |
265 | //check retieve values of custom group | |
266 | unset($groupParams['is_active']); | |
267 | unset($groupParams['title']); | |
268 | unset($groupParams['version']); | |
269 | $this->assertAttributesEquals($groupParams, $groupTree[$customGroupId]); | |
270 | ||
271 | //check retieve values of custom field | |
272 | unset($fieldParams['is_active']); | |
273 | unset($fieldParams['custom_group_id']); | |
274 | unset($fieldParams['version']); | |
275 | $this->assertAttributesEquals($fieldParams, $groupTree[$customGroupId]['fields'][$customFieldId], " in line " . __LINE__); | |
276 | ||
5c988f8a | 277 | $this->customFieldDelete($customField['id']); |
278 | $this->customGroupDelete($customGroup['id']); | |
6a488035 TO |
279 | } |
280 | ||
281 | /** | |
100fef9d | 282 | * Test getTitle() with Invalid Params() |
6a488035 | 283 | */ |
00be9182 | 284 | public function testGetTitleWithInvalidParams() { |
6a488035 TO |
285 | $params = 99; |
286 | $customGroupTitle = CRM_Core_BAO_CustomGroup::getTitle($params); | |
287 | ||
288 | $this->assertNull($customGroupTitle, 'Check that no custom Group Title is retreived'); | |
289 | } | |
290 | ||
291 | /** | |
100fef9d | 292 | * Test getTitle() |
6a488035 | 293 | */ |
00be9182 | 294 | public function testGetTitle() { |
5c988f8a | 295 | $customGroupTitle = 'Custom Group'; |
9099cab3 | 296 | $groupParams = [ |
5c988f8a | 297 | 'title' => $customGroupTitle, |
6a488035 TO |
298 | 'name' => 'my_custom_group', |
299 | 'style' => 'Tab', | |
300 | 'extends' => 'Individual', | |
301 | 'is_active' => 0, | |
9099cab3 | 302 | ]; |
6a488035 | 303 | |
5c988f8a | 304 | $customGroup = $this->customGroupCreate($groupParams); |
305 | $customGroupId = $customGroup['id']; | |
6a488035 TO |
306 | |
307 | //get the custom group title | |
308 | $title = CRM_Core_BAO_CustomGroup::getTitle($customGroupId); | |
309 | ||
310 | //check for object update | |
5c988f8a | 311 | $this->assertEquals($customGroupTitle, $title); |
6a488035 | 312 | |
5c988f8a | 313 | $this->customGroupDelete($customGroupId); |
6a488035 TO |
314 | } |
315 | ||
316 | /** | |
5c988f8a | 317 | * Test deleteGroup. |
6a488035 | 318 | */ |
00be9182 | 319 | public function testDeleteGroup() { |
5c988f8a | 320 | $customGroupTitle = 'My Custom Group'; |
9099cab3 | 321 | $groupParams = [ |
5c988f8a | 322 | 'title' => $customGroupTitle, |
6a488035 TO |
323 | 'name' => 'my_custom_group', |
324 | 'style' => 'Tab', | |
325 | 'extends' => 'Individual', | |
326 | 'is_active' => 1, | |
9099cab3 | 327 | ]; |
6a488035 | 328 | |
5c988f8a | 329 | $customGroup = $this->customGroupCreate($groupParams); |
330 | $groupObject = new CRM_Core_BAO_CustomGroup(); | |
331 | $groupObject->id = $customGroup['id']; | |
332 | $groupObject->find(TRUE); | |
6a488035 | 333 | |
5c988f8a | 334 | $isDelete = CRM_Core_BAO_CustomGroup::deleteGroup($groupObject); |
6a488035 | 335 | |
5c988f8a | 336 | // Check it worked! |
6a488035 | 337 | $this->assertEquals(TRUE, $isDelete); |
5c988f8a | 338 | $this->assertDBNull('CRM_Core_DAO_CustomGroup', $customGroup['id'], 'title', 'id', |
6a488035 TO |
339 | 'Database check for custom group record.' |
340 | ); | |
341 | } | |
342 | ||
343 | /** | |
100fef9d | 344 | * Test createTable() |
6a488035 | 345 | */ |
00be9182 | 346 | public function testCreateTable() { |
9099cab3 | 347 | $groupParams = [ |
5c988f8a | 348 | 'title' => 'My Custom Group', |
6a488035 TO |
349 | 'name' => 'my_custom_group', |
350 | 'style' => 'Tab', | |
351 | 'extends' => 'Individual', | |
352 | 'is_active' => 1, | |
353 | 'version' => 3, | |
9099cab3 | 354 | ]; |
6a488035 TO |
355 | |
356 | $customGroupBAO = new CRM_Core_BAO_CustomGroup(); | |
357 | $customGroupBAO->copyValues($groupParams); | |
358 | $customGroup = $customGroupBAO->save(); | |
359 | $tableName = 'civicrm_value_test_group_' . $customGroup->id; | |
360 | $customGroup->table_name = $tableName; | |
361 | $customGroup = $customGroupBAO->save(); | |
5c988f8a | 362 | CRM_Core_BAO_CustomGroup::createTable($customGroup); |
6a488035 TO |
363 | $customGroupId = $customGroup->id; |
364 | ||
365 | //check db for custom group. | |
366 | $dbCustomGroupTitle = $this->assertDBNotNull('CRM_Core_DAO_CustomGroup', $customGroupId, 'title', 'id', | |
367 | 'Database check for custom group record.' | |
368 | ); | |
369 | //check for custom group table name | |
370 | $this->assertDBCompareValue('CRM_Core_DAO_CustomGroup', $customGroupId, 'table_name', 'id', | |
371 | $tableName, 'Database check for custom group table name.' | |
372 | ); | |
373 | ||
5c988f8a | 374 | $this->customGroupDelete($customGroup->id); |
6a488035 TO |
375 | } |
376 | ||
377 | /** | |
100fef9d | 378 | * Test checkCustomField() |
6a488035 | 379 | */ |
00be9182 | 380 | public function testCheckCustomField() { |
9099cab3 | 381 | $groupParams = [ |
5c988f8a | 382 | 'title' => 'My Custom Group', |
6a488035 TO |
383 | 'name' => 'my_custom_group', |
384 | 'extends' => 'Individual', | |
385 | 'help_pre' => 'Custom Group Help Pre', | |
386 | 'help_post' => 'Custom Group Help Post', | |
387 | 'is_active' => 1, | |
388 | 'collapse_display' => 1, | |
9099cab3 | 389 | ]; |
6a488035 | 390 | |
5c988f8a | 391 | $customGroup = $this->customGroupCreate($groupParams); |
392 | $this->assertNotNull($customGroup['id'], 'pre-requisite group not created successfully'); | |
393 | $customGroupId = $customGroup['id']; | |
6a488035 TO |
394 | |
395 | $customFieldLabel = 'Test Custom Field'; | |
9099cab3 | 396 | $fieldParams = [ |
6a488035 TO |
397 | 'custom_group_id' => $customGroupId, |
398 | 'label' => $customFieldLabel, | |
399 | 'html_type' => 'Text', | |
400 | 'data_type' => 'String', | |
401 | 'is_required' => 1, | |
402 | 'is_searchable' => 0, | |
403 | 'is_active' => 1, | |
9099cab3 | 404 | ]; |
6a488035 | 405 | |
5c988f8a | 406 | $customField = $this->customFieldCreate($fieldParams); |
407 | $customField = $customField['values'][$customField['id']]; | |
6a488035 | 408 | |
5c988f8a | 409 | $customFieldId = $customField['id']; |
6a488035 TO |
410 | |
411 | //check db for custom field | |
412 | $dbCustomFieldLabel = $this->assertDBNotNull('CRM_Core_DAO_CustomField', $customFieldId, 'label', 'id', | |
413 | 'Database check for custom field record.' | |
414 | ); | |
415 | $this->assertEquals($customFieldLabel, $dbCustomFieldLabel); | |
416 | ||
417 | //check the custom field type. | |
9099cab3 | 418 | $params = ['Individual']; |
6a488035 TO |
419 | $usedFor = CRM_Core_BAO_CustomGroup::checkCustomField($customFieldId, $params); |
420 | $this->assertEquals(FALSE, $usedFor); | |
421 | ||
9099cab3 | 422 | $params = ['Contribution', 'Membership', 'Participant']; |
6a488035 TO |
423 | $usedFor = CRM_Core_BAO_CustomGroup::checkCustomField($customFieldId, $params); |
424 | $this->assertEquals(TRUE, $usedFor); | |
425 | ||
5c988f8a | 426 | $this->customFieldDelete($customField['id']); |
427 | $this->customGroupDelete($customGroup['id']); | |
6a488035 TO |
428 | } |
429 | ||
430 | /** | |
100fef9d | 431 | * Test getActiveGroups() with Invalid Params() |
6a488035 | 432 | */ |
00be9182 | 433 | public function testGetActiveGroupsWithInvalidParams() { |
f2040bc6 | 434 | $contactId = $this->individualCreate(); |
6a488035 TO |
435 | $activeGroups = CRM_Core_BAO_CustomGroup::getActiveGroups('ABC', 'civicrm/contact/view/cd', $contactId); |
436 | $this->assertEquals(empty($activeGroups), TRUE, 'Check that Emprt params are retreived'); | |
437 | } | |
438 | ||
00be9182 | 439 | public function testGetActiveGroups() { |
f2040bc6 | 440 | $contactId = $this->individualCreate(); |
5c988f8a | 441 | $customGroupTitle = 'Custom Group'; |
9099cab3 | 442 | $groupParams = [ |
5c988f8a | 443 | 'title' => $customGroupTitle, |
6a488035 TO |
444 | 'name' => 'test_custom_group', |
445 | 'style' => 'Tab', | |
446 | 'extends' => 'Individual', | |
447 | 'weight' => 10, | |
448 | 'is_active' => 1, | |
9099cab3 | 449 | ]; |
6a488035 | 450 | |
5c988f8a | 451 | $customGroup = $this->customGroupCreate($groupParams); |
6a488035 TO |
452 | $activeGroup = CRM_Core_BAO_CustomGroup::getActiveGroups('Individual', 'civicrm/contact/view/cd', $contactId); |
453 | foreach ($activeGroup as $key => $value) { | |
5c988f8a | 454 | if ($value['id'] == $customGroup['id']) { |
6a488035 | 455 | $this->assertEquals($value['path'], 'civicrm/contact/view/cd'); |
5c988f8a | 456 | $this->assertEquals($value['title'], $customGroupTitle); |
457 | $query = 'reset=1&gid=' . $customGroup['id'] . '&cid=' . $contactId; | |
6a488035 TO |
458 | $this->assertEquals($value['query'], $query); |
459 | } | |
460 | } | |
461 | ||
5c988f8a | 462 | $this->customGroupDelete($customGroup['id']); |
93ac19cd | 463 | $this->contactDelete($contactId); |
6a488035 TO |
464 | } |
465 | ||
466 | /** | |
100fef9d | 467 | * Test create() |
6a488035 | 468 | */ |
00be9182 | 469 | public function testCreate() { |
9099cab3 | 470 | $params = [ |
6a488035 TO |
471 | 'title' => 'Test_Group_1', |
472 | 'name' => 'test_group_1', | |
9099cab3 | 473 | 'extends' => [0 => 'Individual', 1 => []], |
6a488035 TO |
474 | 'weight' => 4, |
475 | 'collapse_display' => 1, | |
476 | 'style' => 'Inline', | |
477 | 'help_pre' => 'This is Pre Help For Test Group 1', | |
478 | 'help_post' => 'This is Post Help For Test Group 1', | |
479 | 'is_active' => 1, | |
480 | 'version' => 3, | |
9099cab3 | 481 | ]; |
6a488035 TO |
482 | $customGroup = CRM_Core_BAO_CustomGroup::create($params); |
483 | ||
484 | $dbCustomGroupTitle = $this->assertDBNotNull('CRM_Core_DAO_CustomGroup', $customGroup->id, 'title', 'id', | |
485 | 'Database check for custom group record.' | |
486 | ); | |
487 | $this->assertEquals($params['title'], $dbCustomGroupTitle); | |
8b3b9a2e ARW |
488 | |
489 | $dbCustomGroupTableName = $this->assertDBNotNull('CRM_Core_DAO_CustomGroup', $customGroup->id, 'table_name', 'id', | |
490 | 'Database check for custom group record.' | |
491 | ); | |
492 | $this->assertEquals(strtolower("civicrm_value_{$params['name']}_{$customGroup->id}"), $dbCustomGroupTableName, | |
92915c55 | 493 | "The table name should be suffixed with '_ID' unless specified."); |
8b3b9a2e | 494 | |
5c988f8a | 495 | $this->customGroupDelete($customGroup->id); |
8b3b9a2e ARW |
496 | } |
497 | ||
498 | /** | |
100fef9d | 499 | * Test create() given a table_name |
8b3b9a2e | 500 | */ |
00be9182 | 501 | public function testCreateTableName() { |
9099cab3 | 502 | $params = [ |
8b3b9a2e ARW |
503 | 'title' => 'Test_Group_2', |
504 | 'name' => 'test_group_2', | |
505 | 'table_name' => 'test_otherTableName', | |
9099cab3 | 506 | 'extends' => [0 => 'Individual', 1 => []], |
8b3b9a2e ARW |
507 | 'weight' => 4, |
508 | 'collapse_display' => 1, | |
509 | 'style' => 'Inline', | |
510 | 'help_pre' => 'This is Pre Help For Test Group 1', | |
511 | 'help_post' => 'This is Post Help For Test Group 1', | |
512 | 'is_active' => 1, | |
9099cab3 | 513 | ]; |
8b3b9a2e ARW |
514 | $customGroup = CRM_Core_BAO_CustomGroup::create($params); |
515 | ||
516 | $dbCustomGroupTitle = $this->assertDBNotNull('CRM_Core_DAO_CustomGroup', $customGroup->id, 'title', 'id', | |
517 | 'Database check for custom group record.' | |
518 | ); | |
519 | $this->assertEquals($params['title'], $dbCustomGroupTitle); | |
520 | ||
521 | $dbCustomGroupTableName = $this->assertDBNotNull('CRM_Core_DAO_CustomGroup', $customGroup->id, 'table_name', 'id', | |
522 | 'Database check for custom group record.' | |
523 | ); | |
524 | $this->assertEquals($params['table_name'], $dbCustomGroupTableName); | |
525 | ||
5c988f8a | 526 | $this->customGroupDelete($customGroup->id); |
6a488035 TO |
527 | } |
528 | ||
529 | /** | |
100fef9d | 530 | * Test isGroupEmpty() |
6a488035 | 531 | */ |
00be9182 | 532 | public function testIsGroupEmpty() { |
5c988f8a | 533 | $customGroupTitle = 'Test Custom Group'; |
9099cab3 | 534 | $groupParams = [ |
5c988f8a | 535 | 'title' => $customGroupTitle, |
6a488035 TO |
536 | 'name' => 'test_custom_group', |
537 | 'style' => 'Tab', | |
538 | 'extends' => 'Individual', | |
539 | 'weight' => 10, | |
540 | 'is_active' => 1, | |
9099cab3 | 541 | ]; |
6a488035 | 542 | |
5c988f8a | 543 | $customGroup = $this->customGroupCreate($groupParams); |
544 | $customGroupId = $customGroup['id']; | |
92915c55 | 545 | $isEmptyGroup = CRM_Core_BAO_CustomGroup::isGroupEmpty($customGroupId); |
6a488035 TO |
546 | |
547 | $this->assertEquals($isEmptyGroup, TRUE, 'Check that custom Group is Empty.'); | |
5c988f8a | 548 | $this->customGroupDelete($customGroup['id']); |
6a488035 TO |
549 | } |
550 | ||
551 | /** | |
100fef9d | 552 | * Test getGroupTitles() with Invalid Params() |
6a488035 | 553 | */ |
5c988f8a | 554 | public function testGetGroupTitlesWithInvalidParams() { |
9099cab3 | 555 | $params = [99]; |
6a488035 | 556 | $groupTitles = CRM_Core_BAO_CustomGroup::getGroupTitles($params); |
5c988f8a | 557 | $this->assertTrue(empty($groupTitles), 'Check that no titles are received'); |
6a488035 TO |
558 | } |
559 | ||
560 | /** | |
100fef9d | 561 | * Test getGroupTitles() |
6a488035 | 562 | */ |
5c988f8a | 563 | public function testGetGroupTitles() { |
9099cab3 | 564 | $groupParams = [ |
5c988f8a | 565 | 'title' => 'Test Group', |
6a488035 TO |
566 | 'name' => 'test_custom_group', |
567 | 'style' => 'Tab', | |
568 | 'extends' => 'Individual', | |
569 | 'weight' => 10, | |
570 | 'is_active' => 1, | |
9099cab3 | 571 | ]; |
6a488035 | 572 | |
5c988f8a | 573 | $customGroup = $this->customGroupCreate($groupParams); |
6a488035 | 574 | |
9099cab3 | 575 | $fieldParams = [ |
5c988f8a | 576 | 'label' => 'Custom Field', |
6a488035 TO |
577 | 'html_type' => 'Text', |
578 | 'data_type' => 'String', | |
579 | 'is_required' => 1, | |
580 | 'is_searchable' => 0, | |
581 | 'is_active' => 1, | |
5c988f8a | 582 | 'custom_group_id' => $customGroup['id'], |
9099cab3 | 583 | ]; |
6a488035 | 584 | |
5c988f8a | 585 | $customField = $this->customFieldCreate($fieldParams); |
586 | $customFieldId = $customField['id']; | |
6a488035 | 587 | |
9099cab3 | 588 | $params = [$customFieldId]; |
6a488035 TO |
589 | |
590 | $groupTitles = CRM_Core_BAO_CustomGroup::getGroupTitles($params); | |
591 | ||
5c988f8a | 592 | $this->assertEquals($groupTitles[$customFieldId]['groupTitle'], 'Test Group', 'Check Group Title.'); |
593 | $this->customGroupDelete($customGroup['id']); | |
6a488035 | 594 | } |
96025800 | 595 | |
31349d27 AS |
596 | /** |
597 | * Test that passed dates are extracted from the url when processing custom data. | |
598 | */ | |
599 | public function testExtractGetParamsReturnsDates() { | |
600 | // Create a custom group to contain the custom field. | |
9099cab3 | 601 | $groupParams = [ |
31349d27 AS |
602 | 'title' => 'My Custom Group', |
603 | 'name' => 'my_custom_group', | |
604 | 'extends' => 'Individual', | |
605 | 'is_active' => 1, | |
606 | 'collapse_display' => 1, | |
9099cab3 | 607 | ]; |
31349d27 AS |
608 | $customGroup = $this->customGroupCreate($groupParams); |
609 | $customGroupId = $customGroup['id']; | |
610 | ||
611 | // Create teh custom field. | |
9099cab3 | 612 | $fieldParams = [ |
31349d27 AS |
613 | 'custom_group_id' => $customGroupId, |
614 | 'label' => 'My Custom Date Field', | |
615 | 'html_type' => 'Select Date', | |
616 | 'data_type' => 'Date', | |
617 | 'is_required' => 1, | |
618 | 'is_searchable' => 0, | |
619 | 'is_active' => 1, | |
620 | 'default_value' => '', | |
9099cab3 | 621 | ]; |
31349d27 AS |
622 | $customField = $this->customFieldCreate($fieldParams); |
623 | $customFieldId = $customField['id']; | |
624 | ||
625 | // Create a form object. CRM_Core_BAO_CustomGroup::extractGetParams() will | |
626 | // need this, along with the REQUEST_METHOD and controller too. | |
627 | $form = new CRM_Contribute_Form_Contribution(); | |
628 | $_SERVER['REQUEST_METHOD'] = 'GET'; | |
629 | $form->controller = new CRM_Core_Controller(); | |
630 | ||
631 | // Set the value in $_GET, then extract query string params with | |
632 | $fieldName = 'custom_' . $customFieldId; | |
633 | $_GET[$fieldName] = '2017-06-13'; | |
634 | $extractedGetParams = CRM_Core_BAO_CustomGroup::extractGetParams($form, 'Individual'); | |
635 | ||
636 | $this->assertEquals($extractedGetParams[$fieldName], '2017-06-13'); | |
637 | } | |
638 | ||
6a488035 | 639 | } |