CRM-21148 change form function to wrap Date function
authorJon goldberg <jon@palantetech.coop>
Wed, 28 Mar 2018 08:56:44 +0000 (21:56 +1300)
committereileen <emcnaughton@wikimedia.org>
Mon, 16 Apr 2018 03:54:12 +0000 (15:54 +1200)
CRM/Report/Form.php
CRM/Utils/Date.php
tests/phpunit/CRM/Report/FormTest.php [new file with mode: 0644]

index 181cf0d7cd7e21ca7a2f8c9797a7c263e6da5bf0..6e41d5e1e6a6f2466389d4f4a754f8ad2c0bf9c2 100644 (file)
@@ -2065,17 +2065,7 @@ class CRM_Report_Form extends CRM_Core_Form {
    * @return array
    */
   public function getFromTo($relative, $from, $to, $fromTime = NULL, $toTime = '235959') {
-    //FIX ME not working for relative
-    if ($relative) {
-      list($term, $unit) = CRM_Utils_System::explode('.', $relative, 2);
-      $dateRange = CRM_Utils_Date::relativeToAbsolute($term, $unit);
-      $from = substr($dateRange['from'], 0, 8);
-      //Take only Date Part, Sometime Time part is also present in 'to'
-      $to = substr($dateRange['to'], 0, 8);
-    }
-    $from = CRM_Utils_Date::processDate($from, $fromTime);
-    $to = CRM_Utils_Date::processDate($to, $toTime);
-    return array($from, $to);
+    return CRM_Utils_Date::getFromTo($relative, $from, $to, $fromTime, $toTime);
   }
 
   /**
index bad74c87db748635e7a204bee5973ce2510f9ff2..947dcb62e9d84650df39afe09a96c28a05dfdcd7 100644 (file)
@@ -791,8 +791,8 @@ class CRM_Utils_Date {
    * Get start date and end from
    * the given relative term and unit
    *
-   * @param date $relative
-   *   Eg: term.unit.
+   * @param string $relative Relative format in the format term.unit.
+   *   Eg: previous.day
    *
    * @param string $from
    * @param string $to
@@ -808,14 +808,13 @@ class CRM_Utils_Date {
       $dateRange = self::relativeToAbsolute($term, $unit);
       $from = substr($dateRange['from'], 0, 8);
       $to = substr($dateRange['to'], 0, 8);
-      // relativeToAbsolute returns 8 char date strings or 14 char
-      // time strings.
-      if (strlen($dateRange['from']) === 14) {
-        $fromTime = substr($dateRange['from'], -6, 6);
-      }
-      if (strlen($dateRange['to']) === 14) {
-        $fromTime = substr($dateRange['to'], -6, 6);
-      }
+      // @todo fix relativeToAbsolute & add tests
+      // relativeToAbsolute returns 8 char date strings
+      // or 14 char date + time strings.
+      // We should use those. However, it turns out to be unreliable.
+      // e.g. this.week does NOT return 235959 for 'from'
+      // so our defaults are more reliable.
+      // Currently relativeToAbsolute only supports 'whole' days so that is ok
     }
 
     $from = self::processDate($from, $fromTime);
diff --git a/tests/phpunit/CRM/Report/FormTest.php b/tests/phpunit/CRM/Report/FormTest.php
new file mode 100644 (file)
index 0000000..80a926c
--- /dev/null
@@ -0,0 +1,90 @@
+<?php
+
+/*
+  +--------------------------------------------------------------------+
+  | CiviCRM version 4.7                                                |
+  +--------------------------------------------------------------------+
+  | Copyright CiviCRM LLC (c) 2004-2017                                |
+  +--------------------------------------------------------------------+
+  | This file is a part of CiviCRM.                                    |
+  |                                                                    |
+  | CiviCRM is free software; you can copy, modify, and distribute it  |
+  | under the terms of the GNU Affero General Public License           |
+  | Version 3, 19 November 2007 and the CiviCRM Licensing Exception.   |
+  |                                                                    |
+  | CiviCRM is distributed in the hope that it will be useful, but     |
+  | WITHOUT ANY WARRANTY; without even the implied warranty of         |
+  | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.               |
+  | See the GNU Affero General Public License for more details.        |
+  |                                                                    |
+  | You should have received a copy of the GNU Affero General Public   |
+  | License and the CiviCRM Licensing Exception along                  |
+  | with this program; if not, contact CiviCRM LLC                     |
+  | at info[AT]civicrm[DOT]org. If you have questions about the        |
+  | GNU Affero General Public License or the licensing of CiviCRM,     |
+  | see the CiviCRM license FAQ at http://civicrm.org/licensing        |
+  +--------------------------------------------------------------------+
+ */
+
+/**
+ *  File for the FormTest class
+ *
+ *  (PHP 5)
+ *
+ * @author Jon Goldberg <jon@megaphonetech.com>
+ */
+
+/**
+ *  Test CRM_Report_Form functions.
+ *
+ * @package   CiviCRM
+ * @group headless
+ */
+class CRM_Report_FormTest extends CiviUnitTestCase {
+
+  public function setUp() {
+    // There are only unit tests here at present, we can skip database loading.
+    return TRUE;
+  }
+
+  public function tearDown() {
+    // There are only unit tests here at present, we can skip database loading.
+    return TRUE;
+  }
+
+  public function fromToData() {
+    $cases = array();
+    // Absolute dates
+    $cases[] = array('20170901000000', '20170913235959', 0, '09/01/2017', '09/13/2017');
+    // "Today" relative date filter
+    $date = new DateTime();
+    $expectedFrom = $date->format('Ymd') . '000000';
+    $expectedTo = $date->format('Ymd') . '235959';
+    $cases[] = array($expectedFrom, $expectedTo, 'this.day', '', '');
+    // "yesterday" relative date filter
+    $date = new DateTime();
+    $date->sub(new DateInterval('P1D'));
+    $expectedFrom = $date->format('Ymd') . '000000';
+    $expectedTo = $date->format('Ymd') . '235959';
+    $cases[] = array($expectedFrom, $expectedTo, 'previous.day', '', '');
+    return $cases;
+  }
+
+  /**
+   * Test that getFromTo returns the correct dates.
+   *
+   * @dataProvider fromToData
+   * @param $expectedFrom
+   * @param $expectedTo
+   * @param $relative
+   * @param $from
+   * @param $to
+   */
+  public function testGetFromTo($expectedFrom, $expectedTo, $relative, $from, $to) {
+    $obj = new CRM_Report_Form();
+    list($calculatedFrom, $calculatedTo) = $obj->getFromTo($relative, $from, $to);
+    $this->assertEquals($expectedFrom, $calculatedFrom);
+    $this->assertEquals($expectedTo, $calculatedTo);
+  }
+
+}