From afdf330b9b740e28925c10d101ec12410098140c Mon Sep 17 00:00:00 2001 From: Eileen McNaughton Date: Thu, 31 Aug 2023 12:55:48 +1200 Subject: [PATCH] Switch tests to use createTestEntity for payment processor --- .../CRM/Core/Payment/AuthorizeNetIPNTest.php | 37 ++--- .../CRM/Core/Payment/PayPalIPNTest.php | 10 +- .../CRM/Core/Payment/PayPalProIPNTest.php | 10 +- tests/phpunit/CRM/Export/BAO/ExportTest.php | 141 +++++++++--------- .../CRM/Member/Selector/SearchTest.php | 2 +- tests/phpunit/CiviTest/CiviUnitTestCase.php | 43 +++--- tests/phpunit/api/v3/PaymentTest.php | 4 +- 7 files changed, 120 insertions(+), 127 deletions(-) diff --git a/tests/phpunit/CRM/Core/Payment/AuthorizeNetIPNTest.php b/tests/phpunit/CRM/Core/Payment/AuthorizeNetIPNTest.php index c2be5534b3..a22d702443 100644 --- a/tests/phpunit/CRM/Core/Payment/AuthorizeNetIPNTest.php +++ b/tests/phpunit/CRM/Core/Payment/AuthorizeNetIPNTest.php @@ -1,7 +1,6 @@ _paymentProcessorID = $this->paymentProcessorAuthorizeNetCreate(['is_test' => 0]); + $this->_paymentProcessorID = $this->paymentProcessorAuthorizeNetCreate(['is_test' => 0], 'test'); $this->_contactID = $this->individualCreate(); $contributionPage = $this->callAPISuccess('ContributionPage', 'create', [ 'title' => 'Test Contribution Page', 'financial_type_id' => 'Donation', 'currency' => 'USD', - 'payment_processor' => $this->ids['PaymentProcessor']['authorize_net'], + 'payment_processor' => $this->ids['PaymentProcessor']['test'], 'max_amount' => 1000, 'receipt_from_email' => 'gaia@the.cosmos', 'receipt_from_name' => 'Pachamama', @@ -97,20 +95,15 @@ class CRM_Core_Payment_AuthorizeNetIPNTest extends CiviUnitTestCase { 'installments' => 2, 'hidden_AdditionalDetail' => 1, 'hidden_Premium' => 1, - 'payment_processor_id' => $this->ids['PaymentProcessor']['authorize_net'], + 'payment_processor_id' => $this->ids['PaymentProcessor']['test'], 'currency' => 'USD', 'source' => 'bob sled race', 'contribution_page_id' => $this->ids['ContributionPage'][0], 'is_recur' => TRUE, ]); $form->buildForm(); - try { - $form->postProcess(); - } - catch (PaymentProcessorException $e) { - $this->markTestSkipped('Error from A.net - cannot proceed'); - } - $contribution = Contribution::get()->setLimit(1)->addWhere('contribution_page_id', '=', $this->_contributionPageID)->execute()->first(); + $form->postProcess(); + $contribution = Contribution::get()->setLimit(1)->addWhere('contribution_page_id', '=', $this->ids['ContributionPage'][0])->execute()->first(); $this->ids['Contribution'][0] = $contribution['id']; $this->_contributionRecurID = $contribution['contribution_recur_id']; @@ -155,7 +148,7 @@ class CRM_Core_Payment_AuthorizeNetIPNTest extends CiviUnitTestCase { CRM_Core_BAO_ConfigSetting::enableComponent('CiviCampaign'); $this->setupRecurringPaymentProcessorTransaction([ 'installments' => 3, - ]); + ], []); $this->assertRecurStatus('Pending'); $IPN = new CRM_Core_Payment_AuthorizeNetIPN($this->getRecurTransaction()); @@ -179,7 +172,7 @@ class CRM_Core_Payment_AuthorizeNetIPNTest extends CiviUnitTestCase { $this->assertEquals('second_one', $secondContribution['trxn_id']); $this->assertEquals(date('Y-m-d'), date('Y-m-d', strtotime($secondContribution['receive_date']))); $this->assertEquals('expensive', $secondContribution['amount_level']); - $this->assertEquals($this->ids['campaign'][0], $secondContribution['campaign_id']); + $this->assertEquals($this->ids['Campaign']['default'], $secondContribution['campaign_id']); $IPN = new CRM_Core_Payment_AuthorizeNetIPN(array_merge($this->getRecurSubsequentTransaction(), ['x_subscription_paynum' => 3, 'x_trans_id' => 'three'])); $IPN->main(); @@ -220,7 +213,7 @@ class CRM_Core_Payment_AuthorizeNetIPNTest extends CiviUnitTestCase { //Call IPN with processor id. $IPN = new CRM_Core_Payment_AuthorizeNetIPN($this->getRecurTransaction(['processor_id' => $paymentProcessorID2])); $IPN->main(); - $contribution = $this->callAPISuccess('contribution', 'getsingle', ['id' => $this->_contributionID]); + $contribution = $this->callAPISuccess('contribution', 'getsingle', ['id' => $this->ids['Contribution']['default']]); $this->assertEquals(1, $contribution['contribution_status_id']); $this->assertEquals('6511143069', $contribution['trxn_id']); // source gets set by processor @@ -237,7 +230,7 @@ class CRM_Core_Payment_AuthorizeNetIPNTest extends CiviUnitTestCase { $this->setupRecurringPaymentProcessorTransaction(); $IPN = new CRM_Core_Payment_AuthorizeNetIPN($this->getRecurTransaction()); $IPN->main(); - $contribution = $this->callAPISuccess('contribution', 'getsingle', ['id' => $this->_contributionID]); + $contribution = $this->callAPISuccess('contribution', 'getsingle', ['id' => $this->ids['Contribution']['default']]); $this->assertEquals(1, $contribution['contribution_status_id']); $this->assertEquals('6511143069', $contribution['trxn_id']); // source gets set by processor @@ -301,12 +294,12 @@ class CRM_Core_Payment_AuthorizeNetIPNTest extends CiviUnitTestCase { public function testIPNPaymentMembershipRecurSuccessNoLeakage(): void { $mut = new CiviMailUtils($this, TRUE); $this->setupMembershipRecurringPaymentProcessorTransaction(['is_email_receipt' => TRUE]); - $this->addProfile('supporter_profile', $this->_contributionPageID); - $this->addProfile('honoree_individual', $this->_contributionPageID, 'soft_credit'); + $this->addProfile('supporter_profile', $this->ids['ContributionPage'][0]); + $this->addProfile('honoree_individual', $this->ids['ContributionPage'][0], 'soft_credit'); $this->callAPISuccess('ContributionSoft', 'create', [ 'contact_id' => $this->individualCreate(), - 'contribution_id' => $this->_contributionID, + 'contribution_id' => $this->ids['Contribution']['default'], 'soft_credit_type_id' => 'in_memory_of', 'amount' => 200, ]); @@ -373,7 +366,7 @@ class CRM_Core_Payment_AuthorizeNetIPNTest extends CiviUnitTestCase { public function testIPNPaymentMembershipRecurSuccessNoLeakageOnlineThenOffline(): void { $mut = new CiviMailUtils($this, TRUE); $this->setupMembershipRecurringPaymentProcessorTransaction(['is_email_receipt' => TRUE]); - $this->addProfile('supporter_profile', $this->_contributionPageID); + $this->addProfile('supporter_profile', $this->ids['ContributionPage'][0]); $IPN = new CRM_Core_Payment_AuthorizeNetIPN($this->getRecurTransaction()); $IPN->main(); $mut->checkAllMailLog([ @@ -427,7 +420,7 @@ class CRM_Core_Payment_AuthorizeNetIPNTest extends CiviUnitTestCase { * Additional parameters. * * @return array - * Parameters like AuthorizeNet silent post paramters. + * Parameters like AuthorizeNet silent post parameters. */ public function getRecurTransaction(array $params = []): array { return array_merge([ @@ -451,7 +444,7 @@ class CRM_Core_Payment_AuthorizeNetIPNTest extends CiviUnitTestCase { 'x_freight' => '0.00', 'x_tax_exempt' => 'FALSE', 'x_po_num' => '', - 'x_MD5_Hash' => '1B7C0C5B4DEDD9CAD0636E35E22FC594', + 'x_MD5_Hash' => '1B7C0C5B4DED9CAD0636E35E22FC594', 'x_cvv2_resp_code' => '', 'x_cavv_response' => '', 'x_test_request' => 'false', diff --git a/tests/phpunit/CRM/Core/Payment/PayPalIPNTest.php b/tests/phpunit/CRM/Core/Payment/PayPalIPNTest.php index 131075ad8a..ad2967f712 100644 --- a/tests/phpunit/CRM/Core/Payment/PayPalIPNTest.php +++ b/tests/phpunit/CRM/Core/Payment/PayPalIPNTest.php @@ -196,12 +196,12 @@ class CRM_Core_Payment_PayPalIPNTest extends CiviUnitTestCase { public function getPaypalRecurTransaction(): array { return [ 'contactID' => $this->_contactID, - 'contributionID' => $this->_contributionID, + 'contributionID' => $this->ids['Contribution']['default'], 'invoice' => 'xyz', - 'contributionRecurID' => $this->_contributionRecurID, + 'contributionRecurID' => $this->ids['ContributionRecur']['default'], 'mc_gross' => '15.00', 'module' => 'contribute', - 'payer_id' => '4NHUTA7ZUE92C', + 'payer_id' => '4NHU7ZUE92C', 'payment_status' => 'Completed', 'receiver_email' => 'sunil._1183377782_biz_api1.webaccess.co.in', 'txn_type' => 'subscr_payment', @@ -225,7 +225,7 @@ class CRM_Core_Payment_PayPalIPNTest extends CiviUnitTestCase { 'mc_fee' => '5.00', 'settle_amount' => '95.00', 'module' => 'contribute', - 'payer_id' => 'FV5ZW7TLMQ874', + 'payer_id' => 'FV5ZW7TL874', 'payment_status' => 'Completed', 'receiver_email' => 'sunil._1183377782_biz_api1.webaccess.co.in', 'txn_type' => 'web_accept', @@ -304,7 +304,7 @@ class CRM_Core_Payment_PayPalIPNTest extends CiviUnitTestCase { 'contact_id' => $this->_contactID, 'trxn_id' => NULL, 'invoice_id' => 'xyz', - 'contribution_status_id' => $pendingStatusID, + 'contribution_status_id' => 'Pending', 'is_email_receipt' => TRUE, ]; $this->_contributionID = $this->contributionCreate($params); diff --git a/tests/phpunit/CRM/Core/Payment/PayPalProIPNTest.php b/tests/phpunit/CRM/Core/Payment/PayPalProIPNTest.php index b96a15f6f1..c05fac7de2 100644 --- a/tests/phpunit/CRM/Core/Payment/PayPalProIPNTest.php +++ b/tests/phpunit/CRM/Core/Payment/PayPalProIPNTest.php @@ -63,7 +63,7 @@ class CRM_Core_Payment_PayPalProIPNTest extends CiviUnitTestCase { $_GET = $this->getPaypalProRecurTransaction(); $paypalIPN = new CRM_Core_Payment_PayPalProIPN($this->getPaypalProRecurTransaction()); $paypalIPN->main(); - $contribution = Contribution::get()->addWhere('id', '=', $this->_contributionID) + $contribution = Contribution::get()->addWhere('id', '=', $this->ids['Contribution']['default']) ->addSelect('contribution_status_id:name', 'trxn_id', 'source') ->execute()->first(); $this->assertEquals('Completed', $contribution['contribution_status_id:name']); @@ -75,12 +75,12 @@ class CRM_Core_Payment_PayPalProIPNTest extends CiviUnitTestCase { // created (this relies on the trxn_id being the same). $paypalIPN = new CRM_Core_Payment_PayPalProIPN($this->getPaypalProRecurTransaction()); $paypalIPN->main(); - $contributions = Contribution::get()->addWhere('contribution_recur_id', '=', $this->_contributionRecurID) + $contributions = Contribution::get()->addWhere('contribution_recur_id', '=', $this->ids['ContributionRecur']['default']) ->addSelect('contribution_status_id:name', 'trxn_id', 'source') ->execute(); $this->assertCount(1, $contributions); // source gets set by processor - $contributionRecur = $this->callAPISuccess('contribution_recur', 'getsingle', ['id' => $this->_contributionRecurID]); + $contributionRecur = $this->callAPISuccess('contribution_recur', 'getsingle', ['id' => $this->ids['ContributionRecur']['default']]); $this->assertEquals(5, $contributionRecur['contribution_status_id']); $paypalIPN = new CRM_Core_Payment_PayPalProIPN($this->getPaypalProRecurSubsequentTransaction()); $paypalIPN->main(); @@ -250,7 +250,7 @@ class CRM_Core_Payment_PayPalProIPNTest extends CiviUnitTestCase { * @return array * array representing a Paypal IPN POST */ - public function getSubsequentPaypalExpressTransaction() { + public function getSubsequentPaypalExpressTransaction(): array { return [ 'mc_gross' => '5.00', 'period_type' => ' Regular', @@ -336,7 +336,7 @@ class CRM_Core_Payment_PayPalProIPNTest extends CiviUnitTestCase { 'amount_per_cycle' => '15.00', 'mc_gross' => '15.00', 'payment_date' => '03:59:05 Jul 14, 2013 PDT', - 'rp_invoice_id' => 'i=xyz&m=contribute&c=' . $this->_contactID . '&r=' . $this->_contributionRecurID . '&b=' . $this->_contributionID . '&p=null', + 'rp_invoice_id' => 'i=xyz&m=contribute&c=' . $this->_contactID . '&r=' . $this->ids['ContributionRecur']['default'] . '&b=' . $this->ids['Contribution']['default'] . '&p=null', 'payment_status' => 'Completed', 'business' => 'nowhere@civicrm.org', 'last_name' => 'Roberty', diff --git a/tests/phpunit/CRM/Export/BAO/ExportTest.php b/tests/phpunit/CRM/Export/BAO/ExportTest.php index 2322353c04..ef42cab042 100644 --- a/tests/phpunit/CRM/Export/BAO/ExportTest.php +++ b/tests/phpunit/CRM/Export/BAO/ExportTest.php @@ -71,7 +71,7 @@ class CRM_Export_BAO_ExportTest extends CiviUnitTestCase { /** * Cleanup data. * - * @throws \Exception + * @throws \CRM_Core_Exception */ public function tearDown(): void { $this->quickCleanUpFinancialEntities(); @@ -116,7 +116,7 @@ class CRM_Export_BAO_ExportTest extends CiviUnitTestCase { * Basic test to ensure the exportComponents function can export selected fields for contribution. * * @throws \CRM_Core_Exception - * @throws \League\Csv\Exception + * @throws \League\Csv\UnableToProcessCsv */ public function testExportComponentsContribution(): void { $this->setUpContributionExportData(); @@ -131,7 +131,7 @@ class CRM_Export_BAO_ExportTest extends CiviUnitTestCase { ['contact_type' => 'Individual', 'name' => 'email', 1], ['name' => 'trxn_id'], ]; - $this->hookClass->setHook('civicrm_export', array($this, 'confirmHookWasCalled')); + $this->hookClass->setHook('civicrm_export', [$this, 'confirmHookWasCalled']); $this->doExportTest([ 'ids' => $this->contributionIDs, @@ -161,7 +161,7 @@ class CRM_Export_BAO_ExportTest extends CiviUnitTestCase { * Basic test to ensure the exportComponents function can export with soft credits enabled. * * @throws \CRM_Core_Exception - * @throws \League\Csv\Exception + * @throws \League\Csv\UnableToProcessCsv */ public function testExportComponentsContributionSoftCredits(): void { $this->setUpContributionExportData(); @@ -205,8 +205,7 @@ class CRM_Export_BAO_ExportTest extends CiviUnitTestCase { * Basic test to ensure the exportComponents function can export selected fields for contribution. * * @throws \CRM_Core_Exception - * @throws \League\Csv\Exception - * @throws \CRM_Core_Exception + * @throws \League\Csv\UnableToProcessCsv */ public function testExportComponentsMembership(): void { $this->setUpMembershipExportData(); @@ -327,7 +326,7 @@ class CRM_Export_BAO_ExportTest extends CiviUnitTestCase { * Basic test to ensure the exportComponents function can export selected fields for activity * * @throws \CRM_Core_Exception - * @throws \League\Csv\Exception + * @throws \League\Csv\UnableToProcessCsv */ public function testExportComponentsActivity(): void { $this->setUpActivityExportData(); @@ -350,12 +349,16 @@ class CRM_Export_BAO_ExportTest extends CiviUnitTestCase { /** * Test the function that extracts the arrays used to structure the output. * - * The keys in the output fields array should by matched by field aliases in the sql query (with - * exceptions of course - currently country is one - although maybe a future refactor can change that!). + * The keys in the output fields array should by matched by field aliases in + * the sql query (with exceptions of course - currently country is one - + * although maybe a future refactor can change that!). + * + * We are trying to move towards simpler processing in the per row iteration + * as that may be repeated 100,000 times and in general we should simply be + * able to match the query fields to our expected rows & do a little + * pseudoconstant mapping. * - * We are trying to move towards simpler processing in the per row iteration as that may be - * repeated 100,000 times and in general we should simply be able to match the query fields to - * our expected rows & do a little pseudoconstant mapping. + * @throws \CRM_Core_Exception */ public function testGetExportStructureArrays(): void { // This is how return properties are formatted internally within the function for passing to the BAO query. @@ -407,8 +410,6 @@ class CRM_Export_BAO_ExportTest extends CiviUnitTestCase { /** * Set up some data for us to do testing on. - * - * @throws \CRM_Core_Exception */ public function setUpContributionExportData(): void { $this->setUpContactExportData(); @@ -418,27 +419,21 @@ class CRM_Export_BAO_ExportTest extends CiviUnitTestCase { /** * Set up some data for us to do testing on. - * - * @throws \CRM_Core_Exception */ public function setUpMembershipExportData(): void { $this->setUpContactExportData(); // Create an extra so we don't get false passes due to 1 $this->contactMembershipCreate(['contact_id' => $this->contactIDs[0]]); - - $this->_contactID = $this->contactIDs[0]; - $this->_paymentProcessorID = $this->paymentProcessorCreate(); + $this->paymentProcessorCreate(); $this->setupMembershipRecurringPaymentProcessorTransaction(); - $membershipID = $this->callAPISuccessGetValue('Membership', ['return' => 'id', 'contact_id' => $this->_contactID, 'options' => ['limit' => 1, 'sort' => 'id DESC']]); + $membershipID = $this->callAPISuccessGetValue('Membership', ['return' => 'id', 'contact_id' => $this->ids['Contact']['individual_0'], 'options' => ['limit' => 1, 'sort' => 'id DESC']]); $this->membershipIDs[] = $membershipID; } /** * Set up data to test case export. - * - * @throws \CRM_Core_Exception */ public function setupCaseExportData(): void { $contactID1 = $this->individualCreate(); @@ -498,7 +493,7 @@ class CRM_Export_BAO_ExportTest extends CiviUnitTestCase { $this->callAPISuccess('address', 'create', [ 'contact_id' => $contactB, - 'location_type_id' => "Home", + 'location_type_id' => 'Home', 'master_id' => $addressId, ]); $this->masterAddressID = $addressId; @@ -566,7 +561,7 @@ class CRM_Export_BAO_ExportTest extends CiviUnitTestCase { * This is specific to the example in CRM-14398 * * @throws \CRM_Core_Exception - * @throws \League\Csv\Exception + * @throws \League\Csv\UnableToProcessCsv */ public function testExportPseudoFieldCampaign(): void { $this->setUpContributionExportData(); @@ -581,7 +576,7 @@ class CRM_Export_BAO_ExportTest extends CiviUnitTestCase { 'ids' => [$this->contactIDs[1]], 'exportMode' => CRM_Export_Form_Select::CONTRIBUTE_EXPORT, 'fields' => $selectedFields, - 'componentClause' => "contact_a.id IN (" . implode(",", $this->contactIDs) . ")", + 'componentClause' => 'contact_a.id IN (' . implode(',', $this->contactIDs) . ')', ]); $row = $this->csv->fetchOne(); $this->assertEquals('Big campaign and kinda long too', $row['Campaign Title']); @@ -592,7 +587,7 @@ class CRM_Export_BAO_ExportTest extends CiviUnitTestCase { * Test exporting relationships. * * @throws \CRM_Core_Exception - * @throws \League\Csv\Exception + * @throws \League\Csv\UnableToProcessCsv */ public function testExportRelationships(): void { $organization1 = $this->organizationCreate(['organization_name' => 'Org 1', 'legal_name' => 'pretty legal', 'contact_source' => 'friend who took a law paper once']); @@ -632,10 +627,10 @@ class CRM_Export_BAO_ExportTest extends CiviUnitTestCase { * * @param bool $includeHouseHold * - * @throws CRM_Core_Exception - * @throws \League\Csv\Exception + * @throws \CRM_Core_Exception + * @throws \League\Csv\UnableToProcessCsv */ - public function testExportRelationshipsMergeToHousehold($includeHouseHold): void { + public function testExportRelationshipsMergeToHousehold(bool $includeHouseHold): void { [$householdID, $houseHoldTypeID] = $this->setUpHousehold(); if ($includeHouseHold) { @@ -670,11 +665,11 @@ class CRM_Export_BAO_ExportTest extends CiviUnitTestCase { * * @param bool $includeHouseHold * - * @throws CRM_Core_Exception - * @throws \League\Csv\Exception + * @throws \CRM_Core_Exception + * @throws \League\Csv\UnableToProcessCsv */ - public function testExportMergeToHousehold($includeHouseHold): void { - [$householdID, $houseHoldTypeID] = $this->setUpHousehold(); + public function testExportMergeToHousehold(bool $includeHouseHold): void { + [$householdID] = $this->setUpHousehold(); if ($includeHouseHold) { $this->contactIDs[] = $householdID; @@ -699,7 +694,7 @@ class CRM_Export_BAO_ExportTest extends CiviUnitTestCase { * Test exporting relationships. * * @throws \CRM_Core_Exception - * @throws \League\Csv\Exception + * @throws \League\Csv\UnableToProcessCsv */ public function testExportRelationshipsMergeToHouseholdAllFields(): void { [$householdID] = $this->setUpHousehold(); @@ -718,8 +713,7 @@ class CRM_Export_BAO_ExportTest extends CiviUnitTestCase { * Test custom data exporting. * * @throws \CRM_Core_Exception - * @throws \Civi\API\Exception\UnauthorizedException - * @throws \League\Csv\Exception + * @throws \League\Csv\UnableToProcessCsv */ public function testExportCustomData(): void { $this->setUpContactExportData(); @@ -812,13 +806,13 @@ class CRM_Export_BAO_ExportTest extends CiviUnitTestCase { foreach (array_keys(array_merge($locationTypes, [' ' => ['Primary']])) as $locationType) { $locationTypeID = CRM_Core_PseudoConstant::getKey('CRM_Core_BAO_IM', 'location_type_id', $locationType); $fields[] = ['name' => 'im_provider', 'location_type_id' => $locationTypeID]; - foreach ($relationships as $contactID => $relationship) { + foreach ($relationships as $relationship) { $fields[] = ['name' => 'im_provider', 'relationship_type_id' => $relationship['relationship_type_id'], 'relationship_direction' => 'a_b', 'location_type_id' => $locationTypeID]; } foreach ($providers as $provider) { $providerID = CRM_Core_PseudoConstant::getKey('CRM_Core_BAO_IM', 'provider_id', $provider); $fields[] = ['name' => 'im', 'location_type_id' => $locationTypeID, 'im_provider_id' => $providerID]; - foreach ($relationships as $contactID => $relationship) { + foreach ($relationships as $relationship) { $fields[] = ['name' => 'im', 'location_type_id' => $locationTypeID, 'im_provider_id' => $providerID, 'relationship_type_id' => $relationship['relationship_type_id'], 'relationship_direction' => 'a_b']; } } @@ -897,14 +891,14 @@ class CRM_Export_BAO_ExportTest extends CiviUnitTestCase { $fields[] = ['name' => 'phone', 'location_type_id' => $locationTypeID]; $fields[] = ['name' => 'phone_type', 'location_type_id' => $locationTypeID]; $fields[] = ['name' => 'phone_type_id', 'location_type_id' => $locationTypeID]; - foreach ($relationships as $contactID => $relationship) { + foreach ($relationships as $relationship) { $fields[] = ['name' => 'phone_type_id', 'relationship_type_id' => $relationship['relationship_type_id'], 'relationship_direction' => 'a_b', 'location_type_id' => $locationTypeID]; $fields[] = ['name' => 'phone_type', 'relationship_type_id' => $relationship['relationship_type_id'], 'relationship_direction' => 'a_b', 'location_type_id' => $locationTypeID]; } foreach ($phoneTypes as $phoneType) { $phoneTypeID = CRM_Core_PseudoConstant::getKey('CRM_Core_BAO_Phone', 'phone_type_id', $phoneType); $fields[] = ['name' => 'phone', 'phone_type_id' => $phoneTypeID, 'location_type_id' => $locationTypeID]; - foreach ($relationships as $contactID => $relationship) { + foreach ($relationships as $relationship) { $fields[] = ['name' => 'phone_type_id', 'phone_type_id' => $phoneTypeID, 'relationship_type_id' => $relationship['relationship_type_id'], 'relationship_direction' => 'a_b', 'location_type_id' => $locationTypeID]; $fields[] = ['name' => 'phone_type', 'phone_type_id' => $phoneTypeID, 'relationship_type_id' => $relationship['relationship_type_id'], 'relationship_direction' => 'a_b', 'location_type_id' => $locationTypeID]; } @@ -971,7 +965,7 @@ class CRM_Export_BAO_ExportTest extends CiviUnitTestCase { // ' ' denotes primary location type. foreach (array_keys(array_merge($locationTypes, [' ' => ['Primary']])) as $locationType) { - foreach ($relationships as $contactID => $relationship) { + foreach ($relationships as $relationship) { $fields[] = [ 'contact_type' => 'Individual', 'relationship_type_id' => $relationship['relationship_type_id'], @@ -1229,17 +1223,22 @@ class CRM_Export_BAO_ExportTest extends CiviUnitTestCase { } /** - * Test to ensure that 'Merge All Contacts with the Same Address' works on export. + * Test to ensure that 'Merge All Contacts with the Same Address' works on + * export. * - * 3 contacts are created A, B and C where A and B are individual contacts that share same address via master_id. - * C is a household contact whose member is contact A. + * 3 contacts are created A, B and C where A and B are individual contacts + * that share same address via master_id. C is a household contact whose + * member is contact A. * - * These 3 contacts are selected for export with 'Merge All Contacts with the Same Address' = TRUE - * And at the end export table contain only 1 contact i.e. is C as A and B got merged into 1 as they share same address but then A is Household member of C. - * So C take preference over A and thus C is exported as result. + * These 3 contacts are selected for export with 'Merge All Contacts with the + * Same Address' = TRUE And at the end export table contain only 1 contact + * i.e. is C as A and B got merged into 1 as they share same address but then + * A is Household member of C. So C take preference over A and thus C is + * exported as result. * * @throws \CRM_Core_Exception * @throws \League\Csv\Exception + * @throws \League\Csv\UnableToProcessCsv */ public function testMergeSameAddressOnExport(): void { $this->individualCreate(); @@ -1264,12 +1263,13 @@ class CRM_Export_BAO_ExportTest extends CiviUnitTestCase { ]); $this->assertCount(1, $this->csv); - $row = $this->csv->fetchOne(); + $this->csv->fetchOne(); $this->assertEquals('Household', $this->csv->fetchOne()['Contact Type']); } /** - * Test that deceased and do not mail contacts are removed from contacts before + * Test that deceased and do not mail contacts are removed from contacts + * before * * @dataProvider getReasonsNotToMail * @@ -1277,7 +1277,7 @@ class CRM_Export_BAO_ExportTest extends CiviUnitTestCase { * @param array $addressReason * * @throws \CRM_Core_Exception - * @throws \League\Csv\Exception + * @throws \League\Csv\UnableToProcessCsv */ public function testExportDeceasedDoNotMail($reason, $addressReason): void { $contactA = $this->callAPISuccess('contact', 'create', [ @@ -1341,7 +1341,7 @@ class CRM_Export_BAO_ExportTest extends CiviUnitTestCase { ]); $row = $this->csv->fetchOne(); - $this->assertNotTrue(in_array('Stage', $this->processor->getHeaderRows())); + $this->assertNotContains('Stage', $this->processor->getHeaderRows()); $this->assertEquals('Dear John', $row['Email Greeting']); $this->assertCount(1, $this->csv); } @@ -1379,7 +1379,7 @@ class CRM_Export_BAO_ExportTest extends CiviUnitTestCase { $relationshipTypes = $this->callAPISuccess('RelationshipType', 'get', [])['values']; $houseHoldTypeID = NULL; - foreach ($relationshipTypes as $id => $relationshipType) { + foreach ($relationshipTypes as $relationshipType) { if ($relationshipType['name_a_b'] === 'Household Member of') { $houseHoldTypeID = $relationshipType['id']; } @@ -1402,7 +1402,7 @@ class CRM_Export_BAO_ExportTest extends CiviUnitTestCase { * * @param int $exportMode */ - public function ensureComponentIsEnabled($exportMode): void { + public function ensureComponentIsEnabled(int $exportMode): void { if ($exportMode === CRM_Export_Form_Select::CASE_EXPORT) { CRM_Core_BAO_ConfigSetting::enableComponent('CiviCase'); } @@ -1414,9 +1414,9 @@ class CRM_Export_BAO_ExportTest extends CiviUnitTestCase { * @dataProvider additionalFieldsDataProvider * * @param int $exportMode - * @param $expected + * @param array $expected */ - public function testAdditionalReturnProperties($exportMode, $expected): void { + public function testAdditionalReturnProperties(int $exportMode, array $expected): void { $this->ensureComponentIsEnabled($exportMode); $processor = new CRM_Export_BAO_ExportProcessor($exportMode, NULL, 'AND'); $metadata = $processor->getAdditionalReturnProperties(); @@ -1429,9 +1429,9 @@ class CRM_Export_BAO_ExportTest extends CiviUnitTestCase { * @dataProvider allFieldsDataProvider * * @param int $exportMode - * @param $expected + * @param array $expected */ - public function testDefaultReturnProperties($exportMode, $expected): void { + public function testDefaultReturnProperties(int $exportMode, array $expected): void { $this->ensureComponentIsEnabled($exportMode); $processor = new CRM_Export_BAO_ExportProcessor($exportMode, NULL, 'AND'); $metadata = $processor->getDefaultReturnProperties(); @@ -1446,7 +1446,8 @@ class CRM_Export_BAO_ExportTest extends CiviUnitTestCase { public function additionalFieldsDataProvider(): array { return [ [ - 'anything that will then be defaulting ton contact', + // 0 defaults to 'contact' + 0, $this->getExtraReturnProperties(), ], [ @@ -1483,7 +1484,7 @@ class CRM_Export_BAO_ExportTest extends CiviUnitTestCase { public function allFieldsDataProvider(): array { return [ [ - 'anything that will then be defaulting ton contact', + 0, $this->getBasicReturnProperties(TRUE), ], [ @@ -1617,9 +1618,7 @@ class CRM_Export_BAO_ExportTest extends CiviUnitTestCase { 'phone_type' => 1, ]; if (!$isContactMode) { - unset($returnProperties['groups']); - unset($returnProperties['tags']); - unset($returnProperties['notes']); + unset($returnProperties['groups'], $returnProperties['tags'], $returnProperties['notes']); } return $returnProperties; } @@ -1848,7 +1847,7 @@ class CRM_Export_BAO_ExportTest extends CiviUnitTestCase { * * @dataProvider getSqlColumnsOutput */ - public function testGetSQLColumnsAndHeaders($exportMode, $expected, $expectedHeaders): void { + public function testGetSQLColumnsAndHeaders(int $exportMode, array $expected, array $expectedHeaders): void { $this->ensureComponentIsEnabled($exportMode); // We need some data so that we can get to the end of the export // function. Hopefully one day that won't be required to get metadata info out. @@ -1903,14 +1902,14 @@ class CRM_Export_BAO_ExportTest extends CiviUnitTestCase { public function textExportParticipantSpecifyFieldsNoPayment(): void { $selectedFields = $this->getAllSpecifiableParticipantReturnFields(); foreach ($selectedFields as $index => $field) { - if (substr($field[1], 0, 22) === 'componentPaymentField_') { + if (strpos($field[1], 'componentPaymentField_') === 0) { unset($selectedFields[$index]); } } $expected = $this->getAllSpecifiableParticipantReturnFields(); foreach ($expected as $index => $field) { - if (substr($index, 0, 22) === 'componentPaymentField_') { + if (strpos($index, 'componentPaymentField_') === 0) { unset($expected[$index]); } } @@ -2171,7 +2170,7 @@ class CRM_Export_BAO_ExportTest extends CiviUnitTestCase { public function getSqlColumnsOutput(): array { return [ [ - 'anything that will then be defaulting ton contact', + 0, $this->getBasicSqlColumnDefinition(TRUE), $this->getBasicHeaderDefinition(TRUE), ], @@ -2216,7 +2215,7 @@ class CRM_Export_BAO_ExportTest extends CiviUnitTestCase { * * @return array */ - protected function getBasicHeaderDefinition($isContactExport): array { + protected function getBasicHeaderDefinition(bool $isContactExport): array { $headers = [ 0 => 'Contact ID', 1 => 'Contact Type', @@ -2947,7 +2946,7 @@ class CRM_Export_BAO_ExportTest extends CiviUnitTestCase { $variableFields = ['Created Date', 'Modified Date', 'Contact Hash']; foreach ($expected as $key => $value) { if (in_array($key, $variableFields)) { - $this->assertNotTrue(empty($row[$key])); + $this->assertNotEmpty($row[$key]); } elseif (array_key_exists($key, $alternatives)) { $this->assertContains($row[$key], $alternatives[$key]); @@ -2960,8 +2959,6 @@ class CRM_Export_BAO_ExportTest extends CiviUnitTestCase { /** * Test get preview function on export processor. - * - * @throws \CRM_Core_Exception */ public function testExportGetPreview(): void { $this->setUpContactExportData(); @@ -2999,8 +2996,6 @@ class CRM_Export_BAO_ExportTest extends CiviUnitTestCase { /** * Set up contacts which will be merged with the same address option. - * - * @throws \CRM_Core_Exception */ protected function setUpContactSameAddressExportData(): void { $this->setUpContactExportData(); @@ -3021,6 +3016,8 @@ class CRM_Export_BAO_ExportTest extends CiviUnitTestCase { /** * Test for single select Autocomplete custom field. * + * @throws \CRM_Core_Exception + * @throws \League\Csv\UnableToProcessCsv */ public function testSingleAndMultiSelectAutoComplete(): void { $customGroupId = $this->customGroupCreate([ diff --git a/tests/phpunit/CRM/Member/Selector/SearchTest.php b/tests/phpunit/CRM/Member/Selector/SearchTest.php index 750e911d79..b53381b218 100644 --- a/tests/phpunit/CRM/Member/Selector/SearchTest.php +++ b/tests/phpunit/CRM/Member/Selector/SearchTest.php @@ -21,7 +21,7 @@ class CRM_Member_Selector_SearchTest extends CiviUnitTestCase { */ public function testSelectorGetRows(): void { $contactID = $this->individualCreate(); - $this->_paymentProcessorID = $this->paymentProcessorCreate(); + $this->paymentProcessorCreate(); $this->setupMembershipRecurringPaymentProcessorTransaction(); $membership = $this->callAPISuccessGetSingle('Membership', ['contact_id' => $contactID]); $membershipID = (int) $membership['id']; diff --git a/tests/phpunit/CiviTest/CiviUnitTestCase.php b/tests/phpunit/CiviTest/CiviUnitTestCase.php index f2fc560d36..2186a2b2c0 100644 --- a/tests/phpunit/CiviTest/CiviUnitTestCase.php +++ b/tests/phpunit/CiviTest/CiviUnitTestCase.php @@ -747,14 +747,15 @@ class CiviUnitTestCase extends PHPUnit\Framework\TestCase { * Create test Authorize.net instance. * * @param array $params + * @param string $identifier * * @return int */ - public function paymentProcessorAuthorizeNetCreate(array $params = []): int { + public function paymentProcessorAuthorizeNetCreate(array $params = [], string $identifier = 'authorize_net'): int { $params = array_merge([ 'name' => 'Authorize', 'domain_id' => CRM_Core_Config::domainID(), - 'payment_processor_type_id' => 'AuthNet', + 'payment_processor_type_id:name' => 'AuthNet', 'title' => 'AuthNet', 'is_active' => 1, 'is_default' => 0, @@ -768,8 +769,7 @@ class CiviUnitTestCase extends PHPUnit\Framework\TestCase { 'billing_mode' => 1, ], $params); - $result = $this->callAPISuccess('PaymentProcessor', 'create', $params); - $this->ids['PaymentProcessor']['authorize_net'] = (int) $result['id']; + $result = $this->createTestEntity('PaymentProcessor', $params, $identifier); return (int) $result['id']; } @@ -815,19 +815,19 @@ class CiviUnitTestCase extends PHPUnit\Framework\TestCase { 'domain_id' => 1, 'name' => 'Dummy', 'title' => 'Dummy', - 'payment_processor_type_id' => 'Dummy', + 'payment_processor_type_id:name' => 'Dummy', 'financial_account_id' => 12, 'is_test' => TRUE, 'is_active' => 1, 'user_name' => '', - 'url_site' => 'http://dummy.com', - 'url_recur' => 'http://dummy.com', + 'url_site' => 'https://dummy.com', + 'url_recur' => 'https://dummy.com', 'billing_mode' => 1, 'sequential' => 1, - 'payment_instrument_id' => 'Debit Card', + 'payment_instrument_id:name' => 'Debit Card', ]; $processorParams = array_merge($processorParams, $params); - $processor = $this->callAPISuccess('PaymentProcessor', 'create', $processorParams); + $processor = $this->createTestEntity('PaymentProcessor', $processorParams, 'dummy'); return $processor['id']; } @@ -2128,10 +2128,11 @@ class CiviUnitTestCase extends PHPUnit\Framework\TestCase { * & the best protection against that is the functions this class affords * * @param array $params + * @param string $identifier * * @return int $result['id'] payment processor id */ - public function paymentProcessorCreate($params = []) { + public function paymentProcessorCreate(array $params = [], $identifier = 'test'): int { $params = array_merge([ 'title' => $params['name'] ?? 'demo', 'domain_id' => CRM_Core_Config::domainID(), @@ -2150,7 +2151,7 @@ class CiviUnitTestCase extends PHPUnit\Framework\TestCase { 'financial_type_id' => 1, 'financial_account_id' => 12, // Credit card = 1 so can pass 'by accident'. - 'payment_instrument_id' => 'Debit Card', + 'payment_instrument_id:name' => 'Debit Card', ], $params); if (!is_numeric($params['payment_processor_type_id'])) { // really the api should handle this through getoptions but it's not exactly api call so lets just sort it @@ -2160,7 +2161,7 @@ class CiviUnitTestCase extends PHPUnit\Framework\TestCase { 'return' => 'id', ], 'integer'); } - $result = $this->callAPISuccess('payment_processor', 'create', $params); + $result = $this->createTestEntity('PaymentProcessor', $params, $identifier); return $result['id']; } @@ -2184,20 +2185,23 @@ class CiviUnitTestCase extends PHPUnit\Framework\TestCase { * * @param array $recurParams (Optional) * @param array $contributionParams (Optional) + * @param string $identifier */ - public function setupRecurringPaymentProcessorTransaction(array $recurParams = [], array $contributionParams = []): void { - $this->ids['campaign'][0] = $this->callAPISuccess('Campaign', 'create', ['title' => 'get the money'])['id']; + public function setupRecurringPaymentProcessorTransaction(array $recurParams = [], array $contributionParams = [], string $identifier = 'default'): void { + if (empty($this->ids['Campaign'][$identifier]) && CRM_Core_Component::isEnabled('CiviCampaign')) { + $this->createTestEntity('Campaign', ['title' => 'get the money', 'name' => 'money'], $identifier)['id']; + } $contributionParams = array_merge([ 'total_amount' => '200', 'invoice_id' => 'xyz', 'financial_type_id' => 'Donation', 'contact_id' => $this->ids['Contact']['individual_0'], 'contribution_page_id' => $this->ids['ContributionPage'][0] ?? NULL, - 'payment_processor_id' => $this->_paymentProcessorID, + 'payment_processor_id' => $this->ids['PaymentProcessor']['test'], 'receive_date' => '2019-07-25 07:34:23', 'skipCleanMoney' => TRUE, 'amount_level' => 'expensive', - 'campaign_id' => $this->ids['campaign'][0], + 'campaign_id' => $this->ids['Campaign'][$identifier] ?? NULL, 'source' => 'Online Contribution: Page name', ], $contributionParams); $contributionRecur = $this->callAPISuccess('contribution_recur', 'create', array_merge([ @@ -2209,14 +2213,13 @@ class CiviUnitTestCase extends PHPUnit\Framework\TestCase { 'frequency_interval' => 1, 'contribution_status_id' => 2, 'invoice_id' => $contributionParams['invoice_id'], - 'payment_processor_id' => $this->_paymentProcessorID, + 'payment_processor_id' => $this->ids['PaymentProcessor']['test'], // processor provided ID - use contact ID as proxy. 'processor_id' => $this->ids['Contact']['individual_0'], 'api.Order.create' => $contributionParams, ], $recurParams))['values'][0]; - $this->ids['ContributionRecur']['default'] = $this->_contributionRecurID = $contributionRecur['id']; - $this->ids['Contribution']['default'] = $this->_contributionID = $contributionRecur['api.Order.create']['id']; - $this->ids['Contribution'][0] = $this->_contributionID; + $this->ids['ContributionRecur'][$identifier] = $contributionRecur['id']; + $this->ids['Contribution'][$identifier] = $this->ids['Contribution'][0] = $contributionRecur['api.Order.create']['id']; } /** diff --git a/tests/phpunit/api/v3/PaymentTest.php b/tests/phpunit/api/v3/PaymentTest.php index 863b88ba6e..7a99e890e4 100644 --- a/tests/phpunit/api/v3/PaymentTest.php +++ b/tests/phpunit/api/v3/PaymentTest.php @@ -1185,8 +1185,8 @@ class api_v3_PaymentTest extends CiviUnitTestCase { * Test that Payment.create uses the to_account of the payment processor. */ public function testPaymentWithProcessorWithOddFinancialAccount(): void { - $processor = $this->dummyProcessorCreate(['financial_account_id' => 'Deposit Bank Account', 'payment_instrument_id' => 'Cash']); - $processor2 = $this->dummyProcessorCreate(['financial_account_id' => 'Payment Processor Account', 'name' => 'p2', 'payment_instrument_id' => 'EFT']); + $processor = $this->dummyProcessorCreate(['financial_account_id:name' => 'Deposit Bank Account', 'payment_instrument_id:name' => 'Cash']); + $processor2 = $this->dummyProcessorCreate(['financial_account_id:name' => 'Payment Processor Account', 'name' => 'p2', 'payment_instrument_id:name' => 'EFT']); $contributionParams = [ 'total_amount' => 100, 'currency' => 'USD', -- 2.25.1