Used generalized function, removed unused function
[civicrm-core.git] / tests / phpunit / CRM / Financial / BAO / FinancialTypeAccountTest.php
index 34997dff72540152c8d094cfc1bea907bc8f86b1..88172853d05d21f4af7d1cd58d3df536c20261cb 100644 (file)
@@ -3,7 +3,7 @@
  +--------------------------------------------------------------------+
  | CiviCRM version 4.7                                                |
  +--------------------------------------------------------------------+
- | Copyright CiviCRM LLC (c) 2004-2016                                |
+ | Copyright CiviCRM LLC (c) 2004-2017                                |
  +--------------------------------------------------------------------+
  | This file is a part of CiviCRM.                                    |
  |                                                                    |
@@ -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,
+    list($financialAccount, $financialType, $financialAccountType) = $this->createFinancialAccount(
+      'Revenue',
+      'Income Account is'
     );
-
-    $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,
-    );
-
-    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);
@@ -103,19 +70,13 @@ class CRM_Financial_BAO_FinancialTypeAccountTest extends CiviUnitTestCase {
   }
 
   /**
-   * Check method getFinancialAccount()
+   * Check method retrieve()
    */
   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,54 +85,20 @@ 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.');
     $this->assertEquals($financialAccountType['financial_account_id'], $financialAccount->id, 'Verify Financial Account Id.');
   }
 
-  /**
-   * Check method getFinancialAccount()
-   */
-  public function testGetFinancialAccount() {
-    $params = array(
-      'name' => 'TestFinancialAccount',
-      'accounting_code' => 4800,
-      'is_deductible' => 0,
-      'is_active' => 1,
-      'is_reserved' => 0,
-    );
-    $ids = array();
-    $financialAccount = CRM_Financial_BAO_FinancialAccount::add($params, $ids);
-    $params = array(
-      'financial_account_id' => $financialAccount->id,
-      'payment_processor_type_id' => 1,
-      'domain_id' => 1,
-      'billing_mode' => 1,
-      'name' => 'paymentProcessor',
-    );
-    $processor = CRM_Financial_BAO_PaymentProcessor::create($params);
-
-    $account = CRM_Financial_BAO_FinancialTypeAccount::getFinancialAccount(
-      $processor->id,
-      'civicrm_payment_processor'
-    );
-    $this->assertEquals($account, 'TestFinancialAccount', 'Verify Financial Account Name');
-  }
-
   /**
    * Check method getInstrumentFinancialAccount()
    */
   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,10 +112,80 @@ 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');
   }
 
+  /**
+   * Test validate account relationship with financial account type.
+   */
+  public function testValidateRelationship() {
+    $params = array('labelColumn' => 'name');
+    $financialAccount = CRM_Core_PseudoConstant::get('CRM_Financial_DAO_FinancialAccount', 'financial_account_type_id', $params);
+    $accountRelationships = CRM_Core_PseudoConstant::get('CRM_Financial_DAO_EntityFinancialAccount', 'account_relationship', $params);
+    $financialType = CRM_Contribute_PseudoConstant::financialType();
+    $financialAccountType = new CRM_Financial_DAO_EntityFinancialAccount();
+    $financialAccountType->entity_table = 'civicrm_financial_type';
+    $financialAccountType->entity_id = array_search('Member Dues', $financialType);
+    $financialAccountType->account_relationship = array_search('Credit/Contra Revenue Account is', $accountRelationships);
+    $financialAccountType->financial_account_id = array_search('Liability', $financialAccount);
+    try {
+      CRM_Financial_BAO_FinancialTypeAccount::validateRelationship($financialAccountType);
+      $this->fail("Missed expected exception");
+    }
+    catch (Exception $e) {
+      $this->assertTrue(TRUE, 'Received expected exception');
+      $this->assertEquals($e->getMessage(), "This financial account cannot have 'Credit/Contra Revenue Account is' relationship.");
+    }
+  }
+
+  /**
+   * 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),
+      );
+
+      //CRM-20313: As per unique index added in civicrm_entity_financial_account table,
+      //  first check if there's any record on basis of unique key (entity_table, account_relationship, entity_id)
+      $dao = new CRM_Financial_DAO_EntityFinancialAccount();
+      $dao->copyValues($financialParams);
+      $dao->find();
+      if ($dao->fetch()) {
+        $financialParams['id'] = $dao->id;
+      }
+      $financialParams['financial_account_id'] = $financialAccount->id;
+      $financialAccountType = CRM_Financial_BAO_FinancialTypeAccount::add($financialParams);
+    }
+    return array($financialAccount, $financialType, $financialAccountType);
+  }
+
 }