Extract submit in mapFields to start adding unit testin (CRM-20759)
authoreileen <emcnaughton@wikimedia.org>
Thu, 22 Jun 2017 08:35:02 +0000 (20:35 +1200)
committereileen <emcnaughton@wikimedia.org>
Fri, 23 Jun 2017 02:10:19 +0000 (14:10 +1200)
CRM/Contact/Import/Form/MapField.php
tests/phpunit/CRM/Contact/Import/Form/MapFieldTest.php [new file with mode: 0644]

index 87827426fa073269786a46a0c25e6880f16cb892..9ba6188f002689ba8fcd67557c7a29b67a06f37a 100644 (file)
@@ -665,11 +665,59 @@ class CRM_Contact_Import_Form_MapField extends CRM_Import_Form_MapField {
       $this->controller->resetPage($this->_name);
       return;
     }
-
-    $mapper = array();
-    $mapperKeys = array();
     $mapperKeys = $this->controller->exportValue($this->_name, 'mapper');
-    $mapperKeysMain = array();
+
+    $parser = $this->submit($params, $mapperKeys);
+
+    // add all the necessary variables to the form
+    $parser->set($this);
+  }
+
+  /**
+   * Format custom field name.
+   *
+   * Combine group and field name to avoid conflict.
+   *
+   * @param array $fields
+   *
+   * @return array
+   */
+  public function formatCustomFieldName(&$fields) {
+    //CRM-2676, replacing the conflict for same custom field name from different custom group.
+    $fieldIds = $formattedFieldNames = array();
+    foreach ($fields as $key => $value) {
+      if ($customFieldId = CRM_Core_BAO_CustomField::getKeyID($key)) {
+        $fieldIds[] = $customFieldId;
+      }
+    }
+
+    if (!empty($fieldIds) && is_array($fieldIds)) {
+      $groupTitles = CRM_Core_BAO_CustomGroup::getGroupTitles($fieldIds);
+
+      if (!empty($groupTitles)) {
+        foreach ($groupTitles as $fId => $values) {
+          $key = "custom_{$fId}";
+          $groupTitle = $values['groupTitle'];
+          $formattedFieldNames[$key] = $fields[$key] . ' :: ' . $groupTitle;
+        }
+      }
+    }
+
+    return $formattedFieldNames;
+  }
+
+  /**
+   * Main submit function.
+   *
+   * Extracted to add testing & start refactoring.
+   *
+   * @param $params
+   * @param $mapperKeys
+   *
+   * @return \CRM_Contact_Import_Parser_Contact
+   */
+  public function submit($params, $mapperKeys) {
+    $mapper = $mapperKeysMain = array();
 
     $phoneTypes = CRM_Core_PseudoConstant::get('CRM_Core_DAO_Phone', 'phone_type_id');
     $imProviders = CRM_Core_PseudoConstant::get('CRM_Core_DAO_IM', 'provider_id');
@@ -968,42 +1016,7 @@ class CRM_Contact_Import_Form_MapField extends CRM_Import_Form_MapField {
       $this->get('contactSubType'),
       $this->get('dedupe')
     );
-
-    // add all the necessary variables to the form
-    $parser->set($this);
-  }
-
-  /**
-   * Format custom field name.
-   *
-   * Combine group and field name to avoid conflict.
-   *
-   * @param array $fields
-   *
-   * @return array
-   */
-  public function formatCustomFieldName(&$fields) {
-    //CRM-2676, replacing the conflict for same custom field name from different custom group.
-    $fieldIds = $formattedFieldNames = array();
-    foreach ($fields as $key => $value) {
-      if ($customFieldId = CRM_Core_BAO_CustomField::getKeyID($key)) {
-        $fieldIds[] = $customFieldId;
-      }
-    }
-
-    if (!empty($fieldIds) && is_array($fieldIds)) {
-      $groupTitles = CRM_Core_BAO_CustomGroup::getGroupTitles($fieldIds);
-
-      if (!empty($groupTitles)) {
-        foreach ($groupTitles as $fId => $values) {
-          $key = "custom_{$fId}";
-          $groupTitle = $values['groupTitle'];
-          $formattedFieldNames[$key] = $fields[$key] . ' :: ' . $groupTitle;
-        }
-      }
-    }
-
-    return $formattedFieldNames;
+    return $parser;
   }
 
 }
diff --git a/tests/phpunit/CRM/Contact/Import/Form/MapFieldTest.php b/tests/phpunit/CRM/Contact/Import/Form/MapFieldTest.php
new file mode 100644 (file)
index 0000000..d0a438c
--- /dev/null
@@ -0,0 +1,55 @@
+<?php
+/*
++--------------------------------------------------------------------+
+| CiviCRM version 4.7                                                |
++--------------------------------------------------------------------+
+| Copyright CiviCRM LLC (c) 2004-2017                                |
++--------------------------------------------------------------------+
+| 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
+ * File for the CRM_Contact_Import_Form_MapFieldTest class.
+ */
+
+/**
+ *  Test contact import mapfield.
+ *
+ * @package CiviCRM
+ * @group headless
+ */
+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).
+   */
+  public function testSubmit() {
+    $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());
+  }
+
+}