Eileen has uploaded a new change for review. ( 
https://gerrit.wikimedia.org/r/375945 )

Change subject: Do not save 0 or other invalid values to city field.
......................................................................

Do not save 0 or other invalid values to city field.

Bug: T174980
Change-Id: I5512405590d3fe53aba30f8db8a473ac6ce00111
---
M sites/all/modules/wmf_civicrm/tests/phpunit/AddressImportTest.php
M sites/all/modules/wmf_civicrm/wmf_civicrm.module
2 files changed, 66 insertions(+), 7 deletions(-)


  git pull ssh://gerrit.wikimedia.org:29418/wikimedia/fundraising/crm 
refs/changes/45/375945/1

diff --git a/sites/all/modules/wmf_civicrm/tests/phpunit/AddressImportTest.php 
b/sites/all/modules/wmf_civicrm/tests/phpunit/AddressImportTest.php
index fc576b6..92b00ff 100644
--- a/sites/all/modules/wmf_civicrm/tests/phpunit/AddressImportTest.php
+++ b/sites/all/modules/wmf_civicrm/tests/phpunit/AddressImportTest.php
@@ -48,6 +48,49 @@
   }
 
   /**
+   * Test creating an address not use void data.
+   *
+   * @dataProvider getVoidValues
+   *
+   * @param string $voidValue
+   */
+  public function testAddressImportSkipVoidData($voidValue) {
+    $msg = array(
+      'currency' => 'USD',
+      'date' => time(),
+      'last_name' => 'Mouse',
+      'email' => 'nob...@wikimedia.org',
+      'gateway' => 'test_gateway',
+      'gateway_txn_id' => mt_rand(),
+      'gross' => '1.23',
+      'payment_method' => 'cc',
+      'street_address' => 'really cool place',
+      'postal_code' => $voidValue,
+      'city' => $voidValue,
+      'country' => 'US',
+    );
+
+    $contribution = wmf_civicrm_contribution_message_import($msg);
+    $address = $this->callAPISuccessGetSingle('Address',  array('contact_id' 
=> $contribution['contact_id']));
+    $this->assertTrue(!isset($address['city']));
+    $this->assertTrue(!isset($address['postal_code']));
+  }
+
+  /**
+   * Get values which should not be stored to the DB.
+   *
+   * @return array
+   */
+  public function getVoidValues() {
+    return array(
+      array('0'),
+      array(0),
+      array('NoCity'),
+      array('City/Town'),
+    );
+  }
+
+  /**
    * Test creating an address with void data does not create an address.
    *
    * In this case the contact already exists.
diff --git a/sites/all/modules/wmf_civicrm/wmf_civicrm.module 
b/sites/all/modules/wmf_civicrm/wmf_civicrm.module
index 8b8fdc5..c208dee 100644
--- a/sites/all/modules/wmf_civicrm/wmf_civicrm.module
+++ b/sites/all/modules/wmf_civicrm/wmf_civicrm.module
@@ -1739,13 +1739,7 @@
     );
     $msg = $msg + $defaults;
 
-    // Remove known dummy data.
-    if ($msg['street_address'] === 'N0NE PROVIDED') {
-        $msg['street_address'] = '';
-    }
-    if ($msg['postal_code'] === 0) {
-        $msg['postal_code'] = '';
-    }
+    wmf_civicrm_removeKnownBadStringsFromAddressFields($msg);
 
     if ( empty( $msg['contribution_type_id'] ) ) {
         if ( empty( $msg['contribution_type'] ) ) {
@@ -1910,6 +1904,28 @@
 }
 
 /**
+ * Remove known bad strings from address.
+ *
+ * This function focuses on specific forms of bad data with high
+ * prevalence in the fields we see them in.
+ *
+ * @param array $msg
+ */
+function wmf_civicrm_removeKnownBadStringsFromAddressFields(&$msg) {
+  // Remove known dummy data.
+  if ($msg['street_address'] === 'N0NE PROVIDED') {
+    $msg['street_address'] = '';
+  }
+
+  $invalidAddressStrings = array('0', 'City/Town', 'NoCity');
+  foreach (array('postal_code', 'city') as $fieldName) {
+    if (in_array($msg[$fieldName], $invalidAddressStrings)) {
+      $msg[$fieldName] = '';
+    }
+  }
+}
+
+/**
  * Get the name value for the option group for direct mail.
  *
  * The option group for the direct mail field has been given a distinctive 
title but

-- 
To view, visit https://gerrit.wikimedia.org/r/375945
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings

Gerrit-MessageType: newchange
Gerrit-Change-Id: I5512405590d3fe53aba30f8db8a473ac6ce00111
Gerrit-PatchSet: 1
Gerrit-Project: wikimedia/fundraising/crm
Gerrit-Branch: master
Gerrit-Owner: Eileen <emcnaugh...@wikimedia.org>

_______________________________________________
MediaWiki-commits mailing list
MediaWiki-commits@lists.wikimedia.org
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits

Reply via email to