Fix soft credit on creating membership using backend form
authorJitendra Purohit <jitendra@fuzion.co.nz>
Fri, 9 Apr 2021 14:40:37 +0000 (20:10 +0530)
committereileen <emcnaughton@wikimedia.org>
Sat, 10 Apr 2021 00:43:46 +0000 (12:43 +1200)
CRM/Member/Form/Membership.php
tests/phpunit/CRM/Member/Form/MembershipTest.php

index 5a1c37eff22cac813fd658458ab062df8ffd5328..7e0bd64505d86e7159d97882345aafcc3e2622b0 100644 (file)
@@ -1410,7 +1410,7 @@ DESC limit 1");
         $membershipParams = array_merge($params, $membershipTypeValues[$lineItemValues['membership_type_id']]);
 
         if (!empty($softParams)) {
-          $membershipParams['soft_credit'] = $softParams;
+          $params['soft_credit'] = $softParams;
         }
         unset($membershipParams['contribution_status_id']);
         $membershipParams['skipLineItem'] = TRUE;
index d3591809b496a7a2a3203011f5cfa80dd7accb94..5d140290bc45574405b590f3bc1e6dbf9c3502fa 100644 (file)
@@ -887,6 +887,43 @@ class CRM_Member_Form_MembershipTest extends CiviUnitTestCase {
     }
   }
 
+  /**
+   * Test membership with soft credits.
+   */
+  public function testMembershipSoftCredit() {
+    $this->_softIndividualId = $this->individualCreate();
+
+    $form = $this->getForm();
+    $form->preProcess();
+    $this->createLoggedInUser();
+    $params = $this->getBaseSubmitParams();
+    unset($params['auto_renew'], $params['is_recur']);
+    $params['record_contribution'] = TRUE;
+    $params['soft_credit_type_id'] = $this->callAPISuccessGetValue('OptionValue', [
+      'return' => "value",
+      'name' => "Gift",
+      'option_group_id' => "soft_credit_type",
+    ]);
+    $params['soft_credit_contact_id'] = $this->_softIndividualId;
+    $form->_contactID = $this->_individualId;
+    // $form->_mode = 'test';
+    $form->testSubmit($params);
+    // Membership is created on main contact.
+    $this->callAPISuccessGetSingle('Membership', ['contact_id' => $this->_individualId]);
+
+    // Verify is main contribution is created on soft contact.
+    $contribution = $this->callAPISuccessGetSingle('Contribution', [
+      'contact_id' => $this->_softIndividualId,
+    ]);
+    $this->assertEquals($contribution['soft_credit'][1]['contact_id'], $this->_individualId);
+
+    // Verify if soft credit is created.
+    $this->callAPISuccessGetSingle('ContributionSoft', [
+      'contact_id' => $this->_individualId,
+      'contribution_id' => $contribution['id'],
+    ]);
+  }
+
   /**
    * Test the submit function of the membership form.
    *