Fix tests to not notice when faking form flow
authorEileen McNaughton <emcnaughton@wikimedia.org>
Thu, 16 Nov 2023 22:59:01 +0000 (11:59 +1300)
committerEileen McNaughton <emcnaughton@wikimedia.org>
Fri, 17 Nov 2023 01:40:47 +0000 (14:40 +1300)
api/v3/ContributionPage.php
tests/phpunit/CRM/Contribute/Form/Contribution/ThankYouTest.php
tests/phpunit/CiviTest/CiviUnitTestCase.php

index d744043f712240ef6cdf4e1c23f4c95a2b9e0767..a6649605bef2b5238bff883d14088366fe8bfffb 100644 (file)
@@ -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) {
index aa9b3e373f4372c63c53a496102c20d96b83524e..785dfef640ff8bf71e3a54429e478c09dac038e2 100644 (file)
@@ -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;
   }
 
index 8e6e5f8e162945288af9c11c1a16c17cce9672ee..705e1786dd5f6015f1a75c914b118d4450a86014 100644 (file)
@@ -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.