Unit test for #20342
authorEileen McNaughton <emcnaughton@wikimedia.org>
Thu, 20 May 2021 04:14:42 +0000 (16:14 +1200)
committerEileen McNaughton <emcnaughton@wikimedia.org>
Thu, 20 May 2021 04:14:42 +0000 (16:14 +1200)
tests/phpunit/CRM/Contribute/BAO/ContributionTest.php
tests/phpunit/api/v3/LineItemTest.php

index 5590511d9ec8a6ec921c7a200bfa5afd5aedc4a0..3cf824362e9752e6bbd9883f22c5961f1d224318 100644 (file)
@@ -1163,7 +1163,7 @@ WHERE eft.entity_id = %1 AND ft.to_financial_account_id <> %2";
    */
   public function testCreateProportionalEntry($thousandSeparator) {
     $this->setCurrencySeparators($thousandSeparator);
-    list($contribution, $financialAccount) = $this->createContributionWithTax();
+    [$contribution, $financialAccount] = $this->createContributionWithTax();
     $params = [
       'total_amount' => 55,
       'to_financial_account_id' => $financialAccount->financial_account_id,
@@ -1197,11 +1197,13 @@ WHERE eft.entity_id = %1 AND ft.to_financial_account_id <> %2";
    * @param string $thousandSeparator
    *   punctuation used to refer to thousands.
    *
+   * @throws \CRM_Core_Exception
+   * @throws \CiviCRM_API3_Exception
    * @dataProvider getThousandSeparators
    */
-  public function testCreateProportionalEntryZeroAmount($thousandSeparator) {
+  public function testCreateProportionalEntryZeroAmount(string $thousandSeparator): void {
     $this->setCurrencySeparators($thousandSeparator);
-    list($contribution, $financialAccount) = $this->createContributionWithTax(['total_amount' => 0]);
+    [$contribution, $financialAccount] = $this->createContributionWithTax(['total_amount' => 0]);
     $params = [
       'total_amount' => 0,
       'to_financial_account_id' => $financialAccount->financial_account_id,
@@ -1233,8 +1235,8 @@ WHERE eft.entity_id = %1 AND ft.to_financial_account_id <> %2";
    * Test for function getLastFinancialItemIds().
    */
   public function testgetLastFinancialItemIds() {
-    list($contribution, $financialAccount) = $this->createContributionWithTax();
-    list($ftIds, $taxItems) = CRM_Contribute_BAO_Contribution::getLastFinancialItemIds($contribution['id']);
+    [$contribution, $financialAccount] = $this->createContributionWithTax();
+    [$ftIds, $taxItems] = CRM_Contribute_BAO_Contribution::getLastFinancialItemIds($contribution['id']);
     $this->assertEquals(count($ftIds), 1, 'Invalid count.');
     $this->assertEquals(count($taxItems), 1, 'Invalid count.');
     foreach ($taxItems as $value) {
@@ -1252,7 +1254,7 @@ WHERE eft.entity_id = %1 AND ft.to_financial_account_id <> %2";
    * saved un-rounded).
    */
   public function testCreateProportionalFinancialEntriesViaPaymentCreate() {
-    list($contribution, $financialAccount) = $this->createContributionWithTax([], FALSE);
+    [$contribution, $financialAccount] = $this->createContributionWithTax([], FALSE);
     $params = [
       'total_amount' => 50,
       'to_financial_account_id' => $financialAccount->financial_account_id,
@@ -1279,7 +1281,7 @@ WHERE eft.entity_id = %1 AND ft.to_financial_account_id <> %2";
    * Test to check if amount is proportionally asigned for PI change.
    */
   public function testProportionallyAssignedForPIChange() {
-    list($contribution, $financialAccount) = $this->createContributionWithTax();
+    [$contribution, $financialAccount] = $this->createContributionWithTax();
     $params = [
       'id' => $contribution['id'],
       'payment_instrument_id' => 3,
index 7ff061a4392f90a7d236e31502defc4da4063912..9c73b82133e9485d85170f3f181550677e8fe8bd 100644 (file)
@@ -23,6 +23,7 @@ class api_v3_LineItemTest extends CiviUnitTestCase {
    * Prepare for test.
    *
    * @throws \CRM_Core_Exception
+   * @throws \CiviCRM_API3_Exception
    */
   public function setUp(): void {
     parent::setUp();
@@ -56,8 +57,9 @@ class api_v3_LineItemTest extends CiviUnitTestCase {
    * @param int $version
    *
    * @dataProvider versionThreeAndFour
+   * @throws \CRM_Core_Exception
    */
-  public function testCreateLineItemWithTax($version) {
+  public function testCreateLineItemWithTax($version): void {
     $this->_apiversion = $version;
     $this->enableSalesTaxOnFinancialType('Donation');
     $this->params['financial_type_id'] = 'Donation';
@@ -70,13 +72,15 @@ class api_v3_LineItemTest extends CiviUnitTestCase {
   /**
    * Enable tax for the given financial type.
    *
+   * @param string $type
+   *
+   * @throws \CRM_Core_Exception
    * @todo move to a trait, share.
    *
    * @dataProvider versionThreeAndFour
    *
-   * @param string $type
    */
-  public function enableSalesTaxOnFinancialType($type) {
+  public function enableSalesTaxOnFinancialType($type): void {
     $this->enableTaxAndInvoicing();
     $this->addTaxAccountToFinancialType(CRM_Core_PseudoConstant::getKey('CRM_Contribute_BAO_Contribution', 'financial_type_id', $type));
   }
@@ -90,13 +94,30 @@ class api_v3_LineItemTest extends CiviUnitTestCase {
    *
    * @throws \CRM_Core_Exception
    */
-  public function testCreateLineItem($version) {
+  public function testCreateLineItem(int $version): void {
     $this->_apiversion = $version;
     $result = $this->callAPIAndDocument($this->_entity, 'create', $this->params, __FUNCTION__, __FILE__)['values'];
     $this->assertCount(1, $result);
     $this->getAndCheck($this->params, key($result), $this->_entity);
   }
 
+  /**
+   * Test zero is valid for amount fields.
+   *
+   * https://github.com/civicrm/civicrm-core/pull/20342
+   *
+   * @param int $version
+   *
+   * @dataProvider versionThreeAndFour
+   *
+   * @throws \CRM_Core_Exception
+   */
+  public function testCreateLineItemZero(int $version): void {
+    $this->_apiversion = $version;
+    $this->callAPISuccess('LineItem', 'create', array_merge($this->params, ['unit_price' => 0, 'line_total' => 0]));
+    $this->callAPISuccess('LineItem', 'create', array_merge($this->params, ['unit_price' => 0.0, 'line_total' => 0.0]));
+  }
+
   /**
    * Test basic get line item.
    *
@@ -104,7 +125,7 @@ class api_v3_LineItemTest extends CiviUnitTestCase {
    *
    * @dataProvider versionThreeAndFour
    */
-  public function testGetBasicLineItem($version) {
+  public function testGetBasicLineItem($version): void {
     $this->_apiversion = $version;
     $getParams = [
       'entity_table' => 'civicrm_contribution',
@@ -122,7 +143,7 @@ class api_v3_LineItemTest extends CiviUnitTestCase {
    *
    * @throws \CRM_Core_Exception
    */
-  public function testDeleteLineItem($version) {
+  public function testDeleteLineItem($version): void {
     $this->_apiversion = $version;
     $getParams = [
       'entity_table' => 'civicrm_contribution',
@@ -139,7 +160,7 @@ class api_v3_LineItemTest extends CiviUnitTestCase {
    *
    * @throws \CRM_Core_Exception
    */
-  public function testGetFieldsLineItem() {
+  public function testGetFieldsLineItem(): void {
     $result = $this->callAPISuccess($this->_entity, 'getfields', ['action' => 'create']);
     $this->assertEquals(1, $result['values']['entity_id']['api.required']);
   }