From f8df7165d2d295e3c436e37784b181f87646a5a7 Mon Sep 17 00:00:00 2001 From: eileen Date: Thu, 22 Jun 2017 15:48:15 +1200 Subject: [PATCH] CRM-20758 remove call to deprecated function & add test --- CRM/Contact/Import/Form/DataSource.php | 5 +- CRM/Core/BAO/Mapping.php | 17 +++--- CRM/Export/Form/Select.php | 2 +- CRM/Import/Form/DataSource.php | 5 +- .../Contact/Import/Form/DataSourceTest.php | 53 +++++++++++++++++++ tests/phpunit/CiviTest/CiviUnitTestCase.php | 18 +++++++ 6 files changed, 81 insertions(+), 19 deletions(-) create mode 100644 tests/phpunit/CRM/Contact/Import/Form/DataSourceTest.php diff --git a/CRM/Contact/Import/Form/DataSource.php b/CRM/Contact/Import/Form/DataSource.php index 96bbfcb9f1..dbbf7d6abc 100644 --- a/CRM/Contact/Import/Form/DataSource.php +++ b/CRM/Contact/Import/Form/DataSource.php @@ -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); diff --git a/CRM/Core/BAO/Mapping.php b/CRM/Core/BAO/Mapping.php index 3051ad8169..bebf732f30 100644 --- a/CRM/Core/BAO/Mapping.php +++ b/CRM/Core/BAO/Mapping.php @@ -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; } diff --git a/CRM/Export/Form/Select.php b/CRM/Export/Form/Select.php index d103690c52..72fff69296 100644 --- a/CRM/Export/Form/Select.php +++ b/CRM/Export/Form/Select.php @@ -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); } diff --git a/CRM/Import/Form/DataSource.php b/CRM/Import/Form/DataSource.php index 2270e6b748..0040e2584f 100644 --- a/CRM/Import/Form/DataSource.php +++ b/CRM/Import/Form/DataSource.php @@ -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 index 0000000000..2d509c359c --- /dev/null +++ b/tests/phpunit/CRM/Contact/Import/Form/DataSourceTest.php @@ -0,0 +1,53 @@ +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')); + } + +} diff --git a/tests/phpunit/CiviTest/CiviUnitTestCase.php b/tests/phpunit/CiviTest/CiviUnitTestCase.php index 9c98bd24fd..37a0bcd77f 100644 --- a/tests/phpunit/CiviTest/CiviUnitTestCase.php +++ b/tests/phpunit/CiviTest/CiviUnitTestCase.php @@ -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; + } + } -- 2.25.1