Awight has uploaded a new change for review.

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

Change subject: Implement contact relationship import
......................................................................

Implement contact relationship import

Bug: T88836
Change-Id: I67725bca691b78efd38597559fa93a6687ca84ce
---
M sites/all/modules/offline2civicrm/ChecksFile.php
A sites/all/modules/wmf_civicrm/tests/phpunit/RelationshipTest.php
M sites/all/modules/wmf_civicrm/wmf_civicrm.module
3 files changed, 113 insertions(+), 0 deletions(-)


  git pull ssh://gerrit.wikimedia.org:29418/wikimedia/fundraising/crm 
refs/changes/28/220628/1

diff --git a/sites/all/modules/offline2civicrm/ChecksFile.php 
b/sites/all/modules/offline2civicrm/ChecksFile.php
index afb7c7c..16f9aab 100644
--- a/sites/all/modules/offline2civicrm/ChecksFile.php
+++ b/sites/all/modules/offline2civicrm/ChecksFile.php
@@ -249,6 +249,8 @@
      * FIXME: We need to wrap each loop iteration in a transaction to
      * make this safe.  Otherwise we can easily die before adding the
      * second message, and skip it when resuming the import.
+     *
+     * @param array $contribution
      */
     protected function mungeContribution( $contribution ) {
     }
@@ -302,6 +304,7 @@
             'Postmark Date' => 'postmark_date',
             'Prefix' => 'name_prefix',
             'Received Date' => 'date',
+            'Relationship Type' => 'relationship_type',
             'Restrictions' => 'restrictions',
             'Soft Credit To' => 'soft_credit_to',
             'Source' => 'contribution_source',
@@ -309,6 +312,7 @@
             'Street Address' => 'street_address',
             'Suffix' => 'name_suffix',
             'Tags' => 'contact_tags',
+            'Target Contact ID' => 'relationship_target_contact_id',
             'Thank You Letter Date' => 'thankyou_date',
             'Title' => 'org_contact_title',
             'Total Amount' => 'gross', # deprecated, use Original Amount
diff --git a/sites/all/modules/wmf_civicrm/tests/phpunit/RelationshipTest.php 
b/sites/all/modules/wmf_civicrm/tests/phpunit/RelationshipTest.php
new file mode 100644
index 0000000..bd32e5a
--- /dev/null
+++ b/sites/all/modules/wmf_civicrm/tests/phpunit/RelationshipTest.php
@@ -0,0 +1,85 @@
+<?php
+
+class RelationshipTest extends BaseWmfDrupalPhpUnitTestCase {
+    public static function getInfo() {
+        return array(
+            'name' => 'Relationship',
+            'group' => 'Pipeline',
+            'description' => 'Ensure we record contact relationships.',
+        );
+    }
+
+    public function testRelationship() {
+        $fixtures = CiviFixtures::create();
+
+        $msg = array(
+            'currency' => 'USD',
+            'date' => time(),
+            'email' => '[email protected]',
+            'gateway' => 'test_gateway',
+            'gateway_txn_id' => mt_rand(),
+            'gross' => '1.23',
+            'payment_method' => 'cc',
+
+            'relationship_target_contact_id' => $fixtures->contact_id,
+            'relationship_type' => 'Spouse of',
+        );
+
+        $contribution = wmf_civicrm_contribution_message_import( $msg );
+
+        $api = civicrm_api_classapi();
+
+        $api->RelationshipType->Get( array(
+            'name_a_b' => 'Spouse of',
+        ) );
+        $relationship_type_id = $api->values[0]->id;
+
+        $api->Relationship->Get( array(
+            'contact_id_a' => $contribution['contact_id'],
+        ) );
+
+        $this->assertEquals( $relationship_type_id, 
$api->values[0]->relationship_type_id );
+    }
+
+    /**
+     * @expectedException WmfException
+     */
+    public function testBadRelationshipTarget() {
+        $msg = array(
+            'currency' => 'USD',
+            'date' => time(),
+            'email' => '[email protected]',
+            'gateway' => 'test_gateway',
+            'gateway_txn_id' => mt_rand(),
+            'gross' => '1.23',
+            'payment_method' => 'cc',
+
+            'relationship_target_contact_id' => mt_rand(),
+            'relationship_type' => 'Spouse of',
+        );
+
+        $contribution = wmf_civicrm_contribution_message_import( $msg );
+    }
+
+    /**
+     * @expectedException WmfException
+     */
+    public function testBadRelationshipType() {
+        $fixtures = CiviFixtures::create();
+
+        $msg = array(
+            'currency' => 'USD',
+            'date' => time(),
+            'email' => '[email protected]',
+            'gateway' => 'test_gateway',
+            'gateway_txn_id' => mt_rand(),
+            'gross' => '1.23',
+            'payment_method' => 'cc',
+
+            'relationship_target_contact_id' => $fixtures->contact_id,
+            'relationship_type' => 'Total stranger to',
+        );
+
+        $contribution = wmf_civicrm_contribution_message_import( $msg );
+    }
+}
diff --git a/sites/all/modules/wmf_civicrm/wmf_civicrm.module 
b/sites/all/modules/wmf_civicrm/wmf_civicrm.module
index 10ce909..f8d65cf 100644
--- a/sites/all/modules/wmf_civicrm/wmf_civicrm.module
+++ b/sites/all/modules/wmf_civicrm/wmf_civicrm.module
@@ -188,6 +188,9 @@
 /**
  * Try to import a transaction message into CiviCRM, otherwise
  * throw an exception.
+ *
+ * @param array $msg
+ * @return array Contribution as inserted
  */
 function wmf_civicrm_contribution_message_import( &$msg ) {
     civicrm_initialize();
@@ -1005,6 +1008,27 @@
         }
     }
 
+    // Create a relationship to an existing contact?
+    if ( !empty( $msg['relationship_target_contact_id'] ) ) {
+        $relationship_type = civicrm_api3( "RelationshipType", "Get", array(
+            'name_a_b' => $msg['relationship_type'],
+        ) );
+        if ( !$relationship_type['count'] ) {
+            throw new WmfException( 'IMPORT_CONTACT', "Bad relationship type: 
{$msg['relationship_type']}" );
+        }
+
+        try {
+            civicrm_api3( "Relationship", "Create", array(
+                'contact_id_a' => $contact_id,
+                'contact_id_b' => $msg['relationship_target_contact_id'],
+                'relationship_type_id' => $relationship_type['id'],
+                'is_active' => 1,
+            ) );
+        } catch ( CiviCRM_API3_Exception $ex ) {
+            throw new WmfException( 'IMPORT_CONTACT', $ex->getMessage() );
+        }
+    }
+
     return $contact_result;
 }
 

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

Gerrit-MessageType: newchange
Gerrit-Change-Id: I67725bca691b78efd38597559fa93a6687ca84ce
Gerrit-PatchSet: 1
Gerrit-Project: wikimedia/fundraising/crm
Gerrit-Branch: master
Gerrit-Owner: Awight <[email protected]>

_______________________________________________
MediaWiki-commits mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits

Reply via email to