8730238fa9b9f59dd6c2d5471abbf0c5e4c83f45
4 +--------------------------------------------------------------------+
6 +--------------------------------------------------------------------+
7 | Copyright CiviCRM LLC (c) 2004-2017 |
8 +--------------------------------------------------------------------+
9 | This file is a part of CiviCRM. |
11 | CiviCRM is free software; you can copy, modify, and distribute it |
12 | under the terms of the GNU Affero General Public License |
13 | Version 3, 19 November 2007 and the CiviCRM Licensing Exception. |
15 | CiviCRM is distributed in the hope that it will be useful, but |
16 | WITHOUT ANY WARRANTY; without even the implied warranty of |
17 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
18 | See the GNU Affero General Public License for more details. |
20 | You should have received a copy of the GNU Affero General Public |
21 | License and the CiviCRM Licensing Exception along |
22 | with this program; if not, contact CiviCRM LLC |
23 | at info[AT]civicrm[DOT]org. If you have questions about the |
24 | GNU Affero General Public License or the licensing of CiviCRM, |
25 | see the CiviCRM license FAQ at http://civicrm.org/licensing |
26 +--------------------------------------------------------------------+
30 * File for the DateTest class
34 * @author Jon Goldberg <jon@megaphonetech.com>
38 * Test CRM_Utils_Date functions.
43 class CRM_Utils_DateTest
extends CiviUnitTestCase
{
45 public function setUp() {
46 // There are only unit tests here at present, we can skip database loading.
50 public function tearDown() {
51 // There are only unit tests here at present, we can skip database loading.
55 public function fromToData() {
58 $cases[] = array('20170901000000', '20170913235959', 0, '09/01/2017', '09/13/2017');
59 // "Today" relative date filter
60 $date = new DateTime();
61 $expectedFrom = $date->format('Ymd') . '000000';
62 $expectedTo = $date->format('Ymd') . '235959';
63 $cases[] = array($expectedFrom, $expectedTo, 'this.day', '', '');
64 // "yesterday" relative date filter
65 $date = new DateTime();
66 $date->sub(new DateInterval('P1D'));
67 $expectedFrom = $date->format('Ymd') . '000000';
68 $expectedTo = $date->format('Ymd') . '235959';
69 $cases[] = array($expectedFrom, $expectedTo, 'previous.day', '', '');
74 * Test that getFromTo returns the correct dates.
76 * @dataProvider fromToData
77 * @param $expectedFrom
83 public function testGetFromTo($expectedFrom, $expectedTo, $relative, $from, $to) {
84 $obj = new CRM_Utils_Date();
85 list($calculatedFrom, $calculatedTo) = $obj->getFromTo($relative, $from, $to);
86 $this->assertEquals($expectedFrom, $calculatedFrom);
87 $this->assertEquals($expectedTo, $calculatedTo);
91 * Test relativeToAbsolute function on a range of fiscal year options.
93 * Go backwards one year at a time through the sequence.
95 public function testRelativeToAbsoluteFiscalYear() {
96 $sequence = ['this', 'previous', 'previous_before'];
97 Civi
::settings()->set('fiscalYearStart', ['M' => 7, 'd' => 1]);
98 $fiscalYearStartYear = (strtotime('now') > strtotime((date('Y-07-01')))) ?
date('Y') : (date('Y') - 1);
100 foreach ($sequence as $relativeString) {
101 $date = CRM_Utils_Date
::relativeToAbsolute($relativeString, 'fiscal_year');
102 $this->assertEquals([
103 'from' => $fiscalYearStartYear . '0701',
104 'to' => ($fiscalYearStartYear +
1) . '0630'
105 ], $date, 'relative term is ' . $relativeString);
107 $fiscalYearStartYear--;
112 * Test relativeToAbsolute function on a range of year options.
114 * Go backwards one year at a time through the sequence.
116 public function testRelativeToAbsoluteYear() {
117 $sequence = ['this', 'previous', 'previous_before'];
120 foreach ($sequence as $relativeString) {
121 $date = CRM_Utils_Date
::relativeToAbsolute($relativeString, 'year');
122 $this->assertEquals([
123 'from' => $year . '0101',
124 'to' => $year . '1231',
125 ], $date, 'relative term is ' . $relativeString);
132 * Test relativeToAbsolute function on a range of year options.
134 * Go backwards one year at a time through the sequence.
136 public function testRelativeToAbsoluteYearRange() {
137 $sequence = ['previous_2'];
138 $lastYear = (date('Y') - 1);
140 foreach ($sequence as $relativeString) {
141 $date = CRM_Utils_Date
::relativeToAbsolute($relativeString, 'year');
142 // For previous 2 years the range is e.g 2016-01-01 to 2017-12-31 so we have to subtract
143 // one from the range count to reflect the calendar year being one less apart due
144 // to it being from the beginning of one to the end of the next.
145 $offset = (substr($relativeString, -1, 1)) - 1;
146 $this->assertEquals([
147 'from' => $lastYear - $offset . '0101',
148 'to' => $lastYear . '1231',
149 ], $date, 'relative term is ' . $relativeString);
154 * Test relativeToAbsolute function on a range of year options.
156 * Go backwards one year at a time through the sequence.
158 public function testRelativeToAbsoluteFiscalYearRange() {
159 $sequence = ['previous_2', 'previous_3', 'previous_4'];
160 Civi
::settings()->set('fiscalYearStart', ['M' => 7, 'd' => 1]);
161 $lastFiscalYearEnd = (strtotime('now') > strtotime((date('Y-07-01')))) ?
(date('Y')) : (date('Y') - 1);
163 foreach ($sequence as $relativeString) {
164 $date = CRM_Utils_Date
::relativeToAbsolute($relativeString, 'fiscal_year');
165 // For previous 2 years the range is e.g 2015-07-01 to 2017-06-30 so we have to subtract
166 // one from the range count to reflect the calendar year being one less apart due
167 // to it being from the beginning of one to the end of the next.
168 $offset = (substr($relativeString, -1, 1));
169 $this->assertEquals([
170 'from' => $lastFiscalYearEnd - $offset . '0701',
171 'to' => $lastFiscalYearEnd . '0630',
172 ], $date, 'relative term is ' . $relativeString);
177 * Test customFormat() function
179 public function testCustomFormat() {
180 $dateTime = "2018-11-08 21:46:44";
181 $this->assertEquals(CRM_Utils_Date
::customFormat($dateTime, "%b"), "Nov");
182 $this->assertEquals(CRM_Utils_Date
::customFormat($dateTime, "%B"), "November");
183 $this->assertEquals(CRM_Utils_Date
::customFormat($dateTime, "%d"), "08");
184 $this->assertEquals(CRM_Utils_Date
::customFormat($dateTime, "%e"), " 8");
185 $this->assertEquals(CRM_Utils_Date
::customFormat($dateTime, "%E"), "8");
186 $this->assertEquals(CRM_Utils_Date
::customFormat($dateTime, "%f"), "th");
187 $this->assertEquals(CRM_Utils_Date
::customFormat($dateTime, "%H"), "21");
188 $this->assertEquals(CRM_Utils_Date
::customFormat($dateTime, "%I"), "09");
189 $this->assertEquals(CRM_Utils_Date
::customFormat($dateTime, "%k"), "21");
190 $this->assertEquals(CRM_Utils_Date
::customFormat($dateTime, "%l"), " 9");
191 $this->assertEquals(CRM_Utils_Date
::customFormat($dateTime, "%m"), "11");
192 $this->assertEquals(CRM_Utils_Date
::customFormat($dateTime, "%M"), "46");
193 $this->assertEquals(CRM_Utils_Date
::customFormat($dateTime, "%p"), "pm");
194 $this->assertEquals(CRM_Utils_Date
::customFormat($dateTime, "%P"), "PM");
195 $this->assertEquals(CRM_Utils_Date
::customFormat($dateTime, "%Y"), "2018");
199 * Test customFormat() function
201 public function testCustomFormatTs() {
202 $ts = mktime(21, 46, 44, 11, 8, 2018);
203 $this->assertEquals(CRM_Utils_Date
::customFormatTs($ts, "%b"), "Nov");
204 $this->assertEquals(CRM_Utils_Date
::customFormatTs($ts, "%B"), "November");
205 $this->assertEquals(CRM_Utils_Date
::customFormatTs($ts, "%d"), "08");
206 $this->assertEquals(CRM_Utils_Date
::customFormatTs($ts, "%e"), " 8");
207 $this->assertEquals(CRM_Utils_Date
::customFormatTs($ts, "%E"), "8");
208 $this->assertEquals(CRM_Utils_Date
::customFormatTs($ts, "%f"), "th");
209 $this->assertEquals(CRM_Utils_Date
::customFormatTs($ts, "%H"), "21");
210 $this->assertEquals(CRM_Utils_Date
::customFormatTs($ts, "%I"), "09");
211 $this->assertEquals(CRM_Utils_Date
::customFormatTs($ts, "%k"), "21");
212 $this->assertEquals(CRM_Utils_Date
::customFormatTs($ts, "%l"), " 9");
213 $this->assertEquals(CRM_Utils_Date
::customFormatTs($ts, "%m"), "11");
214 $this->assertEquals(CRM_Utils_Date
::customFormatTs($ts, "%M"), "46");
215 $this->assertEquals(CRM_Utils_Date
::customFormatTs($ts, "%p"), "pm");
216 $this->assertEquals(CRM_Utils_Date
::customFormatTs($ts, "%P"), "PM");
217 $this->assertEquals(CRM_Utils_Date
::customFormatTs($ts, "%Y"), "2018");