More test fixes on cleanup, helper use
authorEileen McNaughton <emcnaughton@wikimedia.org>
Wed, 7 Jun 2023 02:39:04 +0000 (14:39 +1200)
committerEileen McNaughton <emcnaughton@wikimedia.org>
Wed, 7 Jun 2023 02:39:04 +0000 (14:39 +1200)
tests/phpunit/CRM/Event/Form/Task/BadgeTest.php
tests/phpunit/CRM/Event/Import/Parser/ParticipantTest.php
tests/phpunit/api/v3/EventTest.php
tests/phpunit/api/v3/OrderTest.php
tests/phpunit/api/v3/ParticipantPaymentTest.php

index 3db7e4c6b142a7dadbaceedb5613fb2bcacaf268..2bd41ccbec18445650ab8e76e938531a05ed5a69 100644 (file)
@@ -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();
   }
 
index 0eff6061af9ea7e5ba05cad5b699a1b130c2189c..5574312559e1642a01e6e522165e58cb95e4a2fa 100644 (file)
@@ -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'],
index b5a319f4a2902f32bc6287ad158ae80036e9c431..c8a14e5b3811c0b6204693fb7e804d08b9fc9bd9 100644 (file)
@@ -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();
   }
 
index 9dca59820ba35f050a460b19cca83461db852e13..baed967afce2a004fd0f6aca3822a9ec620f7692 100644 (file)
@@ -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',
index 4567f0593da4e5464f3130d9410a548092a2ca3b..ff9ca5df07877c675f31d23e167fa36dda1d1c93 100644 (file)
@@ -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];