Improve PayPalIPN unit tests
authorMatthew Wire <devel@mrwire.co.uk>
Fri, 6 Jul 2018 16:34:18 +0000 (17:34 +0100)
committerMatthew Wire (MJW Consulting) <mjw@mjwconsult.co.uk>
Mon, 23 Jul 2018 17:08:37 +0000 (18:08 +0100)
CRM/Core/Payment/PayPalIPN.php
tests/phpunit/CRM/Core/Payment/PayPalIPNTest.php
tests/phpunit/CiviTest/CiviUnitTestCase.php

index ff16f6af5d0501c70f95a98b0a671e5c85b51adf..fa4f164a420f5b8798b214d847d3d4a59dbcb79d 100644 (file)
@@ -226,7 +226,6 @@ class CRM_Core_Payment_PayPalIPN extends CRM_Core_Payment_BaseIPN {
       // In future moving to create pending & then complete, but this OK for now.
       // Also consider accepting 'Failed' like other processors.
       $input['contribution_status_id'] = $contributionStatuses['Completed'];
-      $input['invoice_id'] = md5(uniqid(rand(), TRUE));
       $input['original_contribution_id'] = $ids['contribution'];
       $input['contribution_recur_id'] = $ids['contributionRecur'];
 
index 8e13270b4d563319d0eea3bb91a5920545125450..9de32d4ea3a8bc052cc5f26a9f99f10d5e69fd09 100644 (file)
@@ -121,24 +121,37 @@ class CRM_Core_Payment_PayPalIPNTest extends CiviUnitTestCase {
    * The second will create a new contribution.
    */
   public function testIPNPaymentRecurSuccess() {
-    $this->setupRecurringPaymentProcessorTransaction();
+    $this->setupRecurringPaymentProcessorTransaction([], ['total_amount' => '15.00']);
     $paypalIPN = new CRM_Core_Payment_PayPalIPN($this->getPaypalRecurTransaction());
     $paypalIPN->main();
-    $contribution = $this->callAPISuccess('contribution', 'getsingle', array('id' => $this->_contributionID));
-    $this->assertEquals(1, $contribution['contribution_status_id']);
-    $this->assertEquals('8XA571746W2698126', $contribution['trxn_id']);
+    $contribution1 = $this->callAPISuccess('contribution', 'getsingle', array('id' => $this->_contributionID));
+    $this->assertEquals(1, $contribution1['contribution_status_id']);
+    $this->assertEquals('8XA571746W2698126', $contribution1['trxn_id']);
     // source gets set by processor
-    $this->assertTrue(substr($contribution['contribution_source'], 0, 20) == "Online Contribution:");
+    $this->assertTrue(substr($contribution1['contribution_source'], 0, 20) == "Online Contribution:");
     $contributionRecur = $this->callAPISuccess('contribution_recur', 'getsingle', array('id' => $this->_contributionRecurID));
     $this->assertEquals(5, $contributionRecur['contribution_status_id']);
     $paypalIPN = new CRM_Core_Payment_PayPalIPN($this->getPaypalRecurSubsequentTransaction());
     $paypalIPN->main();
-    $contribution = $this->callAPISuccess('contribution', 'get', array(
+    $contributions = $this->callAPISuccess('contribution', 'get', array(
         'contribution_recur_id' => $this->_contributionRecurID,
         'sequential' => 1,
       ));
-    $this->assertEquals(2, $contribution['count']);
-    $this->assertEquals('secondone', $contribution['values'][1]['trxn_id']);
+    $this->assertEquals(2, $contributions['count']);
+    $contribution2 = $contributions['values'][1];
+    $this->assertEquals('secondone', $contribution2['trxn_id']);
+    $paramsThatShouldMatch = [
+      'total_amount',
+      'net_amount',
+      'fee_amount',
+      'payment_instrument',
+      'payment_instrument_id',
+      'financial_type',
+      'financial_type_id',
+    ];
+    foreach ($paramsThatShouldMatch as $match) {
+      $this->assertEquals($contribution1[$match], $contribution2[$match]);
+    }
   }
 
   /**
index 71cd39915efe51d5b7e75091c05c49c841df2142..89d4720d09dd3821c11ffd60acd43faa7e00531f 100644 (file)
@@ -2775,8 +2775,24 @@ AND    ( TABLE_NAME LIKE 'civicrm_value_%' )
 
   /**
    * Set up initial recurring payment allowing subsequent IPN payments.
+   *
+   * @param array $recurParams (Optional)
+   * @param array $contributionParams (Optional)
    */
-  public function setupRecurringPaymentProcessorTransaction($params = array()) {
+  public function setupRecurringPaymentProcessorTransaction($recurParams = [], $contributionParams = []) {
+    $contributionParams = array_merge([
+        'total_amount' => '200',
+        'invoice_id' => $this->_invoiceID,
+        'financial_type_id' => 'Donation',
+        'contribution_status_id' => 'Pending',
+        'contact_id' => $this->_contactID,
+        'contribution_page_id' => $this->_contributionPageID,
+        'payment_processor_id' => $this->_paymentProcessorID,
+        'is_test' => 0,
+        'skipCleanMoney' => TRUE,
+      ],
+      $contributionParams
+    );
     $contributionRecur = $this->callAPISuccess('contribution_recur', 'create', array_merge(array(
       'contact_id' => $this->_contactID,
       'amount' => 1000,
@@ -2789,18 +2805,8 @@ AND    ( TABLE_NAME LIKE 'civicrm_value_%' )
       'payment_processor_id' => $this->_paymentProcessorID,
       // processor provided ID - use contact ID as proxy.
       'processor_id' => $this->_contactID,
-      'api.contribution.create' => array(
-        'total_amount' => '200',
-        'invoice_id' => $this->_invoiceID,
-        'financial_type_id' => 1,
-        'contribution_status_id' => 'Pending',
-        'contact_id' => $this->_contactID,
-        'contribution_page_id' => $this->_contributionPageID,
-        'payment_processor_id' => $this->_paymentProcessorID,
-        'is_test' => 0,
-        'skipCleanMoney' => TRUE,
-      ),
-    ), $params));
+      'api.contribution.create' => $contributionParams,
+    ), $recurParams));
     $this->_contributionRecurID = $contributionRecur['id'];
     $this->_contributionID = $contributionRecur['values']['0']['api.contribution.create']['id'];
   }