Merge pull request #14833 from seamuslee001/ids_ip_logging_improvements
[civicrm-core.git] / tests / phpunit / CRM / Utils / DateTest.php
CommitLineData
819e6707
J
1<?php
2
3/*
4 +--------------------------------------------------------------------+
2fe49090 5 | CiviCRM version 5 |
819e6707 6 +--------------------------------------------------------------------+
6b83d5bd 7 | Copyright CiviCRM LLC (c) 2004-2019 |
819e6707
J
8 +--------------------------------------------------------------------+
9 | This file is a part of CiviCRM. |
10 | |
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. |
14 | |
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. |
19 | |
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 +--------------------------------------------------------------------+
27 */
28
29/**
30 * File for the DateTest class
31 *
32 * (PHP 5)
33 *
34 * @author Jon Goldberg <jon@megaphonetech.com>
35 */
36
37/**
38 * Test CRM_Utils_Date functions.
39 *
40 * @package CiviCRM
41 * @group headless
42 */
43class CRM_Utils_DateTest extends CiviUnitTestCase {
44
45 public function setUp() {
46 // There are only unit tests here at present, we can skip database loading.
47 return TRUE;
48 }
49
50 public function tearDown() {
51 // There are only unit tests here at present, we can skip database loading.
52 return TRUE;
53 }
54
55 public function fromToData() {
9099cab3 56 $cases = [];
819e6707 57 // Absolute dates
9099cab3 58 $cases[] = ['20170901000000', '20170913235959', 0, '09/01/2017', '09/13/2017'];
819e6707
J
59 // "Today" relative date filter
60 $date = new DateTime();
61 $expectedFrom = $date->format('Ymd') . '000000';
62 $expectedTo = $date->format('Ymd') . '235959';
9099cab3 63 $cases[] = [$expectedFrom, $expectedTo, 'this.day', '', ''];
819e6707
J
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';
9099cab3 69 $cases[] = [$expectedFrom, $expectedTo, 'previous.day', '', ''];
819e6707
J
70 return $cases;
71 }
72
73 /**
74 * Test that getFromTo returns the correct dates.
75 *
76 * @dataProvider fromToData
77 * @param $expectedFrom
78 * @param $expectedTo
79 * @param $relative
80 * @param $from
81 * @param $to
82 */
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);
88 }
89
1e60a380 90 /**
91 * Test relativeToAbsolute function on a range of fiscal year options.
92 *
93 * Go backwards one year at a time through the sequence.
94 */
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);
99
100 foreach ($sequence as $relativeString) {
101 $date = CRM_Utils_Date::relativeToAbsolute($relativeString, 'fiscal_year');
102 $this->assertEquals([
103 'from' => $fiscalYearStartYear . '0701',
39b959db 104 'to' => ($fiscalYearStartYear + 1) . '0630',
1e60a380 105 ], $date, 'relative term is ' . $relativeString);
106
107 $fiscalYearStartYear--;
108 }
109 }
110
111 /**
112 * Test relativeToAbsolute function on a range of year options.
113 *
114 * Go backwards one year at a time through the sequence.
115 */
116 public function testRelativeToAbsoluteYear() {
117 $sequence = ['this', 'previous', 'previous_before'];
118 $year = date('Y');
119
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);
126
127 $year--;
128 }
129 }
130
ad336d61 131 /**
132 * Test relativeToAbsolute function on a range of year options.
133 *
134 * Go backwards one year at a time through the sequence.
135 */
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',
150 ];
151
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);
159 }
160
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');
166 }
167
1e60a380 168 /**
169 * Test relativeToAbsolute function on a range of year options.
170 *
171 * Go backwards one year at a time through the sequence.
172 */
173 public function testRelativeToAbsoluteYearRange() {
174 $sequence = ['previous_2'];
175 $lastYear = (date('Y') - 1);
176
177 foreach ($sequence as $relativeString) {
178 $date = CRM_Utils_Date::relativeToAbsolute($relativeString, 'year');
179 // For previous 2 years the range is e.g 2016-01-01 to 2017-12-31 so we have to subtract
180 // one from the range count to reflect the calendar year being one less apart due
181 // to it being from the beginning of one to the end of the next.
182 $offset = (substr($relativeString, -1, 1)) - 1;
183 $this->assertEquals([
184 'from' => $lastYear - $offset . '0101',
39b959db 185 'to' => $lastYear . '1231',
1e60a380 186 ], $date, 'relative term is ' . $relativeString);
187 }
188 }
189
190 /**
191 * Test relativeToAbsolute function on a range of year options.
192 *
193 * Go backwards one year at a time through the sequence.
194 */
195 public function testRelativeToAbsoluteFiscalYearRange() {
196 $sequence = ['previous_2', 'previous_3', 'previous_4'];
197 Civi::settings()->set('fiscalYearStart', ['M' => 7, 'd' => 1]);
198 $lastFiscalYearEnd = (strtotime('now') > strtotime((date('Y-07-01')))) ? (date('Y')) : (date('Y') - 1);
199
200 foreach ($sequence as $relativeString) {
201 $date = CRM_Utils_Date::relativeToAbsolute($relativeString, 'fiscal_year');
202 // For previous 2 years the range is e.g 2015-07-01 to 2017-06-30 so we have to subtract
203 // one from the range count to reflect the calendar year being one less apart due
204 // to it being from the beginning of one to the end of the next.
205 $offset = (substr($relativeString, -1, 1));
206 $this->assertEquals([
207 'from' => $lastFiscalYearEnd - $offset . '0701',
39b959db 208 'to' => $lastFiscalYearEnd . '0630',
1e60a380 209 ], $date, 'relative term is ' . $relativeString);
210 }
211 }
212
d2f262da
AS
213 /**
214 * Test customFormat() function
215 */
216 public function testCustomFormat() {
217 $dateTime = "2018-11-08 21:46:44";
218 $this->assertEquals(CRM_Utils_Date::customFormat($dateTime, "%b"), "Nov");
219 $this->assertEquals(CRM_Utils_Date::customFormat($dateTime, "%B"), "November");
220 $this->assertEquals(CRM_Utils_Date::customFormat($dateTime, "%d"), "08");
221 $this->assertEquals(CRM_Utils_Date::customFormat($dateTime, "%e"), " 8");
222 $this->assertEquals(CRM_Utils_Date::customFormat($dateTime, "%E"), "8");
223 $this->assertEquals(CRM_Utils_Date::customFormat($dateTime, "%f"), "th");
224 $this->assertEquals(CRM_Utils_Date::customFormat($dateTime, "%H"), "21");
225 $this->assertEquals(CRM_Utils_Date::customFormat($dateTime, "%I"), "09");
226 $this->assertEquals(CRM_Utils_Date::customFormat($dateTime, "%k"), "21");
227 $this->assertEquals(CRM_Utils_Date::customFormat($dateTime, "%l"), " 9");
228 $this->assertEquals(CRM_Utils_Date::customFormat($dateTime, "%m"), "11");
229 $this->assertEquals(CRM_Utils_Date::customFormat($dateTime, "%M"), "46");
230 $this->assertEquals(CRM_Utils_Date::customFormat($dateTime, "%p"), "pm");
231 $this->assertEquals(CRM_Utils_Date::customFormat($dateTime, "%P"), "PM");
232 $this->assertEquals(CRM_Utils_Date::customFormat($dateTime, "%Y"), "2018");
233 }
234
235 /**
236 * Test customFormat() function
237 */
238 public function testCustomFormatTs() {
239 $ts = mktime(21, 46, 44, 11, 8, 2018);
240 $this->assertEquals(CRM_Utils_Date::customFormatTs($ts, "%b"), "Nov");
241 $this->assertEquals(CRM_Utils_Date::customFormatTs($ts, "%B"), "November");
242 $this->assertEquals(CRM_Utils_Date::customFormatTs($ts, "%d"), "08");
243 $this->assertEquals(CRM_Utils_Date::customFormatTs($ts, "%e"), " 8");
244 $this->assertEquals(CRM_Utils_Date::customFormatTs($ts, "%E"), "8");
245 $this->assertEquals(CRM_Utils_Date::customFormatTs($ts, "%f"), "th");
246 $this->assertEquals(CRM_Utils_Date::customFormatTs($ts, "%H"), "21");
247 $this->assertEquals(CRM_Utils_Date::customFormatTs($ts, "%I"), "09");
248 $this->assertEquals(CRM_Utils_Date::customFormatTs($ts, "%k"), "21");
249 $this->assertEquals(CRM_Utils_Date::customFormatTs($ts, "%l"), " 9");
250 $this->assertEquals(CRM_Utils_Date::customFormatTs($ts, "%m"), "11");
251 $this->assertEquals(CRM_Utils_Date::customFormatTs($ts, "%M"), "46");
252 $this->assertEquals(CRM_Utils_Date::customFormatTs($ts, "%p"), "pm");
253 $this->assertEquals(CRM_Utils_Date::customFormatTs($ts, "%P"), "PM");
254 $this->assertEquals(CRM_Utils_Date::customFormatTs($ts, "%Y"), "2018");
255 }
256
819e6707 257}