Fix data to be valid for one of the a.net tests
authoreileen <emcnaughton@wikimedia.org>
Sun, 10 Nov 2019 23:46:09 +0000 (12:46 +1300)
committereileen <emcnaughton@wikimedia.org>
Mon, 11 Nov 2019 00:50:12 +0000 (13:50 +1300)
The tests were munging the line items in set up - this fixes it - but only for one so far

tests/phpunit/CRM/Core/BAO/FinancialTrxnTest.php
tests/phpunit/CRM/Core/Payment/AuthorizeNetIPNTest.php
tests/phpunit/CRMTraits/Financial/OrderTrait.php [new file with mode: 0644]
tests/phpunit/CiviTest/CiviUnitTestCase.php
tests/phpunit/api/v3/OrderTest.php

index e465da641f1ef66324c616f0b1dbb277c9de3797..dc5f9951d62abc449a6f70577434210febdc653e 100644 (file)
@@ -37,6 +37,8 @@ class CRM_Core_BAO_FinancialTrxnTest extends CiviUnitTestCase {
 
   /**
    * Check method create().
+   *
+   * @throws \CRM_Core_Exception
    */
   public function testCreate() {
     $contactId = $this->individualCreate();
index 8ca6400909828d280851d7a8cfd7b7b2c4103ffa..8b57971ee192e9241e866c2916220f77a6f619c6 100644 (file)
@@ -7,6 +7,8 @@ use Civi\Payment\Exception\PaymentProcessorException;
  * @group headless
  */
 class CRM_Core_Payment_AuthorizeNetIPNTest extends CiviUnitTestCase {
+  use CRMTraits_Financial_OrderTrait;
+
   protected $_contributionID;
   protected $_invoiceID = 'c2r9c15f7be20b4f3fef1f77e4c37424';
   protected $_financialTypeID = 1;
@@ -15,12 +17,16 @@ class CRM_Core_Payment_AuthorizeNetIPNTest extends CiviUnitTestCase {
   protected $_contributionPageID;
   protected $_paymentProcessorID;
 
+  /**
+   *
+   * @throws \CRM_Core_Exception
+   */
   public function setUp() {
     parent::setUp();
     $this->_paymentProcessorID = $this->paymentProcessorAuthorizeNetCreate(['is_test' => 0]);
     $this->_contactID = $this->individualCreate();
     $contributionPage = $this->callAPISuccess('contribution_page', 'create', [
-      'title' => "Test Contribution Page",
+      'title' => 'Test Contribution Page',
       'financial_type_id' => $this->_financialTypeID,
       'currency' => 'USD',
       'payment_processor' => $this->_paymentProcessorID,
@@ -101,6 +107,7 @@ class CRM_Core_Payment_AuthorizeNetIPNTest extends CiviUnitTestCase {
       $this->markTestSkipped('Error from A.net - cannot proceed');
     }
     $this->_contributionID = $contribution->id;
+    $this->ids['Contribution'][0] = $contribution->id;
     $this->_contributionRecurID = $contribution->contribution_recur_id;
     $recur_params = [
       'id' => $this->_contributionRecurID,
@@ -126,6 +133,8 @@ class CRM_Core_Payment_AuthorizeNetIPNTest extends CiviUnitTestCase {
 
   /**
    * Test IPN response updates contribution_recur & contribution for first & second contribution
+   *
+   * @throws \CRM_Core_Exception
    */
   public function testIPNPaymentRecurSuccess() {
     $this->setupRecurringPaymentProcessorTransaction();
@@ -198,14 +207,15 @@ class CRM_Core_Payment_AuthorizeNetIPNTest extends CiviUnitTestCase {
    * Test IPN response updates contribution_recur & contribution for first & second contribution
    */
   public function testIPNPaymentMembershipRecurSuccess() {
-    $this->setupMembershipRecurringPaymentProcessorTransaction();
+    $this->createRepeatMembershipOrder();
     $IPN = new CRM_Core_Payment_AuthorizeNetIPN($this->getRecurTransaction());
     $IPN->main();
-    $contribution = $this->callAPISuccess('contribution', 'getsingle', ['id' => $this->_contributionID]);
+    $contribution = $this->callAPISuccess('contribution', 'getsingle', ['id' => $this->ids['Contribution'][0]]);
     $this->assertEquals(1, $contribution['contribution_status_id']);
     $this->assertEquals('6511143069', $contribution['trxn_id']);
+
     // source gets set by processor
-    $this->assertTrue(substr($contribution['contribution_source'], 0, 20) == "Online Contribution:");
+    $this->assertEquals('Online Contribution:', substr($contribution['contribution_source'], 0, 20));
     $contributionRecur = $this->callAPISuccess('contribution_recur', 'getsingle', ['id' => $this->_contributionRecurID]);
     $this->assertEquals(5, $contributionRecur['contribution_status_id']);
     $IPN = new CRM_Core_Payment_AuthorizeNetIPN($this->getRecurSubsequentTransaction());
@@ -224,6 +234,8 @@ class CRM_Core_Payment_AuthorizeNetIPNTest extends CiviUnitTestCase {
       'contribution_id' => $contribution['values'][1]['id'],
       'entity_table' => 'civicrm_membership',
     ]);
+    $this->validateAllContributions();
+    $this->validateAllPayments();
   }
 
   /**
@@ -394,7 +406,7 @@ class CRM_Core_Payment_AuthorizeNetIPNTest extends CiviUnitTestCase {
       'x_state' => 'WA',
       'x_city' => 'Dallas',
       'x_address' => '41 My ST',
-      'x_invoice_num' => $this->_contributionID,
+      'x_invoice_num' => $this->ids['Contribution'][0],
       'x_cust_id' => $this->_contactID,
       'x_company' => 'nowhere@civicrm.org',
       'x_last_name' => 'Roberts',
diff --git a/tests/phpunit/CRMTraits/Financial/OrderTrait.php b/tests/phpunit/CRMTraits/Financial/OrderTrait.php
new file mode 100644 (file)
index 0000000..1474930
--- /dev/null
@@ -0,0 +1,125 @@
+<?php
+/*
+ +--------------------------------------------------------------------+
+ | CiviCRM version 5                                                  |
+ +--------------------------------------------------------------------+
+ | Copyright CiviCRM LLC (c) 2004-2020                                |
+ +--------------------------------------------------------------------+
+ | This file is a part of CiviCRM.                                    |
+ |                                                                    |
+ | CiviCRM is free software; you can copy, modify, and distribute it  |
+ | under the terms of the GNU Affero General Public License           |
+ | Version 3, 19 November 2007 and the CiviCRM Licensing Exception.   |
+ |                                                                    |
+ | CiviCRM is distributed in the hope that it will be useful, but     |
+ | WITHOUT ANY WARRANTY; without even the implied warranty of         |
+ | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.               |
+ | See the GNU Affero General Public License for more details.        |
+ |                                                                    |
+ | You should have received a copy of the GNU Affero General Public   |
+ | License and the CiviCRM Licensing Exception along                  |
+ | with this program; if not, contact CiviCRM LLC                     |
+ | at info[AT]civicrm[DOT]org. If you have questions about the        |
+ | GNU Affero General Public License or the licensing of CiviCRM,     |
+ | see the CiviCRM license FAQ at http://civicrm.org/licensing        |
+ +--------------------------------------------------------------------+
+ */
+
+/**
+ * Trait OrderTrait
+ *
+ * Trait for setting up orders for tests.
+ */
+trait CRMTraits_Financial_OrderTrait {
+
+  use \Civi\Test\Api3TestTrait;
+
+  /**
+   * Create a pending membership from a recurring order.
+   *
+   * @throws \CRM_Core_Exception
+   */
+  public function createRepeatMembershipOrder() {
+    $this->createExtraneousContribution();
+    $this->ids['contact'][0] = $this->individualCreate();
+    $this->ids['membership_type'][0] = $this->membershipTypeCreate();
+
+    $contributionRecur = $this->callAPISuccess('contribution_recur', 'create', array_merge([
+      'contact_id' => $this->_contactID,
+      'amount' => 1000,
+      'sequential' => 1,
+      'installments' => 5,
+      'frequency_unit' => 'Month',
+      'frequency_interval' => 1,
+      'invoice_id' => $this->_invoiceID,
+      'contribution_status_id' => 2,
+      'payment_processor_id' => $this->_paymentProcessorID,
+      // processor provided ID - use contact ID as proxy.
+      'processor_id' => $this->_contactID,
+    ]));
+
+    $orderID = $this->callAPISuccess('Order', 'create', [
+      'total_amount' => '200',
+      '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,
+      'receive_date' => '2019-07-25 07:34:23',
+      'skipCleanMoney' => TRUE,
+      'contribution_recur_id' => $contributionRecur['id'],
+      'line_items' => [
+        [
+          'params' => [
+            'contact_id' => $this->ids['contact'][0],
+            'membership_type_id' => $this->ids['membership_type'][0],
+            'contribution_recur_id' => $contributionRecur['id'],
+            'source' => 'Payment',
+          ],
+          'line_item' => [
+            [
+              'label' => 'General',
+              'qty' => 1,
+              'unit_price' => 200,
+              'line_total' => 200,
+              'financial_type_id' => 1,
+              'entity_table' => 'civicrm_membership',
+              'price_field_id' => $this->callAPISuccess('price_field', 'getvalue', [
+                'return' => 'id',
+                'label' => 'Membership Amount',
+                'options' => ['limit' => 1, 'sort' => 'id DESC'],
+              ]),
+              'price_field_value_id' => $this->callAPISuccess('price_field_value', 'getvalue', [
+                'return' => 'id',
+                'label' => 'General',
+                'options' => ['limit' => 1, 'sort' => 'id DESC'],
+              ]),
+            ],
+          ],
+        ],
+      ],
+    ])['id'];
+
+    $this->ids['ContributionRecur'][0] = $contributionRecur['id'];
+    $this->ids['Contribution'][0] = $orderID;
+  }
+
+  /**
+   * Create an extraneous contribution to throw off any 'number one bugs'.
+   *
+   * Ie this means our real data starts from 2 & we won't hit 'pretend passes'
+   * just because the number 1 is used for multiple entities.
+   */
+  protected function createExtraneousContribution() {
+    $this->contributionCreate([
+      'contact_id' => $this->_contactID,
+      'is_test' => 1,
+      'financial_type_id' => 1,
+      'invoice_id' => 'abcd',
+      'trxn_id' => 345,
+      'receive_date' => '2019-07-25 07:34:23',
+    ]);
+  }
+
+}
index 610be7dab843cfade3959f9e2971f3bc8dda0187..d5ca8938cee02e577df2c7cc9d0144e3f101762d 100644 (file)
@@ -954,6 +954,7 @@ class CiviUnitTestCase extends PHPUnit\Framework\TestCase {
    *
    * @return int
    *   id of created contribution
+   * @throws \CRM_Core_Exception
    */
   public function contributionCreate($params) {
 
@@ -2426,6 +2427,8 @@ AND    ( TABLE_NAME LIKE 'civicrm_value_%' )
    *
    * @param array $recurParams (Optional)
    * @param array $contributionParams (Optional)
+   *
+   * @throws \CRM_Core_Exception
    */
   public function setupRecurringPaymentProcessorTransaction($recurParams = [], $contributionParams = []) {
     $contributionParams = array_merge([
@@ -2456,12 +2459,15 @@ AND    ( TABLE_NAME LIKE 'civicrm_value_%' )
     ], $recurParams));
     $this->_contributionRecurID = $contributionRecur['id'];
     $this->_contributionID = $contributionRecur['values']['0']['api.contribution.create']['id'];
+    $this->ids['Contribution'][0] = $this->_contributionID;
   }
 
   /**
    * We don't have a good way to set up a recurring contribution with a membership so let's just do one then alter it
    *
    * @param array $params Optionally modify params for membership/recur (duration_unit/frequency_unit)
+   *
+   * @throws \CRM_Core_Exception
    */
   public function setupMembershipRecurringPaymentProcessorTransaction($params = []) {
     $membershipParams = $recurParams = [];
@@ -3431,4 +3437,21 @@ AND    ( TABLE_NAME LIKE 'civicrm_value_%' )
     $this->validatePayments($payments);
   }
 
+  /**
+   * Validate all created contributions.
+   *
+   * @throws \CRM_Core_Exception
+   */
+  protected function validateAllContributions() {
+    $contributions = $this->callAPISuccess('Contribution', 'get', [])['values'];
+    foreach ($contributions as $contribution) {
+      $lineItems = $this->callAPISuccess('LineItem', 'get', ['contribution_id' => $contribution['id']])['values'];
+      $total = 0;
+      foreach ($lineItems as $lineItem) {
+        $total += $lineItem['line_total'];
+      }
+      $this->assertEquals($total, $contribution['total_amount']);
+    }
+  }
+
 }
index 4dbb9074ca6b169635c2bb7768a0215ac7d344fb..eb8112b8dd1d1c517a8020ce2266260745b5420e 100644 (file)
@@ -244,7 +244,7 @@ class api_v3_OrderTest extends CiviUnitTestCase {
         'is_override' => 1,
       ],
     ];
-    $order = $this->callAPIAndDocument('order', 'create', $p, __FUNCTION__, __FILE__);
+    $order = $this->callAPIAndDocument('Order', 'create', $p, __FUNCTION__, __FILE__);
     $params = [
       'contribution_id' => $order['id'],
     ];
@@ -258,7 +258,9 @@ class api_v3_OrderTest extends CiviUnitTestCase {
       ],
     ];
     $this->checkPaymentResult($order, $expectedResult);
-    $this->callAPISuccessGetCount('MembershipPayment', $params, 1);
+    $membershipPayment = $this->callAPISuccessGetSingle('MembershipPayment', $params);
+
+    $membership = $this->callAPISuccessGetSingle('Membership', ['id' => $membershipPayment['id']]);
     $this->callAPISuccess('Contribution', 'Delete', [
       'id' => $order['id'],
     ]);