4 +--------------------------------------------------------------------+
5 | Copyright CiviCRM LLC. All rights reserved. |
7 | This work is published under the GNU AGPLv3 license with some |
8 | permitted exceptions and without any warranty. For full license |
9 | and copyright information, see https://civicrm.org/licensing |
10 +--------------------------------------------------------------------+
14 * File for the DateTest class
18 * @author Jon Goldberg <jon@megaphonetech.com>
22 * Test CRM_Utils_Date functions.
27 class CRM_Utils_DateTest
extends CiviUnitTestCase
{
29 public function setUp() {
30 // There are only unit tests here at present, we can skip database loading.
34 public function tearDown() {
35 // There are only unit tests here at present, we can skip database loading.
39 public function fromToData() {
42 $cases[] = ['20170901000000', '20170913235959', 0, '09/01/2017', '09/13/2017'];
43 // "Today" relative date filter
44 $date = new DateTime();
45 $expectedFrom = $date->format('Ymd') . '000000';
46 $expectedTo = $date->format('Ymd') . '235959';
47 $cases[] = [$expectedFrom, $expectedTo, 'this.day', '', ''];
48 // "yesterday" relative date filter
49 $date = new DateTime();
50 $date->sub(new DateInterval('P1D'));
51 $expectedFrom = $date->format('Ymd') . '000000';
52 $expectedTo = $date->format('Ymd') . '235959';
53 $cases[] = [$expectedFrom, $expectedTo, 'previous.day', '', ''];
58 * Test that getFromTo returns the correct dates.
60 * @dataProvider fromToData
61 * @param $expectedFrom
67 public function testGetFromTo($expectedFrom, $expectedTo, $relative, $from, $to) {
68 $obj = new CRM_Utils_Date();
69 list($calculatedFrom, $calculatedTo) = $obj->getFromTo($relative, $from, $to);
70 $this->assertEquals($expectedFrom, $calculatedFrom);
71 $this->assertEquals($expectedTo, $calculatedTo);
75 * Test relativeToAbsolute function on a range of fiscal year options.
77 * Go backwards one year at a time through the sequence.
79 public function testRelativeToAbsoluteFiscalYear() {
80 $sequence = ['this', 'previous', 'previous_before'];
81 Civi
::settings()->set('fiscalYearStart', ['M' => 7, 'd' => 1]);
82 $fiscalYearStartYear = (strtotime('now') > strtotime((date('Y-07-01')))) ?
date('Y') : (date('Y') - 1);
84 // this_2 = 'These 2 Fiscal years'
85 $date = CRM_Utils_Date
::relativeToAbsolute('this_2', 'fiscal_year');
87 'from' => ($fiscalYearStartYear - 1) . '0701000000',
88 'to' => ($fiscalYearStartYear +
1) . '0630235959',
89 ], $date, 'relative term is this_2.fiscal_year');
91 foreach ($sequence as $relativeString) {
92 $date = CRM_Utils_Date
::relativeToAbsolute($relativeString, 'fiscal_year');
94 'from' => $fiscalYearStartYear . '0701',
95 'to' => ($fiscalYearStartYear +
1) . '0630235959',
96 ], $date, 'relative term is ' . $relativeString);
98 $fiscalYearStartYear--;
104 * Test relativeToAbsolute function on a range of year options.
106 * Go backwards one year at a time through the sequence.
108 public function testRelativeToAbsoluteYear() {
109 $sequence = ['this', 'previous', 'previous_before'];
112 foreach ($sequence as $relativeString) {
113 $date = CRM_Utils_Date
::relativeToAbsolute($relativeString, 'year');
114 $this->assertEquals([
115 'from' => $year . '0101',
116 'to' => $year . '1231',
117 ], $date, 'relative term is ' . $relativeString);
122 // this_2 = 'These 2 years'
123 $date = CRM_Utils_Date
::relativeToAbsolute('this_2', 'year');
124 $thisYear = date('Y');
125 $this->assertEquals([
126 'from' => ($thisYear - 1) . '0101',
127 'to' => $thisYear . '1231',
128 ], $date, 'relative term is this_2 year');
132 * Test relativeToAbsolute function on a range of year options.
134 * Go backwards one year at a time through the sequence.
136 public function testRelativeEnding() {
137 $relativeDateValues = [
138 'ending.week' => '- 6 days',
139 'ending_30.day' => '- 29 days',
140 'ending.year' => '- 1 year + 1 day',
141 'ending_90.day' => '- 89 days',
142 'ending_60.day' => '- 59 days',
143 'ending_2.year' => '- 2 years + 1 day',
144 'ending_3.year' => '- 3 years + 1 day',
145 'ending_18.year' => '- 18 years + 1 day',
146 'ending_18.quarter' => '- 54 months + 1 day',
147 'ending_18.week' => '- 18 weeks + 1 day',
148 'ending_18.month' => '- 18 months + 1 day',
149 'ending_18.day' => '- 17 days',
152 foreach ($relativeDateValues as $key => $value) {
153 $parts = explode('.', $key);
154 $date = CRM_Utils_Date
::relativeToAbsolute($parts[0], $parts[1]);
155 $this->assertEquals([
156 'from' => date('Ymd000000', strtotime($value)),
157 'to' => date('Ymd235959'),
158 ], $date, 'relative term is ' . $key);
161 $date = CRM_Utils_Date
::relativeToAbsolute('ending', 'month');
162 $this->assertEquals([
163 'from' => date('Ymd000000', strtotime('- 29 days')),
164 'to' => date('Ymd235959'),
165 ], $date, 'relative term is ending.week');
169 * Test relativeToAbsolute function on a range of year options.
171 * Go backwards one year at a time through the sequence.
173 public function testRelativeThisFiscal() {
174 $relativeDateValues = [
175 'ending.week' => '- 6 days',
176 'ending_30.day' => '- 29 days',
177 'ending.year' => '- 1 year + 1 day',
178 'ending_90.day' => '- 89 days',
179 'ending_60.day' => '- 59 days',
180 'ending_2.year' => '- 2 years + 1 day',
181 'ending_3.year' => '- 3 years + 1 day',
182 'ending_18.year' => '- 18 years + 1 day',
183 'ending_18.quarter' => '- 54 months + 1 day',
184 'ending_18.week' => '- 18 weeks + 1 day',
185 'ending_18.month' => '- 18 months + 1 day',
186 'ending_18.day' => '- 17 days',
189 foreach ($relativeDateValues as $key => $value) {
190 $parts = explode('.', $key);
191 $date = CRM_Utils_Date
::relativeToAbsolute($parts[0], $parts[1]);
192 $this->assertEquals([
193 'from' => date('Ymd000000', strtotime($value)),
194 'to' => date('Ymd235959'),
195 ], $date, 'relative term is ' . $key);
198 $date = CRM_Utils_Date
::relativeToAbsolute('ending', 'month');
199 $this->assertEquals([
200 'from' => date('Ymd000000', strtotime('- 29 days')),
201 'to' => date('Ymd235959'),
202 ], $date, 'relative term is ending.week');
206 * Test relativeToAbsolute function on a range of year options.
208 * Go backwards one year at a time through the sequence.
210 public function testRelativeToAbsoluteYearRange() {
211 $sequence = ['previous_2'];
212 $lastYear = (date('Y') - 1);
214 foreach ($sequence as $relativeString) {
215 $date = CRM_Utils_Date
::relativeToAbsolute($relativeString, 'year');
216 // For previous 2 years the range is e.g 2016-01-01 to 2017-12-31 so we have to subtract
217 // one from the range count to reflect the calendar year being one less apart due
218 // to it being from the beginning of one to the end of the next.
219 $offset = (substr($relativeString, -1, 1)) - 1;
220 $this->assertEquals([
221 'from' => $lastYear - $offset . '0101',
222 'to' => $lastYear . '1231',
223 ], $date, 'relative term is ' . $relativeString);
228 * Test relativeToAbsolute function on a range of year options.
230 * Go backwards one year at a time through the sequence.
232 public function testRelativeToAbsoluteFiscalYearRange() {
233 $sequence = ['previous_2', 'previous_3', 'previous_4'];
234 Civi
::settings()->set('fiscalYearStart', ['M' => 7, 'd' => 1]);
235 $lastFiscalYearEnd = (strtotime('now') > strtotime((date('Y-07-01')))) ?
(date('Y')) : (date('Y') - 1);
237 foreach ($sequence as $relativeString) {
238 $date = CRM_Utils_Date
::relativeToAbsolute($relativeString, 'fiscal_year');
239 // For previous 2 years the range is e.g 2015-07-01 to 2017-06-30 so we have to subtract
240 // one from the range count to reflect the calendar year being one less apart due
241 // to it being from the beginning of one to the end of the next.
242 $offset = (substr($relativeString, -1, 1));
243 $this->assertEquals([
244 'from' => $lastFiscalYearEnd - $offset . '0701',
245 'to' => $lastFiscalYearEnd . '0630235959',
246 ], $date, 'relative term is ' . $relativeString);
251 * Test customFormat() function
253 public function testCustomFormat() {
254 $dateTime = "2018-11-08 21:46:44";
255 $this->assertEquals(CRM_Utils_Date
::customFormat($dateTime, "%b"), "Nov");
256 $this->assertEquals(CRM_Utils_Date
::customFormat($dateTime, "%B"), "November");
257 $this->assertEquals(CRM_Utils_Date
::customFormat($dateTime, "%d"), "08");
258 $this->assertEquals(CRM_Utils_Date
::customFormat($dateTime, "%e"), " 8");
259 $this->assertEquals(CRM_Utils_Date
::customFormat($dateTime, "%E"), "8");
260 $this->assertEquals(CRM_Utils_Date
::customFormat($dateTime, "%f"), "th");
261 $this->assertEquals(CRM_Utils_Date
::customFormat($dateTime, "%H"), "21");
262 $this->assertEquals(CRM_Utils_Date
::customFormat($dateTime, "%I"), "09");
263 $this->assertEquals(CRM_Utils_Date
::customFormat($dateTime, "%k"), "21");
264 $this->assertEquals(CRM_Utils_Date
::customFormat($dateTime, "%l"), " 9");
265 $this->assertEquals(CRM_Utils_Date
::customFormat($dateTime, "%m"), "11");
266 $this->assertEquals(CRM_Utils_Date
::customFormat($dateTime, "%M"), "46");
267 $this->assertEquals(CRM_Utils_Date
::customFormat($dateTime, "%p"), "pm");
268 $this->assertEquals(CRM_Utils_Date
::customFormat($dateTime, "%P"), "PM");
269 $this->assertEquals(CRM_Utils_Date
::customFormat($dateTime, "%Y"), "2018");
273 * Test customFormat() function
275 public function testCustomFormatTs() {
276 $ts = mktime(21, 46, 44, 11, 8, 2018);
277 $this->assertEquals(CRM_Utils_Date
::customFormatTs($ts, "%b"), "Nov");
278 $this->assertEquals(CRM_Utils_Date
::customFormatTs($ts, "%B"), "November");
279 $this->assertEquals(CRM_Utils_Date
::customFormatTs($ts, "%d"), "08");
280 $this->assertEquals(CRM_Utils_Date
::customFormatTs($ts, "%e"), " 8");
281 $this->assertEquals(CRM_Utils_Date
::customFormatTs($ts, "%E"), "8");
282 $this->assertEquals(CRM_Utils_Date
::customFormatTs($ts, "%f"), "th");
283 $this->assertEquals(CRM_Utils_Date
::customFormatTs($ts, "%H"), "21");
284 $this->assertEquals(CRM_Utils_Date
::customFormatTs($ts, "%I"), "09");
285 $this->assertEquals(CRM_Utils_Date
::customFormatTs($ts, "%k"), "21");
286 $this->assertEquals(CRM_Utils_Date
::customFormatTs($ts, "%l"), " 9");
287 $this->assertEquals(CRM_Utils_Date
::customFormatTs($ts, "%m"), "11");
288 $this->assertEquals(CRM_Utils_Date
::customFormatTs($ts, "%M"), "46");
289 $this->assertEquals(CRM_Utils_Date
::customFormatTs($ts, "%p"), "pm");
290 $this->assertEquals(CRM_Utils_Date
::customFormatTs($ts, "%P"), "PM");
291 $this->assertEquals(CRM_Utils_Date
::customFormatTs($ts, "%Y"), "2018");