--- /dev/null
+<?php
+/*
+ +--------------------------------------------------------------------+
+ | CiviCRM version 4.7 |
+ +--------------------------------------------------------------------+
+ | Copyright CiviCRM LLC (c) 2004-2016 |
+ +--------------------------------------------------------------------+
+ | This file is a part of CiviCRM. |
+ | |
+ | CiviCRM is free software; you can copy, modify, and distribute it |
+ | under the terms of the GNU Affero General Public License |
+ | Version 3, 19 November 2007 and the CiviCRM Licensing Exception. |
+ | |
+ | CiviCRM is distributed in the hope that it will be useful, but |
+ | WITHOUT ANY WARRANTY; without even the implied warranty of |
+ | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
+ | See the GNU Affero General Public License for more details. |
+ | |
+ | You should have received a copy of the GNU Affero General Public |
+ | License and the CiviCRM Licensing Exception along |
+ | with this program; if not, contact CiviCRM LLC |
+ | at info[AT]civicrm[DOT]org. If you have questions about the |
+ | GNU Affero General Public License or the licensing of CiviCRM, |
+ | see the CiviCRM license FAQ at http://civicrm.org/licensing |
+ +--------------------------------------------------------------------+
+ */
+
+/**
+ * File for the CiviCRM APIv3 job functions
+ *
+ * @package CiviCRM_APIv3
+ * @subpackage API_Job
+ *
+ * @copyright CiviCRM LLC (c) 2004-2016
+ * @version $Id: Job.php 30879 2010-11-22 15:45:55Z shot $
+ *
+ */
+
+/**
+ * Tests for job api where custom data is involved.
+ *
+ * Set up for custom data won't work with useTransaction so these are not
+ * compatible with the other job test class.
+ *
+ * @group headless
+ */
+class api_v3_JobTestCustomData extends CiviUnitTestCase {
+ protected $_apiversion = 3;
+
+ public $_entity = 'Job';
+ public $_params = array();
+
+ /**
+ * Custom group ID.
+ *
+ * @var int
+ */
+ public $customFieldID = NULL;
+
+ /**
+ * Custom Field ID.
+ *
+ * @var int
+ */
+ public $customGroupID = NULL;
+
+ public function setUp() {
+ parent::setUp();
+ $this->_params = array(
+ 'sequential' => 1,
+ 'name' => 'API_Test_Job',
+ 'description' => 'A long description written by hand in cursive',
+ 'run_frequency' => 'Daily',
+ 'api_entity' => 'ApiTestEntity',
+ 'api_action' => 'apitestaction',
+ 'parameters' => 'Semi-formal explanation of runtime job parameters',
+ 'is_active' => 1,
+ );
+ }
+
+ /**
+ * Cleanup after tests.
+ */
+ public function tearDown() {
+ $this->quickCleanup(array('civicrm_contact'), TRUE);
+ parent::tearDown();
+ }
+
+ /**
+ * Test the batch merge does not bork on custom date fields.
+ *
+ * Test CRM-18674 date custom field handling.
+ */
+ public function testBatchMergeDateCustomFieldHandling() {
+ $customGroup = $this->customGroupCreate();
+ $this->customGroupID = $customGroup['id'];
+ $customField = $this->customFieldCreate(array(
+ 'custom_group_id' => $this->customGroupID,
+ 'data_type' => 'Date',
+ 'html_type' => 'Select Date',
+ 'default_value' => '',
+ ));
+ $this->customFieldID = $customField['id'];
+ $customFieldLabel = 'custom_' . $this->customFieldID;
+ $contactID = $this->individualCreate();
+ $this->individualCreate(array($customFieldLabel => '2012-12-03'));
+ $result = $this->callAPISuccess('Job', 'process_batch_merge', array());
+ $this->assertEquals(1, count($result['values']['merged']));
+ $contact = $this->callAPISuccess('Contact', 'getsingle', array('id' => $contactID, 'return' => $customFieldLabel));
+ $this->assertEquals('2012-12-03 00:00:00', $contact[$customFieldLabel]);
+ }
+
+ /**
+ * Test the batch merge does not bork on custom date fields.
+ *
+ * Test CRM-18674 date custom field handling.
+ */
+ public function testBatchMergeDateCustomFieldHandlingIsView() {
+ $customGroup = $this->customGroupCreate();
+ $this->customGroupID = $customGroup['id'];
+ $customField = $this->customFieldCreate(array(
+ 'custom_group_id' => $this->customGroupID,
+ 'data_type' => 'Date',
+ 'html_type' => 'Select Date',
+ 'default_value' => '',
+ 'is_view' => 1,
+ ));
+ $this->customFieldID = $customField['id'];
+ $customFieldLabel = 'custom_' . $this->customFieldID;
+ $contactID = $this->individualCreate();
+ $this->individualCreate(array($customFieldLabel => '2012-11-03'));
+ $result = $this->callAPISuccess('Job', 'process_batch_merge', array());
+ $this->assertEquals(1, count($result['values']['merged']));
+ $contact = $this->callAPISuccess('Contact', 'getsingle', array('id' => $contactID, 'return' => $customFieldLabel));
+ $this->assertEquals('2012-11-03 00:00:00', $contact[$customFieldLabel]);
+ }
+
+ /**
+ * Check we get a conflict on the custom field.
+ */
+ public function testBatchMergeDateCustomFieldConflict() {
+ $customGroup = $this->customGroupCreate();
+ $this->customGroupID = $customGroup['id'];
+ $customField = $this->customFieldCreate(array(
+ 'custom_group_id' => $this->customGroupID,
+ 'data_type' => 'Date',
+ 'html_type' => 'Select Date',
+ 'default_value' => '',
+ ));
+ $this->customFieldID = $customField['id'];
+ $customFieldLabel = 'custom_' . $this->customFieldID;
+ $contactID = $this->individualCreate(array($customFieldLabel => '2012-11-03'));
+ $this->individualCreate(array($customFieldLabel => '2013-11-03'));
+ $result = $this->callAPISuccess('Job', 'process_batch_merge', array());
+ $this->assertEquals(0, count($result['values']['merged']));
+ $this->assertEquals(1, count($result['values']['skipped']));
+ $contact = $this->callAPISuccess('Contact', 'getsingle', array('id' => $contactID, 'return' => $customFieldLabel));
+ $this->assertEquals('2012-11-03 00:00:00', $contact[$customFieldLabel]);
+ }
+
+ /**
+ * Check we get a conflict on the custom field.
+ */
+ public function testBatchMergeDateCustomFieldNoConflict() {
+ $customGroup = $this->customGroupCreate();
+ $this->customGroupID = $customGroup['id'];
+ $customField = $this->customFieldCreate(array(
+ 'custom_group_id' => $this->customGroupID,
+ 'data_type' => 'Date',
+ 'html_type' => 'Select Date',
+ 'default_value' => '',
+ ));
+ $this->customFieldID = $customField['id'];
+ $customFieldLabel = 'custom_' . $this->customFieldID;
+ $contactID = $this->individualCreate(array($customFieldLabel => '2012-11-03'));
+ $this->individualCreate(array($customFieldLabel => '2012-11-03'));
+ $result = $this->callAPISuccess('Job', 'process_batch_merge', array());
+ $this->assertEquals(1, count($result['values']['merged']));
+ $this->assertEquals(0, count($result['values']['skipped']));
+ $contact = $this->callAPISuccess('Contact', 'getsingle', array('id' => $contactID, 'return' => $customFieldLabel));
+ $this->assertEquals('2012-11-03 00:00:00', $contact[$customFieldLabel]);
+ }
+
+ /**
+ * Using the api with check perms set to off, make sure custom data is merged.git
+ *
+ * Test CRM-18674 date custom field handling.
+ */
+ public function testBatchMergeDateCustomFieldConflictAndNoCheckPerms() {
+ CRM_Core_Config::singleton()->userPermissionClass->permissions = array('access CiviCRM', 'edit my contact');
+ $customGroup = $this->customGroupCreate();
+ $this->customGroupID = $customGroup['id'];
+ $customField = $this->customFieldCreate(array(
+ 'custom_group_id' => $this->customGroupID,
+ 'data_type' => 'Date',
+ 'html_type' => 'Select Date',
+ 'default_value' => '',
+ ));
+ $this->customFieldID = $customField['id'];
+ $customFieldLabel = 'custom_' . $this->customFieldID;
+ $contactID = $this->individualCreate(array($customFieldLabel => '2012-11-03'));
+ $this->individualCreate(array($customFieldLabel => '2013-11-03'));
+ $result = $this->callAPISuccess('Job', 'process_batch_merge', array('check_permissions' => 0));
+ $this->assertEquals(0, count($result['values']['merged']));
+ $this->assertEquals(1, count($result['values']['skipped']));
+ $contact = $this->callAPISuccess('Contact', 'getsingle', array('id' => $contactID, 'return' => $customFieldLabel));
+ $this->assertEquals('2012-11-03 00:00:00', $contact[$customFieldLabel]);
+ }
+
+ /**
+ * Get data for batch merge.
+ */
+ public function getMergeSets() {
+ $data = array(
+ array(
+ array(
+ 'mode' => 'safe',
+ 'contacts' => array(
+ array(
+ 'first_name' => 'Michael',
+ 'last_name' => 'Jackson',
+ 'email' => 'michael@neverland.com',
+ 'contact_type' => 'Individual',
+ 'contact_sub_type' => 'Student',
+ 'api.Address.create' => array(
+ 'street_address' => 'big house',
+ 'location_type_id' => 'Home',
+ ),
+ ),
+ array(
+ 'first_name' => 'Michael',
+ 'last_name' => 'Jackson',
+ 'email' => 'michael@neverland.com',
+ 'contact_type' => 'Individual',
+ 'contact_sub_type' => 'Student',
+ ),
+ ),
+ 'skipped' => 0,
+ 'merged' => 1,
+ 'expected' => array(
+ array(
+ 'first_name' => 'Michael',
+ 'last_name' => 'Jackson',
+ 'email' => 'michael@neverland.com',
+ 'contact_type' => 'Individual',
+ ),
+ ),
+ ),
+ ),
+ array(
+ array(
+ 'mode' => 'safe',
+ 'contacts' => array(
+ array(
+ 'first_name' => 'Michael',
+ 'last_name' => 'Jackson',
+ 'email' => 'michael@neverland.com',
+ 'contact_type' => 'Individual',
+ 'contact_sub_type' => 'Student',
+ 'api.Address.create' => array(
+ 'street_address' => 'big house',
+ 'location_type_id' => 'Home',
+ ),
+ ),
+ array(
+ 'first_name' => 'Michael',
+ 'last_name' => 'Jackson',
+ 'email' => 'michael@neverland.com',
+ 'contact_type' => 'Individual',
+ 'contact_sub_type' => 'Student',
+ 'api.Address.create' => array(
+ 'street_address' => 'bigger house',
+ 'location_type_id' => 'Home',
+ ),
+ ),
+ ),
+ 'skipped' => 1,
+ 'merged' => 0,
+ 'expected' => array(
+ array(
+ 'first_name' => 'Michael',
+ 'last_name' => 'Jackson',
+ 'email' => 'michael@neverland.com',
+ 'contact_type' => 'Individual',
+ 'street_address' => 'big house',
+ ),
+ array(
+ 'first_name' => 'Michael',
+ 'last_name' => 'Jackson',
+ 'email' => 'michael@neverland.com',
+ 'contact_type' => 'Individual',
+ 'street_address' => 'bigger house',
+ ),
+ ),
+ ),
+ ),
+ array(
+ array(
+ 'mode' => 'aggressive',
+ 'contacts' => array(
+ array(
+ 'first_name' => 'Michael',
+ 'last_name' => 'Jackson',
+ 'email' => 'michael@neverland.com',
+ 'contact_type' => 'Individual',
+ 'contact_sub_type' => 'Student',
+ 'api.Address.create' => array(
+ 'street_address' => 'big house',
+ 'location_type_id' => 'Home',
+ ),
+ ),
+ array(
+ 'first_name' => 'Michael',
+ 'last_name' => 'Jackson',
+ 'email' => 'michael@neverland.com',
+ 'contact_type' => 'Individual',
+ 'contact_sub_type' => 'Student',
+ 'api.Address.create' => array(
+ 'street_address' => 'bigger house',
+ 'location_type_id' => 'Home',
+ ),
+ ),
+ ),
+ 'skipped' => 0,
+ 'merged' => 1,
+ 'expected' => array(
+ array(
+ 'first_name' => 'Michael',
+ 'last_name' => 'Jackson',
+ 'email' => 'michael@neverland.com',
+ 'contact_type' => 'Individual',
+ 'street_address' => 'big house',
+ ),
+ ),
+ ),
+ ),
+ );
+ return $data;
+ }
+
+ /**
+ * @param $op
+ * @param string $objectName
+ * @param int $id
+ * @param array $params
+ */
+ public function hookPreRelationship($op, $objectName, $id, &$params) {
+ if ($op == 'delete') {
+ return;
+ }
+ if ($params['is_active']) {
+ $params['description'] = 'Hooked';
+ }
+ else {
+ $params['description'] = 'Go Go you good thing';
+ }
+ }
+
+}