Merge branch '5.11' of https://github.com/civicrm/civicrm-core
[civicrm-core.git] / tests / phpunit / api / v3 / OptionValueTest.php
index a3b5556d953d358513ab293060dc685750c83164..154cb18222fa054474da087aabc566ac784bafcc 100644 (file)
@@ -3,7 +3,7 @@
  +--------------------------------------------------------------------+
 | CiviCRM version 5                                                  |
 +--------------------------------------------------------------------+
-| Copyright CiviCRM LLC (c) 2004-2018                                |
+| Copyright CiviCRM LLC (c) 2004-2019                                |
 +--------------------------------------------------------------------+
 | This file is a part of CiviCRM.                                    |
 |                                                                    |
@@ -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,
+    ]);
+  }
+
 }