Add geocode import
authorEileen McNaughton <emcnaughton@wikimedia.org>
Mon, 30 May 2022 04:58:49 +0000 (16:58 +1200)
committerEileen McNaughton <emcnaughton@wikimedia.org>
Tue, 31 May 2022 21:31:20 +0000 (09:31 +1200)
CRM/Contact/Import/Parser/Contact.php
CRM/Import/Parser.php
tests/phpunit/CRM/Contact/Import/Form/data/individual_geocode.csv [new file with mode: 0644]
tests/phpunit/CRM/Contact/Import/Parser/ContactTest.php

index dde23ed58a87dee68cee7f33200ace184c952050..7d31903d2243116bbd2aea7ad77a33f2d0a3d0e3 100644 (file)
@@ -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':
index 6aa5255601b8b17fdecf0ab271ffb519104d2705..cd05171b4911c42a79264960ab5a29c982853a7e 100644 (file)
@@ -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 (file)
index 0000000..f9c9e03
--- /dev/null
@@ -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
index d329655483c9a2f13e93a6d87b86b58f1a42aca7..6a427bd70f3bd9cd675347828f5f3e8ce955dd01 100644 (file)
@@ -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.
    *