From ff77d5253a54c6a71caa9509c964c2c364089707 Mon Sep 17 00:00:00 2001
From: Seamus Lee <seamuslee001@gmail.com>
Date: Tue, 5 Nov 2019 07:07:33 +1100
Subject: [PATCH] Add in a unit test of the custom field date range conversion

---
 CRM/Upgrade/Incremental/SmartGroups.php       |  3 +
 .../CRM/Upgrade/Incremental/BaseTest.php      | 57 +++++++++++++++++++
 2 files changed, 60 insertions(+)

diff --git a/CRM/Upgrade/Incremental/SmartGroups.php b/CRM/Upgrade/Incremental/SmartGroups.php
index 1fd19a17c2..338096f367 100644
--- a/CRM/Upgrade/Incremental/SmartGroups.php
+++ b/CRM/Upgrade/Incremental/SmartGroups.php
@@ -307,12 +307,15 @@ class CRM_Upgrade_Incremental_SmartGroups {
             if (isset($formValues[2]['BETWEEN'])) {
               $form_values[] = ['custom_' . $custom_date_fields->id . '_low', '=', $this->getConvertedDateValue($formValues[2]['BETWEEN'][0], FALSE)];
               $form_values[] = ['custom_' . $custom_date_fields->id . '_high', '=', $this->getConvertedDateValue($formValues[2]['BETWEEN'][1], TRUE)];
+              unset($form_values[$index]);
             }
             if (isset($formValues[2]['>='])) {
               $form_values[] = ['custom_' . $custom_date_fields->id . '_low', '=', $this->getConvertedDateValue($formValues[2]['>='], FALSE)];
+              unset($form_values[$index]);
             }
             if (isset($formValues[2]['<='])) {
               $form_values[] = ['custom_' . $custom_date_fields->id . '_high', '=', $this->getConvertedDateValue($formValues[2]['<='], TRUE)];
+              unset($form_values[$index]);
             }
           }
         }
diff --git a/tests/phpunit/CRM/Upgrade/Incremental/BaseTest.php b/tests/phpunit/CRM/Upgrade/Incremental/BaseTest.php
index e5a1067d91..ffe2b28056 100644
--- a/tests/phpunit/CRM/Upgrade/Incremental/BaseTest.php
+++ b/tests/phpunit/CRM/Upgrade/Incremental/BaseTest.php
@@ -5,6 +5,7 @@
  * @group headless
  */
 class CRM_Upgrade_Incremental_BaseTest extends CiviUnitTestCase {
+  use CRMTraits_Custom_CustomDataTrait;
 
   public function tearDown() {
     $this->quickCleanup(['civicrm_saved_search']);
@@ -345,6 +346,62 @@ class CRM_Upgrade_Incremental_BaseTest extends CiviUnitTestCase {
     $this->assertEquals(['relationship_end_date_relative', '=', 'this.month'], $savedSearch['form_values'][5]);
   }
 
+  /**
+   * Test convert custom saved search
+   */
+  public function testSmartGroupCustomDateRangeSearch() {
+    $this->entity = 'Contact';
+    $this->createCustomGroupWithFieldOfType([], 'date');
+    $dateCustomFieldName = $this->getCustomFieldName('date');
+    $this->callAPISuccess('SavedSearch', 'create', [
+      'form_values' => [
+        [$dateCustomFieldName . '_relative', '=', 0],
+        [$dateCustomFieldName, '=', ['BETWEEN' => ['20191001000000', '20191031235959']]],
+      ],
+    ]);
+    $this->callAPISuccess('SavedSearch', 'create', [
+      'form_values' => [
+        [$dateCustomFieldName . '_relative', '=', 0],
+        [$dateCustomFieldName, '=', ['>=' => '20191001000000']],
+      ],
+    ]);
+    $this->callAPISuccess('SavedSearch', 'create', [
+      'form_values' => [
+        [$dateCustomFieldName . '_relative', '=', 0],
+        [$dateCustomFieldName, '=', ['<=' => '20191031235959']],
+      ],
+    ]);
+    $this->callAPISuccess('SavedSearch', 'create', [
+      'form_values' => [
+        [$dateCustomFieldName . '_relative', '=', 'this.month'],
+      ],
+    ]);
+    $smartGroupConversionObject = new CRM_Upgrade_Incremental_SmartGroups();
+    $smartGroupConversionObject->convertCustomSmartGroups();
+    $expectedResults = [
+      1 => [
+        0 => [$dateCustomFieldName . '_relative', '=', 0],
+        2 => [$dateCustomFieldName . '_low', '=', '2019-10-01 00:00:00'],
+        3 => [$dateCustomFieldName . '_high', '=', '2019-10-31 23:59:59'],
+      ],
+      2 => [
+        0 => [$dateCustomFieldName . '_relative', '=', 0],
+        2 => [$dateCustomFieldName . '_low', '=', '2019-10-01 00:00:00'],
+      ],
+      3 => [
+        0 => [$dateCustomFieldName . '_relative', '=', 0],
+        2 => [$dateCustomFieldName . '_high', '=', '2019-10-31 23:59:59'],
+      ],
+      4 => [
+        0 => [$dateCustomFieldName . '_relative', '=', 'this.month'],
+      ],
+    ];
+    $savedSearches = $this->callAPISuccess('SavedSearch', 'get', []);
+    foreach ($savedSearches['values'] as $id => $savedSearch) {
+      $this->assertEquals($expectedResults[$id], $savedSearch['form_values']);
+    }
+  }
+
   /**
    * Test conversion of on hold group.
    */
-- 
2.25.1