From 424df54fb4ab3bf78f025f2baee37a0616a65040 Mon Sep 17 00:00:00 2001 From: Eileen McNaughton Date: Mon, 30 May 2022 16:58:49 +1200 Subject: [PATCH] Add geocode import --- CRM/Contact/Import/Parser/Contact.php | 28 ++----------------- CRM/Import/Parser.php | 3 ++ .../Import/Form/data/individual_geocode.csv | 4 +++ .../CRM/Contact/Import/Parser/ContactTest.php | 17 +++++++++++ 4 files changed, 26 insertions(+), 26 deletions(-) create mode 100644 tests/phpunit/CRM/Contact/Import/Form/data/individual_geocode.csv diff --git a/CRM/Contact/Import/Parser/Contact.php b/CRM/Contact/Import/Parser/Contact.php index dde23ed58a..7d31903d22 100644 --- a/CRM/Contact/Import/Parser/Contact.php +++ b/CRM/Contact/Import/Parser/Contact.php @@ -114,6 +114,8 @@ class CRM_Contact_Import_Parser_Contact extends CRM_Import_Parser { 'postal_greeting_id', 'addressee', 'addressee_id', + 'geo_code_1', + 'geo_code_2', ]; /** @@ -952,32 +954,6 @@ class CRM_Contact_Import_Parser_Contact extends CRM_Import_Parser { } break; - case 'geo_code_1': - if (!empty($value)) { - foreach ($value as $codeValue) { - if (!empty($codeValue['geo_code_1'])) { - if (CRM_Utils_Rule::numeric($codeValue['geo_code_1'])) { - continue; - } - $errors[] = ts('Geo code 1'); - } - } - } - break; - - case 'geo_code_2': - if (!empty($value)) { - foreach ($value as $codeValue) { - if (!empty($codeValue['geo_code_2'])) { - if (CRM_Utils_Rule::numeric($codeValue['geo_code_2'])) { - continue; - } - $errors[] = ts('Geo code 2'); - } - } - } - break; - case 'do_not_email': case 'do_not_phone': case 'do_not_mail': diff --git a/CRM/Import/Parser.php b/CRM/Import/Parser.php index 6aa5255601..cd05171b49 100644 --- a/CRM/Import/Parser.php +++ b/CRM/Import/Parser.php @@ -1186,6 +1186,9 @@ abstract class CRM_Import_Parser { return CRM_Utils_Rule::email($importedValue) ? $importedValue : 'invalid_import_value'; } + if ($fieldMetadata['type'] === CRM_Utils_Type::T_FLOAT) { + return CRM_Utils_Rule::numeric($importedValue) ? $importedValue : 'invalid_import_value'; + } if ($fieldMetadata['type'] === CRM_Utils_Type::T_BOOLEAN) { $value = CRM_Utils_String::strtoboolstr($importedValue); if ($value !== FALSE) { diff --git a/tests/phpunit/CRM/Contact/Import/Form/data/individual_geocode.csv b/tests/phpunit/CRM/Contact/Import/Form/data/individual_geocode.csv new file mode 100644 index 0000000000..f9c9e03214 --- /dev/null +++ b/tests/phpunit/CRM/Contact/Import/Form/data/individual_geocode.csv @@ -0,0 +1,4 @@ +first_name,last_name,geocodeone,GeoCodetwo,expected +Madame,1,1,-1,Valid +Madame,2,a,b,Invalid +Madame,3,1.1123,-1.1123,Valid diff --git a/tests/phpunit/CRM/Contact/Import/Parser/ContactTest.php b/tests/phpunit/CRM/Contact/Import/Parser/ContactTest.php index d329655483..6a427bd70f 100644 --- a/tests/phpunit/CRM/Contact/Import/Parser/ContactTest.php +++ b/tests/phpunit/CRM/Contact/Import/Parser/ContactTest.php @@ -1977,6 +1977,23 @@ class CRM_Contact_Import_Parser_ContactTest extends CiviUnitTestCase { return $userJobID; } + /** + * Test geocode validation. + * + * @throws \API_Exception + * @throws \CRM_Core_Exception + */ + public function testImportGeocodes(): void { + $mapper = [ + ['first_name'], + ['last_name'], + ['geo_code_1', 1], + ['geo_code_2', 1], + ]; + $csv = 'individual_geocode.csv'; + $this->validateMultiRowCsv($csv, $mapper, 'GeoCode2'); + } + /** * Validate the csv file values. * -- 2.25.1