From: Eileen McNaughton Date: Tue, 7 Mar 2023 22:59:17 +0000 (+1300) Subject: Fix failing test X-Git-Url: https://vcs.fsf.org/?a=commitdiff_plain;h=dedb4c336c5fdd085fea2112ba1f6b9f5f95dcc5;p=civicrm-core.git Fix failing test 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 --- diff --git a/CRM/Contribute/Form/ContributionBase.php b/CRM/Contribute/Form/ContributionBase.php index 5d9c97dfdd..cf17bdf441 100644 --- a/CRM/Contribute/Form/ContributionBase.php +++ b/CRM/Contribute/Form/ContributionBase.php @@ -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) { diff --git a/tests/phpunit/CRM/Contribute/Form/ContributionTest.php b/tests/phpunit/CRM/Contribute/Form/ContributionTest.php index 84f322d1c2..8d503f751e 100644 --- a/tests/phpunit/CRM/Contribute/Form/ContributionTest.php +++ b/tests/phpunit/CRM/Contribute/Form/ContributionTest.php @@ -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(); } /** diff --git a/tests/phpunit/CiviTest/CiviUnitTestCase.php b/tests/phpunit/CiviTest/CiviUnitTestCase.php index a6986d28f2..67c8f7195f 100644 --- a/tests/phpunit/CiviTest/CiviUnitTestCase.php +++ b/tests/phpunit/CiviTest/CiviUnitTestCase.php @@ -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));