Eileen has uploaded a new change for review.

  https://gerrit.wikimedia.org/r/310732

Change subject: Only store the full country code if it is on our valid list.
......................................................................

Only store the full country code if it is on our valid list.

If the country code is not on the list then prefer the 2 digit code. This means 
'en' is better than 'en_NO
(Norwegian English). Separately we will look to remove the invalid options we 
already have in our DB.

If neither are valid store the full thing to make it easier to find them

Bug: T96410
Change-Id: Ie7e8a958c2496a738ce738dd827b7ed04b2d9fbd
---
M sites/all/modules/wmf_civicrm/tests/phpunit/ImportMessageTest.php
M sites/all/modules/wmf_civicrm/wmf_civicrm.module
2 files changed, 124 insertions(+), 32 deletions(-)


  git pull ssh://gerrit.wikimedia.org:29418/wikimedia/fundraising/crm 
refs/changes/32/310732/1

diff --git a/sites/all/modules/wmf_civicrm/tests/phpunit/ImportMessageTest.php 
b/sites/all/modules/wmf_civicrm/tests/phpunit/ImportMessageTest.php
index c3ad868..86e8901 100644
--- a/sites/all/modules/wmf_civicrm/tests/phpunit/ImportMessageTest.php
+++ b/sites/all/modules/wmf_civicrm/tests/phpunit/ImportMessageTest.php
@@ -103,7 +103,6 @@
                     'return' => 'name',
                 )), $expected['contact'][$renamedField]);
             }
-
         }
 
         if ( !empty( $expected['contact_custom_values'] ) ) {
@@ -141,35 +140,7 @@
                     'payment_method' => 'cc',
                 ),
                 array(
-                    'contribution' => array(
-                        'address_id' => '',
-                        'amount_level' => '',
-                        'campaign_id' => '',
-                        'cancel_date' => '',
-                        'cancel_reason' => '',
-                        'check_number' => '',
-                        'contribution_page_id' => '',
-                        'contribution_recur_id' => '',
-                        'contribution_status_id' => '1',
-                        'contribution_type_id' => $contribution_type_cash,
-                        'currency' => 'USD',
-                        'fee_amount' => '0',
-                        'invoice_id' => '',
-                        'is_pay_later' => '',
-                        'is_test' => '',
-                        'net_amount' => '1.23',
-                        'non_deductible_amount' => '',
-                        'payment_instrument_id' => $payment_instrument_cc,
-                        'receipt_date' => '',
-                        'receive_date' => '20120501000000',
-                        'source' => 'USD 1.23',
-                        'thankyou_date' => '',
-                        'total_amount' => '1.23',
-                        'trxn_id' => "TEST_GATEWAY {$gateway_txn_id}",
-                        'financial_type_id' => $contribution_type_cash,
-                        'creditnote_id' => '',
-                        'tax_amount' => '',
-                    ),
+                    'contribution' => 
$this->getBaseContribution($gateway_txn_id),
                 ),
             ),
 
@@ -219,6 +190,7 @@
                     'email' => 'nob...@wikimedia.org',
                     'first_name' => 'First',
                     'fee' => '0.03',
+                    'preferred_language' => 'en_US',
                     'gateway' => 'test_gateway',
                     'gateway_txn_id' => $gateway_txn_id,
                     'gift_source' => 'Legacy Gift',
@@ -246,6 +218,7 @@
                         'middle_name' => 'Middle',
                         'prefix' => $new_prefix,
                         'suffix' => 'Sr.',
+                        'preferred_language' => 'en_US',
                     ),
                     'contribution' => array(
                         'address_id' => '',
@@ -296,8 +269,57 @@
                     ),
                 ),
             ),
+          // Invalid language suffix for valid short lang.
+          'invalid language suffix' => array(
+            array(
+              'currency' => 'USD',
+              'date' => '2012-05-01 00:00:00',
+              'email' => 'nob...@wikimedia.org',
+              'gateway' => 'test_gateway',
+              'gateway_txn_id' => $gateway_txn_id,
+              'gross' => '1.23',
+              'payment_method' => 'cc',
+              'preferred_language' => 'en_ZZ',
+              'name_prefix' => $new_prefix,
+              'name_suffix' => 'Sr.',
+            ),
+            array(
+              'contact' => array(
+                'preferred_language' => 'en',
+                'prefix' => $new_prefix,
+                'suffix' => 'Sr.',
+              ),
+              'contribution' => $this->getBaseContribution($gateway_txn_id),
+            ),
+          ),
 
-            // Organization contribution
+          // Invalid language suffix for invalid short lang.
+          'invalid short language' => array(
+            array(
+              'currency' => 'USD',
+              'date' => '2012-05-01 00:00:00',
+              'email' => 'nob...@wikimedia.org',
+              'gateway' => 'test_gateway',
+              'gateway_txn_id' => $gateway_txn_id,
+              'gross' => '1.23',
+              'payment_method' => 'cc',
+              'preferred_language' => 'zz_ZZ',
+              'name_prefix' => $new_prefix,
+              'name_suffix' => 'Sr.',
+              'prefix' => $new_prefix,
+              'suffix' => 'Sr.',
+            ),
+            array(
+              'contact' => array(
+                'preferred_language' => 'zz_ZZ',
+                'prefix' => $new_prefix,
+                'suffix' => 'Sr.',
+              ),
+              'contribution' => $this->getBaseContribution($gateway_txn_id),
+            ),
+          ),
+
+          // Organization contribution
             array(
                 array(
                     'contact_type' => 'Organization',
@@ -441,6 +463,47 @@
     }
 
   /**
+   * Get the basic array of contribution data.
+   *
+   * @param string $gateway_txn_id
+   *
+   * @return array
+   */
+  protected function getBaseContribution($gateway_txn_id) {
+    $contribution_type_cash = wmf_civicrm_get_civi_id( 'contribution_type_id', 
'Cash' );
+    $payment_instrument_cc = wmf_civicrm_get_civi_id( 'payment_instrument_id', 
'Credit Card' );
+    return array(
+      'address_id' => '',
+      'amount_level' => '',
+      'campaign_id' => '',
+      'cancel_date' => '',
+      'cancel_reason' => '',
+      'check_number' => '',
+      'contribution_page_id' => '',
+      'contribution_recur_id' => '',
+      'contribution_status_id' => '1',
+      'contribution_type_id' => $contribution_type_cash,
+      'currency' => 'USD',
+      'fee_amount' => '0',
+      'invoice_id' => '',
+      'is_pay_later' => '',
+      'is_test' => '',
+      'net_amount' => '1.23',
+      'non_deductible_amount' => '',
+      'payment_instrument_id' => $payment_instrument_cc,
+      'receipt_date' => '',
+      'receive_date' => '20120501000000',
+      'source' => 'USD 1.23',
+      'thankyou_date' => '',
+      'total_amount' => '1.23',
+      'trxn_id' => "TEST_GATEWAY {$gateway_txn_id}",
+      'financial_type_id' => $contribution_type_cash,
+      'creditnote_id' => '',
+      'tax_amount' => '',
+    );
+  }
+
+  /**
    * Remove commas from money fields.
    *
    * @param array $array
diff --git a/sites/all/modules/wmf_civicrm/wmf_civicrm.module 
b/sites/all/modules/wmf_civicrm/wmf_civicrm.module
index 41825e1..d324426 100644
--- a/sites/all/modules/wmf_civicrm/wmf_civicrm.module
+++ b/sites/all/modules/wmf_civicrm/wmf_civicrm.module
@@ -1089,7 +1089,19 @@
         }
     }
     if (!empty($contact['preferred_language'])) {
-        wmf_civicrm_ensure_language_exists($contact['preferred_language']);
+        if (!wmf_civicrm_check_language_exists()) {
+          $parts = explode('_', $contact['preferred_language']);
+          if (wmf_civicrm_check_language_exists($parts[0])) {
+            // in other words en_NO will be converted to en
+            // rather than Norwegian English.
+            $contact['preferred_language'] = $parts[0];
+          }
+          else {
+            // otherwise let's create it rather than fail.
+            // seems like the easiest way not to lose visibility, data or the 
plot.
+            wmf_civicrm_ensure_language_exists($contact['preferred_language']);
+          }
+        }
     }
 
     // Copy some fields, if they exist
@@ -1258,6 +1270,23 @@
 }
 
 /**
+ * Check if the language string exists.
+ *
+ * @param string $languageAbbreviation
+ *
+ * @return bool
+ */
+function wmf_civicrm_check_language_exists($languageAbbreviation) {
+  static $languages;
+  if (empty($languages)) {
+    $available_options = civicrm_api3('Contact', 'getoptions', array(
+      'field' => 'preferred_language',
+    ));
+    $languages = $available_options['values'];
+  }
+  return !empty($languages[$languageAbbreviation]);
+}
+/**
  * Ensure the required option value exists.
  *
  * @param string $option_value

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

Gerrit-MessageType: newchange
Gerrit-Change-Id: Ie7e8a958c2496a738ce738dd827b7ed04b2d9fbd
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