From b3eb43c4ca826e36c0f2633ca7af347a2d42fa3e Mon Sep 17 00:00:00 2001 From: Eileen McNaughton Date: Wed, 6 Apr 2022 13:43:33 +1200 Subject: [PATCH] Test cleanup This removes the only call to ContributionSoft::retrieve that I could find in core, hence I added a deprecation notice to the function. There could be some extension uses - although it would never have been supported to call this rather than the api --- CRM/Contribute/BAO/ContributionSoft.php | 1 + .../Import/Parser/ContributionTest.php | 55 ++++++++----------- 2 files changed, 24 insertions(+), 32 deletions(-) diff --git a/CRM/Contribute/BAO/ContributionSoft.php b/CRM/Contribute/BAO/ContributionSoft.php index 7c3c75b49afa..56f52515501d 100644 --- a/CRM/Contribute/BAO/ContributionSoft.php +++ b/CRM/Contribute/BAO/ContributionSoft.php @@ -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); } diff --git a/tests/phpunit/CRM/Contribute/Import/Parser/ContributionTest.php b/tests/phpunit/CRM/Contribute/Import/Parser/ContributionTest.php index 4121cbfb94f9..d8bc0152a544 100644 --- a/tests/phpunit/CRM/Contribute/Import/Parser/ContributionTest.php +++ b/tests/phpunit/CRM/Contribute/Import/Parser/ContributionTest.php @@ -3,6 +3,10 @@ * @file * File for the CRM_Contribute_Import_Parser_ContributionTest class. */ + +use Civi\Api4\Contribution; +use Civi\Api4\ContributionSoft; + /** * Test Contribution import parser. * @@ -10,7 +14,6 @@ * @group headless */ class CRM_Contribute_Import_Parser_ContributionTest extends CiviUnitTestCase { - protected $_tablesToTruncate = []; use CRMTraits_Custom_CustomDataTrait; /** @@ -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', @@ -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']; @@ -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 @@ -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 @@ -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); @@ -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 @@ -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', @@ -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); } /** @@ -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); }