unit test for #18306 - order create api test for contribution
authorJitendra Purohit <jitendra@fuzion.co.nz>
Wed, 7 Oct 2020 11:49:37 +0000 (17:19 +0530)
committereileen <emcnaughton@wikimedia.org>
Fri, 16 Oct 2020 04:42:08 +0000 (17:42 +1300)
tests/phpunit/CRMTraits/Financial/OrderTrait.php
tests/phpunit/api/v3/ContributionTest.php

index c927e71857473add0769b48ae536df80e7bf8533..fbbd2347eb9447e30ae1a72948693d4bece0b62c 100644 (file)
@@ -60,31 +60,62 @@ trait CRMTraits_Financial_OrderTrait {
             'contribution_recur_id' => $contributionRecur['id'],
             'source' => 'Payment',
           ],
+          'line_item' => $this->getMembershipLineItem(),
+        ],
+      ],
+    ])['id'];
+
+    $this->ids['ContributionRecur'][0] = $contributionRecur['id'];
+    $this->ids['Contribution'][0] = $orderID;
+  }
+
+  /**
+   * Create an order with a contribution AND a membership line item.
+   *
+   * @throws \CRM_Core_Exception
+   */
+  protected function createContributionAndMembershipOrder() {
+    $this->ids['membership_type'][0] = $this->membershipTypeCreate();
+    $orderID = $this->callAPISuccess('Order', 'create', [
+      'financial_type_id' => 'Donation',
+      'contact_id' => $this->_contactID,
+      'is_test' => 0,
+      'payment_instrument_id' => 'Check',
+      'receive_date' => date('Y-m-d'),
+      'line_items' => [
+        [
+          'params' => [
+            'contact_id' => $this->_contactID,
+            'source' => 'Payment',
+          ],
           'line_item' => [
             [
-              'label' => 'General',
+              'label' => 'Contribution Amount',
               'qty' => 1,
-              'unit_price' => 200,
-              'line_total' => 200,
-              'financial_type_id' => 1,
-              'entity_table' => 'civicrm_membership',
-              'price_field_id' => $this->callAPISuccess('price_field', 'getvalue', [
+              'unit_price' => 100,
+              'line_total' => 100,
+              'financial_type_id' => CRM_Core_PseudoConstant::getKey('CRM_Contribute_BAO_Contribution', 'financial_type_id', 'Donation'),
+              'entity_table' => 'civicrm_contribution',
+              'price_field_id' => $this->callAPISuccessGetValue('price_field', [
                 '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',
+                'label' => 'Contribution Amount',
                 'options' => ['limit' => 1, 'sort' => 'id DESC'],
               ]),
+              'price_field_value_id' => NULL,
             ],
           ],
         ],
+        [
+          'params' => [
+            'contact_id' => $this->_contactID,
+            'membership_type_id' => 'General',
+            'source' => 'Payment',
+          ],
+          'line_item' => $this->getMembershipLineItem(),
+        ],
       ],
     ])['id'];
 
-    $this->ids['ContributionRecur'][0] = $contributionRecur['id'];
     $this->ids['Contribution'][0] = $orderID;
   }
 
@@ -191,4 +222,31 @@ trait CRMTraits_Financial_OrderTrait {
     ]);
   }
 
+  /**
+   * @return array[]
+   * @throws \CRM_Core_Exception
+   */
+  protected function getMembershipLineItem(): array {
+    return [
+      [
+        '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'],
+        ]),
+      ],
+    ];
+  }
+
 }
index deb222613378c4cb3afc68d7ecfd63ea627ec9a8..c246b9805417971bc9e73535b152add22aff0c66 100644 (file)
@@ -22,6 +22,7 @@ class api_v3_ContributionTest extends CiviUnitTestCase {
 
   use CRMTraits_Profile_ProfileTrait;
   use CRMTraits_Custom_CustomDataTrait;
+  use CRMTraits_Financial_OrderTrait;
 
   protected $_individualId;
   protected $_contribution;
@@ -2767,6 +2768,41 @@ class api_v3_ContributionTest extends CiviUnitTestCase {
     $this->assertEquals($expectedLineItem, $lineItem2['values'][0]);
   }
 
+  /**
+   * Test Contribution with Order api.
+   *
+   * @throws \CRM_Core_Exception|\CiviCRM_API3_Exception
+   */
+  public function testContributionOrder() {
+    $this->_contactID = $this->individualCreate();
+    $this->createContributionAndMembershipOrder();
+    $contribution = $this->callAPISuccess('contribution', 'get')['values'][$this->ids['Contribution'][0]];
+    $this->assertEquals('Pending Label**', $contribution['contribution_status']);
+    $membership = $this->callAPISuccessGetSingle('Membership', ['contact_id' => $this->_contactID]);
+
+    $this->callAPISuccess('Payment', 'create', [
+      'contribution_id' => $this->ids['Contribution'][0],
+      'payment_instrument_id' => 'Check',
+      'total_amount' => 300,
+    ]);
+    $contribution = $this->callAPISuccess('contribution', 'get')['values'][$this->ids['Contribution'][0]];
+    $this->assertEquals('Completed', $contribution['contribution_status']);
+
+    $lineItem = $this->callAPISuccess('LineItem', 'get', [
+      'sequential' => 1,
+      'contribution_id' => $this->ids['Contribution'][0],
+    ])['values'];
+    $this->assertCount(2, $lineItem);
+    $this->assertEquals($this->ids['Contribution'][0], $lineItem[0]['entity_id']);
+    $this->assertEquals('civicrm_contribution', $lineItem[0]['entity_table']);
+    $this->assertEquals($this->ids['Contribution'][0], $lineItem[0]['contribution_id']);
+    $this->assertEquals($this->ids['Contribution'][0], $lineItem[1]['contribution_id']);
+    $this->assertEquals('100.00', $lineItem[0]['line_total']);
+    $this->assertEquals('200.00', $lineItem[1]['line_total']);
+    $this->assertEquals($membership['id'], $lineItem[1]['entity_id']);
+    $this->assertEquals('civicrm_membership', $lineItem[1]['entity_table']);
+  }
+
   /**
    * Test financial_type_id override behaviour with a single line item.
    *