CRM-20759 add e-notice fix and unit tests for saving mappings
authoreileen <emcnaughton@wikimedia.org>
Wed, 28 Jun 2017 00:32:16 +0000 (12:32 +1200)
committereileen <emcnaughton@wikimedia.org>
Mon, 3 Jul 2017 00:03:54 +0000 (12:03 +1200)
CRM/Contact/Import/Form/MapField.php
CRM/Contact/Import/Parser/Contact.php
tests/phpunit/CRM/Contact/Import/Form/MapFieldTest.php

index 3d742937929a77a64a18c550ae2aa87ab1d8c8c5..5f265636f582c2755770525413c02afbd78047da 100644 (file)
@@ -876,15 +876,11 @@ class CRM_Contact_Import_Form_MapField extends CRM_Import_Form_MapField {
       $mappingParams = array(
         'name' => $params['saveMappingName'],
         'description' => $params['saveMappingDesc'],
-        'mapping_type_id' => CRM_Core_OptionGroup::getValue('mapping_type',
-          'Import Contact',
-          'name'
-        ),
+        'mapping_type_id' => 'Import Contact',
       );
 
-      $saveMapping = CRM_Core_BAO_Mapping::add($mappingParams);
+      $saveMapping = civicrm_api3('Mapping', 'create', $mappingParams);
 
-      $locationTypes = CRM_Core_PseudoConstant::get('CRM_Core_DAO_Address', 'location_type_id');
       $contactType = $this->get('contactType');
       switch ($contactType) {
         case CRM_Import_Parser::CONTACT_INDIVIDUAL:
@@ -901,7 +897,7 @@ class CRM_Contact_Import_Form_MapField extends CRM_Import_Form_MapField {
 
       for ($i = 0; $i < $this->_columnCount; $i++) {
         $saveMappingFields = new CRM_Core_DAO_MappingField();
-        $saveMappingFields->mapping_id = $saveMapping->id;
+        $saveMappingFields->mapping_id = $saveMapping['id'];
         $saveMappingFields->contact_type = $cType;
         $saveMappingFields->column_number = $i;
 
@@ -930,7 +926,7 @@ class CRM_Contact_Import_Form_MapField extends CRM_Import_Form_MapField {
         }
         else {
           $saveMappingFields->name = $mapper[$i];
-          $location_id = array_keys($locationTypes, $locations[$i]);
+          $locationTypeID = $parserParameters['mapperLocType'][$i];
           // to get phoneType id and provider id separately
           // before saving mappingFields of phone and IM, CRM-3140
           if (CRM_Utils_Array::value('0', $mapperKeys[$i]) == 'url') {
@@ -943,7 +939,7 @@ class CRM_Contact_Import_Form_MapField extends CRM_Import_Form_MapField {
             elseif (CRM_Utils_Array::value('0', $mapperKeys[$i]) == 'im') {
               $saveMappingFields->im_provider_id = isset($mapperKeys[$i][2]) ? $mapperKeys[$i][2] : NULL;
             }
-            $saveMappingFields->location_type_id = isset($location_id[0]) ? $location_id[0] : NULL;
+            $saveMappingFields->location_type_id = is_numeric($locationTypeID) ? $locationTypeID : NULL;
           }
           $saveMappingFields->relationship_type_id = NULL;
         }
index f5a96c0945379c53b0bed18c2ba4caf3630b19c4..dfa2b32930ef7766de15456c5c7bcb57a5e5d02d 100644 (file)
@@ -2278,14 +2278,9 @@ class CRM_Contact_Import_Parser_Contact extends CRM_Contact_Import_Parser {
     //      Custom
 
     // Cache the various object fields
-    static $fields = NULL;
-
-    if ($fields == NULL) {
-      $fields = array();
-    }
+    static $fields = array();
 
     // first add core contact values since for other Civi modules they are not added
-    require_once 'CRM/Contact/BAO/Contact.php';
     $contactFields = CRM_Contact_DAO_Contact::fields();
     _civicrm_api3_store_values($contactFields, $values, $params);
 
index d0a438c8ad43af1862d8e4c307fdf54214220008..b9f1e8736169de4d5feba7d7120d99e2b88d311b 100644 (file)
@@ -42,14 +42,104 @@ class CRM_Contact_Import_Form_MapFieldTest extends CiviUnitTestCase {
    * Test the form loads without error / notice and mappings are assigned.
    *
    * (Added in conjunction with fixed noting on mapping assignment).
+   *
+   * @dataProvider getSubmitData
+   *
+   * @param array $params
+   * @param array $mapper
+   * @param array $expecteds
    */
-  public function testSubmit() {
+  public function testSubmit($params, $mapper, $expecteds = array()) {
+    CRM_Core_DAO::executeQuery("CREATE TABLE IF NOT EXISTS civicrm_import_job_xxx (`nada` text, `first_name` text, `last_name` text, `address` text) ENGINE=InnoDB DEFAULT CHARACTER SET utf8 COLLATE utf8_unicode_ci");
     $form = $this->getFormObject('CRM_Contact_Import_Form_MapField');
     $form->set('contactType', CRM_Import_Parser::CONTACT_INDIVIDUAL);
-    $form->set('fields', array());
-    $form->_columnNames = array();
-    $form->_importTableName = 'civicrm_cache';
-    $form->submit(array(), array());
+    $form->_columnNames = array('nada', 'first_name', 'last_name', 'address');
+    $form->set('importTableName', 'civicrm_import_job_xxx');
+    $form->preProcess();
+    $form->submit($params, $mapper);
+
+    CRM_Core_DAO::executeQuery("DROP TABLE civicrm_import_job_xxx");
+    if (!empty($expecteds)) {
+      foreach ($expecteds as $expected) {
+        $result = $this->callAPISuccess($expected['entity'], 'get', array_merge($expected['values'], array('sequential' => 1)));
+        $this->assertEquals($expected['count'], $result['count']);
+        if (isset($expected['result'])) {
+          foreach ($expected['result'] as $key => $expectedValues) {
+            foreach ($expectedValues as $valueKey => $value) {
+              $this->assertEquals($value, $result['values'][$key][$valueKey]);
+            }
+          }
+        }
+      }
+    }
+    $this->quickCleanup(array('civicrm_mapping', 'civicrm_mapping_field'));
+  }
+
+  /**
+   * Get data to pass through submit function.
+   *
+   * @return array
+   */
+  public function getSubmitData() {
+    return array(
+      'basic_data' => array(
+        array(
+          'saveMappingName' => '',
+          'saveMappingDesc' => '',
+        ),
+        array(
+          0 => array(0 => 'do_not_import'),
+          1 => array(0 => 'first_name'),
+          2 => array(0 => 'last_name'),
+          3 => array(0 => 'street_address', 1 => 2),
+        ),
+      ),
+      'save_mapping' => array(
+        array(
+          'saveMappingName' => 'new mapping',
+          'saveMappingDesc' => 'save it',
+          'saveMapping' => 1,
+        ),
+        array(
+          0 => array(0 => 'do_not_import'),
+          1 => array(0 => 'first_name'),
+          2 => array(0 => 'last_name'),
+          3 => array(0 => 'street_address', 1 => 2),
+        ),
+        array(
+          array('entity' => 'mapping', 'count' => 1, 'values' => array('name' => 'new mapping')),
+          array(
+            'entity' =>
+            'mapping_field',
+            'count' => 4,
+            'values' => array(),
+            'result' => array(
+              0 => array('name' => '- do not import -'),
+              1 => array('name' => 'First Name'),
+              2 => array('name' => 'Last Name'),
+              3 => array('name' => 'Street Address', 'location_type_id' => 2),
+            ),
+          ),
+        ),
+      ),
+    );
+  }
+
+  /**
+   * Instantiate form object
+   *
+   * @param string $class
+   * @return \CRM_Core_Form
+   */
+  public function getFormObject($class) {
+    $form = parent::getFormObject($class);
+    $contactFields = CRM_Contact_BAO_Contact::importableFields();
+    $fields = array();
+    foreach ($contactFields as $name => $field) {
+      $fields[$name] = $field['title'];
+    }
+    $form->set('fields', $fields);
+    return $form;
   }
 
 }