dev/financial#73 Update Order.create so that total_amount is not required.
authoreileen <emcnaughton@wikimedia.org>
Mon, 14 Oct 2019 04:36:26 +0000 (17:36 +1300)
committereileen <emcnaughton@wikimedia.org>
Mon, 14 Oct 2019 06:57:34 +0000 (19:57 +1300)
As test shows it is otherwise created from the line items

api/v3/Order.php
api/v3/examples/Order/Cancel.ex.php
api/v3/examples/Order/Create.ex.php
api/v3/examples/Order/CreateOrderParticipant.ex.php
tests/phpunit/api/v3/OrderTest.php

index e570855b57a9b1081237f7a8b2959c304d08b3d2..c5b6b5919c4d1a9895d8c777b932dfb5a5720e41 100644 (file)
@@ -80,12 +80,14 @@ function _civicrm_api3_order_get_spec(&$params) {
  * @param array $params
  *   Input parameters.
  *
- * @throws API_Exception
  * @return array
  *   Api result array
+ *
+ * @throws \CiviCRM_API3_Exception
+ * @throws API_Exception
  */
 function civicrm_api3_order_create($params) {
-
+  civicrm_api3_verify_one_mandatory($params, NULL, ['line_items', 'total_amount']);
   $entity = NULL;
   $entityIds = [];
   if (!empty($params['line_items']) && is_array($params['line_items'])) {
@@ -213,7 +215,6 @@ function _civicrm_api3_order_create_spec(&$params) {
   $params['total_amount'] = [
     'name' => 'total_amount',
     'title' => 'Total Amount',
-    'api.required' => TRUE,
   ];
   $params['financial_type_id'] = [
     'name' => 'financial_type_id',
index 2f4327d0d9139fb8f92a8b4186f6291343c9d762..b2d7b2364aeec3bc5f414f53f6966fb98a3e729e 100644 (file)
@@ -74,6 +74,7 @@ function order_cancel_expectedresult() {
         'creditnote_id' => '1',
         'tax_amount' => '',
         'revenue_recognition_date' => '',
+        'is_template' => 0,
         'contribution_type_id' => '1',
       ],
     ],
index 05b76b87a23e361a11ec2d76d35cd0c8ddf269b7..59d9e34373509135c5e353be9d7549c5ae71818c 100644 (file)
@@ -9,7 +9,6 @@ function order_create_example() {
   $params = [
     'contact_id' => 8,
     'receive_date' => '2010-01-20',
-    'total_amount' => 200,
     'financial_type_id' => 'Event Fee',
     'contribution_status_id' => 1,
     'line_items' => [
@@ -106,6 +105,7 @@ function order_create_expectedresult() {
         'creditnote_id' => '',
         'tax_amount' => '',
         'revenue_recognition_date' => '',
+        'is_template' => '',
         'contribution_type_id' => '4',
       ],
     ],
index 9915cf6d7209d48293a03a97e66f81faec234f20..8ea0a03f83f0dc40d952c97957b420568cfafd06 100644 (file)
@@ -116,6 +116,7 @@ function order_create_expectedresult() {
         'creditnote_id' => '',
         'tax_amount' => '',
         'revenue_recognition_date' => '',
+        'is_template' => '',
         'contribution_type_id' => '1',
       ],
     ],
index ef53f55493445ffe6b12fdcac3c802cb514b0a45..08b94acbf1d501427c2c3910028311cece279dde 100644 (file)
@@ -36,11 +36,12 @@ class api_v3_OrderTest extends CiviUnitTestCase {
 
   protected $_individualId;
   protected $_financialTypeId = 1;
-  protected $_apiversion;
   public $debug = 0;
 
   /**
    * Setup function.
+   *
+   * @throws \CRM_Core_Exception
    */
   public function setUp() {
     parent::setUp();
@@ -51,6 +52,8 @@ class api_v3_OrderTest extends CiviUnitTestCase {
 
   /**
    * Clean up after each test.
+   *
+   * @throws \CRM_Core_Exception
    */
   public function tearDown() {
     $this->quickCleanUpFinancialEntities();
@@ -199,6 +202,8 @@ class api_v3_OrderTest extends CiviUnitTestCase {
 
   /**
    * Test create order api for membership
+   *
+   * @throws \CRM_Core_Exception
    */
   public function testAddOrderForMembership() {
     $membershipType = $this->membershipTypeCreate();
@@ -207,7 +212,6 @@ class api_v3_OrderTest extends CiviUnitTestCase {
     $p = [
       'contact_id' => $this->_individualId,
       'receive_date' => '2010-01-20',
-      'total_amount' => 200,
       'financial_type_id' => 'Event Fee',
       'contribution_status_id' => 1,
     ];
@@ -292,6 +296,8 @@ class api_v3_OrderTest extends CiviUnitTestCase {
 
   /**
    * Test create order api for participant
+   *
+   * @throws \CRM_Core_Exception
    */
   public function testAddOrderForParticipant() {
     $event = $this->eventCreate();
@@ -530,8 +536,7 @@ class api_v3_OrderTest extends CiviUnitTestCase {
   }
 
   /**
-   * @expectedException CiviCRM_API3_Exception
-   * @expectedExceptionMessage Line item total doesn't match with total amount.
+   * Test an exception is thrown if line items do not add up to total_amount, no tax.
    */
   public function testCreateOrderIfTotalAmountDoesNotMatchLineItemsAmountsIfNoTaxSupplied() {
     $params = [
@@ -559,12 +564,11 @@ class api_v3_OrderTest extends CiviUnitTestCase {
       ],
     ];
 
-    civicrm_api3('Order', 'create', $params);
+    $this->callAPIFailure('Order', 'create', $params, 'Line item total doesn\'t match with total amount');
   }
 
   /**
-   * @expectedException CiviCRM_API3_Exception
-   * @expectedExceptionMessage Line item total doesn't match with total amount.
+   * Test an exception is thrown if line items do not add up to total_amount, with tax.
    */
   public function testCreateOrderIfTotalAmountDoesNotMatchLineItemsAmountsIfTaxSupplied() {
     $params = [
@@ -594,7 +598,7 @@ class api_v3_OrderTest extends CiviUnitTestCase {
       ],
     ];
 
-    civicrm_api3('Order', 'create', $params);
+    $this->callAPIFailure('Order', 'create', $params, 'Line item total doesn\'t match with total amount.');
   }
 
   public function testCreateOrderIfTotalAmountDoesMatchLineItemsAmountsAndTaxSupplied() {
@@ -625,7 +629,7 @@ class api_v3_OrderTest extends CiviUnitTestCase {
       ],
     ];
 
-    $order = civicrm_api3('Order', 'create', $params);
+    $order = $this->callAPISuccess('Order', 'create', $params);
     $this->assertEquals(1, $order['count']);
   }