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

Change subject: Check contact_hash if contact_id exists
......................................................................


Check contact_hash if contact_id exists

This is to enable passing contact_id from the payments frontend.
The frontend will only pass contact_id along with contact_hash.
When both are present, check that the hash matches the contact id
before updating the contact. Note that we only update email and
mailing address, not name.

Bug: T177663
Change-Id: Id8bbd8711b6742b99a552e363fe604b73df33385
---
M sites/all/modules/wmf_civicrm/tests/phpunit/ImportMessageTest.php
M sites/all/modules/wmf_civicrm/wmf_civicrm.module
2 files changed, 93 insertions(+), 1 deletion(-)

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



diff --git a/sites/all/modules/wmf_civicrm/tests/phpunit/ImportMessageTest.php 
b/sites/all/modules/wmf_civicrm/tests/phpunit/ImportMessageTest.php
index 314d9ca..a6fb81c 100644
--- a/sites/all/modules/wmf_civicrm/tests/phpunit/ImportMessageTest.php
+++ b/sites/all/modules/wmf_civicrm/tests/phpunit/ImportMessageTest.php
@@ -10,6 +10,7 @@
 class ImportMessageTest extends BaseWmfDrupalPhpUnitTestCase {
     protected $contact_custom_mangle;
     protected $contribution_id;
+    protected $contact_id;
     protected $contribution_custom_mangle;
     static protected $fixtures;
 
@@ -59,6 +60,9 @@
     public function tearDown() {
         if ( $this->contribution_id ) {
           $this->callAPISuccess('Contribution', 'delete', array('id' => 
$this->contribution_id));
+        }
+        if ( $this->contact_id ) {
+            $this->cleanUpContact( $this->contact_id );
         }
         parent::tearDown();
     }
@@ -683,6 +687,81 @@
     }
 
   /**
+   * When we get a contact ID and matching hash, update instead of create new
+   * @group contactHash
+   */
+  public function testImportWithContactIdAndHash() {
+    $existingContact = civicrm_api3('Contact', 'Create', array(
+      'contact_type' => 'Individual',
+      'first_name' => 'Test',
+      'last_name' => 'Es' . mt_rand()
+    ));
+    $this->contact_id = $existingContact['id'];
+    $existingContact = $existingContact['values'][$existingContact['id']];
+    civicrm_api3('Email', 'Create', array(
+      'contact_id' => $this->contact_id,
+      'email' => 'booboo' . mt_rand() . '@example.org',
+      'location_type_id' => 1,
+    ));
+    $msg = array(
+      'contact_id' => $existingContact['id'],
+      'contact_hash' => $existingContact['hash'],
+      'currency' => 'USD',
+      'date' => '2017-01-01 00:00:00',
+      'invoice_id' => mt_rand(),
+      'email' => 'newspecialem...@wikimedia.org',
+      'gateway' => 'test_gateway',
+      'gateway_txn_id' => mt_rand(),
+      'gross' => '1.25',
+      'payment_method' => 'cc',
+    );
+    $contribution = wmf_civicrm_contribution_message_import($msg);
+    $this->assertEquals($existingContact['id'], $contribution['contact_id']);
+    $email = $this->callAPISuccessGetSingle(
+      'Email', array('contact_id' => $existingContact['id'], 'location_type' 
=> 1)
+    );
+    $this->assertEquals($msg['email'], $email['email']);
+  }
+
+  /**
+   * If we get a contact ID and a bad hash, leave the existing contact alone
+   * @group contactHash
+   */
+  public function testImportWithContactIdAndBadHash() {
+    $existingContact = civicrm_api3('Contact', 'Create', array(
+      'contact_type' => 'Individual',
+      'first_name' => 'Test',
+      'last_name' => 'Es' . mt_rand()
+    ));
+    $this->contact_id = $existingContact['id'];
+    $existingContact = $existingContact['values'][$existingContact['id']];
+    civicrm_api3('Email', 'Create', array(
+      'contact_id' => $this->contact_id,
+      'email' => 'booboo' . mt_rand() . '@example.org',
+      'location_type_id' => 1,
+    ));
+    $msg = array(
+      'contact_id' => $existingContact['id'],
+      'first_name' => 'Lex',
+      'contact_hash' => 'This is not a valid hash',
+      'currency' => 'USD',
+      'date' => '2017-01-01 00:00:00',
+      'invoice_id' => mt_rand(),
+      'email' => 'newspecialem...@wikimedia.org',
+      'gateway' => 'test_gateway',
+      'gateway_txn_id' => mt_rand(),
+      'gross' => '1.25',
+      'payment_method' => 'cc',
+    );
+    $contribution = wmf_civicrm_contribution_message_import($msg);
+    $this->assertNotEquals($existingContact['id'], 
$contribution['contact_id']);
+    $email = $this->callAPISuccessGetSingle(
+      'Email', array('contact_id' => $existingContact['id'], 'location_type' 
=> 1)
+    );
+    $this->assertNotEquals($msg['email'], $email['email']);
+  }
+
+  /**
    * Assert that 2 arrays are the same in all the ways that matter :-).
    *
    * This has been written for a specific test & will probably take extra work
diff --git a/sites/all/modules/wmf_civicrm/wmf_civicrm.module 
b/sites/all/modules/wmf_civicrm/wmf_civicrm.module
index 8fcef8d..90ca939 100644
--- a/sites/all/modules/wmf_civicrm/wmf_civicrm.module
+++ b/sites/all/modules/wmf_civicrm/wmf_civicrm.module
@@ -222,7 +222,20 @@
             throw new WmfException( 'INVALID_MESSAGE', 'Recurring donation, 
but no subscription ID found.' );
         }
     }
-
+    if ($msg['contact_id'] && isset($msg['contact_hash'])) {
+      // This message came from the donations front-end. We need to verify
+      // the hash before using the existing contact.
+      $existing = civicrm_api3('Contact', 'getSingle', array(
+        'id' => $msg['contact_id'],
+        'return' => 'hash'
+      ));
+      // If the contact doesn't exist, or the hash doesn't match, make it
+      // look like it's a new-donor message.
+      if (!$existing || $existing['hash'] !== $msg['contact_hash']) {
+        $msg['contact_id'] = null;
+        unset($msg['contact_hash']);
+      }
+    }
     if ( !$msg['contact_id'] ) {
       wmf_civicrm_message_create_contact($msg);
     }

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

Gerrit-MessageType: merged
Gerrit-Change-Id: Id8bbd8711b6742b99a552e363fe604b73df33385
Gerrit-PatchSet: 11
Gerrit-Project: wikimedia/fundraising/crm
Gerrit-Branch: master
Gerrit-Owner: Ejegg <ej...@ejegg.com>
Gerrit-Reviewer: XenoRyet <dkozlow...@wikimedia.org>
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