From f2c927f50def26d0a9f315da15ddebdf841799fd Mon Sep 17 00:00:00 2001
From: eileen <emcnaughton@wikimedia.org>
Date: Tue, 19 Jan 2021 10:04:39 +1300
Subject: [PATCH] dev/financial#148 fully deprecate loadObjects function

---
 CRM/Core/Payment/BaseIPN.php                  |   3 +
 CRM/Core/Payment/PayPalProIPN.php             |  15 ++-
 .../phpunit/CRM/Core/Payment/BaseIPNTest.php  | 116 +-----------------
 3 files changed, 13 insertions(+), 121 deletions(-)

diff --git a/CRM/Core/Payment/BaseIPN.php b/CRM/Core/Payment/BaseIPN.php
index 2d6a7e6f11e..a3c5a590607 100644
--- a/CRM/Core/Payment/BaseIPN.php
+++ b/CRM/Core/Payment/BaseIPN.php
@@ -140,6 +140,8 @@ class CRM_Core_Payment_BaseIPN {
   /**
    * Load objects related to contribution.
    *
+   * @deprecated
+   *
    * @input array information from Payment processor
    *
    * @param array $input
@@ -152,6 +154,7 @@ class CRM_Core_Payment_BaseIPN {
    * @throws \CRM_Core_Exception
    */
   public function loadObjects($input, &$ids, &$objects, $required, $paymentProcessorID) {
+    CRM_Core_Error::deprecatedFunctionWarning('use api methods in ipn');
     $contribution = &$objects['contribution'];
     $ids['paymentProcessor'] = $paymentProcessorID;
     $success = $contribution->loadRelatedObjects($input, $ids);
diff --git a/CRM/Core/Payment/PayPalProIPN.php b/CRM/Core/Payment/PayPalProIPN.php
index 0ea9694d4e3..6c7fbd7c5c8 100644
--- a/CRM/Core/Payment/PayPalProIPN.php
+++ b/CRM/Core/Payment/PayPalProIPN.php
@@ -482,9 +482,10 @@ INNER JOIN civicrm_membership_payment mp ON m.id = mp.membership_id AND mp.contr
         unset($ids['contributionPage']);
       }
 
-      if (!$this->loadObjects($input, $ids, $objects, TRUE, $paymentProcessorID)) {
-        return;
-      }
+      $contribution = &$objects['contribution'];
+      $ids['paymentProcessor'] = $paymentProcessorID;
+      $contribution->loadRelatedObjects($input, $ids);
+      $objects = array_merge($objects, $contribution->_relatedObjects);
 
       $input['payment_processor_id'] = $paymentProcessorID;
 
@@ -640,9 +641,11 @@ INNER JOIN civicrm_membership_payment mp ON m.id = mp.membership_id AND mp.contr
       unset($ids['contributionPage']);
     }
 
-    if (!$this->loadObjects($input, $ids, $objects, TRUE, $paymentProcessorID)) {
-      throw new CRM_Core_Exception('Data did not validate');
-    }
+    $contribution = &$objects['contribution'];
+    $ids['paymentProcessor'] = $paymentProcessorID;
+    $contribution->loadRelatedObjects($input, $ids);
+    $objects = array_merge($objects, $contribution->_relatedObjects);
+
     $this->recur($input, $ids, $objects['contributionRecur'], $objects['contribution'], $isFirst);
   }
 
diff --git a/tests/phpunit/CRM/Core/Payment/BaseIPNTest.php b/tests/phpunit/CRM/Core/Payment/BaseIPNTest.php
index a6cafa18b68..930819c0c3d 100644
--- a/tests/phpunit/CRM/Core/Payment/BaseIPNTest.php
+++ b/tests/phpunit/CRM/Core/Payment/BaseIPNTest.php
@@ -98,43 +98,6 @@ class CRM_Core_Payment_BaseIPNTest extends CiviUnitTestCase {
     CRM_Member_PseudoConstant::membershipStatus(NULL, NULL, 'name', TRUE);
   }
 
-  /**
-   * Test the LoadObjects function with recurring membership data.
-   */
-  public function testLoadMembershipObjects() {
-    $this->_setUpMembershipObjects();
-    $this->_setUpRecurringContribution();
-    $this->IPN->loadObjects($this->input, $this->ids, $this->objects, FALSE, $this->_processorId);
-    $this->assertNotEmpty($this->objects['membership']);
-    $this->assertArrayHasKey($this->_membershipId . '_' . $this->_membershipTypeID, $this->objects['membership']);
-    $this->assertTrue(is_a($this->objects['membership'][$this->_membershipId . '_' . $this->_membershipTypeID], 'CRM_Member_BAO_Membership'));
-    $this->assertTrue(is_a($this->objects['financialType'], 'CRM_Financial_BAO_FinancialType'));
-    $this->assertNotEmpty($this->objects['contributionRecur']);
-    $this->assertNotEmpty($this->objects['paymentProcessor']);
-  }
-
-  /**
-   * Test the LoadObjects function with recurring membership data.
-   */
-  public function testLoadMembershipObjectsNoLeakage() {
-    $this->_setUpMembershipObjects();
-    $this->_setUpRecurringContribution();
-    $this->IPN->loadObjects($this->input, $this->ids, $this->objects, FALSE, $this->_processorId);
-    $this->assertEquals('Anthony', $this->objects['contact']->first_name);
-
-    $this->ids['contact'] = $this->_contactId = $this->individualCreate([
-      'first_name' => 'Donald',
-      'last_name' => 'Duck',
-      'email' => 'the-don@duckville.com',
-    ]);
-    $contribution = $this->callAPISuccess('contribution', 'create', array_merge($this->_contributionParams, ['invoice_id' => 'abc']));
-    $this->_contributionId = $contribution['id'];
-    $this->_setUpMembershipObjects();
-    $this->input['invoiceID'] = 'abc';
-    $this->IPN->loadObjects($this->input, $this->ids, $this->objects, FALSE, $this->_processorId);
-    $this->assertEquals('Donald', $this->objects['contact']->first_name);
-  }
-
   /**
    * Test the LoadObjects function with recurring membership data.
    */
@@ -189,8 +152,6 @@ class CRM_Core_Payment_BaseIPNTest extends CiviUnitTestCase {
     $this->_membershipTypeID = $this->membershipTypeCreate(['name' => 'Fowl']);
     $this->_setUpMembershipObjects();
     $this->input['invoiceID'] = 'abc';
-    $this->IPN->loadObjects($this->input, $this->ids, $this->objects, FALSE, $this->_processorId);
-    $this->assertEquals('Donald', $this->objects['contact']->first_name);
     $contribution = new CRM_Contribute_BAO_Contribution();
     $contribution->id = $this->_contributionId;
     $msg = $contribution->composeMessageArray($this->input, $this->ids, $values);
@@ -211,31 +172,14 @@ class CRM_Core_Payment_BaseIPNTest extends CiviUnitTestCase {
     $this->assertContains('Membership Type: General', $msg['body']);
   }
 
-  /**
-   * Test that loadObjects works with participant values.
-   */
-  public function testLoadParticipantObjects() {
-    $this->_setUpParticipantObjects();
-    $this->IPN->loadObjects($this->input, $this->ids, $this->objects, FALSE, $this->_processorId);
-    $this->assertNotEmpty($this->objects['participant']);
-    $this->assertTrue(is_a($this->objects['participant'], 'CRM_Event_BAO_Participant'));
-    $this->assertTrue(is_a($this->objects['financialType'], 'CRM_Financial_BAO_FinancialType'));
-    $this->assertNotEmpty($this->objects['event']);
-    $this->assertTrue(is_a($this->objects['event'], 'CRM_Event_BAO_Event'));
-    $this->assertTrue(is_a($this->objects['contribution'], 'CRM_Contribute_BAO_Contribution'));
-    $this->assertNotEmpty($this->objects['event']->id);
-  }
-
   /**
    * Test the LoadObjects function with a participant.
    */
   public function testComposeMailParticipant() {
     $this->_setUpParticipantObjects();
-    $this->IPN->loadObjects($this->input, $this->ids, $this->objects, FALSE, $this->_processorId);
-    $values = [];
-    $this->assertNotEmpty($this->objects['event']);
     $contribution = new CRM_Contribute_BAO_Contribution();
     $contribution->id = $this->_contributionId;
+    $contribution->loadRelatedObjects($this->input, $this->ids);
     $msg = $contribution->composeMessageArray($this->input, $this->ids, $values);
     $this->assertContains('registration has been received and your status has been updated to Attended.', $msg['body']);
     $this->assertContains('Annual CiviCRM meet', $msg['html']);
@@ -291,43 +235,6 @@ class CRM_Core_Payment_BaseIPNTest extends CiviUnitTestCase {
     $mut->stop();
   }
 
-  /**
-   * Test that loadObjects works with participant values.
-   */
-  public function testLoadPledgeObjects() {
-    $this->_setUpPledgeObjects();
-    $this->IPN->loadObjects($this->input, $this->ids, $this->objects, FALSE, $this->_processorId);
-    $this->assertNotEmpty($this->objects['pledge_payment'][0]);
-    $this->assertTrue(is_a($this->objects['financialType'], 'CRM_Financial_BAO_FinancialType'));
-    $this->assertTrue(is_a($this->objects['contribution'], 'CRM_Contribute_BAO_Contribution'));
-    $this->assertTrue(is_a($this->objects['pledge_payment'][0], 'CRM_Pledge_BAO_PledgePayment'));
-    $this->assertNotEmpty($this->objects['pledge_payment'][0]->id);
-    $this->assertEquals($this->_financialTypeId, $this->objects['financialType']->id);
-    $this->assertEquals($this->_processorId, $this->objects['paymentProcessor']['id']);
-    $this->assertEquals($this->_contributionId, $this->objects['contribution']->id);
-    $this->assertEquals($this->_contactId, $this->objects['contact']->id);
-    $this->assertEquals($this->_pledgeId, $this->objects['pledge_payment'][0]->pledge_id);
-  }
-
-  /**
-   * Test that loadObjects works with participant values.
-   */
-  public function testLoadPledgeObjectsInvalidPledgeID() {
-    $this->_setUpPledgeObjects();
-    $this->ids['pledge_payment'][0] = 0;
-
-    $this->IPN->loadObjects($this->input, $this->ids, $this->objects, TRUE, NULL);
-    $this->assertArrayNotHasKey('pledge_payment', $this->objects);
-
-    $this->ids['pledge_payment'][0] = 999;
-    try {
-      $this->IPN->loadObjects($this->input, $this->ids, $this->objects, TRUE, $this->_processorId);
-    }
-    catch (CRM_Core_Exception $e) {
-      $this->assertEquals('Could not find pledge payment record: 999', $e->getMessage());
-    }
-  }
-
   /**
    * Test the LoadObjects function with a pledge.
    */
@@ -339,27 +246,6 @@ class CRM_Core_Payment_BaseIPNTest extends CiviUnitTestCase {
     $this->assertContains('Contribution Information', $msg['html']);
   }
 
-  /**
-   * Test that an error is returned if required set & no contribution page.
-   */
-  public function testRequiredWithoutProcessorID() {
-    $this->_setUpPledgeObjects();
-    // error is only returned if $required set to True
-    $result = $this->IPN->loadObjects($this->input, $this->ids, $this->objects, FALSE, NULL);
-    $this->assertEquals(TRUE, $result);
-  }
-
-  /**
-   * Test that if part of $input the payment processor loads OK.
-   *
-   * It's preferable to pass it in as it cannot be correctly calculated.
-   */
-  public function testPaymentProcessorLoadsAsParam() {
-    $this->_setUpContributionObjects();
-    $this->input = array_merge($this->input, ['payment_processor_id' => $this->_processorId]);
-    $this->assertTrue($this->IPN->loadObjects($this->input, $this->ids, $this->objects, TRUE, NULL));
-  }
-
   public function testThatCancellingEventPaymentWillCancelAllAdditionalPendingParticipantsAndCreateCancellationActivities() {
     $this->_setUpParticipantObjects('Pending from incomplete transaction');
     $additionalParticipantId = $this->participantCreate([
-- 
GitLab