Only add in relative key when its a custom date field
authorSeamus Lee <seamuslee001@gmail.com>
Thu, 28 Nov 2019 03:22:11 +0000 (14:22 +1100)
committerSeamus Lee <seamuslee001@gmail.com>
Thu, 28 Nov 2019 20:14:04 +0000 (07:14 +1100)
Ensure that we strip the _to _from _high _low from the element name first

CRM/Contact/BAO/SavedSearch.php
tests/phpunit/CRM/Contact/BAO/SavedSearchTest.php

index b936e5022ae4f721c7a5ec8f20a4b78ace962322..f5236dcab784d662af5511bf3a1866524ec30dc9 100644 (file)
@@ -169,14 +169,11 @@ class CRM_Contact_BAO_SavedSearch extends CRM_Contact_DAO_SavedSearch {
           }
         }
       }
-      if (substr($element, 0, 7) == 'custom_' &&
-        (substr($element, -5, 5) == '_from' || substr($element, -3, 3) == '_to')
-      ) {
-        // Ensure the _relative field is set if from or to are set to ensure custom date
-        // fields with 'from' or 'to' values are displayed when the are set in the smart group
-        // being loaded. (CRM-17116)
-        if (!isset($result[CRM_Contact_BAO_Query::getCustomFieldName($element) . '_relative'])) {
-          $result[CRM_Contact_BAO_Query::getCustomFieldName($element) . '_relative'] = 0;
+      // We should only set the relative key for custom date fields if it is not already set in the array.
+      $realField = str_replace(['_relative', '_low', '_high', '_to', '_high'], '', $element);
+      if (substr($element, 0, 7) == 'custom_' && CRM_Contact_BAO_Query::isCustomDateField($realField)) {
+        if (!isset($result[$realField . '_relative'])) {
+          $result[$realField . '_relative'] = 0;
         }
       }
       // check to see if we need to convert the old privacy array
index f6df95585794c37271bffa368b2e4a00416b3cc9..28ea51967f00d3eca845d0a4f7dbd87bbd0b6421 100644 (file)
@@ -33,6 +33,8 @@
  */
 class CRM_Contact_BAO_SavedSearchTest extends CiviUnitTestCase {
 
+  use CRMTraits_Custom_CustomDataTrait;
+
   /**
    * Sets up the fixture, for example, opens a network connection.
    *
@@ -48,11 +50,20 @@ class CRM_Contact_BAO_SavedSearchTest extends CiviUnitTestCase {
    * This method is called after a test is executed.
    */
   protected function tearDown() {
+    if (!empty($this->ids['CustomField'])) {
+      foreach ($this->ids['CustomField'] as $type => $id) {
+        $field = civicrm_api3('CustomField', 'getsingle', ['id' => $id]);
+        $group = civicrm_api3('CustomGroup', 'getsingle', ['id' => $field['custom_group_id']]);
+        CRM_Core_DAO::executeQuery("DROP TABLE IF Exists {$group['table_name']}");
+      }
+    }
     $this->quickCleanup([
       'civicrm_mapping_field',
       'civicrm_mapping',
       'civicrm_group',
       'civicrm_saved_search',
+      'civicrm_custom_field',
+      'civicrm_custom_group',
     ]);
   }
 
@@ -62,6 +73,7 @@ class CRM_Contact_BAO_SavedSearchTest extends CiviUnitTestCase {
    * @throws \Exception
    */
   public function testDefaultValues() {
+    $this->createCustomGroupWithFieldOfType([], 'int');
     $sg = new CRM_Contact_Form_Search_Advanced();
     $sg->controller = new CRM_Core_Controller();
     $formValues = [
@@ -71,6 +83,8 @@ class CRM_Contact_BAO_SavedSearchTest extends CiviUnitTestCase {
       'privacy_toggle' => 2,
       'operator' => 'AND',
       'component_mode' => 1,
+      'custom_' . $this->ids['CustomField']['int'] . '_from' => 0,
+      'custom_' . $this->ids['CustomField']['int'] . '_to' => '',
     ];
     CRM_Core_DAO::executeQuery(
       "INSERT INTO civicrm_saved_search (form_values) VALUES('" . serialize($formValues) . "')"
@@ -84,6 +98,35 @@ class CRM_Contact_BAO_SavedSearchTest extends CiviUnitTestCase {
     $this->checkArrayEquals($defaults, $formValues);
   }
 
+  /**
+   * Test setDefaults for privacy radio buttons.
+   *
+   * @throws \Exception
+   */
+  public function testGetFormValuesWithCustomFields() {
+    $this->createCustomGroupWithFieldsOfAllTypes();
+    $sg = new CRM_Contact_Form_Search_Advanced();
+    $sg->controller = new CRM_Core_Controller();
+    $formValues = [
+      'group_search_selected' => 'group',
+      'privacy_options' => ['do_not_email'],
+      'privacy_operator' => 'OR',
+      'privacy_toggle' => 2,
+      'operator' => 'AND',
+      'component_mode' => 1,
+      'custom_' . $this->ids['CustomField']['int'] . '_from' => 0,
+      'custom_' . $this->ids['CustomField']['int'] . '_to' => '',
+      'custom_' . $this->ids['CustomField']['select_date'] . '_high' => '2019-06-30',
+      'custom_' . $this->ids['CustomField']['select_date'] . '_low' => '2019-06-30',
+    ];
+    CRM_Core_DAO::executeQuery(
+      "INSERT INTO civicrm_saved_search (form_values) VALUES('" . serialize($formValues) . "')"
+    );
+    $returnedFormValues = CRM_Contact_BAO_SavedSearch::getFormValues(CRM_Core_DAO::singleValueQuery('SELECT LAST_INSERT_ID()'));
+    $checkFormValues = $formValues + ['custom_' . $this->ids['CustomField']['select_date'] . '_relative' => 0];
+    $this->checkArrayEquals($returnedFormValues, $checkFormValues);
+  }
+
   /**
    * Test fixValues function.
    *