CRM-20758 remove call to deprecated function & add test
authoreileen <emcnaughton@wikimedia.org>
Thu, 22 Jun 2017 03:48:15 +0000 (15:48 +1200)
committereileen <emcnaughton@wikimedia.org>
Thu, 22 Jun 2017 04:23:17 +0000 (16:23 +1200)
CRM/Contact/Import/Form/DataSource.php
CRM/Core/BAO/Mapping.php
CRM/Export/Form/Select.php
CRM/Import/Form/DataSource.php
tests/phpunit/CRM/Contact/Import/Form/DataSourceTest.php [new file with mode: 0644]
tests/phpunit/CiviTest/CiviUnitTestCase.php

index 96bbfcb9f13f68c384c91c26ab082f55289169cb..dbbf7d6abc2c6e741da5a0c72a4a90fdc5c36bd7 100644 (file)
@@ -168,10 +168,7 @@ class CRM_Contact_Import_Form_DataSource extends CRM_Core_Form {
       ts('For Duplicate Contacts')
     );
 
-    $mappingArray = CRM_Core_BAO_Mapping::getMappings(CRM_Core_OptionGroup::getValue('mapping_type',
-      'Import Contact',
-      'name'
-    ));
+    $mappingArray = CRM_Core_BAO_Mapping::getMappings('Import Contact');
 
     $this->assign('savedMapping', $mappingArray);
     $this->addElement('select', 'savedMapping', ts('Mapping Option'), array('' => ts('- select -')) + $mappingArray);
index 3051ad8169e6dca7cfcc64c006886a9abd640e5c..bebf732f30cbd50f9ffa587850d1c61251a37ac8 100644 (file)
@@ -108,22 +108,19 @@ class CRM_Core_BAO_Mapping extends CRM_Core_DAO_Mapping {
   /**
    * Get the list of mappings.
    *
-   * @param string $mappingTypeId
-   *   Mapping type id.
+   * @param string $mappingType
+   *   Mapping type name.
    *
    * @return array
-   *   array of mapping name
+   *   Array of mapping names, keyed by id.
    */
-  public static function getMappings($mappingTypeId) {
+  public static function getMappings($mappingType) {
+    $result = civicrm_api3('Mapping', 'get', array('mapping_type_id' => $mappingType, 'options' => array('limit' => 1, 'sort' => 'name')));
     $mapping = array();
-    $mappingDAO = new CRM_Core_DAO_Mapping();
-    $mappingDAO->mapping_type_id = $mappingTypeId;
-    $mappingDAO->find();
 
-    while ($mappingDAO->fetch()) {
-      $mapping[$mappingDAO->id] = $mappingDAO->name;
+    foreach ($result['values'] as $key => $value) {
+      $mapping[$key] = $value['name'];
     }
-
     return $mapping;
   }
 
index d103690c5270f3903a6d4ecccb0403a12dfedc47..72fff69296592bf55f932f47577df4bfb4d01b7f 100644 (file)
@@ -488,7 +488,7 @@ FROM   {$this->_componentTable}
     $mappingTypeId = CRM_Core_OptionGroup::getValue('mapping_type', $exportType, 'name');
     $this->set('mappingTypeId', $mappingTypeId);
 
-    $mappings = CRM_Core_BAO_Mapping::getMappings($mappingTypeId);
+    $mappings = CRM_Core_BAO_Mapping::getMappings($exportType);
     if (!empty($mappings)) {
       $this->add('select', 'mapping', ts('Use Saved Field Mapping'), array('' => '-select-') + $mappings);
     }
index 2270e6b74823df08d79b59fdd329f9ac7f41817a..0040e2584f623146a28e47995626c6baa9369831 100644 (file)
@@ -81,10 +81,7 @@ abstract class CRM_Import_Form_DataSource extends CRM_Core_Form {
     $this->setDefaults(array('fieldSeparator' => $config->fieldSeparator));
 
     //get the saved mapping details
-    $mappingArray = CRM_Core_BAO_Mapping::getMappings(CRM_Core_OptionGroup::getValue('mapping_type',
-      'Import ' . static::IMPORT_ENTITY,
-      'name'
-    ));
+    $mappingArray = CRM_Core_BAO_Mapping::getMappings('Import ' . static::IMPORT_ENTITY);
     $this->assign('savedMapping', $mappingArray);
     $this->add('select', 'savedMapping', ts('Mapping Option'), array('' => ts('- select -')) + $mappingArray);
 
diff --git a/tests/phpunit/CRM/Contact/Import/Form/DataSourceTest.php b/tests/phpunit/CRM/Contact/Import/Form/DataSourceTest.php
new file mode 100644 (file)
index 0000000..2d509c3
--- /dev/null
@@ -0,0 +1,53 @@
+<?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_DataSourceTest class.
+ */
+
+/**
+ *  Test contact import datasource.
+ *
+ * @package CiviCRM
+ * @group headless
+ */
+class CRM_Contact_Import_Form_DataSourceTest extends CiviUnitTestCase {
+
+  /**
+   * Test the form loads without error / notice and mappings are assigned.
+   *
+   * (Added in conjunction with fixed noting on mapping assignment).
+   */
+  public function testBuildForm() {
+    $this->callAPISuccess('Mapping', 'create', array('name' => 'Well dressed ducks', 'mapping_type_id' => 'Import Contact'));
+    $form = $this->getFormObject('CRM_Contact_Import_Form_DataSource');
+    $form->buildQuickForm();
+    $this->assertEquals(array(1 => 'Well dressed ducks'), CRM_Core_Smarty::singleton()->get_template_vars('savedMapping'));
+  }
+
+}
index 9c98bd24fdad46a7a2a4ab672b37aab601a78781..37a0bcd77f57c03d42c968d843a1f29be542fc48 100644 (file)
@@ -3889,4 +3889,22 @@ AND    ( TABLE_NAME LIKE 'civicrm_value_%' )
     }
   }
 
+
+  /**
+   * Instantiate form object.
+   *
+   * We need to instantiate the form to run preprocess, which means we have to trick it about the request method.
+   *
+   * @param string $class
+   *   Name of form class.
+   *
+   * @return \CRM_Core_Form
+   */
+  public function getFormObject($class) {
+    $form = new $class();
+    $_SERVER['REQUEST_METHOD'] = 'GET';
+    $form->controller = new CRM_Core_Controller();
+    return $form;
+  }
+
 }