From 2dad6350b4bcc9d062ee0500bdc307ae3789e295 Mon Sep 17 00:00:00 2001 From: Eileen McNaughton Date: Fri, 17 Nov 2023 11:59:01 +1300 Subject: [PATCH] Fix tests to not notice when faking form flow --- api/v3/ContributionPage.php | 19 +++++++++++++++---- .../Form/Contribution/ThankYouTest.php | 19 +++++++++++++------ tests/phpunit/CiviTest/CiviUnitTestCase.php | 1 + 3 files changed, 29 insertions(+), 10 deletions(-) diff --git a/api/v3/ContributionPage.php b/api/v3/ContributionPage.php index d744043f71..a6649605be 100644 --- a/api/v3/ContributionPage.php +++ b/api/v3/ContributionPage.php @@ -103,12 +103,23 @@ function civicrm_api3_contribution_page_validate($params) { // one being generated so we generate one first. $originalRequest = $_REQUEST; $qfKey = $_REQUEST['qfKey'] ?? NULL; + $_REQUEST['id'] = $params['id']; + $requestMethod = $_SERVER['REQUEST_METHOD'] ?? NULL; + // This is set to POST in a test - (probably cos we didn't have full form + // testing when it was written). It needs to be get for long enough to + // get past the constructor. + $_SERVER['REQUEST_METHOD'] = 'GET'; + $form = new CRM_Contribute_Form_Contribution_Main(); + $form->controller = new CRM_Contribute_Controller_Contribution(); + if ($requestMethod) { + $_SERVER['REQUEST_METHOD'] = $requestMethod; + } + $form->controller->setStateMachine(new CRM_Contribute_StateMachine_Contribution($form->controller)); + // The submitted values are on the Main form. + $_SESSION['_' . $form->controller->_name . '_container']['values']['Main'] = $params; if (!$qfKey) { - $_REQUEST['qfKey'] = CRM_Core_Key::get('CRM_Core_Controller', TRUE); + $_REQUEST['qfKey'] = CRM_Core_Key::get('CRM_Contribute_Controller_Contribution', TRUE); } - $form = new CRM_Contribute_Form_Contribution_Main(); - $form->controller = new CRM_Core_Controller(); - $form->set('id', $params['id']); $form->preProcess(); $errors = CRM_Contribute_Form_Contribution_Main::formRule($params, [], $form); if ($errors === TRUE) { diff --git a/tests/phpunit/CRM/Contribute/Form/Contribution/ThankYouTest.php b/tests/phpunit/CRM/Contribute/Form/Contribution/ThankYouTest.php index aa9b3e373f..785dfef640 100644 --- a/tests/phpunit/CRM/Contribute/Form/Contribution/ThankYouTest.php +++ b/tests/phpunit/CRM/Contribute/Form/Contribution/ThankYouTest.php @@ -9,6 +9,8 @@ +--------------------------------------------------------------------+ */ +use Civi\Test\ContributionPageTestTrait; + /** * Test CRM_Contribute_Form_Contribution_ThankYou * @@ -18,6 +20,8 @@ */ class CRM_Contribute_Form_Contribution_ThankYouTest extends CiviUnitTestCase { + use ContributionPageTestTrait; + /** * Clean up DB. */ @@ -60,13 +64,16 @@ class CRM_Contribute_Form_Contribution_ThankYouTest extends CiviUnitTestCase { /** * Get CRM_Contribute_Form_Contribution_ThankYou form with attached contribution. * - * @param $paymentProcessorID + * @param int $paymentProcessorID * @param bool $withPendingContribution * @param bool $isTestContribution * @return CRM_Contribute_Form_Contribution_ThankYou */ - private function getThankYouFormWithContribution($paymentProcessorID, $withPendingContribution = FALSE, $isTestContribution = FALSE) { + private function getThankYouFormWithContribution(int $paymentProcessorID, $withPendingContribution = FALSE, $isTestContribution = FALSE) { $pageContribution = $this->getPageContribution((($withPendingContribution) ? 2 : 1), $isTestContribution); + if (!isset($this->ids['ContributionPage'])) { + $this->contributionPageCreatePaid(['payment_processor' => $paymentProcessorID])['id']; + } $form = $this->getThankYouForm(); $form->_lineItem = []; $form->_bltID = 5; @@ -76,8 +83,9 @@ class CRM_Contribute_Form_Contribution_ThankYouTest extends CiviUnitTestCase { $form->_params['email-5'] = 'demo@example.com'; $form->_params['payment_processor_id'] = $paymentProcessorID; if ($isTestContribution) { - $form->_mode = 'test'; + $_REQUEST['action'] = 1024; } + $form->_values = [ 'custom_pre_id' => NULL, 'custom_post_id' => NULL, @@ -116,9 +124,8 @@ class CRM_Contribute_Form_Contribution_ThankYouTest extends CiviUnitTestCase { * @return CRM_Contribute_Form_Contribution_ThankYou */ private function getThankYouForm() { - $form = new CRM_Contribute_Form_Contribution_ThankYou(); - $_SERVER['REQUEST_METHOD'] = 'GET'; - $form->controller = new CRM_Contribute_Controller_Contribution(); + $form = $this->getFormObject('CRM_Contribute_Form_Contribution_ThankYou', [], ['id' => $this->getContributionPageID()]); + $form->preProcess(); return $form; } diff --git a/tests/phpunit/CiviTest/CiviUnitTestCase.php b/tests/phpunit/CiviTest/CiviUnitTestCase.php index 8e6e5f8e16..705e1786dd 100644 --- a/tests/phpunit/CiviTest/CiviUnitTestCase.php +++ b/tests/phpunit/CiviTest/CiviUnitTestCase.php @@ -2983,6 +2983,7 @@ class CiviUnitTestCase extends PHPUnit\Framework\TestCase { break; case 'CRM_Contribute_Form_Contribution_Confirm': + case 'CRM_Contribute_Form_Contribution_ThankYou': $form->controller = new CRM_Contribute_Controller_Contribution(); $form->controller->setStateMachine(new CRM_Contribute_StateMachine_Contribution($form->controller)); // The submitted values are on the Main form. -- 2.25.1