Skip to content

Commit

Permalink
[NFC] Further cleanup in test class
Browse files Browse the repository at this point in the history
  • Loading branch information
eileenmcnaughton committed May 21, 2021
1 parent 91b8545 commit f044623
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 16 deletions.
8 changes: 7 additions & 1 deletion CRM/Financial/BAO/FinancialItem.php
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,13 @@ public static function createEntityTrxn($params) {
}

/**
* Retrive entity financial trxn details.
* Retrieve entity financial trxn details.
*
* @deprecated - only called by tests - to be replaced with
* $trxn = (array) EntityFinancialTrxn::get()
* ->addWhere('id', '=', $contributionID)
* ->addWhere('entity_table', '=', 'civicrm_contribution')
* ->addSelect('*')->execute();
*
* @param array $params
* an assoc array of name/value pairs.
Expand Down
47 changes: 32 additions & 15 deletions tests/phpunit/api/v3/TaxContributionPageTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
+--------------------------------------------------------------------+
*/

use Civi\Api4\EntityFinancialTrxn;

/**
* Class api_v3_TaxContributionPageTest
* @group headless
Expand Down Expand Up @@ -186,9 +188,10 @@ public function setUpContributionPage(): void {
* @param string $thousandSeparator
* punctuation used to refer to thousands.
*
* @throws \API_Exception
* @throws \CRM_Core_Exception
* @dataProvider getThousandSeparators
*
* @throws \CRM_Core_Exception
*/
public function testCreateContributionOnline(string $thousandSeparator): void {
$this->setCurrencySeparators($thousandSeparator);
Expand Down Expand Up @@ -272,6 +275,7 @@ public function testCreateContributionChainedLineItems(string $thousandSeparator
}

/**
* @throws \API_Exception
* @throws \CRM_Core_Exception
*/
public function testCreateContributionPayLaterOnline(): void {
Expand Down Expand Up @@ -307,9 +311,11 @@ public function testCreateContributionPayLaterOnline(): void {
* @param string $thousandSeparator
* punctuation used to refer to thousands.
*
* @throws \API_Exception
* @throws \CRM_Core_Exception
* @throws \Civi\API\Exception\UnauthorizedException
* @dataProvider getThousandSeparators
*
* @throws \CRM_Core_Exception
*/
public function testCreateContributionPendingOnline(string $thousandSeparator): void {
$this->setCurrencySeparators($thousandSeparator);
Expand All @@ -336,7 +342,9 @@ public function testCreateContributionPendingOnline(string $thousandSeparator):
$this->assertEquals('SSF', $contribution['source']);
$this->assertEquals(20, $contribution['tax_amount']);
$this->assertEquals(2, $contribution['contribution_status_id']);
$this->_checkFinancialRecords($contribution, 'pending');
$trxn = $this->getFinancialTransactionsForContribution($contribution['id']);
$this->assertCount(0, $trxn, 'No Trxn to be created until IPN callback');

$this->setCurrencySeparators($thousandSeparator);
}

Expand Down Expand Up @@ -421,20 +429,15 @@ public function _getFinancialItemAmount(int $contId): ?string {
/**
* @param array $params
* @param string $context
*
* @throws \API_Exception
*/
public function _checkFinancialRecords($params, $context): void {
$entityParams = [
'entity_id' => $params['id'],
'entity_table' => 'civicrm_contribution',
];
if ($context === 'pending') {
$trxn = CRM_Financial_BAO_FinancialItem::retrieveEntityFinancialTrxn($entityParams);
$this->assertNull($trxn, 'No Trxn to be created until IPN callback');
return;
}
$trxn = current(CRM_Financial_BAO_FinancialItem::retrieveEntityFinancialTrxn($entityParams));
$contributionID = $params['id'];
$trxn = $this->getFinancialTransactionsForContribution($contributionID);

$trxnParams = [
'id' => $trxn['financial_trxn_id'],
'id' => $trxn->first()['financial_trxn_id'],
];
if ($context !== 'online' && $context !== 'payLater') {
$compareParams = [
Expand All @@ -459,7 +462,7 @@ public function _checkFinancialRecords($params, $context): void {
}
$this->assertDBCompareValues('CRM_Financial_DAO_FinancialTrxn', $trxnParams, $compareParams);
$entityParams = [
'financial_trxn_id' => $trxn['financial_trxn_id'],
'financial_trxn_id' => $trxn->first()['financial_trxn_id'],
'entity_table' => 'civicrm_financial_item',
];
$entityTrxn = current(CRM_Financial_BAO_FinancialItem::retrieveEntityFinancialTrxn($entityParams));
Expand Down Expand Up @@ -518,4 +521,18 @@ public function testDeleteContribution(): void {
$this->callAPISuccess('contribution', 'delete', ['id' => $contributionID]);
}

/**
* @param $contributionID
*
* @return \Civi\Api4\Generic\Result
* @throws \API_Exception
* @throws \Civi\API\Exception\UnauthorizedException
*/
protected function getFinancialTransactionsForContribution($contributionID): \Civi\Api4\Generic\Result {
return EntityFinancialTrxn::get()
->addWhere('id', '=', $contributionID)
->addWhere('entity_table', '=', 'civicrm_contribution')
->addSelect('*')->execute();
}

}

0 comments on commit f044623

Please sign in to comment.