Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Test cleanup, deprecate ContributionSoft::retrieve #23106

Merged
merged 1 commit into from
Apr 6, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CRM/Contribute/BAO/ContributionSoft.php
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,7 @@ public static function formatSoftCreditParams(&$params, &$form) {
* @deprecated
*/
public static function retrieve($params, &$defaults) {
CRM_Core_Error::deprecatedFunctionWarning('apiv4');
return self::commonRetrieve(self::class, $params, $defaults);
}

Expand Down
55 changes: 23 additions & 32 deletions tests/phpunit/CRM/Contribute/Import/Parser/ContributionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,17 @@
* @file
* File for the CRM_Contribute_Import_Parser_ContributionTest class.
*/

use Civi\Api4\Contribution;
use Civi\Api4\ContributionSoft;

/**
* Test Contribution import parser.
*
* @package CiviCRM
* @group headless
*/
class CRM_Contribute_Import_Parser_ContributionTest extends CiviUnitTestCase {
protected $_tablesToTruncate = [];
use CRMTraits_Custom_CustomDataTrait;

/**
Expand Down Expand Up @@ -39,7 +42,7 @@ public function tearDown(): void {
*
* @throws \Exception
*/
public function testImportParserWithSoftCreditsByExternalIdentifier($thousandSeparator) {
public function testImportParserWithSoftCreditsByExternalIdentifier(string $thousandSeparator): void {
$this->setCurrencySeparators($thousandSeparator);
$contact1Params = [
'first_name' => 'Contact',
Expand All @@ -64,25 +67,21 @@ public function testImportParserWithSoftCreditsByExternalIdentifier($thousandSep
$mapperSoftCredit = [NULL, NULL, NULL, 'external_identifier'];
$mapperSoftCreditType = [NULL, NULL, NULL, '1'];
$this->runImport($values, CRM_Import_Parser::DUPLICATE_UPDATE, CRM_Contribute_Import_Parser::SOFT_CREDIT, $mapperSoftCredit, NULL, $mapperSoftCreditType);
$params = ['contact_id' => $contact1Id];
$values = [];
$contributionsOfMainContact = CRM_Contribute_BAO_Contribution::retrieve($params, $values, $values);
$this->assertEquals(1230.99, $contributionsOfMainContact->total_amount);
$this->assertEquals(1230.99, $contributionsOfMainContact->net_amount);
$this->assertEquals(0, $contributionsOfMainContact->fee_amount);

$params['contact_id'] = $contact2Id;
$contributionsOfSoftContact = CRM_Contribute_BAO_ContributionSoft::retrieve($params, $values);
$this->assertEquals(1, $contributionsOfMainContact->N, 'Contribution not added for primary contact');
$this->assertEquals(1, $contributionsOfSoftContact->N, 'Soft Contribution not added for secondary contact');
$this->callAPISuccess('ContributionSoft', 'Delete', ['id' => $contributionsOfSoftContact->id]);
$this->callAPISuccess('Contribution', 'Delete', ['id' => $contributionsOfMainContact->id]);
$contributionsOfMainContact = Contribution::get()->addWhere('contact_id', '=', $contact1Id)->execute();
$this->assertCount(1, $contributionsOfMainContact, 'Contribution not added for primary contact');
$this->assertEquals(1230.99, $contributionsOfMainContact->first()['total_amount']);
$this->assertEquals(1230.99, $contributionsOfMainContact->first()['net_amount']);
$this->assertEquals(0, $contributionsOfMainContact->first()['fee_amount']);

$contributionsOfSoftContact = ContributionSoft::get()->addWhere('contact_id', '=', $contact2Id)->execute();
$this->assertCount(1, $contributionsOfSoftContact, 'Contribution Soft not added for primary contact');
}

/**
* Test dates are parsed
*/
public function testParsedDates() {
public function testParsedDates(): void {
$mapperKeys = [];
$form = new CRM_Contribute_Import_Parser_Contribution($mapperKeys);
$params = ['receive_date' => '20/10/2019'];
Expand All @@ -98,10 +97,8 @@ public function testParsedDates() {

/**
* Test payment types are passed.
*
* @throws \CRM_Core_Exception
*/
public function testPaymentTypeLabel() {
public function testPaymentTypeLabel(): void {
$contactID = $this->individualCreate();
$values = ['contribution_contact_id' => $contactID, 'total_amount' => 10, 'financial_type' => 'Donation', 'payment_instrument' => 'Check'];
// Note that the expected result should logically be CRM_Import_Parser::valid but writing test to reflect not fix here
Expand All @@ -123,10 +120,8 @@ public function testPaymentTypeLabel() {

/**
* Test handling of contribution statuses.
*
* @throws \CRM_Core_Exception
*/
public function testContributionStatusLabel() {
public function testContributionStatusLabel(): void {
$contactID = $this->individualCreate();
$values = ['contribution_contact_id' => $contactID, 'total_amount' => 10, 'financial_type' => 'Donation', 'payment_instrument' => 'Check', 'contribution_status_id' => 'Pending'];
// Note that the expected result should logically be CRM_Import_Parser::valid but writing test to reflect not fix here
Expand Down Expand Up @@ -157,11 +152,9 @@ public function testContributionStatusLabel() {
}

/**
* Test dates are parsed
*
* @throws \CRM_Core_Exception
* Test dates are parsed.
*/
public function testParsedCustomDates() {
public function testParsedCustomDates(): void {
$this->createCustomGroupWithFieldOfType([], 'date');
$mapperKeys = [];
$form = new CRM_Contribute_Import_Parser_Contribution($mapperKeys);
Expand All @@ -181,7 +174,7 @@ public function testParsedCustomDates() {
$this->callAPISuccess('CustomGroup', 'delete', ['id' => $this->ids['CustomGroup']['Custom Group']]);
}

public function testParsedCustomOption() {
public function testParsedCustomOption(): void {
$contactID = $this->individualCreate();
$values = ['contribution_contact_id' => $contactID, 'total_amount' => 10, 'financial_type' => 'Donation', 'payment_instrument' => 'Check', 'contribution_status_id' => 'Pending'];
// Note that the expected result should logically be CRM_Import_Parser::valid but writing test to reflect not fix here
Expand All @@ -200,10 +193,8 @@ public function testParsedCustomOption() {

/**
* Test phone is included if it is part of dedupe rule.
*
* @throws \CRM_Core_Exception
*/
public function testPhoneMatchOnContact() {
public function testPhoneMatchOnContact(): void {
// Update existing unsupervised rule, change to general.
$unsupervisedRuleGroup = $this->callApiSuccess('RuleGroup', 'getsingle', [
'used' => 'Unsupervised',
Expand All @@ -230,7 +221,7 @@ public function testPhoneMatchOnContact() {
'rule_field' => 'phone_numeric',
]);
$fields = CRM_Contribute_BAO_Contribution::importableFields();
$this->assertTrue(array_key_exists('phone', $fields));
$this->assertArrayHasKey('phone', $fields);
}

/**
Expand All @@ -239,14 +230,14 @@ public function testPhoneMatchOnContact() {
* @param array $originalValues
*
* @param int $onDuplicateAction
* @param int $expectedResult
* @param int|null $expectedResult
* @param array|null $mapperSoftCredit
* @param array|null $mapperPhoneType
* @param array|null $mapperSoftCreditType
* @param array|null $fields
* Array of field names. Will be calculated from $originalValues if not passed in.
*/
protected function runImport($originalValues, $onDuplicateAction, $expectedResult, $mapperSoftCredit = NULL, $mapperPhoneType = NULL, $mapperSoftCreditType = NULL, $fields = NULL) {
protected function runImport(array $originalValues, int $onDuplicateAction, ?int $expectedResult, array $mapperSoftCredit = NULL, array $mapperPhoneType = NULL, array $mapperSoftCreditType = NULL, array $fields = NULL): void {
if (!$fields) {
$fields = array_keys($originalValues);
}
Expand Down