Cleanup signature for test function getFormObject
authorEileen McNaughton <emcnaughton@wikimedia.org>
Thu, 31 Aug 2023 07:41:01 +0000 (19:41 +1200)
committerEileen McNaughton <emcnaughton@wikimedia.org>
Thu, 31 Aug 2023 11:27:05 +0000 (23:27 +1200)
I want to make form test functions available outside core (to wean some extensions off calling methods on the
forms that we no longer call from core. They were copy & paste from when we did). Also, I want to have some nicer form
functions.

I think getFormObject is OK-ish but I don't like the parameters that are there to support
search tests, so I am trying to split that out to allow signature change on the main function

tests/phpunit/CRM/Activity/BAO/ActivityTest.php
tests/phpunit/CRM/Contact/Form/Task/PrintDocumentTest.php
tests/phpunit/CRM/Contribute/Form/ContributionPageTranslationTest.php
tests/phpunit/CRM/Contribute/Form/Task/EmailTest.php
tests/phpunit/CRM/Contribute/Form/Task/PDFTest.php
tests/phpunit/CRM/Contribute/Form/TaskTest.php
tests/phpunit/CRM/Event/Form/Task/BadgeTest.php
tests/phpunit/CRM/Mailing/Form/Task/AdhocMailingTest.php
tests/phpunit/CRM/Member/Form/Task/LabelTest.php
tests/phpunit/CRM/Member/Form/Task/PDFLetterTest.php
tests/phpunit/CiviTest/CiviUnitTestCase.php

index f263647872c7402b0b78841e1a8882e694da0b22..7a5f3d7c940d3c34a6aee6415dd0855fb58ca582 100644 (file)
@@ -1636,7 +1636,7 @@ $text
       'campaign_id' => $campaign_id,
       'from_email_address' => 'from@example.com',
       'to' => $contactId1 . '::email@example.com,' . $contactId2 . '::email2@example.com',
-    ], '', []);
+    ]);
     $form->set('cid', $contactId1 . ',' . $contactId2);
     $form->buildForm();
     $form->postProcess();
@@ -1702,7 +1702,7 @@ $textValue
         'name' => $fileUri,
       ],
       'attachDesc_1' => '',
-    ], NULL, []);
+    ]);
     $form->set('cid', $contactId1 . ',' . $contactId2 . ',' . $contactId3);
     $form->buildForm();
     $form->postProcess();
@@ -1752,7 +1752,6 @@ $textValue
     $subject = __FUNCTION__ . ' subject';
     $html = __FUNCTION__ . ' html';
     $text = __FUNCTION__ . ' text';
-    $userID = $loggedInUser;
 
     $filepath = Civi::paths()->getPath('[civicrm.files]/custom');
     $fileName = 'test_email_create.txt';
@@ -1775,7 +1774,7 @@ $textValue
         'name' => $fileUri,
       ],
       'attachDesc_1' => '',
-    ], NULL, []);
+    ]);
     $form->set('cid', $contactId1 . ',' . $contactId2);
     $form->buildForm();
     $form->postProcess();
index 79088675334f3e389c800977789687ba8cac9625..9358186edc2e1cc86df62978d7caa6985892ef66 100644 (file)
@@ -47,19 +47,21 @@ class CRM_Contact_Form_Task_PrintDocumentTest extends CiviUnitTestCase {
    *  Assert the content of document
    *
    * @param array $formValues
-   * @param array $type
+   * @param string $type
+   *
+   * @throws \CRM_Core_Exception
    */
-  public function _testDocumentContent($formValues, $type) {
+  public function _testDocumentContent(array $formValues, $type): void {
     $html = [];
     /** @var CRM_Contact_Form_Task_PDF $form */
-    $form = $this->getFormObject('CRM_Contact_Form_Task_PDF', [], NULL, [
+    $form = $this->getSearchFormObject('CRM_Contact_Form_Task_PDF', [], NULL, [
       'radio_ts' => 'ts_sel',
       'task' => CRM_Member_Task::PDF_LETTER,
     ]);
-    list($formValues) = $form->processMessageTemplate($formValues);
-    list($html_message, $zip) = CRM_Utils_PDF_Document::unzipDoc($formValues['document_file_path'], $formValues['document_type']);
+    [$formValues] = $form->processMessageTemplate($formValues);
+    [$html_message, $zip] = CRM_Utils_PDF_Document::unzipDoc($formValues['document_file_path'], $formValues['document_type']);
 
-    foreach ($this->_contactIds as $item => $contactId) {
+    foreach ($this->_contactIds as $contactId) {
       $html[] = CRM_Core_BAO_MessageTemplate::renderTemplate(['messageTemplate' => ['msg_html' => $html_message], 'contactId' => $contactId, 'disableSmarty' => TRUE])['html'];
     }
 
index 23598a53a82982cfe138261cc41aa11f27e5d100..49cc5190e53e98c4c5b08ea1a4446290efdc835b 100644 (file)
@@ -57,14 +57,14 @@ class CRM_Contribute_Form_ContributionPageTranslationTest extends CiviUnitTestCa
       'is_credit_card_only' => '',
     ];
 
-    $contributionpage = CRM_Contribute_BAO_ContributionPage::create($params);
+    $contributionPage = CRM_Contribute_BAO_ContributionPage::create($params);
 
     // The BAO does not save these
-    $params['id'] = $contributionpage->id;
+    $params['id'] = $contributionPage->id;
     $params['honor_block_title'] = 'In Honor Title EN';
     $params['honor_block_text'] = 'In Honor Text EN';
 
-    $form = $this->getFormObject('CRM_Contribute_Form_ContributionPage_Settings', $params, 'Settings');
+    $form = $this->getFormObject('CRM_Contribute_Form_ContributionPage_Settings', $params);
     $form->postProcess();
 
     // Now update the page with In Honor (soft credit) text in French
@@ -73,11 +73,11 @@ class CRM_Contribute_Form_ContributionPageTranslationTest extends CiviUnitTestCa
     $params['honor_block_title'] = 'In Honor Title FR';
     $params['honor_block_text'] = 'In Honor Text FR';
 
-    $form = $this->getFormObject('CRM_Contribute_Form_ContributionPage_Settings', $params, 'Settings');
+    $form = $this->getFormObject('CRM_Contribute_Form_ContributionPage_Settings', $params);
     $form->postProcess();
 
     $uf = $this->callAPISuccess('UFJoin', 'getsingle', [
-      'entity_id' => $contributionpage->id,
+      'entity_id' => $contributionPage->id,
       'module' => 'soft_credit',
     ]);
 
@@ -88,7 +88,7 @@ class CRM_Contribute_Form_ContributionPageTranslationTest extends CiviUnitTestCa
     $this->assertEquals('In Honor Title FR', $json['soft_credit']['fr_FR']['honor_block_title']);
     $this->assertEquals('In Honor Text FR', $json['soft_credit']['fr_FR']['honor_block_text']);
 
-    $this->callAPISuccess('ContributionPage', 'delete', ['id' => $contributionpage->id]);
+    $this->callAPISuccess('ContributionPage', 'delete', ['id' => $contributionPage->id]);
   }
 
 }
index 0271e3eaa793ec117341cf3491b10aa4c6e01d8b..a1eb84633ebd0b41cf9fbeb5ecd0703ecc67ae78 100644 (file)
@@ -47,7 +47,7 @@ class CRM_Contribute_Form_Task_EmailTest extends CiviUnitTestCase {
     $contribution1 = $this->contributionCreate(['contact_id' => $contact2, 'invoice_number' => 'soy']);
     $contribution2 = $this->contributionCreate(['total_amount' => 999, 'contact_id' => $contact1, 'invoice_number' => 'saucy']);
     $contribution3 = $this->contributionCreate(['total_amount' => 999, 'contact_id' => $contact1, 'invoice_number' => 'ranch']);
-    $form = $this->getFormObject('CRM_Contribute_Form_Task_Email', [
+    $form = $this->getSearchFormObject('CRM_Contribute_Form_Task_Email', [
       'cc_id' => '',
       'bcc_id' => '',
       'to' => implode(',', [
index 4d96bb997566a38ae1d140db583231a1b5ab4aab..3188545d05ffc03725fc6d841661d249c2f22863 100644 (file)
@@ -51,7 +51,7 @@ class CRM_Contribute_Form_Task_PDFTest extends CiviUnitTestCase {
       }
     }
 
-    $form = $this->getFormObject('CRM_Contribute_Form_Task_PDF', [
+    $form = $this->getSearchFormObject('CRM_Contribute_Form_Task_PDF', [
       'receipt_update' => 1,
     ], NULL, $searchValues);
     $form->buildForm();
index 59e3f0efc7024931d22d88dd266d422123dbcd5c..45dc0f933939eefc8a80f3e47f2be6c1e8c6661b 100644 (file)
@@ -71,7 +71,7 @@ class CRM_Contribute_Form_TaskTest extends CiviUnitTestCase {
     foreach ($fields as $val) {
       // Assert contribIds are returned in a sorted order.
       /** @var CRM_Contribute_Form_Task $form */
-      $form = $this->getFormObject('CRM_Contribute_Form_Task', ['radio_ts' => 'ts_all'], 'Search');
+      $form = $this->getSearchFormObject('CRM_Contribute_Form_Task', ['radio_ts' => 'ts_all'], 'Search');
       $form->set(CRM_Utils_Sort::SORT_ORDER, "`{$val}` asc");
       CRM_Contribute_Form_Task::preProcessCommon($form);
 
index 2bd41ccbec18445650ab8e76e938531a05ed5a69..9ef1bc748aca9a4a76ee8d760051780ac421cfa5 100644 (file)
@@ -49,7 +49,7 @@ class CRM_Event_Form_Task_BadgeTest extends CiviUnitTestCase {
     $_REQUEST['id'] = $participantID;
     $_REQUEST['cid'] = $contactID;
     /** @var CRM_Event_Form_Task_Badge $form */
-    $form = $this->getFormObject(
+    $form = $this->getSearchFormObject(
       'CRM_Event_Form_Task_Badge',
       ['badge_id' => 1],
       NULL,
index 267bb380c08bd195647a11af7e9d65d20e73068a..c411f57369e6cfa8dcb8fe8cc1d00d702a494450 100644 (file)
@@ -20,7 +20,6 @@ class CRM_Mailing_Form_Task_AdhocMailingTest extends CiviUnitTestCase {
    * A hidden smart group is a group used for sending emails.
    *
    * @throws \CRM_Core_Exception
-   * @throws \Exception
    */
   public function testCreateHiddenGroupFromSearchBuilder(): void {
     $this->createLoggedInUser();
@@ -33,7 +32,7 @@ class CRM_Mailing_Form_Task_AdhocMailingTest extends CiviUnitTestCase {
       '_qf_Builder_refresh' => 'Search',
       'radio_ts' => '',
     ];
-    $form = $this->getFormObject('CRM_Mailing_Form_Task_AdhocMailing', $formValues, 'Builder');
+    $form = $this->getSearchFormObject('CRM_Mailing_Form_Task_AdhocMailing', $formValues, 'Builder');
     $form->setAction(CRM_Core_Action::PROFILE);
     $form->set('formValues', $formValues);
     $form->set('isSearchBuilder', 1);
index 3ef8c10957fc506c002764d4c31343804c65c441..92fc629f0ec36a5d5c2c2328f4980182923e6f7a 100644 (file)
@@ -30,7 +30,7 @@ class CRM_Member_Form_Task_LabelTest extends CiviUnitTestCase {
     $this->assertArrayHasKey(201, $tasks);
     $this->contactMembershipCreate(['contact_id' => $this->individualCreate()]);
     /** @var CRM_Member_Form_Task_Label $form */
-    $form = $this->getFormObject('CRM_Member_Form_Task_Label', [
+    $form = $this->getSearchFormObject('CRM_Member_Form_Task_Label', [
       'task' => 201,
       'radio_ts' => 'ts_sel',
     ], 'Search');
index 6fba0c8bd4940b91f9a342608486b111b4b68147..d67e39d640c0e9cd2b7338374237bed77362b000 100644 (file)
@@ -79,7 +79,7 @@ class CRM_Member_Form_Task_PDFLetterTest extends CiviUnitTestCase {
     }
 
     /** @var CRM_Member_Form_Task_PDFLetter $form */
-    $form = $this->getFormObject('CRM_Member_Form_Task_PDFLetter', [
+    $form = $this->getSearchFormObject('CRM_Member_Form_Task_PDFLetter', [
       'subject' => '{contact.first_name} {membership.source}',
       'html_message' => $htmlMessage,
     ], NULL, $searchFormValues);
index 2186a2b2c05de40d3651f89af96a5dca34357359..4f6ed252226ca6bb52cf8626e22eb702955b2f0b 100644 (file)
@@ -2923,28 +2923,18 @@ class CiviUnitTestCase extends PHPUnit\Framework\TestCase {
    *
    * @param array $formValues
    *
-   * @param string|null $pageName
-   *
-   * @param array $searchFormValues
-   *   Values for the search form if the form is a task eg.
-   *   for selected ids 6 & 8:
-   *   [
-   *      'radio_ts' => 'ts_sel',
-   *      'task' => CRM_Member_Task::PDF_LETTER,
-   *      'mark_x_6' => 1,
-   *      'mark_x_8' => 1,
-   *   ]
-   * @param \HTML_QuickForm_Controller|null $controller
+   * @param array $urlParameters
    *
    * @return \CRM_Core_Form|CRM_Event_Form_Registration_Register
    *
    * @noinspection PhpReturnDocTypeMismatchInspection
    */
-  public function getFormObject(string $class, array $formValues = [], ?string $pageName = '', array $searchFormValues = [], $controller = NULL) {
+  public function getFormObject(string $class, array $formValues = [], array $urlParameters = []) {
     $_POST = $formValues;
     /** @var CRM_Core_Form $form */
     $form = new $class();
     $_SERVER['REQUEST_METHOD'] = 'GET';
+    $_REQUEST += $urlParameters;
     switch ($class) {
       case 'CRM_Event_Cart_Form_Checkout_Payment':
       case 'CRM_Event_Cart_Form_Checkout_ParticipantsAndPrices':
@@ -3061,26 +3051,63 @@ class CiviUnitTestCase extends PHPUnit\Framework\TestCase {
         break;
 
       case strpos($class, '_Form_') !== FALSE:
-        $form->controller = new CRM_Core_Controller_Simple($class, $pageName);
+        $form->controller = new CRM_Core_Controller_Simple($class, $form->getName());
         break;
 
       default:
         $form->controller = new CRM_Core_Controller();
     }
-    if (!$pageName) {
-      $pageName = $form->getName();
-    }
+
     $form->controller->setStateMachine(new CRM_Core_StateMachine($form->controller));
-    $_SESSION['_' . $form->controller->_name . '_container']['values'][$pageName] = $formValues;
-    if ($searchFormValues) {
-      $_SESSION['_' . $form->controller->_name . '_container']['values']['Search'] = $searchFormValues;
-    }
+    $_SESSION['_' . $form->controller->_name . '_container']['values'][$form->getName()] = $formValues;
     if (isset($formValues['_qf_button_name'])) {
       $_SESSION['_' . $form->controller->_name . '_container']['_qf_button_name'] = $formValues['_qf_button_name'];
     }
     return $form;
   }
 
+  /**
+   * Instantiate form object.
+   *
+   * We need to instantiate the form to run preprocess, which means we have to trick it about the request method.
+   *
+   * @param string $class
+   *   Name of form class.
+   *
+   * @param array $formValues
+   * @param string|null $pageName
+   * @param array $searchFormValues
+   *   Values for the search form if the form is a task eg.
+   *   for selected ids 6 & 8:
+   *   [
+   *      'radio_ts' => 'ts_sel',
+   *      'task' => CRM_Member_Task::PDF_LETTER,
+   *      'mark_x_6' => 1,
+   *      'mark_x_8' => 1,
+   *   ]
+   *
+   * @return \CRM_Core_Form
+   *
+   * @noinspection PhpReturnDocTypeMismatchInspection
+   */
+  public function getSearchFormObject(string $class, array $formValues = [], ?string $pageName = 'Search', array $searchFormValues = []) {
+    $_POST = $formValues;
+    /** @var CRM_Core_Form $form */
+    $form = new $class();
+    $pageName = $pageName ?: $form->getName();
+    if (strpos($class, 'Search') !== FALSE) {
+      $form->controller = new CRM_Contact_Controller_Search();
+    }
+    else {
+      $form->controller = new CRM_Core_Controller_Simple($class, $pageName);
+    }
+    $_SERVER['REQUEST_METHOD'] = 'GET';
+    $_SESSION['_' . $form->controller->_name . '_container']['values']['Search'] = $searchFormValues;
+    $form->controller->setStateMachine(new CRM_Core_StateMachine($form->controller));
+    $_SESSION['_' . $form->controller->_name . '_container']['values'][$pageName] = $formValues;
+    return $form;
+  }
+
   /**
    * Get possible thousand separators.
    *