3 +--------------------------------------------------------------------+
4 | Copyright CiviCRM LLC. All rights reserved. |
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 |
9 +--------------------------------------------------------------------+
13 * File for the CiviCRM APIv3 job functions
15 * @package CiviCRM_APIv3
18 * @copyright CiviCRM LLC https://civicrm.org/licensing
19 * @version $Id: Job.php 30879 2010-11-22 15:45:55Z shot $
24 * Tests for job api where custom data is involved.
26 * Set up for custom data won't work with useTransaction so these are not
27 * compatible with the other job test class.
31 class api_v3_JobTestCustomDataTest
extends CiviUnitTestCase
{
32 protected $_apiversion = 3;
34 public $_entity = 'Job';
41 public $customFieldID = NULL;
44 * ID of a custom field of integer type.
48 public $customIntFieldID = NULL;
51 * ID of a custom field of integer type.
55 public $customBoolFieldID = NULL;
58 * ID of a custom field of integer type.
62 public $customStringCheckboxID = NULL;
69 public $customGroupID = NULL;
71 public function setUp() {
73 $customGroup = $this->customGroupCreate();
74 $this->customGroupID
= $customGroup['id'];
75 $customField = $this->customFieldCreate([
76 'custom_group_id' => $this->customGroupID
,
77 'data_type' => 'Date',
78 'html_type' => 'Select Date',
79 'default_value' => '',
81 $this->customFieldID
= $customField['id'];
82 $customField = $this->customFieldCreate([
83 'custom_group_id' => $this->customGroupID
,
84 'data_type' => 'Integer',
85 'html_type' => 'Text',
86 'default_value' => '',
87 'label' => 'Int Field',
89 $this->customIntFieldID
= $customField['id'];
90 $customField = $this->customFieldCreate([
91 'custom_group_id' => $this->customGroupID
,
92 'data_type' => 'Boolean',
93 'html_type' => 'Radio',
94 'default_value' => '',
95 'label' => 'Radio Field',
97 $this->customBoolFieldID
= $customField['id'];
98 $customField = $this->customFieldCreate([
99 'custom_group_id' => $this->customGroupID
,
100 'data_type' => 'String',
101 'html_type' => 'CheckBox',
102 'default_value' => NULL,
103 'label' => 'checkbox Field',
104 'option_values' => ['black' => 'black', 'white' => 'white'],
106 $this->customStringCheckboxID
= $customField['id'];
110 * Cleanup after tests.
112 * @throws \CRM_Core_Exception
114 public function tearDown() {
115 $this->quickCleanup(['civicrm_contact'], TRUE);
120 * Test the batch merge does not bork on custom date fields.
122 * @dataProvider getCheckboxData
124 * Test CRM-19074 checkbox field handling.
126 * Given a custom field with 2 checkboxes (black & white) possible values are:
127 * 1) SEPARATOR + black + SEPARATOR
128 * 2) SEPARATOR + white + SEPARATOR
129 * 3) SEPARATOR + black + SEPARATOR + white + SEPARATOR
130 * 3) '' (ie empty string means both set to 0)
131 * 4) NULL (ie not set)
132 * - in safe mode NULL is not a conflict with any option but the other
133 * combos are a conflict.
135 * @param array $dataSet
137 * @throws \CRM_Core_Exception
139 public function testBatchMergeCheckboxCustomFieldHandling($dataSet) {
140 $customFieldLabel = 'custom_' . $this->customStringCheckboxID
;
141 $contact1Params = is_array($dataSet['contacts'][0]) ?
[$customFieldLabel => $dataSet['contacts'][0]] : [];
142 $contact2Params = is_array($dataSet['contacts'][1]) ?
[$customFieldLabel => $dataSet['contacts'][1]] : [];
143 $contactID = $this->individualCreate($contact1Params);
144 $this->individualCreate($contact2Params);
145 $result = $this->callAPISuccess('Job', 'process_batch_merge', ['mode' => $dataSet['mode']]);
146 $this->assertCount($dataSet['merged'], $result['values']['merged']);
147 $this->assertCount($dataSet['skipped'], $result['values']['skipped']);
148 $contact = $this->callAPISuccess('Contact', 'getsingle', ['id' => $contactID, 'return' => $customFieldLabel]);
149 $this->assertEquals($dataSet['expected'], $contact[$customFieldLabel]);
153 * Get the various data combinations for a checkbox field.
157 public function getCheckboxData() {
160 'null_merges_with_set' => [
168 'expected' => ['black'],
172 'null_merges_with_set_reverse' => [
180 'expected' => ['black'],
185 'empty_conflicts_with_set' => [
193 'expected' => ['white'],
197 'empty_conflicts_with_set' => [
198 'mode' => 'aggressive',
205 'expected' => ['white'],
213 * Test the batch merge does not bork on custom date fields.
215 * Test CRM-18674 date custom field handling.
217 public function testBatchMergeDateCustomFieldHandling() {
218 $customFieldLabel = 'custom_' . $this->customFieldID
;
219 $contactID = $this->individualCreate();
220 $this->individualCreate([$customFieldLabel => '2012-12-03']);
221 $result = $this->callAPISuccess('Job', 'process_batch_merge', []);
222 $this->assertEquals(1, count($result['values']['merged']));
223 $contact = $this->callAPISuccess('Contact', 'getsingle', ['id' => $contactID, 'return' => $customFieldLabel]);
224 $this->assertEquals('2012-12-03 00:00:00', $contact[$customFieldLabel]);
228 * Test the batch merge does not bork on custom date fields.
230 * Test CRM-18674 date custom field handling.
232 public function testBatchMergeDateCustomFieldHandlingIsView() {
233 $this->customFieldCreate([
234 'label' => 'OnlyView',
235 'custom_group_id' => $this->customGroupID
,
238 $customFieldLabel = 'custom_' . $this->customFieldID
;
239 $contactID = $this->individualCreate();
240 $this->individualCreate([$customFieldLabel => '2012-11-03']);
241 $result = $this->callAPISuccess('Job', 'process_batch_merge', []);
242 $this->assertEquals(1, count($result['values']['merged']));
243 $contact = $this->callAPISuccess('Contact', 'getsingle', ['id' => $contactID, 'return' => $customFieldLabel]);
244 $this->assertEquals('2012-11-03 00:00:00', $contact[$customFieldLabel]);
248 * Check we get a conflict on the custom field.
250 public function testBatchMergeDateCustomFieldConflict() {
251 $customFieldLabel = 'custom_' . $this->customFieldID
;
252 $contactID = $this->individualCreate([$customFieldLabel => '2012-11-03']);
253 $this->individualCreate([$customFieldLabel => '2013-11-03']);
254 $result = $this->callAPISuccess('Job', 'process_batch_merge', []);
255 $this->assertEquals(0, count($result['values']['merged']));
256 $this->assertEquals(1, count($result['values']['skipped']));
257 $contact = $this->callAPISuccess('Contact', 'getsingle', ['id' => $contactID, 'return' => $customFieldLabel]);
258 $this->assertEquals('2012-11-03 00:00:00', $contact[$customFieldLabel]);
262 * Check we get a conflict on the custom field.
264 public function testBatchMergeDateCustomFieldNoConflict() {
265 $customFieldLabel = 'custom_' . $this->customFieldID
;
266 $contactID = $this->individualCreate([$customFieldLabel => '2012-11-03']);
267 $this->individualCreate([$customFieldLabel => '2012-11-03']);
268 $result = $this->callAPISuccess('Job', 'process_batch_merge', []);
269 $this->assertEquals(1, count($result['values']['merged']));
270 $this->assertEquals(0, count($result['values']['skipped']));
271 $contact = $this->callAPISuccess('Contact', 'getsingle', ['id' => $contactID, 'return' => $customFieldLabel]);
272 $this->assertEquals('2012-11-03 00:00:00', $contact[$customFieldLabel]);
276 * Check we get a no conflict on the custom field & integer merges.
278 public function testBatchMergeIntCustomFieldNoConflict() {
279 $customFieldLabel = 'custom_' . $this->customIntFieldID
;
280 $contactID = $this->individualCreate([]);
281 $this->individualCreate([$customFieldLabel => 20]);
282 $result = $this->callAPISuccess('Job', 'process_batch_merge', []);
283 $this->assertEquals(1, count($result['values']['merged']));
284 $this->assertEquals(0, count($result['values']['skipped']));
285 $contact = $this->callAPISuccess('Contact', 'getsingle', ['id' => $contactID, 'return' => $customFieldLabel]);
286 $this->assertEquals(20, $contact[$customFieldLabel]);
290 * Check we get a conflict on the integer custom field.
292 public function testBatchMergeIntCustomFieldConflict() {
293 $customFieldLabel = 'custom_' . $this->customIntFieldID
;
294 $contactID = $this->individualCreate([$customFieldLabel => 20]);
295 $this->individualCreate([$customFieldLabel => 1]);
296 $result = $this->callAPISuccess('Job', 'process_batch_merge', []);
297 $this->assertEquals(0, count($result['values']['merged']));
298 $this->assertEquals(1, count($result['values']['skipped']));
299 $contact = $this->callAPISuccess('Contact', 'getsingle', ['id' => $contactID, 'return' => $customFieldLabel]);
300 $this->assertEquals(20, $contact[$customFieldLabel]);
304 * Check we get a conflict on the integer custom field when the conflicted field is 0.
306 public function testBatchMergeIntCustomFieldConflictZero() {
307 $customFieldLabel = 'custom_' . $this->customIntFieldID
;
308 $contactID = $this->individualCreate([$customFieldLabel => 0]);
309 $this->individualCreate([$customFieldLabel => 20]);
310 $result = $this->callAPISuccess('Job', 'process_batch_merge', []);
311 $this->assertEquals(0, count($result['values']['merged']));
312 $this->assertEquals(1, count($result['values']['skipped']));
313 $contact = $this->callAPISuccess('Contact', 'getsingle', ['id' => $contactID, 'return' => $customFieldLabel]);
314 $this->assertEquals(0, $contact[$customFieldLabel]);
318 * Using the api with check perms set to off, make sure custom data is merged.
320 * Test CRM-18674 date custom field handling.
322 public function testBatchMergeDateCustomFieldConflictAndNoCheckPerms() {
323 CRM_Core_Config
::singleton()->userPermissionClass
->permissions
= ['access CiviCRM', 'edit my contact'];
324 CRM_Core_DAO
::executeQuery("DELETE FROM civicrm_cache");
325 CRM_Utils_System
::flushCache();
326 $customFieldLabel = 'custom_' . $this->customFieldID
;
327 $contactID = $this->individualCreate([$customFieldLabel => '2012-11-03']);
328 $this->individualCreate([$customFieldLabel => '2013-11-03']);
329 $result = $this->callAPISuccess('Job', 'process_batch_merge', ['check_permissions' => 0]);
330 $this->assertEquals(0, count($result['values']['merged']));
331 $this->assertEquals(1, count($result['values']['skipped']));
332 $contact = $this->callAPISuccess('Contact', 'getsingle', ['id' => $contactID, 'return' => $customFieldLabel]);
333 $this->assertEquals('2012-11-03 00:00:00', $contact[$customFieldLabel]);
337 * Using the api with check perms set to off, make sure custom data is merged.
339 * Test CRM-18674 date custom field handling.
341 public function testBatchMergeDateCustomFieldNoConflictAndNoCheckPerms() {
342 CRM_Core_Config
::singleton()->userPermissionClass
->permissions
= ['access CiviCRM', 'edit my contact'];
343 CRM_Core_DAO
::executeQuery("DELETE FROM civicrm_cache");
344 CRM_Utils_System
::flushCache();
345 $customFieldLabel = 'custom_' . $this->customFieldID
;
346 $contactID = $this->individualCreate();
347 $this->individualCreate([$customFieldLabel => '2013-11-03']);
348 $result = $this->callAPISuccess('Job', 'process_batch_merge', ['check_permissions' => 0]);
349 $this->assertEquals(1, count($result['values']['merged']));
350 $this->assertEquals(0, count($result['values']['skipped']));
351 $contact = $this->callAPISuccess('Contact', 'getsingle', ['id' => $contactID, 'return' => $customFieldLabel]);
352 $this->assertEquals('2013-11-03 00:00:00', $contact[$customFieldLabel]);
356 * Using the api with check perms set to off, make sure custom data is merged.
358 * Test CRM-19113 custom data lost when permissions in play.
360 public function testBatchMergeIntCustomFieldNoConflictAndNoCheckPerms() {
361 CRM_Core_Config
::singleton()->userPermissionClass
->permissions
= ['access CiviCRM', 'edit my contact'];
362 CRM_Core_DAO
::executeQuery("DELETE FROM civicrm_cache");
363 CRM_Utils_System
::flushCache();
364 $customFieldLabel = 'custom_' . $this->customIntFieldID
;
365 $contactID = $this->individualCreate(['custom_' . $this->customBoolFieldID
=> 1]);
366 $this->individualCreate([$customFieldLabel => 1, 'custom_' . $this->customBoolFieldID
=> 1]);
367 $result = $this->callAPISuccess('Job', 'process_batch_merge', ['check_permissions' => 0]);
368 $this->assertEquals(1, count($result['values']['merged']));
369 $this->assertEquals(0, count($result['values']['skipped']));
370 $contact = $this->callAPISuccess('Contact', 'getsingle', [
372 'return' => [$customFieldLabel, 'custom_' . $this->customBoolFieldID
],
374 $this->assertEquals(1, $contact[$customFieldLabel]);
375 $this->assertEquals(1, $contact['custom_' . $this->customBoolFieldID
]);
379 * Check we get a conflict on the customs field when the data conflicts for booleans.
381 public function testBatchMergeCustomFieldConflicts() {
382 $this->individualCreate(['custom_' . $this->customBoolFieldID
=> 0]);
383 $this->individualCreate(['custom_' . $this->customBoolFieldID
=> 1]);
384 $result = $this->callAPISuccess('Job', 'process_batch_merge', []);
385 $this->assertEquals(0, count($result['values']['merged']));
386 $this->assertEquals(1, count($result['values']['skipped']));
390 * Check we get a conflict on the customs field when the data conflicts for booleans (reverse).
392 public function testBatchMergeCustomFieldConflictsReverse() {
393 $this->individualCreate(['custom_' . $this->customBoolFieldID
=> 1]);
394 $this->individualCreate(['custom_' . $this->customBoolFieldID
=> 0]);
395 $result = $this->callAPISuccess('Job', 'process_batch_merge', []);
396 $this->assertEquals(0, count($result['values']['merged']));
397 $this->assertEquals(1, count($result['values']['skipped']));
401 * Check we get a conflict on the customs field when the data conflicts for booleans (reverse).
403 * @throws \CRM_Core_Exception
405 public function testBatchMergeCustomFieldNoConflictsOneBlank() {
406 $this->individualCreate(['custom_' . $this->customBoolFieldID
=> 1]);
407 $this->individualCreate();
408 $result = $this->callAPISuccess('Job', 'process_batch_merge', []);
409 $this->assertCount(1, $result['values']['merged']);
410 $this->assertCount(0, $result['values']['skipped']);
414 * Check we get a conflict on the customs field when the data conflicts for booleans (reverse).
416 * @throws \CRM_Core_Exception
418 public function testBatchMergeCustomFieldNoConflictsOneBlankReverse() {
419 $contactID = $this->individualCreate();
420 $this->individualCreate(['custom_' . $this->customBoolFieldID
=> 1]);
421 $result = $this->callAPISuccess('Job', 'process_batch_merge', []);
422 $this->assertCount(1, $result['values']['merged']);
423 $this->assertCount(0, $result['values']['skipped']);
424 $this->assertEquals(1, $this->callAPISuccessGetValue('Contact', ['id' => $contactID, 'return' => 'custom_' . $this->customBoolFieldID
]));