Add test to ensure correct assignment of activities for on-behalf repeattransactions
authoreileen <emcnaughton@wikimedia.org>
Mon, 14 Dec 2020 01:24:22 +0000 (14:24 +1300)
committereileen <emcnaughton@wikimedia.org>
Wed, 23 Dec 2020 20:22:57 +0000 (09:22 +1300)
CRM/Contribute/BAO/ContributionPage.php
CRM/Financial/BAO/Payment.php
tests/phpunit/CRM/Contribute/Form/Contribution/ConfirmTest.php

index 90e8ec6ef594883aa2884edcd4e6fc49f0c9f3bd..f06671dea7aa9213335944719ce4dcd8e2c1c59c 100644 (file)
@@ -45,6 +45,7 @@ class CRM_Contribute_BAO_ContributionPage extends CRM_Contribute_DAO_Contributio
       CRM_Price_BAO_PriceFieldValue::updateFinancialType($params['id'], 'civicrm_contribution_page', $params['financial_type_id']);
     }
     CRM_Utils_Hook::post($hook, 'ContributionPage', $dao->id, $dao);
+    unset(\Civi::$statics['CRM_Core_PseudoConstant']);
     return $dao;
   }
 
index 6642379d62e0714e81c4758aa1e7d83f1dbedcd3..fb2d374f76582cf73a9c3b36558a5026cfd6a9f5 100644 (file)
@@ -155,6 +155,7 @@ class CRM_Financial_BAO_Payment {
           'is_email_receipt' => $params['is_send_contribution_notification'],
           'trxn_date' => $params['trxn_date'],
           'payment_instrument_id' => $paymentTrxnParams['payment_instrument_id'],
+          'payment_processor_id' => $paymentTrxnParams['payment_processor_id'] ?? NULL,
         ]);
         // Get the trxn
         $trxnId = CRM_Core_BAO_FinancialTrxn::getFinancialTrxnId($contribution['id'], 'DESC');
index 0143f443ef40bcfdb0f12d147067f764a1f871d4..84f42bad60a8a3740e2950ad34b0bffec251093e 100644 (file)
@@ -26,53 +26,38 @@ class CRM_Contribute_Form_Contribution_ConfirmTest extends CiviUnitTestCase {
   }
 
   /**
-   * CRM-21200: Test that making online payment for pending contribution doesn't overwite the contribution details
+   * CRM-21200: Test that making online payment for pending contribution
+   * doesn't overwite the contribution details
+   *
+   * @throws \CiviCRM_API3_Exception
+   * @throws \CRM_Core_Exception
    */
-  public function testPayNowPayment() {
-    $contactID = $this->individualCreate();
+  public function testPayNowPayment(): void {
+    $individualID = $this->individualCreate();
     $paymentProcessorID = $this->paymentProcessorCreate(['payment_processor_type_id' => 'Dummy']);
     CRM_Core_Config::singleton()->userPermissionClass->permissions = [];
 
     // create a contribution page which is later used to make pay-later contribution
-    $result = $this->callAPISuccess('ContributionPage', 'create', [
-      'title' => 'Test Contribution Page',
-      'financial_type_id' => CRM_Core_PseudoConstant::getKey('CRM_Contribute_BAO_Contribution', 'financial_type_id', 'Campaign Contribution'),
-      'currency' => 'USD',
-      'financial_account_id' => 1,
-      'payment_processor' => $paymentProcessorID,
-      'is_active' => 1,
-      'is_allow_other_amount' => 1,
-      'min_amount' => 20,
-      'max_amount' => 2000,
-    ]);
-    $contributionPageID1 = $result['id'];
+    $contributionPageID1 = $this->createContributionPage(['payment_processor' => $paymentProcessorID]);
+
     // create pending contribution
     $contribution = $this->callAPISuccess('Contribution', 'create', [
-      'contact_id' => $contactID,
-      'financial_type_id' => CRM_Core_PseudoConstant::getKey('CRM_Contribute_BAO_Contribution', 'financial_type_id', 'Campaign Contribution'),
+      'contact_id' => $individualID,
+      'financial_type_id' => 'Campaign Contribution',
       'currency' => 'USD',
       'total_amount' => 100.00,
-      'contribution_status_id' => CRM_Core_PseudoConstant::getKey('CRM_Contribute_BAO_Contribution', 'contribution_status_id', 'Pending'),
+      'contribution_status_id' => 'Pending',
       'contribution_page_id' => $contributionPageID1,
       'source' => 'backoffice pending contribution',
     ]);
 
     // create a contribution page which is later used to make online payment for pending contribution
-    $result = $this->callAPISuccess('ContributionPage', 'create', [
-      'title' => 'Test Contribution Page',
-      'financial_type_id' => CRM_Core_PseudoConstant::getKey('CRM_Contribute_BAO_Contribution', 'financial_type_id', 'Campaign Contribution'),
-      'currency' => 'USD',
-      'financial_account_id' => 1,
-      'payment_processor' => $paymentProcessorID,
-      'is_active' => 1,
-      'is_allow_other_amount' => 1,
-      'min_amount' => 10,
-      'max_amount' => 1000,
-    ]);
-    $form = new CRM_Contribute_Form_Contribution_Confirm();
-    $contributionPageID2 = $result['id'];
+    $contributionPageID2 = $this->createContributionPage(['payment_processor' => $paymentProcessorID]);
+
+    /* @var CRM_Contribute_Form_Contribution_Confirm $form*/
+    $form = $this->getFormObject('CRM_Contribute_Form_Contribution_Confirm');
     $form->_id = $contributionPageID2;
-    $form->_values = $result['values'][$contributionPageID2];
+
     $form->_paymentProcessor = [
       'id' => $paymentProcessorID,
       'billing_mode' => CRM_Core_Payment::BILLING_MODE_FORM,
@@ -81,7 +66,6 @@ class CRM_Contribute_Form_Contribution_ConfirmTest extends CiviUnitTestCase {
       'payment_instrument_id' => CRM_Core_PseudoConstant::getKey('CRM_Contribute_BAO_Contribution', 'payment_instrument_id', 'Credit card'),
     ];
     $form->_params = [
-      'qfKey' => 'do not care',
       'contribution_id' => $contribution['id'],
       'credit_card_number' => 4111111111111111,
       'cvv2' => 234,
@@ -103,17 +87,19 @@ class CRM_Contribute_Form_Contribution_ConfirmTest extends CiviUnitTestCase {
       'is_quick_config' => 1,
       'description' => $contribution['values'][$contribution['id']]['source'],
       'skipLineItem' => 0,
+      'frequency_interval' => 1,
+      'frequency_unit' => 'month',
     ];
 
     $processConfirmResult = $form->processConfirm(
       $form->_params,
-      $contactID,
-      $form->_values['financial_type_id'],
+      $individualID,
+      CRM_Core_PseudoConstant::getKey('CRM_Contribute_BAO_Contribution', 'financial_type_id', 'Campaign Contribution'),
       0, FALSE
     );
 
     // Make sure that certain parameters are set on return from processConfirm
-    $this->assertEquals($form->_values['financial_type_id'], $processConfirmResult['financial_type_id']);
+    $this->assertEquals('Campaign Contribution', CRM_Core_PseudoConstant::getName('CRM_Contribute_BAO_Contribution', 'financial_type_id', $processConfirmResult['financial_type_id']));
 
     // Based on the processed contribution, complete transaction which update the contribution status based on payment result.
     if (!empty($processConfirmResult['contribution'])) {
@@ -140,39 +126,80 @@ class CRM_Contribute_Form_Contribution_ConfirmTest extends CiviUnitTestCase {
     // check that contribution status is changed to 'Completed' from 'Pending'
     $this->assertEquals('Completed', $contribution['contribution_status']);
 
-    //delete contribution.
+    // Delete contribution.
+    // @todo - figure out why & document properly. If this is just to partially
+    // re-use some test set up then split into 2 tests.
     $this->callAPISuccess('contribution', 'delete', [
       'id' => $processConfirmResult['contribution']->id,
     ]);
 
     //Process on behalf contribution.
     unset($form->_params['contribution_id']);
-    $form->_contactID = $form->_values['related_contact'] = $form->_params['onbehalf_contact_id'] = $contactID;
-    $form->_params['contact_id'] = $this->organizationCreate();
+    $form->_contactID = $form->_values['related_contact'] = $form->_params['onbehalf_contact_id'] = $individualID;
+    $organizationID = $this->organizationCreate();
+    $form->_params['contact_id'] = $organizationID;
     $this->callAPISuccess('Relationship', 'create', [
-      'contact_id_a' => $contactID,
-      'contact_id_b' => $form->_params['contact_id'],
+      'contact_id_a' => $individualID,
+      'contact_id_b' => $organizationID,
       'relationship_type_id' => 5,
       'is_current_employer' => 1,
     ]);
+
+    $form->_params['onbehalf_contact_id'] = $individualID;
+    $form->_values['id'] = $contributionPageID1;
     $form->processConfirm(
       $form->_params,
-      $form->_params['onbehalf_contact_id'],
-      $form->_values['financial_type_id'],
-      0, FALSE
+      $organizationID,
+      CRM_Core_PseudoConstant::getKey('CRM_Contribute_BAO_Contribution', 'financial_type_id', 'Campaign Contribution'),
+      0, TRUE
     );
     //check if contribution is created on org.
     $contribution = $this->callAPISuccessGetSingle('Contribution', [
-      'contact_id' => $form->_params['onbehalf_contact_id'],
+      'contact_id' => $organizationID,
     ]);
 
-    $activity = civicrm_api3('Activity', 'get', [
-      'sequential' => 1,
+    $activity = $this->callAPISuccessGetSingle('Activity', [
       'source_record_id' => $contribution['id'],
       'contact_id' => $form->_params['onbehalf_contact_id'],
       'activity_type_id' => 'Contribution',
+      'return' => 'target_contact_id',
+    ]);
+    $this->assertEquals([$form->_params['contact_id']], $activity['target_contact_id']);
+    $this->assertEquals($individualID, $activity['source_contact_id']);
+    $repeatContribution = $this->callAPISuccess('Contribution', 'repeattransaction', [
+      'original_contribution_id' => $contribution['id'],
+      'contribution_status_id' => 'Pending',
+      'api.Payment.create' => [
+        'total_amount' => 100,
+        'payment_processor_id' => $paymentProcessorID,
+      ],
     ]);
-    $this->assertEquals(1, $activity['count']);
+    $activity = $this->callAPISuccessGetSingle('Activity', [
+      'source_record_id' => $repeatContribution['id'],
+      'activity_type_id' => 'Contribution',
+      'return' => ['target_contact_id', 'source_contact_id'],
+    ]);
+    $this->assertEquals([$organizationID], $activity['target_contact_id']);
+    $this->assertEquals($individualID, $activity['source_contact_id']);
+  }
+
+  /**
+   * @param array $params
+   *
+   * @return mixed
+   * @throws \CRM_Core_Exception
+   */
+  protected function createContributionPage(array $params): int {
+    return (int) $this->callAPISuccess('ContributionPage', 'create', array_merge([
+      'title' => 'Test Contribution Page',
+      'financial_type_id' => 'Campaign Contribution',
+      'currency' => 'USD',
+      'financial_account_id' => 1,
+      'is_active' => 1,
+      'is_allow_other_amount' => 1,
+      'min_amount' => 20,
+      'max_amount' => 2000,
+    ], $params))['id'];
   }
 
 }