Mercurial > hg > Members > shoshi > webvirt
comparison cake/tests/cases/libs/view/helpers/time.test.php @ 0:261e66bd5a0c
hg init
author | Shoshi TAMAKI <shoshi@cr.ie.u-ryukyu.ac.jp> |
---|---|
date | Sun, 24 Jul 2011 21:08:31 +0900 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:261e66bd5a0c |
---|---|
1 <?php | |
2 /** | |
3 * TimeHelperTest file | |
4 * | |
5 * PHP versions 4 and 5 | |
6 * | |
7 * CakePHP(tm) Tests <http://book.cakephp.org/view/1196/Testing> | |
8 * Copyright 2005-2010, Cake Software Foundation, Inc. (http://cakefoundation.org) | |
9 * | |
10 * Licensed under The Open Group Test Suite License | |
11 * Redistributions of files must retain the above copyright notice. | |
12 * | |
13 * @copyright Copyright 2005-2010, Cake Software Foundation, Inc. (http://cakefoundation.org) | |
14 * @link http://book.cakephp.org/view/1196/Testing CakePHP(tm) Tests | |
15 * @package cake | |
16 * @subpackage cake.tests.cases.libs.view.helpers | |
17 * @since CakePHP(tm) v 1.2.0.4206 | |
18 * @license http://www.opensource.org/licenses/opengroup.php The Open Group Test Suite License | |
19 */ | |
20 if (!defined('CAKEPHP_UNIT_TEST_EXECUTION')) { | |
21 define('CAKEPHP_UNIT_TEST_EXECUTION', 1); | |
22 } | |
23 App::import('Helper', 'Time'); | |
24 | |
25 /** | |
26 * TimeHelperTest class | |
27 * | |
28 * @package cake | |
29 * @subpackage cake.tests.cases.libs.view.helpers | |
30 */ | |
31 class TimeHelperTest extends CakeTestCase { | |
32 | |
33 /** | |
34 * setUp method | |
35 * | |
36 * @access public | |
37 * @return void | |
38 */ | |
39 function setUp() { | |
40 $this->Time = new TimeHelper(); | |
41 } | |
42 | |
43 /** | |
44 * tearDown method | |
45 * | |
46 * @access public | |
47 * @return void | |
48 */ | |
49 function tearDown() { | |
50 unset($this->Time); | |
51 } | |
52 | |
53 /** | |
54 * testToQuarter method | |
55 * | |
56 * @access public | |
57 * @return void | |
58 */ | |
59 function testToQuarter() { | |
60 $result = $this->Time->toQuarter('2007-12-25'); | |
61 $this->assertEqual($result, 4); | |
62 | |
63 $result = $this->Time->toQuarter('2007-9-25'); | |
64 $this->assertEqual($result, 3); | |
65 | |
66 $result = $this->Time->toQuarter('2007-3-25'); | |
67 $this->assertEqual($result, 1); | |
68 | |
69 $result = $this->Time->toQuarter('2007-3-25', true); | |
70 $this->assertEqual($result, array('2007-01-01', '2007-03-31')); | |
71 | |
72 $result = $this->Time->toQuarter('2007-5-25', true); | |
73 $this->assertEqual($result, array('2007-04-01', '2007-06-30')); | |
74 | |
75 $result = $this->Time->toQuarter('2007-8-25', true); | |
76 $this->assertEqual($result, array('2007-07-01', '2007-09-30')); | |
77 | |
78 $result = $this->Time->toQuarter('2007-12-25', true); | |
79 $this->assertEqual($result, array('2007-10-01', '2007-12-31')); | |
80 } | |
81 | |
82 /** | |
83 * testTimeAgoInWords method | |
84 * | |
85 * @access public | |
86 * @return void | |
87 */ | |
88 function testTimeAgoInWords() { | |
89 $result = $this->Time->timeAgoInWords(strtotime('+4 months +2 weeks +3 days'), array('end' => '8 years'), true); | |
90 $this->assertEqual($result, '4 months, 2 weeks, 3 days'); | |
91 | |
92 $result = $this->Time->timeAgoInWords(strtotime('+4 months +2 weeks +2 days'), array('end' => '8 years'), true); | |
93 $this->assertEqual($result, '4 months, 2 weeks, 2 days'); | |
94 | |
95 $result = $this->Time->timeAgoInWords(strtotime('+4 months +2 weeks +1 day'), array('end' => '8 years'), true); | |
96 $this->assertEqual($result, '4 months, 2 weeks, 1 day'); | |
97 | |
98 $result = $this->Time->timeAgoInWords(strtotime('+3 months +2 weeks +1 day'), array('end' => '8 years'), true); | |
99 $this->assertEqual($result, '3 months, 2 weeks, 1 day'); | |
100 | |
101 $result = $this->Time->timeAgoInWords(strtotime('+3 months +2 weeks'), array('end' => '8 years'), true); | |
102 $this->assertEqual($result, '3 months, 2 weeks'); | |
103 | |
104 $result = $this->Time->timeAgoInWords(strtotime('+3 months +1 week +6 days'), array('end' => '8 years'), true); | |
105 $this->assertEqual($result, '3 months, 1 week, 6 days'); | |
106 | |
107 $result = $this->Time->timeAgoInWords(strtotime('+2 months +2 weeks +1 day'), array('end' => '8 years'), true); | |
108 $this->assertEqual($result, '2 months, 2 weeks, 1 day'); | |
109 | |
110 $result = $this->Time->timeAgoInWords(strtotime('+2 months +2 weeks'), array('end' => '8 years'), true); | |
111 $this->assertEqual($result, '2 months, 2 weeks'); | |
112 | |
113 $result = $this->Time->timeAgoInWords(strtotime('+2 months +1 week +6 days'), array('end' => '8 years'), true); | |
114 $this->assertEqual($result, '2 months, 1 week, 6 days'); | |
115 | |
116 $result = $this->Time->timeAgoInWords(strtotime('+1 month +1 week +6 days'), array('end' => '8 years'), true); | |
117 $this->assertEqual($result, '1 month, 1 week, 6 days'); | |
118 | |
119 for($i = 0; $i < 200; $i ++) { | |
120 $years = mt_rand(0, 3); | |
121 $months = mt_rand(0, 11); | |
122 $weeks = mt_rand(0, 3); | |
123 $days = mt_rand(0, 6); | |
124 $hours = 0; | |
125 $minutes = 0; | |
126 $seconds = 0; | |
127 $relative_date = ''; | |
128 | |
129 if ($years > 0) { | |
130 // years and months and days | |
131 $relative_date .= ($relative_date ? ', -' : '-') . $years . ' year' . ($years > 1 ? 's' : ''); | |
132 $relative_date .= $months > 0 ? ($relative_date ? ', -' : '-') . $months . ' month' . ($months > 1 ? 's' : '') : ''; | |
133 $relative_date .= $weeks > 0 ? ($relative_date ? ', -' : '-') . $weeks . ' week' . ($weeks > 1 ? 's' : '') : ''; | |
134 $relative_date .= $days > 0 ? ($relative_date ? ', -' : '-') . $days . ' day' . ($days > 1 ? 's' : '') : ''; | |
135 } elseif (abs($months) > 0) { | |
136 // months, weeks and days | |
137 $relative_date .= ($relative_date ? ', -' : '-') . $months . ' month' . ($months > 1 ? 's' : ''); | |
138 $relative_date .= $weeks > 0 ? ($relative_date ? ', -' : '-') . $weeks . ' week' . ($weeks > 1 ? 's' : '') : ''; | |
139 $relative_date .= $days > 0 ? ($relative_date ? ', -' : '-') . $days . ' day' . ($days > 1 ? 's' : '') : ''; | |
140 } elseif (abs($weeks) > 0) { | |
141 // weeks and days | |
142 $relative_date .= ($relative_date ? ', -' : '-') . $weeks . ' week' . ($weeks > 1 ? 's' : ''); | |
143 $relative_date .= $days > 0 ? ($relative_date ? ', -' : '-') . $days . ' day' . ($days > 1 ? 's' : '') : ''; | |
144 } elseif (abs($days) > 0) { | |
145 // days and hours | |
146 $relative_date .= ($relative_date ? ', -' : '-') . $days . ' day' . ($days > 1 ? 's' : ''); | |
147 $relative_date .= $hours > 0 ? ($relative_date ? ', -' : '-') . $hours . ' hour' . ($hours > 1 ? 's' : '') : ''; | |
148 } elseif (abs($hours) > 0) { | |
149 // hours and minutes | |
150 $relative_date .= ($relative_date ? ', -' : '-') . $hours . ' hour' . ($hours > 1 ? 's' : ''); | |
151 $relative_date .= $minutes > 0 ? ($relative_date ? ', -' : '-') . $minutes . ' minute' . ($minutes > 1 ? 's' : '') : ''; | |
152 } elseif (abs($minutes) > 0) { | |
153 // minutes only | |
154 $relative_date .= ($relative_date ? ', -' : '-') . $minutes . ' minute' . ($minutes > 1 ? 's' : ''); | |
155 } else { | |
156 // seconds only | |
157 $relative_date .= ($relative_date ? ', -' : '-') . $seconds . ' second' . ($seconds != 1 ? 's' : ''); | |
158 } | |
159 | |
160 if (date('j/n/y', strtotime(str_replace(',','',$relative_date))) != '1/1/70') { | |
161 $result = $this->Time->timeAgoInWords(strtotime(str_replace(',','',$relative_date)), array('end' => '8 years'), true); | |
162 if ($relative_date == '0 seconds') { | |
163 $relative_date = '0 seconds ago'; | |
164 } | |
165 | |
166 $relative_date = str_replace('-', '', $relative_date) . ' ago'; | |
167 $this->assertEqual($result, $relative_date); | |
168 | |
169 } | |
170 } | |
171 | |
172 for ($i = 0; $i < 200; $i ++) { | |
173 $years = mt_rand(0, 3); | |
174 $months = mt_rand(0, 11); | |
175 $weeks = mt_rand(0, 3); | |
176 $days = mt_rand(0, 6); | |
177 $hours = 0; | |
178 $minutes = 0; | |
179 $seconds = 0; | |
180 | |
181 $relative_date = ''; | |
182 | |
183 if ($years > 0) { | |
184 // years and months and days | |
185 $relative_date .= ($relative_date ? ', ' : '') . $years . ' year' . ($years > 1 ? 's' : ''); | |
186 $relative_date .= $months > 0 ? ($relative_date ? ', ' : '') . $months . ' month' . ($months > 1 ? 's' : '') : ''; | |
187 $relative_date .= $weeks > 0 ? ($relative_date ? ', ' : '') . $weeks . ' week' . ($weeks > 1 ? 's' : '') : ''; | |
188 $relative_date .= $days > 0 ? ($relative_date ? ', ' : '') . $days . ' day' . ($days > 1 ? 's' : '') : ''; | |
189 } elseif (abs($months) > 0) { | |
190 // months, weeks and days | |
191 $relative_date .= ($relative_date ? ', ' : '') . $months . ' month' . ($months > 1 ? 's' : ''); | |
192 $relative_date .= $weeks > 0 ? ($relative_date ? ', ' : '') . $weeks . ' week' . ($weeks > 1 ? 's' : '') : ''; | |
193 $relative_date .= $days > 0 ? ($relative_date ? ', ' : '') . $days . ' day' . ($days > 1 ? 's' : '') : ''; | |
194 } elseif (abs($weeks) > 0) { | |
195 // weeks and days | |
196 $relative_date .= ($relative_date ? ', ' : '') . $weeks . ' week' . ($weeks > 1 ? 's' : ''); | |
197 $relative_date .= $days > 0 ? ($relative_date ? ', ' : '') . $days . ' day' . ($days > 1 ? 's' : '') : ''; | |
198 } elseif (abs($days) > 0) { | |
199 // days and hours | |
200 $relative_date .= ($relative_date ? ', ' : '') . $days . ' day' . ($days > 1 ? 's' : ''); | |
201 $relative_date .= $hours > 0 ? ($relative_date ? ', ' : '') . $hours . ' hour' . ($hours > 1 ? 's' : '') : ''; | |
202 } elseif (abs($hours) > 0) { | |
203 // hours and minutes | |
204 $relative_date .= ($relative_date ? ', ' : '') . $hours . ' hour' . ($hours > 1 ? 's' : ''); | |
205 $relative_date .= $minutes > 0 ? ($relative_date ? ', ' : '') . $minutes . ' minute' . ($minutes > 1 ? 's' : '') : ''; | |
206 } elseif (abs($minutes) > 0) { | |
207 // minutes only | |
208 $relative_date .= ($relative_date ? ', ' : '') . $minutes . ' minute' . ($minutes > 1 ? 's' : ''); | |
209 } else { | |
210 // seconds only | |
211 $relative_date .= ($relative_date ? ', ' : '') . $seconds . ' second' . ($seconds != 1 ? 's' : ''); | |
212 } | |
213 | |
214 if (date('j/n/y', strtotime(str_replace(',','',$relative_date))) != '1/1/70') { | |
215 $result = $this->Time->timeAgoInWords(strtotime(str_replace(',','',$relative_date)), array('end' => '8 years'), true); | |
216 if ($relative_date == '0 seconds') { | |
217 $relative_date = '0 seconds ago'; | |
218 } | |
219 | |
220 $relative_date = str_replace('-', '', $relative_date) . ''; | |
221 $this->assertEqual($result, $relative_date); | |
222 } | |
223 } | |
224 | |
225 $result = $this->Time->timeAgoInWords(strtotime('-2 years -5 months -2 days'), array('end' => '3 years'), true); | |
226 $this->assertEqual($result, '2 years, 5 months, 2 days ago'); | |
227 | |
228 $result = $this->Time->timeAgoInWords('2007-9-25'); | |
229 $this->assertEqual($result, 'on 25/9/07'); | |
230 | |
231 $result = $this->Time->timeAgoInWords('2007-9-25', 'Y-m-d'); | |
232 $this->assertEqual($result, 'on 2007-09-25'); | |
233 | |
234 $result = $this->Time->timeAgoInWords('2007-9-25', 'Y-m-d', true); | |
235 $this->assertEqual($result, 'on 2007-09-25'); | |
236 | |
237 $result = $this->Time->timeAgoInWords(strtotime('-2 weeks -2 days'), 'Y-m-d', false); | |
238 $this->assertEqual($result, '2 weeks, 2 days ago'); | |
239 | |
240 $result = $this->Time->timeAgoInWords(strtotime('+2 weeks +2 days'), 'Y-m-d', true); | |
241 $this->assertPattern('/^2 weeks, [1|2] day(s)?$/', $result); | |
242 | |
243 $result = $this->Time->timeAgoInWords(strtotime('+2 months +2 days'), array('end' => '1 month')); | |
244 $this->assertEqual($result, 'on ' . date('j/n/y', strtotime('+2 months +2 days'))); | |
245 | |
246 $result = $this->Time->timeAgoInWords(strtotime('+2 months +2 days'), array('end' => '3 month')); | |
247 $this->assertPattern('/2 months/', $result); | |
248 | |
249 $result = $this->Time->timeAgoInWords(strtotime('+2 months +12 days'), array('end' => '3 month')); | |
250 $this->assertPattern('/2 months, 1 week/', $result); | |
251 | |
252 $result = $this->Time->timeAgoInWords(strtotime('+3 months +5 days'), array('end' => '4 month')); | |
253 $this->assertEqual($result, '3 months, 5 days'); | |
254 | |
255 $result = $this->Time->timeAgoInWords(strtotime('-2 months -2 days'), array('end' => '3 month')); | |
256 $this->assertEqual($result, '2 months, 2 days ago'); | |
257 | |
258 $result = $this->Time->timeAgoInWords(strtotime('-2 months -2 days'), array('end' => '3 month')); | |
259 $this->assertEqual($result, '2 months, 2 days ago'); | |
260 | |
261 $result = $this->Time->timeAgoInWords(strtotime('+2 months +2 days'), array('end' => '3 month')); | |
262 $this->assertPattern('/2 months/', $result); | |
263 | |
264 $result = $this->Time->timeAgoInWords(strtotime('+2 months +2 days'), array('end' => '1 month', 'format' => 'Y-m-d')); | |
265 $this->assertEqual($result, 'on ' . date('Y-m-d', strtotime('+2 months +2 days'))); | |
266 | |
267 $result = $this->Time->timeAgoInWords(strtotime('-2 months -2 days'), array('end' => '1 month', 'format' => 'Y-m-d')); | |
268 $this->assertEqual($result, 'on ' . date('Y-m-d', strtotime('-2 months -2 days'))); | |
269 | |
270 $result = $this->Time->timeAgoInWords(strtotime('-13 months -5 days'), array('end' => '2 years')); | |
271 $this->assertEqual($result, '1 year, 1 month, 5 days ago'); | |
272 | |
273 $fourHours = $this->Time->timeAgoInWords(strtotime('-5 days -2 hours'), array('userOffset' => -4)); | |
274 $result = $this->Time->timeAgoInWords(strtotime('-5 days -2 hours'), array('userOffset' => 4)); | |
275 $this->assertEqual($fourHours, $result); | |
276 | |
277 $result = $this->Time->timeAgoInWords(strtotime('-2 hours')); | |
278 $expected = '2 hours ago'; | |
279 $this->assertEqual($expected, $result); | |
280 | |
281 $result = $this->Time->timeAgoInWords(strtotime('-12 minutes')); | |
282 $expected = '12 minutes ago'; | |
283 $this->assertEqual($expected, $result); | |
284 | |
285 $result = $this->Time->timeAgoInWords(strtotime('-12 seconds')); | |
286 $expected = '12 seconds ago'; | |
287 $this->assertEqual($expected, $result); | |
288 | |
289 $time = strtotime('-3 years -12 months'); | |
290 $result = $this->Time->timeAgoInWords($time); | |
291 $expected = 'on ' . date('j/n/y', $time); | |
292 $this->assertEqual($expected, $result); | |
293 } | |
294 | |
295 /** | |
296 * testRelative method | |
297 * | |
298 * @access public | |
299 * @return void | |
300 */ | |
301 function testRelative() { | |
302 $result = $this->Time->relativeTime('-1 week'); | |
303 $this->assertEqual($result, '1 week ago'); | |
304 $result = $this->Time->relativeTime('+1 week'); | |
305 $this->assertEqual($result, '1 week'); | |
306 } | |
307 | |
308 /** | |
309 * testNice method | |
310 * | |
311 * @access public | |
312 * @return void | |
313 */ | |
314 function testNice() { | |
315 $time = time() + 2 * DAY; | |
316 $this->assertEqual(date('D, M jS Y, H:i', $time), $this->Time->nice($time)); | |
317 | |
318 $time = time() - 2 * DAY; | |
319 $this->assertEqual(date('D, M jS Y, H:i', $time), $this->Time->nice($time)); | |
320 | |
321 $time = time(); | |
322 $this->assertEqual(date('D, M jS Y, H:i', $time), $this->Time->nice($time)); | |
323 | |
324 $time = 0; | |
325 $this->assertEqual(date('D, M jS Y, H:i', time()), $this->Time->nice($time)); | |
326 | |
327 $time = null; | |
328 $this->assertEqual(date('D, M jS Y, H:i', time()), $this->Time->nice($time)); | |
329 } | |
330 | |
331 /** | |
332 * testNiceShort method | |
333 * | |
334 * @access public | |
335 * @return void | |
336 */ | |
337 function testNiceShort() { | |
338 $time = time() + 2 * DAY; | |
339 if (date('Y', $time) == date('Y')) { | |
340 $this->assertEqual(date('M jS, H:i', $time), $this->Time->niceShort($time)); | |
341 } else { | |
342 $this->assertEqual(date('M jS Y, H:i', $time), $this->Time->niceShort($time)); | |
343 } | |
344 | |
345 $time = time(); | |
346 $this->assertEqual('Today, ' . date('H:i', $time), $this->Time->niceShort($time)); | |
347 | |
348 $time = time() - DAY; | |
349 $this->assertEqual('Yesterday, ' . date('H:i', $time), $this->Time->niceShort($time)); | |
350 } | |
351 | |
352 /** | |
353 * testDaysAsSql method | |
354 * | |
355 * @access public | |
356 * @return void | |
357 */ | |
358 function testDaysAsSql() { | |
359 $begin = time(); | |
360 $end = time() + DAY; | |
361 $field = 'my_field'; | |
362 $expected = '(my_field >= \''.date('Y-m-d', $begin).' 00:00:00\') AND (my_field <= \''.date('Y-m-d', $end).' 23:59:59\')'; | |
363 $this->assertEqual($expected, $this->Time->daysAsSql($begin, $end, $field)); | |
364 } | |
365 | |
366 /** | |
367 * testDayAsSql method | |
368 * | |
369 * @access public | |
370 * @return void | |
371 */ | |
372 function testDayAsSql() { | |
373 $time = time(); | |
374 $field = 'my_field'; | |
375 $expected = '(my_field >= \''.date('Y-m-d', $time).' 00:00:00\') AND (my_field <= \''.date('Y-m-d', $time).' 23:59:59\')'; | |
376 $this->assertEqual($expected, $this->Time->dayAsSql($time, $field)); | |
377 } | |
378 | |
379 /** | |
380 * testToUnix method | |
381 * | |
382 * @access public | |
383 * @return void | |
384 */ | |
385 function testToUnix() { | |
386 $this->assertEqual(time(), $this->Time->toUnix(time())); | |
387 $this->assertEqual(strtotime('+1 day'), $this->Time->toUnix('+1 day')); | |
388 $this->assertEqual(strtotime('+0 days'), $this->Time->toUnix('+0 days')); | |
389 $this->assertEqual(strtotime('-1 days'), $this->Time->toUnix('-1 days')); | |
390 $this->assertEqual(false, $this->Time->toUnix('')); | |
391 $this->assertEqual(false, $this->Time->toUnix(null)); | |
392 } | |
393 | |
394 /** | |
395 * testToAtom method | |
396 * | |
397 * @access public | |
398 * @return void | |
399 */ | |
400 function testToAtom() { | |
401 $this->assertEqual(date('Y-m-d\TH:i:s\Z'), $this->Time->toAtom(time())); | |
402 } | |
403 | |
404 /** | |
405 * testToRss method | |
406 * | |
407 * @access public | |
408 * @return void | |
409 */ | |
410 function testToRss() { | |
411 $this->assertEqual(date('r'), $this->Time->toRss(time())); | |
412 } | |
413 | |
414 /** | |
415 * testFormat method | |
416 * | |
417 * @access public | |
418 * @return void | |
419 */ | |
420 function testFormat() { | |
421 $format = 'D-M-Y'; | |
422 $arr = array(time(), strtotime('+1 days'), strtotime('+1 days'), strtotime('+0 days')); | |
423 foreach ($arr as $val) { | |
424 $this->assertEqual(date($format, $val), $this->Time->format($format, $val)); | |
425 } | |
426 | |
427 $result = $this->Time->format('Y-m-d', null, 'never'); | |
428 $this->assertEqual($result, 'never'); | |
429 } | |
430 | |
431 /** | |
432 * testOfGmt method | |
433 * | |
434 * @access public | |
435 * @return void | |
436 */ | |
437 function testGmt() { | |
438 $hour = 3; | |
439 $min = 4; | |
440 $sec = 2; | |
441 $month = 5; | |
442 $day = 14; | |
443 $year = 2007; | |
444 $time = mktime($hour, $min, $sec, $month, $day, $year); | |
445 $expected = gmmktime($hour, $min, $sec, $month, $day, $year); | |
446 $this->assertEqual($expected, $this->Time->gmt(date('Y-n-j G:i:s', $time))); | |
447 | |
448 $hour = date('H'); | |
449 $min = date('i'); | |
450 $sec = date('s'); | |
451 $month = date('m'); | |
452 $day = date('d'); | |
453 $year = date('Y'); | |
454 $expected = gmmktime($hour, $min, $sec, $month, $day, $year); | |
455 $this->assertEqual($expected, $this->Time->gmt(null)); | |
456 } | |
457 | |
458 /** | |
459 * testIsToday method | |
460 * | |
461 * @access public | |
462 * @return void | |
463 */ | |
464 function testIsToday() { | |
465 $result = $this->Time->isToday('+1 day'); | |
466 $this->assertFalse($result); | |
467 $result = $this->Time->isToday('+1 days'); | |
468 $this->assertFalse($result); | |
469 $result = $this->Time->isToday('+0 day'); | |
470 $this->assertTrue($result); | |
471 $result = $this->Time->isToday('-1 day'); | |
472 $this->assertFalse($result); | |
473 } | |
474 | |
475 /** | |
476 * testIsThisWeek method | |
477 * | |
478 * @access public | |
479 * @return void | |
480 */ | |
481 function testIsThisWeek() { | |
482 // A map of days which goes from -1 day of week to +1 day of week | |
483 $map = array( | |
484 'Mon' => array(-1, 7), 'Tue' => array(-2, 6), 'Wed' => array(-3, 5), | |
485 'Thu' => array(-4, 4), 'Fri' => array(-5, 3), 'Sat' => array(-6, 2), | |
486 'Sun' => array(-7, 1) | |
487 ); | |
488 $days = $map[date('D')]; | |
489 | |
490 for ($day = $days[0] + 1; $day < $days[1]; $day++) { | |
491 $this->assertTrue($this->Time->isThisWeek(($day > 0 ? '+' : '') . $day . ' days')); | |
492 } | |
493 $this->assertFalse($this->Time->isThisWeek($days[0] . ' days')); | |
494 $this->assertFalse($this->Time->isThisWeek('+' . $days[1] . ' days')); | |
495 } | |
496 | |
497 /** | |
498 * testIsThisMonth method | |
499 * | |
500 * @access public | |
501 * @return void | |
502 */ | |
503 function testIsThisMonth() { | |
504 $result = $this->Time->isThisMonth('+0 day'); | |
505 $this->assertTrue($result); | |
506 $result = $this->Time->isThisMonth($time = mktime(0, 0, 0, date('m'), mt_rand(1, 28), date('Y'))); | |
507 $this->assertTrue($result); | |
508 $result = $this->Time->isThisMonth(mktime(0, 0, 0, date('m'), mt_rand(1, 28), date('Y') - mt_rand(1, 12))); | |
509 $this->assertFalse($result); | |
510 $result = $this->Time->isThisMonth(mktime(0, 0, 0, date('m'), mt_rand(1, 28), date('Y') + mt_rand(1, 12))); | |
511 $this->assertFalse($result); | |
512 | |
513 } | |
514 | |
515 /** | |
516 * testIsThisYear method | |
517 * | |
518 * @access public | |
519 * @return void | |
520 */ | |
521 function testIsThisYear() { | |
522 $result = $this->Time->isThisYear('+0 day'); | |
523 $this->assertTrue($result); | |
524 $result = $this->Time->isThisYear(mktime(0, 0, 0, mt_rand(1, 12), mt_rand(1, 28), date('Y'))); | |
525 $this->assertTrue($result); | |
526 } | |
527 /** | |
528 * testWasYesterday method | |
529 * | |
530 * @access public | |
531 * @return void | |
532 */ | |
533 function testWasYesterday() { | |
534 $result = $this->Time->wasYesterday('+1 day'); | |
535 $this->assertFalse($result); | |
536 $result = $this->Time->wasYesterday('+1 days'); | |
537 $this->assertFalse($result); | |
538 $result = $this->Time->wasYesterday('+0 day'); | |
539 $this->assertFalse($result); | |
540 $result = $this->Time->wasYesterday('-1 day'); | |
541 $this->assertTrue($result); | |
542 $result = $this->Time->wasYesterday('-1 days'); | |
543 $this->assertTrue($result); | |
544 $result = $this->Time->wasYesterday('-2 days'); | |
545 $this->assertFalse($result); | |
546 } | |
547 /** | |
548 * testIsTomorrow method | |
549 * | |
550 * @access public | |
551 * @return void | |
552 */ | |
553 function testIsTomorrow() { | |
554 $result = $this->Time->isTomorrow('+1 day'); | |
555 $this->assertTrue($result); | |
556 $result = $this->Time->isTomorrow('+1 days'); | |
557 $this->assertTrue($result); | |
558 $result = $this->Time->isTomorrow('+0 day'); | |
559 $this->assertFalse($result); | |
560 $result = $this->Time->isTomorrow('-1 day'); | |
561 $this->assertFalse($result); | |
562 } | |
563 | |
564 /** | |
565 * testWasWithinLast method | |
566 * | |
567 * @access public | |
568 * @return void | |
569 */ | |
570 function testWasWithinLast() { | |
571 $this->assertTrue($this->Time->wasWithinLast('1 day', '-1 day')); | |
572 $this->assertTrue($this->Time->wasWithinLast('1 week', '-1 week')); | |
573 $this->assertTrue($this->Time->wasWithinLast('1 year', '-1 year')); | |
574 $this->assertTrue($this->Time->wasWithinLast('1 second', '-1 second')); | |
575 $this->assertTrue($this->Time->wasWithinLast('1 minute', '-1 minute')); | |
576 $this->assertTrue($this->Time->wasWithinLast('1 year', '-1 year')); | |
577 $this->assertTrue($this->Time->wasWithinLast('1 month', '-1 month')); | |
578 $this->assertTrue($this->Time->wasWithinLast('1 day', '-1 day')); | |
579 | |
580 $this->assertTrue($this->Time->wasWithinLast('1 week', '-1 day')); | |
581 $this->assertTrue($this->Time->wasWithinLast('2 week', '-1 week')); | |
582 $this->assertFalse($this->Time->wasWithinLast('1 second', '-1 year')); | |
583 $this->assertTrue($this->Time->wasWithinLast('10 minutes', '-1 second')); | |
584 $this->assertTrue($this->Time->wasWithinLast('23 minutes', '-1 minute')); | |
585 $this->assertFalse($this->Time->wasWithinLast('0 year', '-1 year')); | |
586 $this->assertTrue($this->Time->wasWithinLast('13 month', '-1 month')); | |
587 $this->assertTrue($this->Time->wasWithinLast('2 days', '-1 day')); | |
588 | |
589 $this->assertFalse($this->Time->wasWithinLast('1 week', '-2 weeks')); | |
590 $this->assertFalse($this->Time->wasWithinLast('1 second', '-2 seconds')); | |
591 $this->assertFalse($this->Time->wasWithinLast('1 day', '-2 days')); | |
592 $this->assertFalse($this->Time->wasWithinLast('1 hour', '-2 hours')); | |
593 $this->assertFalse($this->Time->wasWithinLast('1 month', '-2 months')); | |
594 $this->assertFalse($this->Time->wasWithinLast('1 year', '-2 years')); | |
595 | |
596 $this->assertFalse($this->Time->wasWithinLast('1 day', '-2 weeks')); | |
597 $this->assertFalse($this->Time->wasWithinLast('1 day', '-2 days')); | |
598 $this->assertFalse($this->Time->wasWithinLast('0 days', '-2 days')); | |
599 $this->assertTrue($this->Time->wasWithinLast('1 hour', '-20 seconds')); | |
600 $this->assertTrue($this->Time->wasWithinLast('1 year', '-60 minutes -30 seconds')); | |
601 $this->assertTrue($this->Time->wasWithinLast('3 years', '-2 months')); | |
602 $this->assertTrue($this->Time->wasWithinLast('5 months', '-4 months')); | |
603 | |
604 $this->assertTrue($this->Time->wasWithinLast('5 ', '-3 days')); | |
605 $this->assertTrue($this->Time->wasWithinLast('1 ', '-1 hour')); | |
606 $this->assertTrue($this->Time->wasWithinLast('1 ', '-1 minute')); | |
607 $this->assertTrue($this->Time->wasWithinLast('1 ', '-23 hours -59 minutes -59 seconds')); | |
608 } | |
609 /** | |
610 * testUserOffset method | |
611 * | |
612 * @access public | |
613 * @return void | |
614 */ | |
615 function testUserOffset() { | |
616 if ($this->skipIf(!class_exists('DateTimeZone'), '%s DateTimeZone class not available.')) { | |
617 return; | |
618 } | |
619 | |
620 | |
621 $timezoneServer = new DateTimeZone(date_default_timezone_get()); | |
622 $timeServer = new DateTime('now', $timezoneServer); | |
623 $yourTimezone = $timezoneServer->getOffset($timeServer) / HOUR; | |
624 | |
625 $expected = time(); | |
626 $result = $this->Time->fromString(time(), $yourTimezone); | |
627 $this->assertEqual($result, $expected); | |
628 } | |
629 | |
630 /** | |
631 * test fromString() | |
632 * | |
633 * @access public | |
634 * @return void | |
635 */ | |
636 function testFromString() { | |
637 $result = $this->Time->fromString(''); | |
638 $this->assertFalse($result); | |
639 | |
640 $result = $this->Time->fromString(0, 0); | |
641 $this->assertFalse($result); | |
642 | |
643 $result = $this->Time->fromString('+1 hour'); | |
644 $expected = strtotime('+1 hour'); | |
645 $this->assertEqual($result, $expected); | |
646 | |
647 $timezone = date('Z', time()); | |
648 $result = $this->Time->fromString('+1 hour', $timezone); | |
649 $expected = $this->Time->convert(strtotime('+1 hour'), $timezone); | |
650 $this->assertEqual($result, $expected); | |
651 } | |
652 | |
653 /** | |
654 * test converting time specifiers using a time definition localfe file | |
655 * | |
656 * @access public | |
657 * @return void | |
658 */ | |
659 function testConvertSpecifiers() { | |
660 App::build(array( | |
661 'locales' => array(TEST_CAKE_CORE_INCLUDE_PATH . 'tests' . DS . 'test_app' . DS . 'locale' . DS) | |
662 ), true); | |
663 Configure::write('Config.language', 'time_test'); | |
664 $time = strtotime('Thu Jan 14 11:43:39 2010'); | |
665 | |
666 $result = $this->Time->convertSpecifiers('%a', $time); | |
667 $expected = 'jue'; | |
668 $this->assertEqual($result, $expected); | |
669 | |
670 $result = $this->Time->convertSpecifiers('%A', $time); | |
671 $expected = 'jueves'; | |
672 $this->assertEqual($result, $expected); | |
673 | |
674 $result = $this->Time->convertSpecifiers('%c', $time); | |
675 $expected = 'jue %d ene %Y %H:%M:%S %Z'; | |
676 $this->assertEqual($result, $expected); | |
677 | |
678 $result = $this->Time->convertSpecifiers('%C', $time); | |
679 $expected = '20'; | |
680 $this->assertEqual($result, $expected); | |
681 | |
682 $result = $this->Time->convertSpecifiers('%D', $time); | |
683 $expected = '%m/%d/%y'; | |
684 $this->assertEqual($result, $expected); | |
685 | |
686 $result = $this->Time->convertSpecifiers('%b', $time); | |
687 $expected = 'ene'; | |
688 $this->assertEqual($result, $expected); | |
689 | |
690 $result = $this->Time->convertSpecifiers('%h', $time); | |
691 $expected = 'ene'; | |
692 $this->assertEqual($result, $expected); | |
693 | |
694 $result = $this->Time->convertSpecifiers('%B', $time); | |
695 $expected = 'enero'; | |
696 $this->assertEqual($result, $expected); | |
697 | |
698 $result = $this->Time->convertSpecifiers('%n', $time); | |
699 $expected = "\n"; | |
700 $this->assertEqual($result, $expected); | |
701 | |
702 $result = $this->Time->convertSpecifiers('%n', $time); | |
703 $expected = "\n"; | |
704 $this->assertEqual($result, $expected); | |
705 | |
706 $result = $this->Time->convertSpecifiers('%p', $time); | |
707 $expected = 'AM'; | |
708 $this->assertEqual($result, $expected); | |
709 | |
710 $result = $this->Time->convertSpecifiers('%P', $time); | |
711 $expected = 'am'; | |
712 $this->assertEqual($result, $expected); | |
713 | |
714 $result = $this->Time->convertSpecifiers('%r', $time); | |
715 $expected = '%I:%M:%S AM'; | |
716 $this->assertEqual($result, $expected); | |
717 | |
718 $result = $this->Time->convertSpecifiers('%R', $time); | |
719 $expected = '11:43'; | |
720 $this->assertEqual($result, $expected); | |
721 | |
722 $result = $this->Time->convertSpecifiers('%t', $time); | |
723 $expected = "\t"; | |
724 $this->assertEqual($result, $expected); | |
725 | |
726 $result = $this->Time->convertSpecifiers('%T', $time); | |
727 $expected = '%H:%M:%S'; | |
728 $this->assertEqual($result, $expected); | |
729 | |
730 $result = $this->Time->convertSpecifiers('%u', $time); | |
731 $expected = 4; | |
732 $this->assertEqual($result, $expected); | |
733 | |
734 $result = $this->Time->convertSpecifiers('%x', $time); | |
735 $expected = '%d/%m/%y'; | |
736 $this->assertEqual($result, $expected); | |
737 | |
738 $result = $this->Time->convertSpecifiers('%X', $time); | |
739 $expected = '%H:%M:%S'; | |
740 $this->assertEqual($result, $expected); | |
741 } | |
742 | |
743 /** | |
744 * test convert %e on windows. | |
745 * | |
746 * @return void | |
747 */ | |
748 function testConvertPercentE() { | |
749 if ($this->skipIf(DS !== '\\', 'Cannot run windows tests on non-windows OS')) { | |
750 return; | |
751 } | |
752 $time = strtotime('Thu Jan 14 11:43:39 2010'); | |
753 $result = $this->Time->convertSpecifiers('%e', $time); | |
754 $expected = '14'; | |
755 $this->assertEqual($result, $expected); | |
756 | |
757 $result = $this->Time->convertSpecifiers('%e', strtotime('2011-01-01')); | |
758 $expected = ' 1'; | |
759 $this->assertEqual($result, $expected); | |
760 } | |
761 | |
762 /** | |
763 * test formatting dates taking in account preferred i18n locale file | |
764 * | |
765 * @access public | |
766 * @return void | |
767 */ | |
768 function testI18nFormat() { | |
769 App::build(array( | |
770 'locales' => array(TEST_CAKE_CORE_INCLUDE_PATH . 'tests' . DS . 'test_app' . DS . 'locale' . DS) | |
771 ), true); | |
772 Configure::write('Config.language', 'time_test'); | |
773 $time = strtotime('Thu Jan 14 13:59:28 2010'); | |
774 | |
775 $result = $this->Time->i18nFormat($time); | |
776 $expected = '14/01/10'; | |
777 $this->assertEqual($result, $expected); | |
778 | |
779 $result = $this->Time->i18nFormat($time, '%c'); | |
780 $expected = 'jue 14 ene 2010 13:59:28 ' . strftime('%Z', $time); | |
781 $this->assertEqual($result, $expected); | |
782 | |
783 $result = $this->Time->i18nFormat($time, 'Time is %r, and date is %x'); | |
784 $expected = 'Time is 01:59:28 PM, and date is 14/01/10'; | |
785 $this->assertEqual($result, $expected); | |
786 | |
787 $result = $this->Time->i18nFormat('invalid date', '%x', 'Date invalid'); | |
788 $expected = 'Date invalid'; | |
789 $this->assertEqual($result, $expected); | |
790 } | |
791 | |
792 /** | |
793 * test new format() syntax which inverts first and secod parameters | |
794 * | |
795 * @access public | |
796 * @return void | |
797 */ | |
798 function testFormatNewSyntax() { | |
799 $time = time(); | |
800 $this->assertEqual($this->Time->format($time), $this->Time->i18nFormat($time)); | |
801 $this->assertEqual($this->Time->format($time, '%c'), $this->Time->i18nFormat($time, '%c')); | |
802 } | |
803 } |