Add query object support for receive_date_high & receive_date_low and generically...
authoreileen <emcnaughton@wikimedia.org>
Sun, 23 Jun 2019 22:32:52 +0000 (10:32 +1200)
committereileen <emcnaughton@wikimedia.org>
Sun, 23 Jun 2019 22:44:16 +0000 (10:44 +1200)
CRM/Contact/BAO/Query.php
tests/phpunit/CRM/Contribute/BAO/QueryTest.php

index 3b4c7802fd0a60b8162ca88c8cc6460749387569..53e37d4c315634f5ea54380705342edb620c2df7 100644 (file)
@@ -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" ||
index 8942d740cfca5c3cd58921181539f316124f969d..3d6ad9f6efd2555f9b6bf7f4e700bebe28864d53 100644 (file)
@@ -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));
+  }
+
 }