Fix wrong current month showing on Membership Dashboard
[civicrm-core.git] / tests / phpunit / CRM / Utils / DateTest.php
index 76a230a8dca64ed18aa183b4914356a20982666b..8730238fa9b9f59dd6c2d5471abbf0c5e4c83f45 100644 (file)
@@ -87,4 +87,134 @@ class CRM_Utils_DateTest extends CiviUnitTestCase {
     $this->assertEquals($expectedTo, $calculatedTo);
   }
 
+  /**
+   * Test relativeToAbsolute function on a range of fiscal year options.
+   *
+   * Go backwards one year at a time through the sequence.
+   */
+  public function testRelativeToAbsoluteFiscalYear() {
+    $sequence = ['this', 'previous', 'previous_before'];
+    Civi::settings()->set('fiscalYearStart', ['M' => 7, 'd' => 1]);
+    $fiscalYearStartYear = (strtotime('now') > strtotime((date('Y-07-01')))) ? date('Y') : (date('Y') - 1);
+
+    foreach ($sequence as $relativeString) {
+      $date = CRM_Utils_Date::relativeToAbsolute($relativeString, 'fiscal_year');
+      $this->assertEquals([
+        'from' => $fiscalYearStartYear . '0701',
+        'to' => ($fiscalYearStartYear + 1) . '0630'
+      ], $date, 'relative term is ' . $relativeString);
+
+      $fiscalYearStartYear--;
+    }
+  }
+
+  /**
+   * Test relativeToAbsolute function on a range of year options.
+   *
+   * Go backwards one year at a time through the sequence.
+   */
+  public function testRelativeToAbsoluteYear() {
+    $sequence = ['this', 'previous', 'previous_before'];
+    $year = date('Y');
+
+    foreach ($sequence as $relativeString) {
+      $date = CRM_Utils_Date::relativeToAbsolute($relativeString, 'year');
+      $this->assertEquals([
+        'from' => $year . '0101',
+        'to' => $year . '1231',
+      ], $date, 'relative term is ' . $relativeString);
+
+      $year--;
+    }
+  }
+
+  /**
+   * Test relativeToAbsolute function on a range of year options.
+   *
+   * Go backwards one year at a time through the sequence.
+   */
+  public function testRelativeToAbsoluteYearRange() {
+    $sequence = ['previous_2'];
+    $lastYear = (date('Y') - 1);
+
+    foreach ($sequence as $relativeString) {
+      $date = CRM_Utils_Date::relativeToAbsolute($relativeString, 'year');
+      // For previous 2 years the range is e.g 2016-01-01 to 2017-12-31 so we have to subtract
+      // one from the range count to reflect the calendar year being one less apart due
+      // to it being from the beginning of one to the end of the next.
+      $offset = (substr($relativeString, -1, 1)) - 1;
+      $this->assertEquals([
+        'from' => $lastYear - $offset . '0101',
+        'to' => $lastYear  . '1231',
+      ], $date, 'relative term is ' . $relativeString);
+    }
+  }
+
+  /**
+   * Test relativeToAbsolute function on a range of year options.
+   *
+   * Go backwards one year at a time through the sequence.
+   */
+  public function testRelativeToAbsoluteFiscalYearRange() {
+    $sequence = ['previous_2', 'previous_3', 'previous_4'];
+    Civi::settings()->set('fiscalYearStart', ['M' => 7, 'd' => 1]);
+    $lastFiscalYearEnd = (strtotime('now') > strtotime((date('Y-07-01')))) ? (date('Y')) : (date('Y') - 1);
+
+    foreach ($sequence as $relativeString) {
+      $date = CRM_Utils_Date::relativeToAbsolute($relativeString, 'fiscal_year');
+      // For previous 2 years the range is e.g 2015-07-01 to 2017-06-30 so we have to subtract
+      // one from the range count to reflect the calendar year being one less apart due
+      // to it being from the beginning of one to the end of the next.
+      $offset = (substr($relativeString, -1, 1));
+      $this->assertEquals([
+        'from' => $lastFiscalYearEnd - $offset . '0701',
+        'to' => $lastFiscalYearEnd  . '0630',
+      ], $date, 'relative term is ' . $relativeString);
+    }
+  }
+
+  /**
+   * Test customFormat() function
+   */
+  public function testCustomFormat() {
+    $dateTime = "2018-11-08 21:46:44";
+    $this->assertEquals(CRM_Utils_Date::customFormat($dateTime, "%b"), "Nov");
+    $this->assertEquals(CRM_Utils_Date::customFormat($dateTime, "%B"), "November");
+    $this->assertEquals(CRM_Utils_Date::customFormat($dateTime, "%d"), "08");
+    $this->assertEquals(CRM_Utils_Date::customFormat($dateTime, "%e"), " 8");
+    $this->assertEquals(CRM_Utils_Date::customFormat($dateTime, "%E"), "8");
+    $this->assertEquals(CRM_Utils_Date::customFormat($dateTime, "%f"), "th");
+    $this->assertEquals(CRM_Utils_Date::customFormat($dateTime, "%H"), "21");
+    $this->assertEquals(CRM_Utils_Date::customFormat($dateTime, "%I"), "09");
+    $this->assertEquals(CRM_Utils_Date::customFormat($dateTime, "%k"), "21");
+    $this->assertEquals(CRM_Utils_Date::customFormat($dateTime, "%l"), " 9");
+    $this->assertEquals(CRM_Utils_Date::customFormat($dateTime, "%m"), "11");
+    $this->assertEquals(CRM_Utils_Date::customFormat($dateTime, "%M"), "46");
+    $this->assertEquals(CRM_Utils_Date::customFormat($dateTime, "%p"), "pm");
+    $this->assertEquals(CRM_Utils_Date::customFormat($dateTime, "%P"), "PM");
+    $this->assertEquals(CRM_Utils_Date::customFormat($dateTime, "%Y"), "2018");
+  }
+
+  /**
+   * Test customFormat() function
+   */
+  public function testCustomFormatTs() {
+    $ts = mktime(21, 46, 44, 11, 8, 2018);
+    $this->assertEquals(CRM_Utils_Date::customFormatTs($ts, "%b"), "Nov");
+    $this->assertEquals(CRM_Utils_Date::customFormatTs($ts, "%B"), "November");
+    $this->assertEquals(CRM_Utils_Date::customFormatTs($ts, "%d"), "08");
+    $this->assertEquals(CRM_Utils_Date::customFormatTs($ts, "%e"), " 8");
+    $this->assertEquals(CRM_Utils_Date::customFormatTs($ts, "%E"), "8");
+    $this->assertEquals(CRM_Utils_Date::customFormatTs($ts, "%f"), "th");
+    $this->assertEquals(CRM_Utils_Date::customFormatTs($ts, "%H"), "21");
+    $this->assertEquals(CRM_Utils_Date::customFormatTs($ts, "%I"), "09");
+    $this->assertEquals(CRM_Utils_Date::customFormatTs($ts, "%k"), "21");
+    $this->assertEquals(CRM_Utils_Date::customFormatTs($ts, "%l"), " 9");
+    $this->assertEquals(CRM_Utils_Date::customFormatTs($ts, "%m"), "11");
+    $this->assertEquals(CRM_Utils_Date::customFormatTs($ts, "%M"), "46");
+    $this->assertEquals(CRM_Utils_Date::customFormatTs($ts, "%p"), "pm");
+    $this->assertEquals(CRM_Utils_Date::customFormatTs($ts, "%P"), "PM");
+    $this->assertEquals(CRM_Utils_Date::customFormatTs($ts, "%Y"), "2018");
+  }
+
 }