CIVICRM-990: Unit test to make sure regular expression in Participant search works.
authorAlok Patel <alok@agileware.com.au>
Mon, 1 Apr 2019 12:20:18 +0000 (17:50 +0530)
committerAlok Patel <alok@agileware.com.au>
Mon, 1 Apr 2019 12:20:18 +0000 (17:50 +0530)
CRM/Event/Form/Search.php
tests/phpunit/CRM/Event/Form/SearchTest.php [new file with mode: 0644]

index a1421cbe3a9fd0e4fdd9988efbd0b98b2134e08e..78232e7ee4bbb407ef04bf0bd37b917d554e4743 100644 (file)
@@ -295,36 +295,19 @@ class CRM_Event_Form_Search extends CRM_Core_Form_Search {
   }
 
   /**
-   * The post processing of the form gets done here.
-   *
-   * Key things done during post processing are
-   *      - check for reset or next request. if present, skip post procesing.
-   *      - now check if user requested running a saved search, if so, then
-   *        the form values associated with the saved search are used for searching.
-   *      - if user has done a submit with new values the regular post submissing is
-   *        done.
-   * The processing consists of using a Selector / Controller framework for getting the
-   * search results.
-   *
-   * @param
-   *
-   * @return void
+   * Test submit the form.
+   * @param $formValues
    */
-  public function postProcess() {
-    if ($this->_done) {
-      return;
-    }
-
-    $this->_done = TRUE;
-
-    if (!empty($_POST)) {
-      $this->_formValues = $this->controller->exportValues($this->_name);
-      CRM_Contact_BAO_Query::processSpecialFormValue($this->_formValues, array('participant_status_id'));
-    }
+  public function testSubmit($formValues) {
+    $this->submit($formValues);
+  }
 
-    if (empty($this->_formValues)) {
-      $this->_formValues = $this->controller->exportValues($this->_name);
-    }
+  /**
+   * Submit the search form with given values.
+   * @param $formValues
+   */
+  private function submit($formValues) {
+    $this->_formValues = $formValues;
 
     $this->fixFormValues();
 
@@ -400,6 +383,42 @@ class CRM_Event_Form_Search extends CRM_Core_Form_Search {
     $controller->run();
   }
 
+  /**
+   * The post processing of the form gets done here.
+   *
+   * Key things done during post processing are
+   *      - check for reset or next request. if present, skip post procesing.
+   *      - now check if user requested running a saved search, if so, then
+   *        the form values associated with the saved search are used for searching.
+   *      - if user has done a submit with new values the regular post submissing is
+   *        done.
+   * The processing consists of using a Selector / Controller framework for getting the
+   * search results.
+   *
+   * @param
+   *
+   * @return void
+   */
+  public function postProcess() {
+    if ($this->_done) {
+      return;
+    }
+
+    $this->_done = TRUE;
+    $formValues = array();
+
+    if (!empty($_POST)) {
+      $formValues = $this->controller->exportValues($this->_name);
+      CRM_Contact_BAO_Query::processSpecialFormValue($this->_formValues, array('participant_status_id'));
+    }
+
+    if (empty($this->_formValues)) {
+      $formValues = $this->controller->exportValues($this->_name);
+    }
+
+    $this->submit($formValues);
+  }
+
   /**
    * add the rules (mainly global rules) for form.
    * All local rules are added near the element
diff --git a/tests/phpunit/CRM/Event/Form/SearchTest.php b/tests/phpunit/CRM/Event/Form/SearchTest.php
new file mode 100644 (file)
index 0000000..d6b22fd
--- /dev/null
@@ -0,0 +1,57 @@
+<?php
+
+/**
+ * @group headless
+ */
+class CRM_Event_Form_SearchTest extends CiviUnitTestCase {
+
+  public function setUp() {
+    parent::setUp();
+    $this->individualID = $this->individualCreate();
+  }
+
+  /**
+   *  Test that search form returns correct number of rows for complex regex filters.
+   */
+  public function testSearch() {
+    $priceFieldValues = $this->createPriceSet('event', NULL, array(
+      'html_type'    => 'Radio',
+      'option_label' => array('1' => 'Radio Label A (inc. GST)', '2' => 'Radio Label B (inc. GST)'),
+      'option_name'  => array('1' => 'Radio Label A', '2' => 'Radio Label B'),
+    ));
+
+    $priceFieldValues = $priceFieldValues['values'];
+    $participantPrice = NULL;
+    foreach ($priceFieldValues as $priceFieldValue) {
+      $participantPrice = $priceFieldValue;
+      break;
+    }
+
+    $event = $this->eventCreate();
+    $individualID = $this->individualCreate();
+    $today = new DateTime();
+    $this->participantCreate(array(
+      'event_id'      => $event['id'],
+      'contact_id'    => $individualID,
+      'status_id'     => 1,
+      'fee_level'     => $participantPrice['label'],
+      'fee_amount'    => $participantPrice['amount'],
+      'fee_currency'  => 'USD',
+      'register_date' => $today->format('YmdHis'),
+    ));
+
+    $form = new CRM_Event_Form_Search();
+    $form->controller = new CRM_Event_Controller_Search();
+    $form->preProcess();
+    $form->testSubmit(array(
+      'participant_test' => 0,
+      'participant_fee_id' => array(
+        $participantPrice['id'],
+      ),
+      'radio_ts'         => 'ts_all',
+    ));
+    $rows = $form->controller->get('rows');
+    $this->assertEquals(1, count($rows), 'Exactly one row should be returned for given price field value.');
+  }
+
+}