Commit | Line | Data |
---|---|---|
6c0c7017 | 1 | <?php |
2 | /* | |
3 | +--------------------------------------------------------------------+ | |
7d61e75f | 4 | | Copyright CiviCRM LLC. All rights reserved. | |
6c0c7017 | 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 | | |
6c0c7017 | 9 | +--------------------------------------------------------------------+ |
10 | */ | |
11 | ||
12 | /** | |
13 | * File for the CiviCRM APIv3 job functions | |
14 | * | |
15 | * @package CiviCRM_APIv3 | |
16 | * @subpackage API_Job | |
17 | * | |
ca5cec67 | 18 | * @copyright CiviCRM LLC https://civicrm.org/licensing |
6c0c7017 | 19 | * @version $Id: Job.php 30879 2010-11-22 15:45:55Z shot $ |
20 | * | |
21 | */ | |
22 | ||
23 | /** | |
24 | * Tests for job api where custom data is involved. | |
25 | * | |
26 | * Set up for custom data won't work with useTransaction so these are not | |
27 | * compatible with the other job test class. | |
28 | * | |
29 | * @group headless | |
30 | */ | |
69239109 | 31 | class api_v3_JobTestCustomDataTest extends CiviUnitTestCase { |
6c0c7017 | 32 | protected $_apiversion = 3; |
33 | ||
34 | public $_entity = 'Job'; | |
6c0c7017 | 35 | |
36 | /** | |
37 | * Custom group ID. | |
38 | * | |
39 | * @var int | |
40 | */ | |
41 | public $customFieldID = NULL; | |
42 | ||
4ac6bdf3 | 43 | /** |
44 | * ID of a custom field of integer type. | |
45 | * | |
46 | * @var int | |
47 | */ | |
48 | public $customIntFieldID = NULL; | |
49 | ||
50 | /** | |
51 | * ID of a custom field of integer type. | |
52 | * | |
53 | * @var int | |
54 | */ | |
55 | public $customBoolFieldID = NULL; | |
56 | ||
57 | /** | |
58 | * ID of a custom field of integer type. | |
59 | * | |
60 | * @var int | |
61 | */ | |
62 | public $customStringCheckboxID = NULL; | |
63 | ||
6c0c7017 | 64 | /** |
65 | * Custom Field ID. | |
66 | * | |
67 | * @var int | |
68 | */ | |
69 | public $customGroupID = NULL; | |
70 | ||
71 | public function setUp() { | |
72 | parent::setUp(); | |
4ac6bdf3 | 73 | $customGroup = $this->customGroupCreate(); |
74 | $this->customGroupID = $customGroup['id']; | |
9099cab3 | 75 | $customField = $this->customFieldCreate([ |
4ac6bdf3 | 76 | 'custom_group_id' => $this->customGroupID, |
77 | 'data_type' => 'Date', | |
78 | 'html_type' => 'Select Date', | |
79 | 'default_value' => '', | |
9099cab3 | 80 | ]); |
4ac6bdf3 | 81 | $this->customFieldID = $customField['id']; |
9099cab3 | 82 | $customField = $this->customFieldCreate([ |
4ac6bdf3 | 83 | 'custom_group_id' => $this->customGroupID, |
84 | 'data_type' => 'Integer', | |
85 | 'html_type' => 'Text', | |
86 | 'default_value' => '', | |
87 | 'label' => 'Int Field', | |
9099cab3 | 88 | ]); |
4ac6bdf3 | 89 | $this->customIntFieldID = $customField['id']; |
9099cab3 | 90 | $customField = $this->customFieldCreate([ |
4ac6bdf3 | 91 | 'custom_group_id' => $this->customGroupID, |
92 | 'data_type' => 'Boolean', | |
93 | 'html_type' => 'Radio', | |
94 | 'default_value' => '', | |
95 | 'label' => 'Radio Field', | |
9099cab3 | 96 | ]); |
4ac6bdf3 | 97 | $this->customBoolFieldID = $customField['id']; |
9099cab3 | 98 | $customField = $this->customFieldCreate([ |
4ac6bdf3 | 99 | 'custom_group_id' => $this->customGroupID, |
100 | 'data_type' => 'String', | |
101 | 'html_type' => 'CheckBox', | |
102 | 'default_value' => NULL, | |
103 | 'label' => 'checkbox Field', | |
9099cab3 CW |
104 | 'option_values' => ['black' => 'black', 'white' => 'white'], |
105 | ]); | |
4ac6bdf3 | 106 | $this->customStringCheckboxID = $customField['id']; |
6c0c7017 | 107 | } |
108 | ||
109 | /** | |
110 | * Cleanup after tests. | |
111 | */ | |
112 | public function tearDown() { | |
9099cab3 | 113 | $this->quickCleanup(['civicrm_contact'], TRUE); |
6c0c7017 | 114 | parent::tearDown(); |
115 | } | |
116 | ||
4ac6bdf3 | 117 | /** |
118 | * Test the batch merge does not bork on custom date fields. | |
119 | * | |
120 | * @dataProvider getCheckboxData | |
121 | * | |
122 | * Test CRM-19074 checkbox field handling. | |
123 | * | |
124 | * Given a custom field with 2 checkboxes (black & white) possible values are: | |
125 | * 1) SEPARATOR + black + SEPARATOR | |
126 | * 2) SEPARATOR + white + SEPARATOR | |
127 | * 3) SEPARATOR + black + SEPARATOR + white + SEPARATOR | |
128 | * 3) '' (ie empty string means both set to 0) | |
129 | * 4) NULL (ie not set) | |
130 | * - in safe mode NULL is not a conflict with any option but the other | |
131 | * combos are a conflict. | |
132 | */ | |
133 | public function testBatchMergeCheckboxCustomFieldHandling($dataSet) { | |
134 | $customFieldLabel = 'custom_' . $this->customStringCheckboxID; | |
9099cab3 CW |
135 | $contact1Params = is_array($dataSet['contacts'][0]) ? [$customFieldLabel => $dataSet['contacts'][0]] : []; |
136 | $contact2Params = is_array($dataSet['contacts'][1]) ? [$customFieldLabel => $dataSet['contacts'][1]] : []; | |
4ac6bdf3 | 137 | $contactID = $this->individualCreate($contact1Params); |
138 | $this->individualCreate($contact2Params); | |
9099cab3 | 139 | $result = $this->callAPISuccess('Job', 'process_batch_merge', ['mode' => $dataSet['mode']]); |
4ac6bdf3 | 140 | $this->assertEquals($dataSet['merged'], count($result['values']['merged'])); |
141 | $this->assertEquals($dataSet['skipped'], count($result['values']['skipped'])); | |
9099cab3 | 142 | $contact = $this->callAPISuccess('Contact', 'getsingle', ['id' => $contactID, 'return' => $customFieldLabel]); |
4ac6bdf3 | 143 | $this->assertEquals($dataSet['expected'], $contact[$customFieldLabel]); |
144 | } | |
145 | ||
146 | /** | |
147 | * Get the various data combinations for a checkbox field. | |
148 | * | |
149 | * @return array | |
150 | */ | |
151 | public function getCheckboxData() { | |
9099cab3 CW |
152 | $data = [ |
153 | [ | |
154 | 'null_merges_with_set' => [ | |
4ac6bdf3 | 155 | 'mode' => 'safe', |
9099cab3 | 156 | 'contacts' => [ |
4ac6bdf3 | 157 | NULL, |
9099cab3 CW |
158 | ['black'], |
159 | ], | |
4ac6bdf3 | 160 | 'skipped' => 0, |
161 | 'merged' => 1, | |
9099cab3 CW |
162 | 'expected' => ['black'], |
163 | ], | |
164 | ], | |
165 | [ | |
166 | 'null_merges_with_set_reverse' => [ | |
4ac6bdf3 | 167 | 'mode' => 'safe', |
9099cab3 CW |
168 | 'contacts' => [ |
169 | ['black'], | |
4ac6bdf3 | 170 | NULL, |
9099cab3 | 171 | ], |
4ac6bdf3 | 172 | 'skipped' => 0, |
173 | 'merged' => 1, | |
9099cab3 | 174 | 'expected' => ['black'], |
4ac6bdf3 | 175 | |
9099cab3 CW |
176 | ], |
177 | ], | |
178 | [ | |
179 | 'empty_conflicts_with_set' => [ | |
4ac6bdf3 | 180 | 'mode' => 'safe', |
9099cab3 CW |
181 | 'contacts' => [ |
182 | ['white'], | |
183 | ['black'], | |
184 | ], | |
4ac6bdf3 | 185 | 'skipped' => 1, |
186 | 'merged' => 0, | |
9099cab3 CW |
187 | 'expected' => ['white'], |
188 | ], | |
189 | ], | |
190 | [ | |
191 | 'empty_conflicts_with_set' => [ | |
4ac6bdf3 | 192 | 'mode' => 'aggressive', |
9099cab3 CW |
193 | 'contacts' => [ |
194 | ['white'], | |
195 | ['black'], | |
196 | ], | |
4ac6bdf3 | 197 | 'skipped' => 0, |
198 | 'merged' => 1, | |
9099cab3 CW |
199 | 'expected' => ['white'], |
200 | ], | |
201 | ], | |
202 | ]; | |
4ac6bdf3 | 203 | return $data; |
204 | } | |
39b959db | 205 | |
6c0c7017 | 206 | /** |
207 | * Test the batch merge does not bork on custom date fields. | |
208 | * | |
209 | * Test CRM-18674 date custom field handling. | |
210 | */ | |
211 | public function testBatchMergeDateCustomFieldHandling() { | |
6c0c7017 | 212 | $customFieldLabel = 'custom_' . $this->customFieldID; |
213 | $contactID = $this->individualCreate(); | |
9099cab3 CW |
214 | $this->individualCreate([$customFieldLabel => '2012-12-03']); |
215 | $result = $this->callAPISuccess('Job', 'process_batch_merge', []); | |
6c0c7017 | 216 | $this->assertEquals(1, count($result['values']['merged'])); |
9099cab3 | 217 | $contact = $this->callAPISuccess('Contact', 'getsingle', ['id' => $contactID, 'return' => $customFieldLabel]); |
6c0c7017 | 218 | $this->assertEquals('2012-12-03 00:00:00', $contact[$customFieldLabel]); |
219 | } | |
220 | ||
221 | /** | |
222 | * Test the batch merge does not bork on custom date fields. | |
223 | * | |
224 | * Test CRM-18674 date custom field handling. | |
225 | */ | |
226 | public function testBatchMergeDateCustomFieldHandlingIsView() { | |
9099cab3 | 227 | $this->customFieldCreate([ |
4b047375 CR |
228 | 'label' => 'OnlyView', |
229 | 'custom_group_id' => $this->customGroupID, | |
6c0c7017 | 230 | 'is_view' => 1, |
9099cab3 | 231 | ]); |
6c0c7017 | 232 | $customFieldLabel = 'custom_' . $this->customFieldID; |
233 | $contactID = $this->individualCreate(); | |
9099cab3 CW |
234 | $this->individualCreate([$customFieldLabel => '2012-11-03']); |
235 | $result = $this->callAPISuccess('Job', 'process_batch_merge', []); | |
6c0c7017 | 236 | $this->assertEquals(1, count($result['values']['merged'])); |
9099cab3 | 237 | $contact = $this->callAPISuccess('Contact', 'getsingle', ['id' => $contactID, 'return' => $customFieldLabel]); |
4b047375 | 238 | $this->assertEquals('2012-11-03 00:00:00', $contact[$customFieldLabel]); |
6c0c7017 | 239 | } |
240 | ||
241 | /** | |
242 | * Check we get a conflict on the custom field. | |
243 | */ | |
244 | public function testBatchMergeDateCustomFieldConflict() { | |
6c0c7017 | 245 | $customFieldLabel = 'custom_' . $this->customFieldID; |
9099cab3 CW |
246 | $contactID = $this->individualCreate([$customFieldLabel => '2012-11-03']); |
247 | $this->individualCreate([$customFieldLabel => '2013-11-03']); | |
248 | $result = $this->callAPISuccess('Job', 'process_batch_merge', []); | |
6c0c7017 | 249 | $this->assertEquals(0, count($result['values']['merged'])); |
250 | $this->assertEquals(1, count($result['values']['skipped'])); | |
9099cab3 | 251 | $contact = $this->callAPISuccess('Contact', 'getsingle', ['id' => $contactID, 'return' => $customFieldLabel]); |
6c0c7017 | 252 | $this->assertEquals('2012-11-03 00:00:00', $contact[$customFieldLabel]); |
253 | } | |
254 | ||
255 | /** | |
256 | * Check we get a conflict on the custom field. | |
257 | */ | |
258 | public function testBatchMergeDateCustomFieldNoConflict() { | |
6c0c7017 | 259 | $customFieldLabel = 'custom_' . $this->customFieldID; |
9099cab3 CW |
260 | $contactID = $this->individualCreate([$customFieldLabel => '2012-11-03']); |
261 | $this->individualCreate([$customFieldLabel => '2012-11-03']); | |
262 | $result = $this->callAPISuccess('Job', 'process_batch_merge', []); | |
6c0c7017 | 263 | $this->assertEquals(1, count($result['values']['merged'])); |
264 | $this->assertEquals(0, count($result['values']['skipped'])); | |
9099cab3 | 265 | $contact = $this->callAPISuccess('Contact', 'getsingle', ['id' => $contactID, 'return' => $customFieldLabel]); |
6c0c7017 | 266 | $this->assertEquals('2012-11-03 00:00:00', $contact[$customFieldLabel]); |
267 | } | |
268 | ||
e2cecb9c | 269 | /** |
270 | * Check we get a no conflict on the custom field & integer merges. | |
271 | */ | |
272 | public function testBatchMergeIntCustomFieldNoConflict() { | |
4ac6bdf3 | 273 | $customFieldLabel = 'custom_' . $this->customIntFieldID; |
9099cab3 CW |
274 | $contactID = $this->individualCreate([]); |
275 | $this->individualCreate([$customFieldLabel => 20]); | |
276 | $result = $this->callAPISuccess('Job', 'process_batch_merge', []); | |
e2cecb9c | 277 | $this->assertEquals(1, count($result['values']['merged'])); |
278 | $this->assertEquals(0, count($result['values']['skipped'])); | |
9099cab3 | 279 | $contact = $this->callAPISuccess('Contact', 'getsingle', ['id' => $contactID, 'return' => $customFieldLabel]); |
e2cecb9c | 280 | $this->assertEquals(20, $contact[$customFieldLabel]); |
281 | } | |
282 | ||
283 | /** | |
284 | * Check we get a conflict on the integer custom field. | |
285 | */ | |
286 | public function testBatchMergeIntCustomFieldConflict() { | |
4ac6bdf3 | 287 | $customFieldLabel = 'custom_' . $this->customIntFieldID; |
9099cab3 CW |
288 | $contactID = $this->individualCreate([$customFieldLabel => 20]); |
289 | $this->individualCreate([$customFieldLabel => 1]); | |
290 | $result = $this->callAPISuccess('Job', 'process_batch_merge', []); | |
e2cecb9c | 291 | $this->assertEquals(0, count($result['values']['merged'])); |
292 | $this->assertEquals(1, count($result['values']['skipped'])); | |
9099cab3 | 293 | $contact = $this->callAPISuccess('Contact', 'getsingle', ['id' => $contactID, 'return' => $customFieldLabel]); |
e2cecb9c | 294 | $this->assertEquals(20, $contact[$customFieldLabel]); |
295 | } | |
296 | ||
297 | /** | |
298 | * Check we get a conflict on the integer custom field when the conflicted field is 0. | |
299 | */ | |
300 | public function testBatchMergeIntCustomFieldConflictZero() { | |
4ac6bdf3 | 301 | $customFieldLabel = 'custom_' . $this->customIntFieldID; |
9099cab3 CW |
302 | $contactID = $this->individualCreate([$customFieldLabel => 0]); |
303 | $this->individualCreate([$customFieldLabel => 20]); | |
304 | $result = $this->callAPISuccess('Job', 'process_batch_merge', []); | |
e2cecb9c | 305 | $this->assertEquals(0, count($result['values']['merged'])); |
306 | $this->assertEquals(1, count($result['values']['skipped'])); | |
9099cab3 | 307 | $contact = $this->callAPISuccess('Contact', 'getsingle', ['id' => $contactID, 'return' => $customFieldLabel]); |
e2cecb9c | 308 | $this->assertEquals(0, $contact[$customFieldLabel]); |
309 | } | |
310 | ||
6c0c7017 | 311 | /** |
4ac6bdf3 | 312 | * Using the api with check perms set to off, make sure custom data is merged. |
6c0c7017 | 313 | * |
314 | * Test CRM-18674 date custom field handling. | |
315 | */ | |
316 | public function testBatchMergeDateCustomFieldConflictAndNoCheckPerms() { | |
9099cab3 | 317 | CRM_Core_Config::singleton()->userPermissionClass->permissions = ['access CiviCRM', 'edit my contact']; |
4ac6bdf3 | 318 | CRM_Core_DAO::executeQuery("DELETE FROM civicrm_cache"); |
319 | CRM_Utils_System::flushCache(); | |
6c0c7017 | 320 | $customFieldLabel = 'custom_' . $this->customFieldID; |
9099cab3 CW |
321 | $contactID = $this->individualCreate([$customFieldLabel => '2012-11-03']); |
322 | $this->individualCreate([$customFieldLabel => '2013-11-03']); | |
323 | $result = $this->callAPISuccess('Job', 'process_batch_merge', ['check_permissions' => 0]); | |
6c0c7017 | 324 | $this->assertEquals(0, count($result['values']['merged'])); |
325 | $this->assertEquals(1, count($result['values']['skipped'])); | |
9099cab3 | 326 | $contact = $this->callAPISuccess('Contact', 'getsingle', ['id' => $contactID, 'return' => $customFieldLabel]); |
6c0c7017 | 327 | $this->assertEquals('2012-11-03 00:00:00', $contact[$customFieldLabel]); |
328 | } | |
329 | ||
330 | /** | |
4ac6bdf3 | 331 | * Using the api with check perms set to off, make sure custom data is merged. |
332 | * | |
333 | * Test CRM-18674 date custom field handling. | |
6c0c7017 | 334 | */ |
4ac6bdf3 | 335 | public function testBatchMergeDateCustomFieldNoConflictAndNoCheckPerms() { |
9099cab3 | 336 | CRM_Core_Config::singleton()->userPermissionClass->permissions = ['access CiviCRM', 'edit my contact']; |
4ac6bdf3 | 337 | CRM_Core_DAO::executeQuery("DELETE FROM civicrm_cache"); |
338 | CRM_Utils_System::flushCache(); | |
339 | $customFieldLabel = 'custom_' . $this->customFieldID; | |
340 | $contactID = $this->individualCreate(); | |
9099cab3 CW |
341 | $this->individualCreate([$customFieldLabel => '2013-11-03']); |
342 | $result = $this->callAPISuccess('Job', 'process_batch_merge', ['check_permissions' => 0]); | |
4ac6bdf3 | 343 | $this->assertEquals(1, count($result['values']['merged'])); |
344 | $this->assertEquals(0, count($result['values']['skipped'])); | |
9099cab3 | 345 | $contact = $this->callAPISuccess('Contact', 'getsingle', ['id' => $contactID, 'return' => $customFieldLabel]); |
4ac6bdf3 | 346 | $this->assertEquals('2013-11-03 00:00:00', $contact[$customFieldLabel]); |
6c0c7017 | 347 | } |
348 | ||
349 | /** | |
4ac6bdf3 | 350 | * Using the api with check perms set to off, make sure custom data is merged. |
351 | * | |
c8f7094b | 352 | * Test CRM-19113 custom data lost when permissions in play. |
6c0c7017 | 353 | */ |
4ac6bdf3 | 354 | public function testBatchMergeIntCustomFieldNoConflictAndNoCheckPerms() { |
9099cab3 | 355 | CRM_Core_Config::singleton()->userPermissionClass->permissions = ['access CiviCRM', 'edit my contact']; |
4ac6bdf3 | 356 | CRM_Core_DAO::executeQuery("DELETE FROM civicrm_cache"); |
357 | CRM_Utils_System::flushCache(); | |
358 | $customFieldLabel = 'custom_' . $this->customIntFieldID; | |
9099cab3 CW |
359 | $contactID = $this->individualCreate(['custom_' . $this->customBoolFieldID => 1]); |
360 | $this->individualCreate([$customFieldLabel => 1, 'custom_' . $this->customBoolFieldID => 1]); | |
361 | $result = $this->callAPISuccess('Job', 'process_batch_merge', ['check_permissions' => 0]); | |
4ac6bdf3 | 362 | $this->assertEquals(1, count($result['values']['merged'])); |
363 | $this->assertEquals(0, count($result['values']['skipped'])); | |
9099cab3 | 364 | $contact = $this->callAPISuccess('Contact', 'getsingle', [ |
c8f7094b | 365 | 'id' => $contactID, |
9099cab3 CW |
366 | 'return' => [$customFieldLabel, 'custom_' . $this->customBoolFieldID], |
367 | ]); | |
4ac6bdf3 | 368 | $this->assertEquals(1, $contact[$customFieldLabel]); |
c8f7094b | 369 | $this->assertEquals(1, $contact['custom_' . $this->customBoolFieldID]); |
4ac6bdf3 | 370 | } |
371 | ||
372 | /** | |
373 | * Check we get a conflict on the customs field when the data conflicts for booleans. | |
374 | */ | |
375 | public function testBatchMergeCustomFieldConflicts() { | |
9099cab3 CW |
376 | $this->individualCreate(['custom_' . $this->customBoolFieldID => 0]); |
377 | $this->individualCreate(['custom_' . $this->customBoolFieldID => 1]); | |
378 | $result = $this->callAPISuccess('Job', 'process_batch_merge', []); | |
4ac6bdf3 | 379 | $this->assertEquals(0, count($result['values']['merged'])); |
380 | $this->assertEquals(1, count($result['values']['skipped'])); | |
381 | } | |
382 | ||
383 | /** | |
384 | * Check we get a conflict on the customs field when the data conflicts for booleans (reverse). | |
385 | */ | |
386 | public function testBatchMergeCustomFieldConflictsReverse() { | |
9099cab3 CW |
387 | $this->individualCreate(['custom_' . $this->customBoolFieldID => 1]); |
388 | $this->individualCreate(['custom_' . $this->customBoolFieldID => 0]); | |
389 | $result = $this->callAPISuccess('Job', 'process_batch_merge', []); | |
4ac6bdf3 | 390 | $this->assertEquals(0, count($result['values']['merged'])); |
391 | $this->assertEquals(1, count($result['values']['skipped'])); | |
392 | } | |
393 | ||
394 | /** | |
395 | * Check we get a conflict on the customs field when the data conflicts for booleans (reverse). | |
396 | */ | |
397 | public function testBatchMergeCustomFieldConflictsOneBlank() { | |
9099cab3 | 398 | $this->individualCreate(['custom_' . $this->customBoolFieldID => 1]); |
4ac6bdf3 | 399 | $this->individualCreate(); |
9099cab3 | 400 | $result = $this->callAPISuccess('Job', 'process_batch_merge', []); |
4ac6bdf3 | 401 | $this->assertEquals(1, count($result['values']['merged'])); |
402 | $this->assertEquals(0, count($result['values']['skipped'])); | |
403 | } | |
404 | ||
405 | /** | |
406 | * Check we get a conflict on the customs field when the data conflicts for booleans (reverse). | |
407 | */ | |
408 | public function testBatchMergeCustomFieldConflictsOneBlankReverse() { | |
409 | $this->individualCreate(); | |
9099cab3 CW |
410 | $this->individualCreate(['custom_' . $this->customBoolFieldID => 1]); |
411 | $result = $this->callAPISuccess('Job', 'process_batch_merge', []); | |
4ac6bdf3 | 412 | $this->assertEquals(1, count($result['values']['merged'])); |
413 | $this->assertEquals(0, count($result['values']['skipped'])); | |
6c0c7017 | 414 | } |
415 | ||
416 | } |