3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.7 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2018 |
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
10 | CiviCRM is free software; you can copy, modify, and distribute it |
11 | under the terms of the GNU Affero General Public License |
12 | Version 3, 19 November 2007 and the CiviCRM Licensing Exception. |
14 | CiviCRM is distributed in the hope that it will be useful, but |
15 | WITHOUT ANY WARRANTY; without even the implied warranty of |
16 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
17 | See the GNU Affero General Public License for more details. |
19 | You should have received a copy of the GNU Affero General Public |
20 | License and the CiviCRM Licensing Exception along |
21 | with this program; if not, contact CiviCRM LLC |
22 | at info[AT]civicrm[DOT]org. If you have questions about the |
23 | GNU Affero General Public License or the licensing of CiviCRM, |
24 | see the CiviCRM license FAQ at http://civicrm.org/licensing |
25 +--------------------------------------------------------------------+
29 * File for the CiviCRM APIv3 job functions
31 * @package CiviCRM_APIv3
34 * @copyright CiviCRM LLC (c) 2004-2018
35 * @version $Id: Job.php 30879 2010-11-22 15:45:55Z shot $
40 * Tests for job api where custom data is involved.
42 * Set up for custom data won't work with useTransaction so these are not
43 * compatible with the other job test class.
47 class api_v3_JobTestCustomDataTest
extends CiviUnitTestCase
{
48 protected $_apiversion = 3;
50 public $_entity = 'Job';
57 public $customFieldID = NULL;
60 * ID of a custom field of integer type.
64 public $customIntFieldID = NULL;
67 * ID of a custom field of integer type.
71 public $customBoolFieldID = NULL;
74 * ID of a custom field of integer type.
78 public $customStringCheckboxID = NULL;
85 public $customGroupID = NULL;
87 public function setUp() {
89 $customGroup = $this->customGroupCreate();
90 $this->customGroupID
= $customGroup['id'];
91 $customField = $this->customFieldCreate(array(
92 'custom_group_id' => $this->customGroupID
,
93 'data_type' => 'Date',
94 'html_type' => 'Select Date',
95 'default_value' => '',
97 $this->customFieldID
= $customField['id'];
98 $customField = $this->customFieldCreate(array(
99 'custom_group_id' => $this->customGroupID
,
100 'data_type' => 'Integer',
101 'html_type' => 'Text',
102 'default_value' => '',
103 'label' => 'Int Field',
105 $this->customIntFieldID
= $customField['id'];
106 $customField = $this->customFieldCreate(array(
107 'custom_group_id' => $this->customGroupID
,
108 'data_type' => 'Boolean',
109 'html_type' => 'Radio',
110 'default_value' => '',
111 'label' => 'Radio Field',
113 $this->customBoolFieldID
= $customField['id'];
114 $customField = $this->customFieldCreate(array(
115 'custom_group_id' => $this->customGroupID
,
116 'data_type' => 'String',
117 'html_type' => 'CheckBox',
118 'default_value' => NULL,
119 'label' => 'checkbox Field',
120 'option_values' => array('black' => 'black', 'white' => 'white'),
122 $this->customStringCheckboxID
= $customField['id'];
126 * Cleanup after tests.
128 public function tearDown() {
129 $this->quickCleanup(array('civicrm_contact'), TRUE);
134 * Test the batch merge does not bork on custom date fields.
136 * @dataProvider getCheckboxData
138 * Test CRM-19074 checkbox field handling.
140 * Given a custom field with 2 checkboxes (black & white) possible values are:
141 * 1) SEPARATOR + black + SEPARATOR
142 * 2) SEPARATOR + white + SEPARATOR
143 * 3) SEPARATOR + black + SEPARATOR + white + SEPARATOR
144 * 3) '' (ie empty string means both set to 0)
145 * 4) NULL (ie not set)
146 * - in safe mode NULL is not a conflict with any option but the other
147 * combos are a conflict.
149 public function testBatchMergeCheckboxCustomFieldHandling($dataSet) {
150 $customFieldLabel = 'custom_' . $this->customStringCheckboxID
;
151 $contact1Params = is_array($dataSet['contacts'][0]) ?
array($customFieldLabel => $dataSet['contacts'][0]) : array();
152 $contact2Params = is_array($dataSet['contacts'][1]) ?
array($customFieldLabel => $dataSet['contacts'][1]) : array();
153 $contactID = $this->individualCreate($contact1Params);
154 $this->individualCreate($contact2Params);
155 $result = $this->callAPISuccess('Job', 'process_batch_merge', array('mode' => $dataSet['mode']));
156 $this->assertEquals($dataSet['merged'], count($result['values']['merged']));
157 $this->assertEquals($dataSet['skipped'], count($result['values']['skipped']));
158 $contact = $this->callAPISuccess('Contact', 'getsingle', array('id' => $contactID, 'return' => $customFieldLabel));
159 $this->assertEquals($dataSet['expected'], $contact[$customFieldLabel]);
163 * Get the various data combinations for a checkbox field.
167 public function getCheckboxData() {
170 'null_merges_with_set' => array(
178 'expected' => array('black'),
182 'null_merges_with_set_reverse' => array(
190 'expected' => array('black'),
195 'empty_conflicts_with_set' => array(
203 'expected' => array('white'),
207 'empty_conflicts_with_set' => array(
208 'mode' => 'aggressive',
215 'expected' => array('white'),
222 * Test the batch merge does not bork on custom date fields.
224 * Test CRM-18674 date custom field handling.
226 public function testBatchMergeDateCustomFieldHandling() {
227 $customFieldLabel = 'custom_' . $this->customFieldID
;
228 $contactID = $this->individualCreate();
229 $this->individualCreate(array($customFieldLabel => '2012-12-03'));
230 $result = $this->callAPISuccess('Job', 'process_batch_merge', array());
231 $this->assertEquals(1, count($result['values']['merged']));
232 $contact = $this->callAPISuccess('Contact', 'getsingle', array('id' => $contactID, 'return' => $customFieldLabel));
233 $this->assertEquals('2012-12-03 00:00:00', $contact[$customFieldLabel]);
237 * Test the batch merge does not bork on custom date fields.
239 * Test CRM-18674 date custom field handling.
241 public function testBatchMergeDateCustomFieldHandlingIsView() {
242 $this->customFieldCreate(array(
243 'label' => 'OnlyView',
244 'custom_group_id' => $this->customGroupID
,
247 $customFieldLabel = 'custom_' . $this->customFieldID
;
248 $contactID = $this->individualCreate();
249 $this->individualCreate(array($customFieldLabel => '2012-11-03'));
250 $result = $this->callAPISuccess('Job', 'process_batch_merge', array());
251 $this->assertEquals(1, count($result['values']['merged']));
252 $contact = $this->callAPISuccess('Contact', 'getsingle', array('id' => $contactID, 'return' => $customFieldLabel));
253 $this->assertEquals('2012-11-03 00:00:00', $contact[$customFieldLabel]);
257 * Check we get a conflict on the custom field.
259 public function testBatchMergeDateCustomFieldConflict() {
260 $customFieldLabel = 'custom_' . $this->customFieldID
;
261 $contactID = $this->individualCreate(array($customFieldLabel => '2012-11-03'));
262 $this->individualCreate(array($customFieldLabel => '2013-11-03'));
263 $result = $this->callAPISuccess('Job', 'process_batch_merge', array());
264 $this->assertEquals(0, count($result['values']['merged']));
265 $this->assertEquals(1, count($result['values']['skipped']));
266 $contact = $this->callAPISuccess('Contact', 'getsingle', array('id' => $contactID, 'return' => $customFieldLabel));
267 $this->assertEquals('2012-11-03 00:00:00', $contact[$customFieldLabel]);
271 * Check we get a conflict on the custom field.
273 public function testBatchMergeDateCustomFieldNoConflict() {
274 $customFieldLabel = 'custom_' . $this->customFieldID
;
275 $contactID = $this->individualCreate(array($customFieldLabel => '2012-11-03'));
276 $this->individualCreate(array($customFieldLabel => '2012-11-03'));
277 $result = $this->callAPISuccess('Job', 'process_batch_merge', array());
278 $this->assertEquals(1, count($result['values']['merged']));
279 $this->assertEquals(0, count($result['values']['skipped']));
280 $contact = $this->callAPISuccess('Contact', 'getsingle', array('id' => $contactID, 'return' => $customFieldLabel));
281 $this->assertEquals('2012-11-03 00:00:00', $contact[$customFieldLabel]);
285 * Check we get a no conflict on the custom field & integer merges.
287 public function testBatchMergeIntCustomFieldNoConflict() {
288 $customFieldLabel = 'custom_' . $this->customIntFieldID
;
289 $contactID = $this->individualCreate(array());
290 $this->individualCreate(array($customFieldLabel => 20));
291 $result = $this->callAPISuccess('Job', 'process_batch_merge', array());
292 $this->assertEquals(1, count($result['values']['merged']));
293 $this->assertEquals(0, count($result['values']['skipped']));
294 $contact = $this->callAPISuccess('Contact', 'getsingle', array('id' => $contactID, 'return' => $customFieldLabel));
295 $this->assertEquals(20, $contact[$customFieldLabel]);
299 * Check we get a conflict on the integer custom field.
301 public function testBatchMergeIntCustomFieldConflict() {
302 $customFieldLabel = 'custom_' . $this->customIntFieldID
;
303 $contactID = $this->individualCreate(array($customFieldLabel => 20));
304 $this->individualCreate(array($customFieldLabel => 1));
305 $result = $this->callAPISuccess('Job', 'process_batch_merge', array());
306 $this->assertEquals(0, count($result['values']['merged']));
307 $this->assertEquals(1, count($result['values']['skipped']));
308 $contact = $this->callAPISuccess('Contact', 'getsingle', array('id' => $contactID, 'return' => $customFieldLabel));
309 $this->assertEquals(20, $contact[$customFieldLabel]);
313 * Check we get a conflict on the integer custom field when the conflicted field is 0.
315 public function testBatchMergeIntCustomFieldConflictZero() {
316 $customFieldLabel = 'custom_' . $this->customIntFieldID
;
317 $contactID = $this->individualCreate(array($customFieldLabel => 0));
318 $this->individualCreate(array($customFieldLabel => 20));
319 $result = $this->callAPISuccess('Job', 'process_batch_merge', array());
320 $this->assertEquals(0, count($result['values']['merged']));
321 $this->assertEquals(1, count($result['values']['skipped']));
322 $contact = $this->callAPISuccess('Contact', 'getsingle', array('id' => $contactID, 'return' => $customFieldLabel));
323 $this->assertEquals(0, $contact[$customFieldLabel]);
327 * Using the api with check perms set to off, make sure custom data is merged.
329 * Test CRM-18674 date custom field handling.
331 public function testBatchMergeDateCustomFieldConflictAndNoCheckPerms() {
332 CRM_Core_Config
::singleton()->userPermissionClass
->permissions
= array('access CiviCRM', 'edit my contact');
333 CRM_Core_DAO
::executeQuery("DELETE FROM civicrm_cache");
334 CRM_Utils_System
::flushCache();
335 $customFieldLabel = 'custom_' . $this->customFieldID
;
336 $contactID = $this->individualCreate(array($customFieldLabel => '2012-11-03'));
337 $this->individualCreate(array($customFieldLabel => '2013-11-03'));
338 $result = $this->callAPISuccess('Job', 'process_batch_merge', array('check_permissions' => 0));
339 $this->assertEquals(0, count($result['values']['merged']));
340 $this->assertEquals(1, count($result['values']['skipped']));
341 $contact = $this->callAPISuccess('Contact', 'getsingle', array('id' => $contactID, 'return' => $customFieldLabel));
342 $this->assertEquals('2012-11-03 00:00:00', $contact[$customFieldLabel]);
346 * Using the api with check perms set to off, make sure custom data is merged.
348 * Test CRM-18674 date custom field handling.
350 public function testBatchMergeDateCustomFieldNoConflictAndNoCheckPerms() {
351 CRM_Core_Config
::singleton()->userPermissionClass
->permissions
= array('access CiviCRM', 'edit my contact');
352 CRM_Core_DAO
::executeQuery("DELETE FROM civicrm_cache");
353 CRM_Utils_System
::flushCache();
354 $customFieldLabel = 'custom_' . $this->customFieldID
;
355 $contactID = $this->individualCreate();
356 $this->individualCreate(array($customFieldLabel => '2013-11-03'));
357 $result = $this->callAPISuccess('Job', 'process_batch_merge', array('check_permissions' => 0));
358 $this->assertEquals(1, count($result['values']['merged']));
359 $this->assertEquals(0, count($result['values']['skipped']));
360 $contact = $this->callAPISuccess('Contact', 'getsingle', array('id' => $contactID, 'return' => $customFieldLabel));
361 $this->assertEquals('2013-11-03 00:00:00', $contact[$customFieldLabel]);
365 * Using the api with check perms set to off, make sure custom data is merged.
367 * Test CRM-19113 custom data lost when permissions in play.
369 public function testBatchMergeIntCustomFieldNoConflictAndNoCheckPerms() {
370 CRM_Core_Config
::singleton()->userPermissionClass
->permissions
= array('access CiviCRM', 'edit my contact');
371 CRM_Core_DAO
::executeQuery("DELETE FROM civicrm_cache");
372 CRM_Utils_System
::flushCache();
373 $customFieldLabel = 'custom_' . $this->customIntFieldID
;
374 $contactID = $this->individualCreate(array('custom_' . $this->customBoolFieldID
=> 1));
375 $this->individualCreate(array($customFieldLabel => 1, 'custom_' . $this->customBoolFieldID
=> 1));
376 $result = $this->callAPISuccess('Job', 'process_batch_merge', array('check_permissions' => 0));
377 $this->assertEquals(1, count($result['values']['merged']));
378 $this->assertEquals(0, count($result['values']['skipped']));
379 $contact = $this->callAPISuccess('Contact', 'getsingle', array(
381 'return' => array($customFieldLabel, 'custom_' . $this->customBoolFieldID
),
383 $this->assertEquals(1, $contact[$customFieldLabel]);
384 $this->assertEquals(1, $contact['custom_' . $this->customBoolFieldID
]);
388 * Check we get a conflict on the customs field when the data conflicts for booleans.
390 public function testBatchMergeCustomFieldConflicts() {
391 $this->individualCreate(array('custom_' . $this->customBoolFieldID
=> 0));
392 $this->individualCreate(array('custom_' . $this->customBoolFieldID
=> 1));
393 $result = $this->callAPISuccess('Job', 'process_batch_merge', array());
394 $this->assertEquals(0, count($result['values']['merged']));
395 $this->assertEquals(1, count($result['values']['skipped']));
399 * Check we get a conflict on the customs field when the data conflicts for booleans (reverse).
401 public function testBatchMergeCustomFieldConflictsReverse() {
402 $this->individualCreate(array('custom_' . $this->customBoolFieldID
=> 1));
403 $this->individualCreate(array('custom_' . $this->customBoolFieldID
=> 0));
404 $result = $this->callAPISuccess('Job', 'process_batch_merge', array());
405 $this->assertEquals(0, count($result['values']['merged']));
406 $this->assertEquals(1, count($result['values']['skipped']));
410 * Check we get a conflict on the customs field when the data conflicts for booleans (reverse).
412 public function testBatchMergeCustomFieldConflictsOneBlank() {
413 $this->individualCreate(array('custom_' . $this->customBoolFieldID
=> 1));
414 $this->individualCreate();
415 $result = $this->callAPISuccess('Job', 'process_batch_merge', array());
416 $this->assertEquals(1, count($result['values']['merged']));
417 $this->assertEquals(0, count($result['values']['skipped']));
421 * Check we get a conflict on the customs field when the data conflicts for booleans (reverse).
423 public function testBatchMergeCustomFieldConflictsOneBlankReverse() {
424 $this->individualCreate();
425 $this->individualCreate(array('custom_' . $this->customBoolFieldID
=> 1));
426 $result = $this->callAPISuccess('Job', 'process_batch_merge', array());
427 $this->assertEquals(1, count($result['values']['merged']));
428 $this->assertEquals(0, count($result['values']['skipped']));