From ae9d2a2fb1c9d92752aec67b201396e244a2ac2d Mon Sep 17 00:00:00 2001 From: Eileen McNaughton Date: Wed, 7 Jun 2023 14:39:04 +1200 Subject: [PATCH] More test fixes on cleanup, helper use --- .../phpunit/CRM/Event/Form/Task/BadgeTest.php | 3 +- .../Event/Import/Parser/ParticipantTest.php | 7 +- tests/phpunit/api/v3/EventTest.php | 7 +- tests/phpunit/api/v3/OrderTest.php | 13 ++- .../phpunit/api/v3/ParticipantPaymentTest.php | 82 +++++++------------ 5 files changed, 41 insertions(+), 71 deletions(-) diff --git a/tests/phpunit/CRM/Event/Form/Task/BadgeTest.php b/tests/phpunit/CRM/Event/Form/Task/BadgeTest.php index 3db7e4c6b1..2bd41ccbec 100644 --- a/tests/phpunit/CRM/Event/Form/Task/BadgeTest.php +++ b/tests/phpunit/CRM/Event/Form/Task/BadgeTest.php @@ -13,7 +13,8 @@ class CRM_Event_Form_Task_BadgeTest extends CiviUnitTestCase { use CRMTraits_Custom_CustomDataTrait; public function tearDown(): void { - $this->quickCleanup(['civicrm_participant', 'civicrm_print_label'], TRUE); + $this->quickCleanUpFinancialEntities(); + $this->quickCleanup(['civicrm_print_label'], TRUE); parent::tearDown(); } diff --git a/tests/phpunit/CRM/Event/Import/Parser/ParticipantTest.php b/tests/phpunit/CRM/Event/Import/Parser/ParticipantTest.php index 0eff6061af..5574312559 100644 --- a/tests/phpunit/CRM/Event/Import/Parser/ParticipantTest.php +++ b/tests/phpunit/CRM/Event/Import/Parser/ParticipantTest.php @@ -27,9 +27,8 @@ class CRM_Event_Import_Parser_ParticipantTest extends CiviUnitTestCase { * This method is called after a test is executed. */ public function tearDown(): void { + $this->quickCleanUpFinancialEntities(); $this->quickCleanup([ - 'civicrm_event', - 'civicrm_participant', 'civicrm_contact', 'civicrm_email', 'civicrm_user_job', @@ -117,7 +116,7 @@ class CRM_Event_Import_Parser_ParticipantTest extends CiviUnitTestCase { * Test that an external id will not match to a deleted contact.. */ public function testImportWithExternalID() :void { - $this->eventCreate(['title' => 'Rain-forest Cup Youth Soccer Tournament']); + $this->eventCreatePaid(['title' => 'Rain-forest Cup Youth Soccer Tournament']); $this->individualCreate(['external_identifier' => 'ref-77', 'is_deleted' => TRUE]); $this->importCSV('participant_with_ext_id.csv', [ ['name' => 'event_id'], @@ -142,7 +141,7 @@ class CRM_Event_Import_Parser_ParticipantTest extends CiviUnitTestCase { * Test that imports work generally. */ public function testImportParticipant() :void { - $this->eventCreate(['title' => 'Rain-forest Cup Youth Soccer Tournament']); + $this->eventCreatePaid(['title' => 'Rain-forest Cup Youth Soccer Tournament']); $contactID = $this->individualCreate(['external_identifier' => 'ref-77']); $this->importCSV('participant_with_ext_id.csv', [ ['name' => 'event_id'], diff --git a/tests/phpunit/api/v3/EventTest.php b/tests/phpunit/api/v3/EventTest.php index b5a319f4a2..c8a14e5b38 100644 --- a/tests/phpunit/api/v3/EventTest.php +++ b/tests/phpunit/api/v3/EventTest.php @@ -92,11 +92,8 @@ class api_v3_EventTest extends CiviUnitTestCase { } public function tearDown(): void { - $tablesToTruncate = [ - 'civicrm_participant', - 'civicrm_event', - ]; - $this->quickCleanup($tablesToTruncate, TRUE); + $this->quickCleanUpFinancialEntities(); + $this->quickCleanup([], TRUE); parent::tearDown(); } diff --git a/tests/phpunit/api/v3/OrderTest.php b/tests/phpunit/api/v3/OrderTest.php index 9dca59820b..baed967afc 100644 --- a/tests/phpunit/api/v3/OrderTest.php +++ b/tests/phpunit/api/v3/OrderTest.php @@ -371,7 +371,7 @@ class api_v3_OrderTest extends CiviUnitTestCase { ], ], ]; - $order = $this->callAPISuccess('Order', 'create', $orderCreateParams, __FUNCTION__, __FILE__); + $order = $this->callAPISuccess('Order', 'create', $orderCreateParams); // Create expected dates immediately before order creation to minimise chance of day changing over. //$expectedStart = date('Y-m-d'); $expectedEnd = date('Y-m-d', strtotime('+ 1 year - 1 day')); @@ -497,8 +497,7 @@ class api_v3_OrderTest extends CiviUnitTestCase { * @throws \CRM_Core_Exception */ public function testAddOrderForParticipant(): void { - $event = $this->eventCreate(); - $eventId = $event['id']; + $this->eventCreatePaid(); $p = [ 'contact_id' => $this->_individualId, 'receive_date' => '2010-01-20', @@ -523,7 +522,7 @@ class api_v3_OrderTest extends CiviUnitTestCase { 'line_item' => $lineItems, 'params' => [ 'contact_id' => $this->_individualId, - 'event_id' => $eventId, + 'event_id' => $this->getEventID(), 'role_id' => 1, 'register_date' => '2007-07-21 00:00:00', 'source' => 'Online Event Registration: API Testing', @@ -551,11 +550,11 @@ class api_v3_OrderTest extends CiviUnitTestCase { // Enable the "Pending from approval" status which is not enabled by default $pendingFromApprovalParticipantStatus = civicrm_api3('ParticipantStatusType', 'getsingle', [ - 'name' => "Pending from approval", + 'name' => 'Pending from approval', ]); civicrm_api3('ParticipantStatusType', 'create', [ 'id' => $pendingFromApprovalParticipantStatus['id'], - 'name' => "Pending from approval", + 'name' => 'Pending from approval', 'is_active' => 1, ]); @@ -563,7 +562,7 @@ class api_v3_OrderTest extends CiviUnitTestCase { 'line_item' => $lineItems, 'params' => [ 'contact_id' => $this->individualCreate(), - 'event_id' => $eventId, + 'event_id' => $this->getEventID(), 'role_id' => 1, 'register_date' => '2007-07-21 00:00:00', 'source' => 'Online Event Registration: API Testing', diff --git a/tests/phpunit/api/v3/ParticipantPaymentTest.php b/tests/phpunit/api/v3/ParticipantPaymentTest.php index 4567f0593d..ff9ca5df07 100644 --- a/tests/phpunit/api/v3/ParticipantPaymentTest.php +++ b/tests/phpunit/api/v3/ParticipantPaymentTest.php @@ -53,40 +53,34 @@ class api_v3_ParticipantPaymentTest extends CiviUnitTestCase { */ protected $participantID4; - /** - * @var int - */ - protected $eventID; - /** * Set up for tests. */ public function setUp(): void { parent::setUp(); $this->useTransaction(TRUE); - $event = $this->eventCreate(); - $this->eventID = $event['id']; + $this->eventCreatePaid(); $this->contactID = $this->individualCreate(); $this->individualCreate(); $this->participantID = $this->participantCreate([ - 'contactID' => $this->contactID, - 'eventID' => $this->eventID, + 'contact_id' => $this->contactID, + 'event_id' => $this->getEventID(), ]); $contactID2 = $this->individualCreate(); $this->participantID2 = $this->participantCreate([ - 'contactID' => $contactID2, - 'eventID' => $this->eventID, + 'contact_id' => $contactID2, + 'event_id' => $this->getEventID(), ]); $this->participantID3 = $this->participantCreate([ - 'contactID' => $contactID2, - 'eventID' => $this->eventID, + 'contact_id' => $contactID2, + 'event_id' => $this->getEventID(), ]); $this->contactID3 = $this->individualCreate(); $this->participantID4 = $this->participantCreate([ - 'contactID' => $this->contactID3, - 'eventID' => $this->eventID, + 'contact_id' => $this->contactID3, + 'event_id' => $this->getEventID(), ]); } @@ -94,21 +88,17 @@ class api_v3_ParticipantPaymentTest extends CiviUnitTestCase { * Check with valid array. */ public function testPaymentCreate(): void { - //Create Contribution & get contribution ID - $contributionID = $this->contributionCreate(['contact_id' => $this->contactID]); - - //Create Participant Payment record With Values - $params = [ + $this->callAPIAndDocument('ParticipantPayment', 'create', [ 'participant_id' => $this->participantID, - 'contribution_id' => $contributionID, - ]; - - $this->callAPIAndDocument('participantPayment', 'create', $params, __FUNCTION__, __FILE__); + 'contribution_id' => $this->contributionCreate(['contact_id' => $this->individualCreate()]), + ], __FUNCTION__, __FILE__); } /** * Test getPaymentInfo() returns correct * information of the participant payment + * + * @throws \CRM_Core_Exception */ public function testPaymentInfoForEvent(): void { //Create Contribution & get contribution ID @@ -130,7 +120,7 @@ class api_v3_ParticipantPaymentTest extends CiviUnitTestCase { /** * Check financial records for offline Participants. */ - public function testPaymentOffline() { + public function testPaymentOffline(): void { // create contribution w/o fee $contributionID = $this->contributionCreate([ @@ -151,19 +141,15 @@ class api_v3_ParticipantPaymentTest extends CiviUnitTestCase { // Update Payment $participantPayment = $this->callAPISuccess('participantPayment', 'create', $params); $this->assertEquals($participantPayment['id'], $participantPaymentID); - $this->assertTrue(array_key_exists('id', $participantPayment)); + $this->assertArrayHasKey('id', $participantPayment); // check Financial records $this->_checkFinancialRecords($params, 'offline'); - $params = [ - 'id' => $participantPaymentID, - ]; - $deletePayment = $this->callAPISuccess('participantPayment', 'delete', $params); } /** * Check financial records for online Participant. */ - public function testPaymentOnline() { + public function testPaymentOnline(): void { $pageParams['processor_id'] = $this->processorCreate(); $contributionPage = $this->contributionPageCreate($pageParams); @@ -185,19 +171,15 @@ class api_v3_ParticipantPaymentTest extends CiviUnitTestCase { // Update Payment $participantPayment = $this->callAPISuccess('participantPayment', 'create', $params); $this->assertEquals($participantPayment['id'], $participantPaymentID); - $this->assertTrue(array_key_exists('id', $participantPayment)); + $this->assertArrayHasKey('id', $participantPayment); // check Financial records $this->_checkFinancialRecords($params, 'online'); - $params = [ - 'id' => $participantPaymentID, - ]; - $this->callAPISuccess('participantPayment', 'delete', $params); } /** * Check financial records for online Participant pay later scenario. */ - public function testPaymentPayLaterOnline() { + public function testPaymentPayLaterOnline(): void { $pageParams['processor_id'] = $this->processorCreate(); $pageParams['is_pay_later'] = 1; $contributionPage = $this->contributionPageCreate($pageParams); @@ -222,28 +204,20 @@ class api_v3_ParticipantPaymentTest extends CiviUnitTestCase { // check Financial Records $this->_checkFinancialRecords($params, 'payLater'); $this->assertEquals($participantPayment['id'], $participantPaymentID); - $this->assertTrue(array_key_exists('id', $participantPayment)); - $params = [ - 'id' => $participantPaymentID, - ]; - $this->callAPISuccess('participantPayment', 'delete', $params); + $this->assertArrayHasKey('id', $participantPayment); } /** - * Check with wrong id. + * Check with invalid id. */ - public function testPaymentDeleteWithWrongID() { - $params = [ - 'id' => 0, - ]; - $deletePayment = $this->callAPIFailure('participantPayment', 'delete', $params); - $this->assertEquals($deletePayment['error_message'], 'Error while deleting participantPayment'); + public function testPaymentDeleteWithWrongID(): void { + $this->callAPIFailure('ParticipantPayment', 'delete', ['id' => 0], 'Error while deleting participantPayment'); } /** * Check with valid array. */ - public function testPaymentDelete() { + public function testPaymentDelete(): void { $contributionID = $this->contributionCreate([ 'contact_id' => $this->contactID, ]); @@ -259,7 +233,7 @@ class api_v3_ParticipantPaymentTest extends CiviUnitTestCase { /** * Test civicrm_participantPayment_get - success expected. */ - public function testGet() { + public function testGet(): void { $contributionID = $this->contributionCreate(['contact_id' => $this->contactID3]); $this->participantPaymentCreate($this->participantID4, $contributionID); @@ -278,7 +252,7 @@ class api_v3_ParticipantPaymentTest extends CiviUnitTestCase { * @param array $params * @param $context */ - public function _checkFinancialRecords($params, $context) { + public function _checkFinancialRecords($params, $context): void { $entityParams = [ 'entity_id' => $params['id'], 'entity_table' => 'civicrm_contribution', @@ -330,7 +304,7 @@ class api_v3_ParticipantPaymentTest extends CiviUnitTestCase { 'financial_account_id' => 1, ]; } - elseif ($context == 'payLater') { + elseif ($context === 'payLater') { $compareParams = [ 'amount' => 100, 'status_id' => 3, @@ -343,7 +317,7 @@ class api_v3_ParticipantPaymentTest extends CiviUnitTestCase { /** * test getParticipantIds() function */ - public function testGetParticipantIds() { + public function testGetParticipantIDs(): void { $contributionID = $this->contributionCreate(['contact_id' => $this->contactID]); $expectedParticipants = [$this->participantID, $this->participantID2]; -- 2.25.1