From 5b5ea9b618c87e94bd3c5013d38d39f0d7708a40 Mon Sep 17 00:00:00 2001
From: eileen <emcnaughton@wikimedia.org>
Date: Mon, 24 Jun 2019 10:32:52 +1200
Subject: [PATCH] Add query object support for receive_date_high &
 receive_date_low and generically date fields

---
 CRM/Contact/BAO/Query.php                     |  7 +++++++
 .../phpunit/CRM/Contribute/BAO/QueryTest.php  | 20 +++++++++++++++++++
 2 files changed, 27 insertions(+)

diff --git a/CRM/Contact/BAO/Query.php b/CRM/Contact/BAO/Query.php
index 3b4c7802fd..53e37d4c31 100644
--- a/CRM/Contact/BAO/Query.php
+++ b/CRM/Contact/BAO/Query.php
@@ -2140,6 +2140,12 @@ class CRM_Contact_BAO_Query {
       $field = CRM_Utils_Array::value($locType[0], $this->_fields);
 
       if (!$field) {
+        // Strip any trailing _high & _low that might be appended.
+        $realFieldName = str_replace(['_high', '_low'], '', $name);
+        if (isset($this->_fields[$realFieldName])) {
+          $field = $this->_fields[str_replace(['_high', '_low'], '', $realFieldName)];
+          $this->dateQueryBuilder($values, $field['table_name'], $realFieldName, $realFieldName, $field['title']);
+        }
         return;
       }
     }
@@ -5214,6 +5220,7 @@ civicrm_relationship.start_date > {$today}
     $appendTimeStamp = TRUE,
     $dateFormat = 'YmdHis'
   ) {
+    // @todo - remove dateFormat - pretty sure it's never passed in...
     list($name, $op, $value, $grouping, $wildcard) = $values;
 
     if ($name == "{$fieldName}_low" ||
diff --git a/tests/phpunit/CRM/Contribute/BAO/QueryTest.php b/tests/phpunit/CRM/Contribute/BAO/QueryTest.php
index 8942d740cf..3d6ad9f6ef 100644
--- a/tests/phpunit/CRM/Contribute/BAO/QueryTest.php
+++ b/tests/phpunit/CRM/Contribute/BAO/QueryTest.php
@@ -2,12 +2,14 @@
 
 /**
  *  Include dataProvider for tests
+ *
  * @group headless
  */
 class CRM_Contribute_BAO_QueryTest extends CiviUnitTestCase {
 
   public function tearDown() {
     $this->quickCleanUpFinancialEntities();
+    parent::tearDown();
   }
 
   /**
@@ -20,6 +22,8 @@ class CRM_Contribute_BAO_QueryTest extends CiviUnitTestCase {
    *   order by a passed in list. It makes sense for option groups & small sets
    *   but may not do for long lists like states - performance testing not done on that yet.
    *
+   * @throws \CRM_Core_Exception
+   *
    * @dataProvider getSortFields
    */
   public function testSearchPseudoReturnProperties($sort, $isUseKeySort) {
@@ -64,4 +68,20 @@ class CRM_Contribute_BAO_QueryTest extends CiviUnitTestCase {
     ];
   }
 
+  /**
+   * Test receive_date_high, low & relative work.
+   *
+   * @throws \CRM_Core_Exception
+   */
+  public function testRelativeContributionDates() {
+    $this->contributionCreate(['receive_date' => '2018-01-02', 'contact_id' => $this->individualCreate()]);
+    $this->contributionCreate(['receive_date' => '2017-01-02', 'contact_id' => $this->individualCreate()]);
+    $queryObj = new CRM_Contact_BAO_Query([['receive_date_low', '=', 20170101, 1, 0]]);
+    $this->assertEquals(2, $queryObj->searchQuery(0, 0, NULL, TRUE));
+    $queryObj = new CRM_Contact_BAO_Query([['receive_date_low', '=', 20180101, 1, 0]]);
+    $this->assertEquals(1, $queryObj->searchQuery(0, 0, NULL, TRUE));
+    $queryObj = new CRM_Contact_BAO_Query([['receive_date_high', '=', 20180101, 1, 0]]);
+    $this->assertEquals(1, $queryObj->searchQuery(0, 0, NULL, TRUE));
+  }
+
 }
-- 
2.25.1