1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace SilverStripe\ORM\Tests; |
4
|
|
|
|
5
|
|
|
use SilverStripe\Dev\SapphireTest; |
6
|
|
|
use SilverStripe\i18n\i18n; |
7
|
|
|
use SilverStripe\ORM\FieldType\DBDatetime; |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* Tests for {@link Datetime} class. |
11
|
|
|
*/ |
12
|
|
|
class DBDatetimeTest extends SapphireTest |
13
|
|
|
{ |
14
|
|
|
protected function setUp() |
15
|
|
|
{ |
16
|
|
|
parent::setUp(); |
17
|
|
|
i18n::set_locale('en_NZ'); |
18
|
|
|
} |
19
|
|
|
|
20
|
|
|
public function testNowWithSystemDate() |
21
|
|
|
{ |
22
|
|
|
$systemDatetime = DBDatetime::create_field('Datetime', date('Y-m-d H:i:s')); |
23
|
|
|
$nowDatetime = DBDatetime::now(); |
24
|
|
|
|
25
|
|
|
$this->assertEquals($systemDatetime->Date(), $nowDatetime->Date()); |
|
|
|
|
26
|
|
|
} |
27
|
|
|
|
28
|
|
|
public function testNowWithMockDate() |
29
|
|
|
{ |
30
|
|
|
// Test setting |
31
|
|
|
$mockDate = '2001-12-31 22:10:59'; |
32
|
|
|
DBDatetime::set_mock_now($mockDate); |
33
|
|
|
$systemDatetime = DBDatetime::create_field('Datetime', date('Y-m-d H:i:s')); |
34
|
|
|
$nowDatetime = DBDatetime::now(); |
35
|
|
|
$this->assertNotEquals($systemDatetime->Date(), $nowDatetime->Date()); |
36
|
|
|
$this->assertEquals($nowDatetime->getValue(), $mockDate); |
37
|
|
|
|
38
|
|
|
// Test clearing |
39
|
|
|
DBDatetime::clear_mock_now(); |
40
|
|
|
$systemDatetime = DBDatetime::create_field('Datetime', date('Y-m-d H:i:s')); |
41
|
|
|
$nowDatetime = DBDatetime::now(); |
42
|
|
|
$this->assertEquals($systemDatetime->Date(), $nowDatetime->Date()); |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
public function testSetNullAndZeroValues() |
46
|
|
|
{ |
47
|
|
|
$date = DBDatetime::create_field('Datetime', ''); |
48
|
|
|
$this->assertNull($date->getValue(), 'Empty string evaluates to NULL'); |
49
|
|
|
|
50
|
|
|
$date = DBDatetime::create_field('Datetime', null); |
51
|
|
|
$this->assertNull($date->getValue(), 'NULL is set as NULL'); |
52
|
|
|
|
53
|
|
|
$date = DBDatetime::create_field('Datetime', false); |
54
|
|
|
$this->assertNull($date->getValue(), 'Boolean FALSE evaluates to NULL'); |
55
|
|
|
|
56
|
|
|
$date = DBDatetime::create_field('Datetime', '0'); |
57
|
|
|
$this->assertEquals('1970-01-01 00:00:00', $date->getValue(), 'String zero is UNIX epoch time'); |
58
|
|
|
|
59
|
|
|
$date = DBDatetime::create_field('Datetime', 0); |
60
|
|
|
$this->assertEquals('1970-01-01 00:00:00', $date->getValue(), 'Numeric zero is UNIX epoch time'); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
public function testExtendedDateTimes() |
64
|
|
|
{ |
65
|
|
|
$date = DBDatetime::create_field('Datetime', '1600-10-10 15:32:24'); |
66
|
|
|
$this->assertEquals('10 Oct 1600 15 32 24', $date->Format('d MMM y H m s')); |
|
|
|
|
67
|
|
|
|
68
|
|
|
$date = DBDatetime::create_field('Datetime', '3000-10-10 15:32:24'); |
69
|
|
|
$this->assertEquals('10 Oct 3000 15 32 24', $date->Format('d MMM y H m s')); |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* Coverage for dates using hindi-numerals |
74
|
|
|
*/ |
75
|
|
|
public function testHindiNumerals() |
76
|
|
|
{ |
77
|
|
|
// Parent locale is english; Can be localised to arabic |
78
|
|
|
$date = DBDatetime::create_field('Datetime', '1600-10-10 15:32:24'); |
79
|
|
|
$this->assertEquals('10 Oct 1600 15 32 24', $date->Format('d MMM y H m s')); |
80
|
|
|
$this->assertEquals('١٠ أكتوبر ١٦٠٠ ١٥ ٣٢ ٢٤', $date->Format('d MMM y H m s', 'ar')); |
81
|
|
|
|
82
|
|
|
// Parent locale is arabic; Datavalue uses ISO date |
83
|
|
|
i18n::set_locale('ar'); |
84
|
|
|
$date = DBDatetime::create_field('Datetime', '1600-10-10 15:32:24'); |
85
|
|
|
$this->assertEquals('١٠ أكتوبر ١٦٠٠ ١٥ ٣٢ ٢٤', $date->Format('d MMM y H m s')); |
86
|
|
|
$this->assertEquals('1600-10-10 15:32:24', $date->getValue()); |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
public function testNice() |
90
|
|
|
{ |
91
|
|
|
$date = DBDatetime::create_field('Datetime', '2001-12-31 22:10:59'); |
92
|
|
|
// note: Some localisation packages exclude the ',' in default medium format |
93
|
|
|
$this->assertRegExp('#31/12/2001(,)? 10:10:59 PM#i', $date->Nice()); |
|
|
|
|
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
public function testDate() |
97
|
|
|
{ |
98
|
|
|
$date = DBDatetime::create_field('Datetime', '2001-12-31 22:10:59'); |
99
|
|
|
$this->assertEquals('31/12/2001', $date->Date()); |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
public function testTime() |
103
|
|
|
{ |
104
|
|
|
$date = DBDatetime::create_field('Datetime', '2001-12-31 22:10:59'); |
105
|
|
|
$this->assertRegexp('#10:10:59 PM#i', $date->Time()); |
|
|
|
|
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
public function testTime24() |
109
|
|
|
{ |
110
|
|
|
$date = DBDatetime::create_field('Datetime', '2001-12-31 22:10:59'); |
111
|
|
|
$this->assertEquals('22:10', $date->Time24()); |
|
|
|
|
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
public function testURLDateTime() |
115
|
|
|
{ |
116
|
|
|
$date = DBDatetime::create_field('Datetime', '2001-12-31 22:10:59'); |
117
|
|
|
$this->assertEquals('2001-12-31%2022%3A10%3A59', $date->URLDateTime()); |
|
|
|
|
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
public function testAgoInPast() |
121
|
|
|
{ |
122
|
|
|
DBDatetime::set_mock_now('2000-12-31 12:00:00'); |
123
|
|
|
|
124
|
|
|
$this->assertEquals( |
125
|
|
|
'10 years ago', |
126
|
|
|
DBDatetime::create_field('Datetime', '1990-12-31 12:00:00')->Ago(), |
|
|
|
|
127
|
|
|
'Exact past match on years' |
128
|
|
|
); |
129
|
|
|
|
130
|
|
|
$this->assertEquals( |
131
|
|
|
'10 years ago', |
132
|
|
|
DBDatetime::create_field('Datetime', '1990-12-30 12:00:00')->Ago(), |
133
|
|
|
'Approximate past match on years' |
134
|
|
|
); |
135
|
|
|
|
136
|
|
|
$this->assertEquals( |
137
|
|
|
'1 year ago', |
138
|
|
|
DBDatetime::create_field('Datetime', '1999-12-30 12:00:12')->Ago(true, 1), |
139
|
|
|
'Approximate past match in singular, significance=1' |
140
|
|
|
); |
141
|
|
|
|
142
|
|
|
$this->assertEquals( |
143
|
|
|
'12 months ago', |
144
|
|
|
DBDatetime::create_field('Datetime', '1999-12-30 12:00:12')->Ago(), |
145
|
|
|
'Approximate past match in singular' |
146
|
|
|
); |
147
|
|
|
|
148
|
|
|
$this->assertEquals( |
149
|
|
|
'50 mins ago', |
150
|
|
|
DBDatetime::create_field('Datetime', '2000-12-31 11:10:11')->Ago(), |
151
|
|
|
'Approximate past match on minutes' |
152
|
|
|
); |
153
|
|
|
|
154
|
|
|
$this->assertEquals( |
155
|
|
|
'59 secs ago', |
156
|
|
|
DBDatetime::create_field('Datetime', '2000-12-31 11:59:01')->Ago(), |
157
|
|
|
'Approximate past match on seconds' |
158
|
|
|
); |
159
|
|
|
|
160
|
|
|
$this->assertEquals( |
161
|
|
|
'less than a minute ago', |
162
|
|
|
DBDatetime::create_field('Datetime', '2000-12-31 11:59:01')->Ago(false), |
163
|
|
|
'Approximate past match on seconds with $includeSeconds=false' |
164
|
|
|
); |
165
|
|
|
|
166
|
|
|
$this->assertEquals( |
167
|
|
|
'1 min ago', |
168
|
|
|
DBDatetime::create_field('Datetime', '2000-12-31 11:58:50')->Ago(false), |
169
|
|
|
'Test between 1 and 2 minutes with includeSeconds=false' |
170
|
|
|
); |
171
|
|
|
|
172
|
|
|
$this->assertEquals( |
173
|
|
|
'70 secs ago', |
174
|
|
|
DBDatetime::create_field('Datetime', '2000-12-31 11:58:50')->Ago(true), |
175
|
|
|
'Test between 1 and 2 minutes with includeSeconds=true' |
176
|
|
|
); |
177
|
|
|
|
178
|
|
|
$this->assertEquals( |
179
|
|
|
'4 mins ago', |
180
|
|
|
DBDatetime::create_field('Datetime', '2000-12-31 11:55:50')->Ago(), |
181
|
|
|
'Past match on minutes' |
182
|
|
|
); |
183
|
|
|
|
184
|
|
|
$this->assertEquals( |
185
|
|
|
'1 hour ago', |
186
|
|
|
DBDatetime::create_field('Datetime', '2000-12-31 10:50:58')->Ago(true, 1), |
187
|
|
|
'Past match on hours, significance=1' |
188
|
|
|
); |
189
|
|
|
|
190
|
|
|
$this->assertEquals( |
191
|
|
|
'3 hours ago', |
192
|
|
|
DBDatetime::create_field('Datetime', '2000-12-31 08:50:58')->Ago(), |
193
|
|
|
'Past match on hours' |
194
|
|
|
); |
195
|
|
|
|
196
|
|
|
DBDatetime::clear_mock_now(); |
197
|
|
|
} |
198
|
|
|
|
199
|
|
|
public function testAgoInFuture() |
200
|
|
|
{ |
201
|
|
|
DBDatetime::set_mock_now('2000-12-31 00:00:00'); |
202
|
|
|
|
203
|
|
|
$this->assertEquals( |
204
|
|
|
'in 10 years', |
205
|
|
|
DBDatetime::create_field('Datetime', '2010-12-31 12:00:00')->Ago(), |
206
|
|
|
'Exact past match on years' |
207
|
|
|
); |
208
|
|
|
|
209
|
|
|
$this->assertEquals( |
210
|
|
|
'in 1 hour', |
211
|
|
|
DBDatetime::create_field('Datetime', '2000-12-31 1:01:05')->Ago(true, 1), |
212
|
|
|
'Approximate past match on minutes, significance=1' |
213
|
|
|
); |
214
|
|
|
|
215
|
|
|
$this->assertEquals( |
216
|
|
|
'in 61 mins', |
217
|
|
|
DBDatetime::create_field('Datetime', '2000-12-31 1:01:05')->Ago(), |
218
|
|
|
'Approximate past match on minutes' |
219
|
|
|
); |
220
|
|
|
|
221
|
|
|
DBDatetime::clear_mock_now(); |
222
|
|
|
} |
223
|
|
|
|
224
|
|
|
public function testRfc3999() |
225
|
|
|
{ |
226
|
|
|
// Dates should be formatted as: 2018-01-24T14:05:53+00:00 |
|
|
|
|
227
|
|
|
$date = DBDatetime::create_field('Datetime', '2010-12-31 16:58:59'); |
228
|
|
|
$this->assertEquals('2010-12-31T16:58:59+00:00', $date->Rfc3339()); |
|
|
|
|
229
|
|
|
} |
230
|
|
|
} |
231
|
|
|
|