From 2ef8ea5a9a55df08583ebd60d340cd23a5746736 Mon Sep 17 00:00:00 2001 From: Pradeep Nayak Date: Sun, 1 Jul 2018 12:18:30 +0530 Subject: [PATCH] dev/financial/issues/24, Add unit test for Link payment method to financial account when created using api (#12365) * Added api test to create new payment method * updated api test --- tests/phpunit/api/v3/OptionValueTest.php | 82 ++++++++++++++++++++++++ 1 file changed, 82 insertions(+) diff --git a/tests/phpunit/api/v3/OptionValueTest.php b/tests/phpunit/api/v3/OptionValueTest.php index a3b5556d95..1d42e3c207 100644 --- a/tests/phpunit/api/v3/OptionValueTest.php +++ b/tests/phpunit/api/v3/OptionValueTest.php @@ -427,4 +427,86 @@ class api_v3_OptionValueTest extends CiviUnitTestCase { ); } + /** + * Test to create and update payment method with financial account. + */ + public function testCreateUpdateOptionValueForPaymentInstrument() { + $assetFinancialAccountId = $this->callAPISuccessGetValue('FinancialAccount', [ + 'return' => "id", + 'financial_account_type_id' => "Asset", + 'options' => ['limit' => 1], + ]); + // create new payment method with financial account + $ov = $this->callAPISuccess('OptionValue', 'create', [ + 'financial_account_id' => $assetFinancialAccountId, + 'option_group_id' => "payment_instrument", + 'label' => "Dummy Payment Method", + ]); + + //check if relationship is created between Payment method and Financial Account + $this->checkPaymentMethodFinancialAccountRelationship($ov['id'], $assetFinancialAccountId); + + // update payment method to have different non-asset financial Account + $nonAssetFinancialAccountId = $this->callAPISuccessGetValue('FinancialAccount', [ + 'return' => "id", + 'financial_account_type_id' => ['NOT IN' => ["Asset"]], + 'options' => ['limit' => 1], + ]); + try { + $result = $this->callAPISuccess('OptionValue', 'create', [ + 'financial_account_id' => $nonAssetFinancialAccountId, + 'id' => $ov['id'], + ]); + throw new API_Exception(ts('Should throw error.')); + } + catch (Exception $e) { + try { + $assetAccountRelValue = $this->callAPISuccessGetValue('EntityFinancialAccount', [ + 'return' => "account_relationship", + 'entity_table' => "civicrm_option_value", + 'entity_id' => $ov['id'], + 'financial_account_id' => $nonAssetFinancialAccountId, + ]); + throw new API_Exception(ts('Should throw error.')); + } + catch (Exception $e) { + $this->checkPaymentMethodFinancialAccountRelationship($ov['id'], $assetFinancialAccountId); + } + } + // update payment method to have different asset financial Account + $assetFinancialAccountId = $this->callAPISuccessGetValue('FinancialAccount', [ + 'return' => "id", + 'financial_account_type_id' => "Asset", + 'options' => ['limit' => 1], + 'id' => ['NOT IN' => [$assetFinancialAccountId]], + ]); + $result = $this->callAPISuccess('OptionValue', 'create', [ + 'financial_account_id' => $assetFinancialAccountId, + 'id' => $ov['id'], + ]); + //check if relationship is updated between Payment method and Financial Account + $this->checkPaymentMethodFinancialAccountRelationship($ov['id'], $assetFinancialAccountId); + } + + /** + * Function to check relationship between FA and Payment method. + * + * @param int $paymentMethodId + * @param int $financialAccountId + */ + protected function checkPaymentMethodFinancialAccountRelationship($paymentMethodId, $financialAccountId) { + $assetAccountRelValue = $this->callAPISuccessGetValue('EntityFinancialAccount', [ + 'return' => "account_relationship", + 'entity_table' => "civicrm_option_value", + 'entity_id' => $paymentMethodId, + 'financial_account_id' => $financialAccountId, + ]); + $checkAssetAccountIs = $this->callAPISuccessGetValue('OptionValue', [ + 'return' => "id", + 'option_group_id' => "account_relationship", + 'name' => "Asset Account is", + 'value' => $assetAccountRelValue, + ]); + } + } -- 2.25.1