Fix failing test
authorEileen McNaughton <emcnaughton@wikimedia.org>
Tue, 7 Mar 2023 22:59:17 +0000 (11:59 +1300)
committerEileen McNaughton <emcnaughton@wikimedia.org>
Tue, 7 Mar 2023 22:59:17 +0000 (11:59 +1300)
Test was - testing a non-form class 'as if' it were a form
and pretending to check for an exception it was not asserting / hitting

CRM/Contribute/Form/ContributionBase.php
tests/phpunit/CRM/Contribute/Form/ContributionTest.php
tests/phpunit/CiviTest/CiviUnitTestCase.php

index 5d9c97dfdd09a361a930f716ddfef0b20ac64b4b..cf17bdf4414d4fb82f7be85ccd8951278c40b2ce 100644 (file)
@@ -339,8 +339,12 @@ class CRM_Contribute_Form_ContributionBase extends CRM_Core_Form {
     $this->_bltID = $this->get('bltID');
     $this->_paymentProcessor = $this->get('paymentProcessor');
 
-    $this->order = new CRM_Financial_BAO_Order();
-    $this->order->setPriceSetID($this->getPriceSetID());
+    // In tests price set id is not always set - it is unclear if this is just
+    // poor test set up or it is possible in 'the real world'
+    if ($this->getPriceSetID()) {
+      $this->order = new CRM_Financial_BAO_Order();
+      $this->order->setPriceSetID($this->getPriceSetID());
+    }
     $this->_priceSet = $this->get('priceSet');
 
     if (!$this->_values) {
index 84f322d1c2793a413987c0bde0fcbfe91dc6cbc6..8d503f751e98a8daf1b8a0f54515c6747d7aecd6 100644 (file)
@@ -1367,12 +1367,9 @@ Paid By: Check',
 
   /**
    * Check payment processor is correctly assigned for a contribution page.
-   *
-   * @throws \CRM_Core_Exception
-   * @throws \CRM_Contribute_Exception_InactiveContributionPageException
    */
   public function testContributionBasePreProcess(): void {
-    //Create contribution page with only pay later enabled.
+    // Create contribution page with only pay later enabled.
     $params = [
       'title' => 'Test Contribution Page',
       'financial_type_id' => 1,
@@ -1387,14 +1384,11 @@ Paid By: Check',
       'receipt_from_name' => 'Ego Freud',
     ];
 
-    $page1 = $this->callAPISuccess('ContributionPage', 'create', $params);
-
-    //Execute CRM_Contribute_Form_ContributionBase preProcess
-    //and check the assignment of payment processors
-    $form = new CRM_Contribute_Form_ContributionBase();
-    $form->controller = new CRM_Core_Controller();
-    $form->set('id', $page1['id']);
-    $_REQUEST['id'] = $page1['id'];
+    // Execute CRM_Contribute_Form_ContributionBase preProcess (via child class).
+    // Check the assignment of payment processors.
+    /* @var \CRM_Contribute_Form_Contribution_Main $form */
+    $form = $this->getFormObject('CRM_Contribute_Form_Contribution_Main', ['payment_processor_id' => 0]);
+    $_REQUEST['id'] = $this->callAPISuccess('ContributionPage', 'create', $params)['id'];
 
     $form->preProcess();
     $this->assertEquals('pay_later', $form->_paymentProcessor['name']);
@@ -1403,12 +1397,12 @@ Paid By: Check',
     $params['is_pay_later'] = 0;
     $page2 = $this->callAPISuccess('ContributionPage', 'create', $params);
 
-    //Assert an exception is thrown on loading the contribution page.
-    $form = new CRM_Contribute_Form_ContributionBase();
-    $form->controller = new CRM_Core_Controller();
-    $_REQUEST['id'] = $page2['id'];
-    $form->set('id', $page2['id']);
-    $form->preProcess();
+    // @todo - these lines were supposed to assert an exception is thrown on loading the contribution page.
+    // However the test has been quietly passing with that not happening.
+    /* @var \CRM_Contribute_Form_Contribution_Main $form */
+    // $form = $this->getFormObject('CRM_Contribute_Form_Contribution_Main', ['payment_processor_id' => 0]);
+    // $_REQUEST['id'] = $page2['id'];
+    // $form->preProcess();
   }
 
   /**
index a6986d28f24b4526eec4d9dcde0b53e50b1929c1..67c8f7195f66e7400e8329a538b6056f0cc8bb77 100644 (file)
@@ -3162,6 +3162,10 @@ class CiviUnitTestCase extends PHPUnit\Framework\TestCase {
         }
         break;
 
+      case 'CRM_Contribute_Form_Contribution_Main':
+        $form->controller = new CRM_Contribute_Controller_Contribution();
+        break;
+
       case 'CRM_Contribute_Form_Contribution_Confirm':
         $form->controller = new CRM_Contribute_Controller_Contribution();
         $form->controller->setStateMachine(new CRM_Contribute_StateMachine_Contribution($form->controller));