Add Mapping and mappingfield API
authorTim Mallezie <tim.mallezie@chiro.be>
Mon, 29 Jun 2015 10:36:32 +0000 (12:36 +0200)
committerSudha Bisht <sudha.bisht@webaccessglobal.com>
Thu, 10 Dec 2015 08:12:36 +0000 (13:42 +0530)
api/v3/Mapping.php [new file with mode: 0644]
api/v3/MappingField.php [new file with mode: 0644]
tests/phpunit/CiviTest/CiviUnitTestCase.php
tests/phpunit/api/v3/MappingFieldTest.php [new file with mode: 0644]
tests/phpunit/api/v3/MappingTest.php [new file with mode: 0644]

diff --git a/api/v3/Mapping.php b/api/v3/Mapping.php
new file mode 100644 (file)
index 0000000..bce8eaa
--- /dev/null
@@ -0,0 +1,69 @@
+<?php
+
+/*
+  +--------------------------------------------------------------------+
+  | CiviCRM version 4.6                                                |
+  +--------------------------------------------------------------------+
+  | Copyright CiviCRM LLC (c) 2004-2015                                |
+  +--------------------------------------------------------------------+
+  | 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        |
+  +--------------------------------------------------------------------+
+ */
+
+/**
+ * This api exposes CiviCRM Mapping records.
+ *
+ * @package CiviCRM_APIv3
+ */
+
+/**
+ * Add a Mapping.
+ *
+ * @param array $params
+ *
+ * @return array
+ */
+function civicrm_api3_mapping_create($params) {
+  return _civicrm_api3_basic_create(_civicrm_api3_get_BAO(__FUNCTION__), $params);
+}
+
+/**
+ * Deletes an existing Mapping.
+ *
+ * @param array $params
+ *
+ * @return array
+ *   API result Array
+ */
+function civicrm_api3_mapping_delete($params) {
+  return _civicrm_api3_basic_delete(_civicrm_api3_get_BAO(__FUNCTION__), $params);
+}
+
+/**
+ * Retrieve one or more Mappings.
+ *
+ * @param array $params
+ *   An associative array of name/value pairs.
+ *
+ * @return array
+ *   details of found Mappings
+ */
+function civicrm_api3_mapping_get($params) {
+  return _civicrm_api3_basic_get(_civicrm_api3_get_BAO(__FUNCTION__), $params);
+}
diff --git a/api/v3/MappingField.php b/api/v3/MappingField.php
new file mode 100644 (file)
index 0000000..50a88f4
--- /dev/null
@@ -0,0 +1,81 @@
+<?php
+
+/*
+  +--------------------------------------------------------------------+
+  | CiviCRM version 4.6                                                |
+  +--------------------------------------------------------------------+
+  | Copyright CiviCRM LLC (c) 2004-2015                                |
+  +--------------------------------------------------------------------+
+  | 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        |
+  +--------------------------------------------------------------------+
+ */
+
+/**
+ * This api exposes CiviCRM MappingField records.
+ *
+ * @package CiviCRM_APIv3
+ */
+
+/**
+ * Add a Mapping Field.
+ *
+ * @param array $params
+ *
+ * @return array
+ */
+function civicrm_api3_mapping_field_create($params) {
+  return _civicrm_api3_basic_create(_civicrm_api3_get_BAO(__FUNCTION__), $params);
+}
+
+/**
+ * Adjust Metadata for Create action.
+ *
+ * The metadata is used for setting defaults, documentation & validation.
+ *
+ * @param array $params
+ *   Array of parameters determined by getfields.
+ */
+function _civicrm_api3_mapping_field_create_spec(&$params) {
+  $params['mapping_id']['api.required'] = 1;
+}
+
+/**
+ * Deletes an existing Mapping Field.
+ *
+ * @param array $params
+ *
+ * @return array
+ *   API result Array
+ */
+function civicrm_api3_mapping_field_delete($params) {
+  return _civicrm_api3_basic_delete(_civicrm_api3_get_BAO(__FUNCTION__), $params);
+}
+
+/**
+ * Retrieve one or more Mapping Fields.
+ *
+ * @param array $params
+ *   An associative array of name/value pairs.
+ *
+ * @return array
+ *   details of found Mapping Fields
+ */
+function civicrm_api3_mapping_field_get($params) {
+  return _civicrm_api3_basic_get(_civicrm_api3_get_BAO(__FUNCTION__), $params);
+}
index 1400c9bdffd93d060115c7c1c90b3687ef5dae6f..d2c47e953c2bedaea4516b33ed3c95baa8236ec1 100755 (executable)
@@ -1868,6 +1868,43 @@ class CiviUnitTestCase extends PHPUnit_Extensions_Database_TestCase {
     $locationType->delete();
   }
 
+  /**
+   * Add a Mapping.
+   *
+   * @param array $params
+   * @return CRM_Core_DAO_Mapping
+   *   Mapping id of created mapping
+   */
+  public function mappingCreate($params = NULL) {
+    if ($params === NULL) {
+      $params = array(
+        'name' => 'Mapping name',
+        'description' => 'Mapping description',
+        // 'Export Contact' mapping.
+        'mapping_type_id' => 7,
+      );
+    }
+
+    $mapping = new CRM_Core_DAO_Mapping();
+    $mapping->copyValues($params);
+    $mapping->save();
+    // clear getfields cache
+    CRM_Core_PseudoConstant::flush();
+    $this->callAPISuccess('mapping', 'getfields', array('version' => 3, 'cache_clear' => 1));
+    return $mapping;
+  }
+
+  /**
+   * Delete a Mapping
+   *
+   * @param int $mappingId
+   */
+  public function mappingDelete($mappingId) {
+    $mapping = new CRM_Core_DAO_Mapping();
+    $mapping->id = $mappingId;
+    $mapping->delete();
+  }
+
   /**
    * Add a Group.
    *
diff --git a/tests/phpunit/api/v3/MappingFieldTest.php b/tests/phpunit/api/v3/MappingFieldTest.php
new file mode 100644 (file)
index 0000000..b8adf50
--- /dev/null
@@ -0,0 +1,92 @@
+<?php
+/*
+ +--------------------------------------------------------------------+
+ | CiviCRM version 4.6                                                |
+ +--------------------------------------------------------------------+
+ | Copyright CiviCRM LLC (c) 2004-2015                                |
+ +--------------------------------------------------------------------+
+ | 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        |
+ +--------------------------------------------------------------------+
+ */
+
+require_once 'CiviTest/CiviUnitTestCase.php';
+
+
+/**
+ *  Test APIv3 civicrm_mapping_field_* functions
+ *
+ * @package CiviCRM_APIv3
+ * @subpackage API_Contact
+ */
+class api_v3_MappingFieldTest extends CiviUnitTestCase {
+
+  protected $_apiversion = 3;
+  protected $params;
+  protected $id;
+  protected $_entity;
+
+  public $DBResetRequired = FALSE;
+
+  public function setUp() {
+    parent::setUp();
+    $this->useTransaction(TRUE);
+
+    $this->_entity = 'mapping_field';
+    $this->_mappingID = $this->mappingCreate();
+    $this->params = array(
+      'mapping_id' => $this->_mappingID,
+      'name' => 'last_name',
+      'contact_type' => 'Individual',
+      'column_number' => 2,
+      'grouping' => 1,
+    );
+  }
+
+  public function testCreateMappingField() {
+    $result = $this->callAPIAndDocument($this->_entity, 'create', $this->params, __FUNCTION__, __FILE__);
+    $this->assertEquals(1, $result['count']);
+    $this->getAndCheck($this->params, $result['id'], $this->_entity);
+    $this->assertNotNull($result['values'][$result['id']]['id']);
+  }
+
+  public function testGetMappingField() {
+    $result = $this->callAPISuccess($this->_entity, 'create', $this->params);
+    $result = $this->callAPIAndDocument($this->_entity, 'get', $this->params, __FUNCTION__, __FILE__);
+    $this->assertEquals(1, $result['count']);
+    $this->assertNotNull($result['values'][$result['id']]['id']);
+    $this->callAPISuccess($this->_entity, 'delete', array('id' => $result['id']));
+  }
+
+  public function testDeleteMappingField() {
+    $result = $this->callAPISuccess($this->_entity, 'create', $this->params);
+    $deleteParams = array('id' => $result['id']);
+    $result = $this->callAPIAndDocument($this->_entity, 'delete', $deleteParams, __FUNCTION__, __FILE__);
+    $checkDeleted = $this->callAPISuccess($this->_entity, 'get', array());
+    $this->assertEquals(0, $checkDeleted['count']);
+  }
+
+  public function testDeleteMappingFieldInvalid() {
+    $result = $this->callAPISuccess($this->_entity, 'create', $this->params);
+    $deleteParams = array('id' => 600);
+    $result = $this->callAPIFailure($this->_entity, 'delete', $deleteParams);
+    $checkDeleted = $this->callAPISuccess($this->_entity, 'get', array());
+    $this->assertEquals(1, $checkDeleted['count']);
+  }
+
+}
diff --git a/tests/phpunit/api/v3/MappingTest.php b/tests/phpunit/api/v3/MappingTest.php
new file mode 100644 (file)
index 0000000..f1865fb
--- /dev/null
@@ -0,0 +1,90 @@
+<?php
+/*
+ +--------------------------------------------------------------------+
+ | CiviCRM version 4.6                                                |
+ +--------------------------------------------------------------------+
+ | Copyright CiviCRM LLC (c) 2004-2015                                |
+ +--------------------------------------------------------------------+
+ | 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        |
+ +--------------------------------------------------------------------+
+ */
+
+require_once 'CiviTest/CiviUnitTestCase.php';
+
+
+/**
+ *  Test APIv3 civicrm_mapping_* functions
+ *
+ * @package CiviCRM_APIv3
+ * @subpackage API_Contact
+ */
+class api_v3_MappingTest extends CiviUnitTestCase {
+
+  protected $_apiversion = 3;
+  protected $params;
+  protected $id;
+  protected $_entity;
+
+  public $DBResetRequired = FALSE;
+
+  public function setUp() {
+    parent::setUp();
+    $this->useTransaction(TRUE);
+
+    $this->_entity = 'mapping';
+    $this->params = array(
+      'name' => 'Mapping name',
+      'description' => 'Mapping description',
+      // 'Export Contact' mapping.
+      'mapping_type_id' => 7,
+    );
+  }
+
+  public function testCreateMapping() {
+    $result = $this->callAPIAndDocument($this->_entity, 'create', $this->params, __FUNCTION__, __FILE__);
+    $this->assertEquals(1, $result['count']);
+    $this->getAndCheck($this->params, $result['id'], $this->_entity);
+    $this->assertNotNull($result['values'][$result['id']]['id']);
+  }
+
+  public function testGetMapping() {
+    $result = $this->callAPISuccess($this->_entity, 'create', $this->params);
+    $result = $this->callAPIAndDocument($this->_entity, 'get', $this->params, __FUNCTION__, __FILE__);
+    $this->assertEquals(1, $result['count']);
+    $this->assertNotNull($result['values'][$result['id']]['id']);
+    $this->callAPISuccess($this->_entity, 'delete', array('id' => $result['id']));
+  }
+
+  public function testDeleteMapping() {
+    $result = $this->callAPISuccess($this->_entity, 'create', $this->params);
+    $deleteParams = array('id' => $result['id']);
+    $result = $this->callAPIAndDocument($this->_entity, 'delete', $deleteParams, __FUNCTION__, __FILE__);
+    $checkDeleted = $this->callAPISuccess($this->_entity, 'get', array());
+    $this->assertEquals(0, $checkDeleted['count']);
+  }
+
+  public function testDeleteMappingInvalid() {
+    $result = $this->callAPISuccess($this->_entity, 'create', $this->params);
+    $deleteParams = array('id' => 600);
+    $result = $this->callAPIFailure($this->_entity, 'delete', $deleteParams);
+    $checkDeleted = $this->callAPISuccess($this->_entity, 'get', array());
+    $this->assertEquals(1, $checkDeleted['count']);
+  }
+
+}