jenkins-bot has submitted this change and it was merged. ( 
https://gerrit.wikimedia.org/r/379467 )

Change subject: When updating an email return early on email match, set on_hold 
to 0 if email is not on hold in DB already.
......................................................................


When updating an email return early on email match, set on_hold to 0 if email 
is not on hold in DB already.

Bug: T170350
Change-Id: If092a9131c85d5ec332818d1eb9984142c1c3dda
---
M sites/all/modules/wmf_civicrm/tests/phpunit/ImportMessageTest.php
M sites/all/modules/wmf_civicrm/wmf_civicrm.module
2 files changed, 99 insertions(+), 10 deletions(-)

Approvals:
  jenkins-bot: Verified
  Ejegg: Looks good to me, approved



diff --git a/sites/all/modules/wmf_civicrm/tests/phpunit/ImportMessageTest.php 
b/sites/all/modules/wmf_civicrm/tests/phpunit/ImportMessageTest.php
index cc18669..3cba6c7 100644
--- a/sites/all/modules/wmf_civicrm/tests/phpunit/ImportMessageTest.php
+++ b/sites/all/modules/wmf_civicrm/tests/phpunit/ImportMessageTest.php
@@ -473,7 +473,77 @@
         $this->assertEquals($fixtures->contact_group_id, $group['group_id']);
     }
 
-    public function testDuplicateHandling() {
+  /**
+   * Test that existing on hold setting is retained.
+   */
+    public function testKeepOnHold() {
+      self::$fixtures = CiviFixtures::create();
+      $this->callAPISuccess('Email', 'create', array(
+        'email' => 'aga...@wikimedia.org',
+        'on_hold' => 1,
+        'location_type_id' => 1,
+        'contact_id' => self::$fixtures->contact_id,
+      ));
+
+      $msg = array(
+        'contact_id' => self::$fixtures->contact_id,
+        'contribution_recur_id' => self::$fixtures->contribution_recur_id,
+        'currency' => 'USD',
+        'date' => '2014-01-01 00:00:00',
+        'effort_id' => 2,
+        'email' => 'aga...@wikimedia.org',
+        'gateway' => 'test_gateway',
+        'gateway_txn_id' => mt_rand(),
+        'gross' => self::$fixtures->recur_amount,
+        'payment_method' => 'cc',
+      );
+      $contribution = wmf_civicrm_contribution_message_import($msg);
+      $emails = $this->callAPISuccess('Email', 'get', array('contact_id' => 
self::$fixtures->contact_id, 'sequential' => 1));
+      $this->assertEquals(1, $emails['count']);
+
+      $this->assertEquals(1, $emails['values'][0]['on_hold']);
+      $this->assertEquals('aga...@wikimedia.org', 
$emails['values'][0]['email']);
+
+      $this->callAPISuccess('Contribution', 'delete', array('id' => 
$contribution['id']));
+
+    }
+
+  /**
+   * Test that existing on hold setting is removed if the email changes.
+   */
+  public function testRemoveOnHoldWhenUpdating() {
+    self::$fixtures = CiviFixtures::create();
+    $this->callAPISuccess('Email', 'create', array(
+      'email' => 'aga...@wikimedia.org',
+      'on_hold' => 1,
+      'location_type_id' => 1,
+      'contact_id' => self::$fixtures->contact_id,
+    ));
+
+    $msg = array(
+      'contact_id' => self::$fixtures->contact_id,
+      'contribution_recur_id' => self::$fixtures->contribution_recur_id,
+      'currency' => 'USD',
+      'date' => '2014-01-01 00:00:00',
+      'effort_id' => 2,
+      'email' => 'pan...@wikimedia.org',
+      'gateway' => 'test_gateway',
+      'gateway_txn_id' => mt_rand(),
+      'gross' => self::$fixtures->recur_amount,
+      'payment_method' => 'cc',
+    );
+    $contribution = wmf_civicrm_contribution_message_import($msg);
+    $emails = $this->callAPISuccess('Email', 'get', array('contact_id' => 
self::$fixtures->contact_id, 'sequential' => 1));
+    $this->assertEquals(1, $emails['count']);
+
+    $this->assertEquals(0, $emails['values'][0]['on_hold']);
+    $this->assertEquals('pan...@wikimedia.org', $emails['values'][0]['email']);
+
+    $this->callAPISuccess('Contribution', 'delete', array('id' => 
$contribution['id']));
+  }
+
+
+  public function testDuplicateHandling() {
         $fixtures = CiviFixtures::create();
         $error = null;
         $msg = array(
diff --git a/sites/all/modules/wmf_civicrm/wmf_civicrm.module 
b/sites/all/modules/wmf_civicrm/wmf_civicrm.module
index 51f4683..8888087 100644
--- a/sites/all/modules/wmf_civicrm/wmf_civicrm.module
+++ b/sites/all/modules/wmf_civicrm/wmf_civicrm.module
@@ -1401,8 +1401,9 @@
 /**
  * Updates the email for a contact.
  *
- * @param string $msg
+ * @param array $msg
  * @param int $contact_id
+ *
  * @throws \WmfException
  */
 function wmf_civicrm_message_email_update($msg, $contact_id ) {
@@ -1413,16 +1414,34 @@
   try {
     $loc_type_id = isset($msg['email_location_type_id']) ? 
$msg['email_location_type_id'] : wmf_civicrm_get_default_location_type_id();
     $isPrimary = isset($msg['email_location_type_id']) ? 0 : 1;
-    civicrm_api3( "Email", "Replace", array(
-      'debug' => 1,
+
+    $emailParams = array(
+      'email' => $msg['email'],
+      'is_primary' => $isPrimary,
+      'is_billing' => $isPrimary,
+      'contact_id' => $contact_id,
+    );
+
+    // Look up contact's existing email to get the id and to determine
+    // if the email has changed.
+    $existingEmails = civicrm_api3( "Email", 'get', array(
       'location_type_id' => $loc_type_id,
       'contact_id' => $contact_id,
-      'values' => array(array(
-        'email' => $msg[ 'email' ],
-        'is_primary' => $isPrimary,
-        'is_billing' => $isPrimary,
-      ),
-    )));
+      'sequential' => 1,
+    ));
+
+    if ($existingEmails['count']) {
+      $existingEmail = $existingEmails['values'][0];
+      if (strtolower($existingEmail['email']) === strtolower($msg['email'])) {
+        // A case could be made for ensuring isPrimary / isBilling it set
+        // but unclear if the incoming would necesarily be more 
primary-y/billingy
+        return;
+      }
+      $emailParams['id'] = $existingEmail['id'];
+      $emailParams['on_hold'] = 0;
+    }
+
+    civicrm_api3( "Email", "create", $emailParams);
   }
   catch (CiviCRM_API3_Exception $e) {
     throw new WmfException( 'IMPORT_CONTACT', "Couldn't store email for the 
contact.", $e->getExtraParams());

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

Gerrit-MessageType: merged
Gerrit-Change-Id: If092a9131c85d5ec332818d1eb9984142c1c3dda
Gerrit-PatchSet: 8
Gerrit-Project: wikimedia/fundraising/crm
Gerrit-Branch: master
Gerrit-Owner: Eileen <emcnaugh...@wikimedia.org>
Gerrit-Reviewer: Eileen <emcnaugh...@wikimedia.org>
Gerrit-Reviewer: Ejegg <ej...@ejegg.com>
Gerrit-Reviewer: jenkins-bot <>

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

Reply via email to