From b70cd45c4056826761cb3f06ff903bb02b398e6e Mon Sep 17 00:00:00 2001 From: Pradeep Nayak Date: Mon, 4 Jul 2016 20:37:29 +0530 Subject: [PATCH] =?utf8?q?[ready-for-core-team-review]CRM-16189,=20added?= =?utf8?q?=20code=20to=20validate=20relationship=20before=20creating=20ent?= =?utf8?q?rie=E2=80=A6=20(#8578)?= MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit * CRM-16189, added code to validate relationship before creating entries in civicrm_entity_financial_account ---------------------------------------- * CRM-16189: Improve support for Accrual Method bookkeeping https://issues.civicrm.org/jira/browse/CRM-16189 * CRM-16189, fixed CRM test ---------------------------------------- * CRM-16189: Improve support for Accrual Method bookkeeping https://issues.civicrm.org/jira/browse/CRM-16189 * --CRM-16189, added return defination --- CRM/Financial/BAO/FinancialTypeAccount.php | 7 +- .../BAO/FinancialTypeAccountTest.php | 118 ++++++++---------- 2 files changed, 56 insertions(+), 69 deletions(-) diff --git a/CRM/Financial/BAO/FinancialTypeAccount.php b/CRM/Financial/BAO/FinancialTypeAccount.php index 76f2daef80..4ec5dd6a0e 100644 --- a/CRM/Financial/BAO/FinancialTypeAccount.php +++ b/CRM/Financial/BAO/FinancialTypeAccount.php @@ -86,13 +86,12 @@ class CRM_Financial_BAO_FinancialTypeAccount extends CRM_Financial_DAO_EntityFin $financialTypeAccount->entity_table = $params['entity_table']; $financialTypeAccount->find(TRUE); } - else { - $financialTypeAccount->id = CRM_Utils_Array::value('entityFinancialAccount', $ids); - } if (!empty($ids['entityFinancialAccount'])) { $financialTypeAccount->id = $ids['entityFinancialAccount']; + $financialTypeAccount->find(TRUE); } $financialTypeAccount->copyValues($params); + self::validateRelationship($financialTypeAccount); $financialTypeAccount->save(); return $financialTypeAccount; } @@ -247,7 +246,7 @@ WHERE cog.name = 'payment_instrument' "; 'account_type_code' => 'INC', 'is_active' => 1, ); - $financialAccount = CRM_Financial_BAO_FinancialAccount::add($params, CRM_Core_DAO::$_nullArray); + $financialAccount = CRM_Financial_BAO_FinancialAccount::add($params); } else { $existingFinancialAccount[$dao->financial_account_type_id] = $dao->id; diff --git a/tests/phpunit/CRM/Financial/BAO/FinancialTypeAccountTest.php b/tests/phpunit/CRM/Financial/BAO/FinancialTypeAccountTest.php index 1c350af1bb..57f7a9701c 100644 --- a/tests/phpunit/CRM/Financial/BAO/FinancialTypeAccountTest.php +++ b/tests/phpunit/CRM/Financial/BAO/FinancialTypeAccountTest.php @@ -40,28 +40,10 @@ class CRM_Financial_BAO_FinancialTypeAccountTest extends CiviUnitTestCase { * Check method add() */ public function testAdd() { - $params = array( - 'name' => 'TestFinancialAccount_1', - 'accounting_code' => 4800, - 'contact_id' => 1, - 'is_deductible' => 0, - 'is_active' => 1, - 'is_reserved' => 0, - ); - - $ids = array(); - $financialAccount = CRM_Financial_BAO_FinancialAccount::add($params, $ids); - $params['name'] = 'test_financialType1'; - $financialType = CRM_Financial_BAO_FinancialType::add($params, $ids); - $relationTypeId = key(CRM_Core_PseudoConstant::accountOptionValues('account_relationship', NULL, " AND v.name LIKE 'Income Account is' ")); - $financialParams = array( - 'entity_table' => 'civicrm_financial_type', - 'entity_id' => $financialType->id, - 'account_relationship' => $relationTypeId, - 'financial_account_id' => $financialAccount->id, + list($financialAccount, $financialType, $financialAccountType) = $this->createFinancialAccount( + 'Revenue', + 'Income Account is' ); - - CRM_Financial_BAO_FinancialTypeAccount::add($financialParams, $ids); $result = $this->assertDBNotNull( 'CRM_Financial_DAO_EntityFinancialAccount', $financialAccount->id, @@ -76,26 +58,11 @@ class CRM_Financial_BAO_FinancialTypeAccountTest extends CiviUnitTestCase { * Check method del() */ public function testDel() { - $params = array( - 'name' => 'TestFinancialAccount_2', - 'is_deductible' => 0, - 'is_active' => 1, - 'is_reserved' => 0, + list($financialAccount, $financialType, $financialAccountType) = $this->createFinancialAccount( + 'Expenses', + 'Expense Account is' ); - $ids = array(); - $defaults = array(); - $financialAccount = CRM_Financial_BAO_FinancialAccount::add($params, $ids); - $params['name'] = 'test_financialType2'; - $financialType = CRM_Financial_BAO_FinancialType::add($params, $ids); - $relationTypeId = key(CRM_Core_PseudoConstant::accountOptionValues('account_relationship', NULL, " AND v.name LIKE 'Expense Account is' ")); - $financialParams = array( - 'entity_table' => 'civicrm_financial_type', - 'entity_id' => $financialType->id, - 'account_relationship' => $relationTypeId, - 'financial_account_id' => $financialAccount->id, - ); - $financialAccountType = CRM_Financial_BAO_FinancialTypeAccount::add($financialParams, $ids); CRM_Financial_BAO_FinancialTypeAccount::del($financialAccountType->id); $params = array('id' => $financialAccountType->id); $result = CRM_Financial_BAO_FinancialType::retrieve($params, $defaults); @@ -106,16 +73,10 @@ class CRM_Financial_BAO_FinancialTypeAccountTest extends CiviUnitTestCase { * Check method getFinancialAccount() */ public function testRetrieve() { - $params = array( - 'name' => 'TestFinancialAccount_3', - 'is_deductible' => 0, - 'is_active' => 1, - 'is_reserved' => 0, + list($financialAccount, $financialType, $financialAccountType) = $this->createFinancialAccount( + 'Asset', + 'Asset Account is' ); - $ids = array(); - $financialAccount = CRM_Financial_BAO_FinancialAccount::add($params, $ids); - $params['name'] = 'test_financialType3'; - $financialType = CRM_Financial_BAO_FinancialType::add($params, $ids); $relationTypeId = key(CRM_Core_PseudoConstant::accountOptionValues('account_relationship', NULL, " AND v.name LIKE 'Asset Account is' ")); $financialParams = array( 'entity_table' => 'civicrm_financial_type', @@ -124,7 +85,6 @@ class CRM_Financial_BAO_FinancialTypeAccountTest extends CiviUnitTestCase { 'financial_account_id' => $financialAccount->id, ); - CRM_Financial_BAO_FinancialTypeAccount::add($financialParams, $ids); $defaults = array(); $financialAccountType = CRM_Financial_BAO_FinancialTypeAccount::retrieve($financialParams, $defaults); $this->assertEquals($financialAccountType['entity_id'], $financialType->id, 'Verify Entity Id.'); @@ -135,15 +95,9 @@ class CRM_Financial_BAO_FinancialTypeAccountTest extends CiviUnitTestCase { * Check method getFinancialAccount() */ public function testGetFinancialAccount() { - $params = array( - 'name' => 'TestFinancialAccount', - 'accounting_code' => 4800, - 'is_deductible' => 0, - 'is_active' => 1, - 'is_reserved' => 0, + list($financialAccount, $financialType, $financialAccountType) = $this->createFinancialAccount( + 'Asset' ); - $ids = array(); - $financialAccount = CRM_Financial_BAO_FinancialAccount::add($params, $ids); $params = array( 'financial_account_id' => $financialAccount->id, 'payment_processor_type_id' => 1, @@ -157,7 +111,7 @@ class CRM_Financial_BAO_FinancialTypeAccountTest extends CiviUnitTestCase { $processor->id, 'civicrm_payment_processor' ); - $this->assertEquals($account, 'TestFinancialAccount', 'Verify Financial Account Name'); + $this->assertEquals($account, $financialAccount->name, 'Verify Financial Account Name'); } /** @@ -165,13 +119,9 @@ class CRM_Financial_BAO_FinancialTypeAccountTest extends CiviUnitTestCase { */ public function testGetInstrumentFinancialAccount() { $paymentInstrumentValue = 1; - $params = array( - 'name' => 'Donations', - 'is_deductible' => 0, - 'is_active' => 1, + list($financialAccount, $financialType, $financialAccountType) = $this->createFinancialAccount( + 'Asset' ); - $ids = array(); - $financialAccount = CRM_Financial_BAO_FinancialAccount::add($params, $ids); $optionParams = array( 'name' => 'Credit Card', 'value' => $paymentInstrumentValue, @@ -185,7 +135,7 @@ class CRM_Financial_BAO_FinancialTypeAccountTest extends CiviUnitTestCase { 'financial_account_id' => $financialAccount->id, ); - CRM_Financial_BAO_FinancialTypeAccount::add($financialParams, $ids); + CRM_Financial_BAO_FinancialTypeAccount::add($financialParams); $financialAccountId = CRM_Financial_BAO_FinancialTypeAccount::getInstrumentFinancialAccount($paymentInstrumentValue); $this->assertEquals($financialAccountId, $financialAccount->id, 'Verify Payment Instrument'); @@ -214,4 +164,42 @@ class CRM_Financial_BAO_FinancialTypeAccountTest extends CiviUnitTestCase { } } + /** + * Function to create Financial Account. + * + * @param string $financialAccountType + * + * @param string $relationType + * + * @return array + * obj CRM_Financial_DAO_FinancialAccount, obj CRM_Financial_DAO_FinancialType, obj CRM_Financial_DAO_EntityFinancialAccount + */ + public function createFinancialAccount($financialAccountType, $relationType = NULL) { + $params = array('labelColumn' => 'name'); + $relationTypes = CRM_Core_PseudoConstant::get('CRM_Financial_DAO_EntityFinancialAccount', 'account_relationship', $params); + $financialAccountTypes = CRM_Core_PseudoConstant::get('CRM_Financial_DAO_FinancialAccount', 'financial_account_type_id', $params); + $params = array( + 'name' => 'TestFinancialAccount_' . rand(), + 'contact_id' => 1, + 'is_deductible' => 0, + 'is_active' => 1, + 'is_reserved' => 0, + 'financial_account_type_id' => array_search($financialAccountType, $financialAccountTypes), + ); + $financialAccount = CRM_Financial_BAO_FinancialAccount::add($params); + $financialType = $financialAccountType = NULL; + if ($relationType) { + $params['name'] = 'test_financialType1'; + $financialType = CRM_Financial_BAO_FinancialType::add($params); + $financialParams = array( + 'entity_table' => 'civicrm_financial_type', + 'entity_id' => $financialType->id, + 'account_relationship' => array_search($relationType, $relationTypes), + 'financial_account_id' => $financialAccount->id, + ); + $financialAccountType = CRM_Financial_BAO_FinancialTypeAccount::add($financialParams); + } + return array($financialAccount, $financialType, $financialAccountType); + } + } -- 2.25.1