don't allow multiple currencies in a batch
[civicrm-core.git] / tests / phpunit / api / v3 / EntityBatchTest.php
index d15953c989491ff0e046e3b80b3e53d96d23ba6e..8250199962cd6019230611dfec6968afb0178db2 100644 (file)
@@ -31,9 +31,16 @@ class api_v3_EntityBatchTest extends CiviUnitTestCase {
 
     $entityParams = ['contact_id' => $this->individualCreate()];
 
+    $contributionId = $this->contributionCreate($entityParams);
+    $financialTrxnId = array_values($this->callAPISuccess('EntityFinancialTrxn', 'get', [
+      'entity_id' => $contributionId,
+      'entity_table' => 'civicrm_contribution',
+      'return' => ['financial_trxn_id'],
+    ])['values'])[0]['financial_trxn_id'];
+
     $this->_entity = 'EntityBatch';
     $this->params = [
-      'entity_id' => $this->contributionCreate($entityParams),
+      'entity_id' => $financialTrxnId,
       'batch_id' => $this->batchCreate(),
       'entity_table' => 'civicrm_financial_trxn',
     ];
@@ -71,4 +78,42 @@ class api_v3_EntityBatchTest extends CiviUnitTestCase {
     $this->assertEquals(0, $checkDeleted['count']);
   }
 
+  /**
+   * Ensure that submitting multiple currencies results in an error.
+   * @throws \CRM_Core_Exception
+   */
+  public function testMultipleCurrencies(): void {
+    $params['name'] = $params['title'] = 'MultiCurrencyBatch';
+    $params['status_id'] = 1;
+    $batchId = $this->callAPISuccess('batch', 'create', $params)['id'];
+
+    $contributionId = $this->contributionCreate(['contact_id' => $this->individualCreate()]);
+    $financialTrxnId = array_values($this->callAPISuccess('EntityFinancialTrxn', 'get', [
+      'entity_id' => $contributionId,
+      'entity_table' => 'civicrm_contribution',
+      'return' => ['financial_trxn_id'],
+    ])['values'])[0]['financial_trxn_id'];
+    $firstEntityBatchParams = [
+      'entity_id' => $financialTrxnId,
+      'batch_id' => $batchId,
+      'entity_table' => 'civicrm_financial_trxn',
+    ];
+    $result = $this->callAPISuccess($this->_entity, 'create', $firstEntityBatchParams);
+    $this->assertEquals(1, $result['count']);
+    $secondContributionId = $this->contributionCreate(['contact_id' => $this->individualCreate(), 'currency' => 'CAD']);
+
+    $secondFinancialTrxnId = array_values($this->callAPISuccess('EntityFinancialTrxn', 'get', [
+      'entity_id' => $secondContributionId,
+      'entity_table' => 'civicrm_contribution',
+      'return' => ['financial_trxn_id'],
+    ])['values'])[0]['financial_trxn_id'];
+    $secondEntityBatchParams = [
+      'entity_id' => $secondFinancialTrxnId,
+      'batch_id' => $batchId,
+      'entity_table' => 'civicrm_financial_trxn',
+    ];
+    $result = $this->callAPIFailure($this->_entity, 'create', $secondEntityBatchParams);
+    $this->assertEquals('You can not add items of two different currencies to a single contribution batch.', $result['error_message']);
+  }
+
 }