From bc6d52d28e5dd9d1212f5b206c377c64415d0e3e Mon Sep 17 00:00:00 2001 From: eileen Date: Thu, 6 May 2021 11:57:06 +1200 Subject: [PATCH] Extract sales tax config to a trait --- .../phpunit/CRMTraits/Financial/TaxTrait.php | 63 +++++++++++++++++++ tests/phpunit/api/v3/ContributionTest.php | 40 +++--------- 2 files changed, 73 insertions(+), 30 deletions(-) create mode 100644 tests/phpunit/CRMTraits/Financial/TaxTrait.php diff --git a/tests/phpunit/CRMTraits/Financial/TaxTrait.php b/tests/phpunit/CRMTraits/Financial/TaxTrait.php new file mode 100644 index 0000000000..35da539dd6 --- /dev/null +++ b/tests/phpunit/CRMTraits/Financial/TaxTrait.php @@ -0,0 +1,63 @@ +ids['FinancialAccount'][$key] = $this->callAPISuccess('FinancialAccount', 'create', array_merge([ + 'name' => 'Test Tax financial account ', + 'contact_id' => $this->createLoggedInUser(), + 'financial_account_type_id' => 2, + 'is_tax' => 1, + 'tax_rate' => 5.00, + 'is_reserved' => 0, + 'is_active' => 1, + 'is_default' => 0, + ], $params))['id']; + } + + /** + * Create a financial type with related sales tax config. + * + * @param string $key + * @param array $financialTypeParams + * @param array $financialAccountParams + * + * @throws \CRM_Core_Exception + */ + public function createFinancialTypeWithSalesTax(string $key = 'taxable', array $financialTypeParams = [], array $financialAccountParams = []): void { + $this->ids['FinancialType'][$key] = $this->callAPISuccess('FinancialType', 'create', array_merge([ + 'name' => 'Test taxable financial Type', + 'is_reserved' => 0, + 'is_active' => 1, + ], $financialTypeParams))['id']; + $this->createFinancialTaxAccount($key, $financialAccountParams); + $financialAccountParams = [ + 'entity_table' => 'civicrm_financial_type', + 'entity_id' => $this->ids['FinancialType'][$key], + 'account_relationship' => 10, + 'financial_account_id' => $this->ids['FinancialAccount'][$key], + ]; + CRM_Financial_BAO_FinancialTypeAccount::add($financialAccountParams); + } + +} diff --git a/tests/phpunit/api/v3/ContributionTest.php b/tests/phpunit/api/v3/ContributionTest.php index 082b5d0f57..fd9f77945c 100644 --- a/tests/phpunit/api/v3/ContributionTest.php +++ b/tests/phpunit/api/v3/ContributionTest.php @@ -23,6 +23,7 @@ class api_v3_ContributionTest extends CiviUnitTestCase { use CRMTraits_Profile_ProfileTrait; use CRMTraits_Custom_CustomDataTrait; use CRMTraits_Financial_OrderTrait; + use CRMTraits_Financial_TaxTrait; protected $_individualId; protected $_contribution; @@ -2235,40 +2236,19 @@ class api_v3_ContributionTest extends CiviUnitTestCase { } /** - * CRM-19126 Add test to verify when complete transaction is called tax amount is not changed. + * CRM-19126 Add test to verify when complete transaction is called tax + * amount is not changed. * * @param string $thousandSeparator * punctuation used to refer to thousands. * * @dataProvider getThousandSeparators + * @throws \CRM_Core_Exception */ - public function testCheckTaxAmount($thousandSeparator) { + public function testCheckTaxAmount(string $thousandSeparator): void { $this->setCurrencySeparators($thousandSeparator); - $contact = $this->createLoggedInUser(); - $financialType = $this->callAPISuccess('financial_type', 'create', [ - 'name' => 'Test taxable financial Type', - 'is_reserved' => 0, - 'is_active' => 1, - ]); - $financialAccount = $this->callAPISuccess('financial_account', 'create', [ - 'name' => 'Test Tax financial account ', - 'contact_id' => $contact, - 'financial_account_type_id' => 2, - 'is_tax' => 1, - 'tax_rate' => 5.00, - 'is_reserved' => 0, - 'is_active' => 1, - 'is_default' => 0, - ]); - $financialTypeId = $financialType['id']; - $financialAccountId = $financialAccount['id']; - $financialAccountParams = [ - 'entity_table' => 'civicrm_financial_type', - 'entity_id' => $financialTypeId, - 'account_relationship' => 10, - 'financial_account_id' => $financialAccountId, - ]; - CRM_Financial_BAO_FinancialTypeAccount::add($financialAccountParams); + $this->createFinancialTypeWithSalesTax(); + $financialTypeId = $this->ids['FinancialType']['taxable']; $params = array_merge($this->_params, ['contribution_status_id' => 2, 'financial_type_id' => $financialTypeId]); $contribution = $this->callAPISuccess('contribution', 'create', $params); @@ -2521,7 +2501,7 @@ class api_v3_ContributionTest extends CiviUnitTestCase { * @throws \CRM_Core_Exception */ public function testRepeatTransactionMembershipRenewCompletedContribution() { - list($originalContribution, $membership) = $this->setUpAutoRenewMembership(); + [$originalContribution, $membership] = $this->setUpAutoRenewMembership(); $contribution = $this->callAPISuccess('Contribution', 'repeattransaction', [ 'contribution_recur_id' => $originalContribution['values'][1]['contribution_recur_id'], @@ -2600,7 +2580,7 @@ class api_v3_ContributionTest extends CiviUnitTestCase { if (in_array($contributionStatus['name'], ['Completed', 'In Progress', 'Partially paid'])) { return; } - list($originalContribution, $membership) = $this->setUpAutoRenewMembership(); + [$originalContribution, $membership] = $this->setUpAutoRenewMembership(); $this->callAPISuccess('Contribution', 'repeattransaction', [ 'original_contribution_id' => $originalContribution['id'], @@ -4455,7 +4435,7 @@ class api_v3_ContributionTest extends CiviUnitTestCase { * CRM-20008 Tests repeattransaction creates pending membership. */ public function testRepeatTransactionMembershipCreatePendingContribution() { - list($originalContribution, $membership) = $this->setUpAutoRenewMembership(); + [$originalContribution, $membership] = $this->setUpAutoRenewMembership(); $this->callAPISuccess('membership', 'create', [ 'id' => $membership['id'], 'end_date' => 'yesterday', -- 2.25.1