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)
committerJitendra Purohit <jitendra@fuzion.co.nz>
Fri, 9 Apr 2021 14:40:37 +0000 (20:10 +0530)
CRM/Member/Form/Membership.php
tests/phpunit/CRM/Member/Form/MembershipTest.php

index a074540ff9de1aab99d6b236a8be209f5601c232..18ec26e81e2c3244278f9d153015e3c798e7bdcd 100644 (file)
@@ -1295,7 +1295,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 e649cd86ecaa41cc7af06517c72ee72830c7ff47..1c37f7a0d61c07c9aa6afd40284d522b23a2f382 100644 (file)
@@ -903,6 +903,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.
    *