Skip to content

Commit

Permalink
[#314] added ExistingContribution matcher tests
Browse files Browse the repository at this point in the history
  • Loading branch information
bjendres committed Jan 17, 2023
1 parent a47c9a8 commit 2d169c4
Show file tree
Hide file tree
Showing 5 changed files with 161 additions and 6 deletions.
12 changes: 7 additions & 5 deletions CRM/Banking/PluginImpl/Matcher/ExistingContribution.php
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,7 @@ public function getPotentialContributionsForContact($contact_id, CRM_Banking_Mat
$config = $this->_plugin_config;

// check in cache
$range_back = (int) $config->received_range_days;
$cache_key = "_contributions_${contact_id}_{$range_back}_{$config->received_date_check}";
$contributions = $context->getCachedEntry($cache_key);
if ($contributions != NULL) return $contributions;
Expand Down Expand Up @@ -236,10 +237,11 @@ public function match(CRM_Banking_BAO_BankTransaction $btx, CRM_Banking_Matcher_
// resolve accepted states
$accepted_status_ids = $this->getAcceptedContributionStatusIDs();

$contributions = array();
$contribution2contact = array();
$contribution2totalamount = array();
$contributions_identified = array();
$contributions = [];
$contribution2contact = [];
$contribution2totalamount = [];
$contributions_identified = [];
$contacts_found = [];

// check if this is actually enabled
if ($config->contribution_search) {
Expand Down Expand Up @@ -477,7 +479,7 @@ public function update_parameters(CRM_Banking_Matcher_Suggestion $match, $parame
*/
function visualize_match( CRM_Banking_Matcher_Suggestion $match, $btx) {
$config = $this->_plugin_config;
$smarty_vars = array();
$smarty_vars = [];

// load the data
$contribution_id = $match->getParameter('contribution_id');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
use Civi\Test\CiviEnvBuilder;

/**
* Test ContributionMatcher module
* Test CreateContributionMatcher module
*
* @group headless
*/
Expand Down
87 changes: 87 additions & 0 deletions tests/phpunit/CRM/Banking/Matcher/MatchContributionTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
<?php
/*-------------------------------------------------------+
| Project 60 - CiviBanking - Unit Test |
| Copyright (C) 2022 SYSTOPIA |
| Author: B. Endres (endres -at- systopia.de) |
| http://www.systopia.de/ |
+--------------------------------------------------------+
| This program is released as free software under the |
| Affero GPL v3 license. You can redistribute it and/or |
| modify it under the terms of this license which you |
| can read by viewing the included agpl.txt or online |
| at www.gnu.org/licenses/agpl.html. Removal of this |
| copyright header is strictly prohibited without |
| written permission from the original author(s). |
+--------------------------------------------------------*/

use CRM_Banking_ExtensionUtil as E;
use Civi\Test\HeadlessInterface;
use Civi\Test\HookInterface;
use Civi\Test\TransactionalInterface;
use Civi\Test\CiviEnvBuilder;

/**
* Test ContributionMatcher module
*
* @group headless
*/
class CRM_Banking_Matcher_MatchContributionMatcherTest extends CRM_Banking_TestBase implements HeadlessInterface, HookInterface, TransactionalInterface {

/**
* Setup used when HeadlessInterface is implemented.
*
* Civi\Test has many helpers, like install(), uninstall(), sql(), and sqlFile().
*
* @link https://github.com/civicrm/org.civicrm.testapalooza/blob/master/civi-test.md
*
* @return \Civi\Test\CiviEnvBuilder
*
* @throws \CRM_Extension_Exception_ParseException
*/
public function setUpHeadless(): CiviEnvBuilder {
return parent::setUpHeadless();
}

public function setUp():void {
parent::setUp();
}

public function tearDown():void {
parent::tearDown();
}

/**
* Basic test to see if the contribution matcher fires
* and passes on the respective variables
*/
public function testContributionMatcherFires():void {
$pending_contribution = $this->createContribution([
'contribution_status_id' => 'Pending',
]);

// create a transaction to process
$this->createTransaction(
[
'purpose' => 'This is a donation',
'name' => "doesn't matter",
'amount' => $pending_contribution['total_amount'],
'contact_id' => $pending_contribution['contact_id'],
'booking_date' => date('Y-m-d', strtotime($pending_contribution['receive_date'])),
'value_date' => date('Y-m-d', strtotime($pending_contribution['receive_date'])),
'currency' => $pending_contribution['currency'],
]
);

$this->configureCiviBankingModule(
$this->getTestResourcePath('matcher/configuration/ExistingContribution-01.civibanking'));

// run the matcher
$this->runMatchers();

// check if the contribution status has been update
$completed_contribution = $this->callAPISuccess(
'Contribution', 'getsingle', ['id' => $pending_contribution['id']]);

$this->assertEquals(1, $completed_contribution['contribution_status_id'], "Contribution wasn't completed.");
}
}
36 changes: 36 additions & 0 deletions tests/phpunit/CRM/Banking/TestBase.php
Original file line number Diff line number Diff line change
Expand Up @@ -673,4 +673,40 @@ public function getRandomOptionValue($option_group_id)
"SELECT value FROM civicrm_option_value WHERE option_group_id = %1 ORDER BY RAND() LIMIT 1;",
[1 => [$option_group_id, 'String']]);
}

/**
* Create a new contribution with random parameters
*
* @param array $attributes
* pass values you want to see in your contribution
*
* @return array
* generated contribution data
*/
public function createContribution($attributes = [])
{
if (empty($attributes['contact_id'])) {
$attributes['contact_id'] = $this->createContact();
}
if (empty($attributes['financial_type_id'])) {
$attributes['financial_type_id'] = $this->getRandomFinancialTypeID();
}
if (empty($attributes['payment_instrument_id'])) {
$attributes['payment_instrument_id'] = $this->getRandomOptionValue('payment_instrument');
}
if (empty($attributes['total_amount'])) {
$attributes['total_amount'] = random_int(100, 10000000) / 100.0;
}
if (empty($attributes['contribution_status_id'])) {
$attributes['contribution_status_id'] = 'Completed';
}
if (empty($attributes['currency'])) {
$config = CRM_Core_Config::singleton();
$attributes['currency'] = $config->defaultCurrency;
}

// create and return the contribution
$result = $this->callAPISuccess('Contribution', 'create', $attributes);
return reset($result['values']);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
{
"plugin_type_name": "match",
"plugin_class_name": "matcher_contribution",
"name": "Existing Contribution",
"description": "Will try to find pending existing contributions to reconcile.",
"weight": "20",
"config": {
"auto_exec": true,
"threshold": 0.1,
"mode": "default",
"accepted_contribution_states": [
"Pending"
],
"received_date_minimum": "-60 days",
"received_date_maximum": "+1 days",
"date_penalty": 0.2,
"request_amount_confirmation": true,
"payment_instrument_penalty": 0.2,
"amount_relative_minimum": 0.8,
"amount_relative_maximum": 1.2,
"amount_absolute_minimum": 0,
"amount_absolute_maximum": 0,
"amount_penalty": 0.6,
"value_propagation": {
"btx.financial_type_id": "contribution.financial_type_id",
"btx.campaign_id": "contribution.campaign_id"
}
},
"state": {}
}

0 comments on commit 2d169c4

Please sign in to comment.