]> CyberLeo.Net >> Repos - Github/sugarcrm.git/blob - tests/include/TimeDateTest.php
Added unit tests.
[Github/sugarcrm.git] / tests / include / TimeDateTest.php
1 <?php
2 require_once 'include/TimeDate.php';
3
4 class TimeDateTest extends Sugar_PHPUnit_Framework_TestCase
5 {
6         /**
7          * @var TimeDate
8          */
9         protected $time_date;
10
11         const DEFAULT_TIME_FORMAT = 'H:i';
12
13         protected $date_tests = array(
14                 array("db" => '2005-10-25 07:00:00', "df" => 'd-m-Y', 'tz' => 'America/Los_Angeles', "display" => '25-10-2005', "dbdate" => "2005-10-25 00:00:00"),
15                 // add times
16                 array("db" => '2005-10-26 06:42:00', "df" => 'd-m-Y', "tf" => 'h.iA', 'tz' => 'America/Los_Angeles', "display" => '25-10-2005 11.42PM', "dbdate" => "2005-10-25 23:42:00"),
17                 // GMT+0 timezone
18                 array("db" => '2005-11-25 00:00:00', "df" => 'd-m-Y', 'tz' => 'Europe/London', "display" => '25-11-2005', "dbdate" => "2005-11-25 00:00:00"),
19                 // GMT+1
20                 array("db" => '2005-11-24 23:00:00', "dbdate" => "2005-11-25", "df" => 'd;m;Y', 'tz' => 'Europe/Oslo', "display" => '25;11;2005', "dbdate" => "2005-11-25 00:00:00"),
21                 // DST in effect
22                 array("db" => '2005-10-24 23:00:00', "dbdate" => "2005-10-25", "df" => 'd-m-Y', 'tz' => 'Europe/London', "display" => '25-10-2005', "dbdate" => "2005-10-25 00:00:00"),
23                 // different format
24                 array("db" => '1997-10-25 07:00:00', "df" => 'Y-m-d', 'tz' => 'America/Los_Angeles', "display" => '1997-10-25', "dbdate" => "1997-10-25 00:00:00"),
25                 array("db" => '1997-01-25 00:00:00', "df" => 'm-d-Y', 'tz' => 'Europe/London', "display" => '01-25-1997', "dbdate" => "1997-01-25 00:00:00"),
26                 // with times
27                 array("db" => '2005-10-25 10:42:24', "df" => 'd/m/Y', "tf" => "H:i:s", 'tz' => 'America/Los_Angeles', "display" => '25/10/2005 03:42:24', "dbdate" => "2005-10-25 03:42:24"),
28                 array("db" => '2005-10-25 02:42:24', "df" => 'd/m/Y', "tf" => "H:i:s", 'tz' => 'Europe/London', "display" => '25/10/2005 03:42:24', "dbdate" => "2005-10-25 03:42:24"),
29                 array("db" => '2005-10-25 01:42:24', "df" => 'd/m/Y', "tf" => "H:i:s", 'tz' => 'Asia/Jerusalem', "display" => '25/10/2005 03:42:24', "dbdate" => "2005-10-25 03:42:24"),
30                 // FAIL! FIXME: same format leads to no TZ conversion
31                 array("db" => '2005-10-25 10:42:24', "df" => 'Y-m-d', "tf" => "H:i:s", 'tz' => 'America/Los_Angeles', "display" => '2005-10-25 03:42:24', "dbdate" => "2005-10-25 03:42:24"),
32                 // short times
33                 array("db" => '2005-10-25 10:42:00', "df" => 'd/m/Y', "tf" => "H:i", 'tz' => 'America/Los_Angeles', "display" => '25/10/2005 03:42', "dbdate" => "2005-10-25 03:42:00"),
34                 array("db" => '2005-10-25 22:00:00', "df" => 'd/m/Y', "tf" => "ha", 'tz' => 'America/Los_Angeles', "display" => '25/10/2005 03pm', "dbdate" => "2005-10-25 15:00:00"),
35                 array("db" => '2005-10-25 10:00:00', "df" => 'd/m/Y', "tf" => "h", 'tz' => 'America/Los_Angeles', "display" => '25/10/2005 03', "dbdate" => "2005-10-25 03:00:00"),
36                 array("db" => '2005-10-25 20:00:00', "df" => 'd/m/Y', "tf" => "H", 'tz' => 'America/Los_Angeles', "display" => '25/10/2005 13', "dbdate" => "2005-10-25 13:00:00"),
37         );
38
39         protected $time_tests = array(
40                 // full time
41                 array("db" => "11:45:00", "display" => "11:45"),
42                 array("db" => "05:17:28", "tf" => "H.i.s", "display" => "05.17.28"),
43                 // short ones
44                 array("db" => "17:34:00", "tf" => "H:i", "display" => "17:34"),
45                 array("db" => "11:42:00", "tf" => "h.iA", "display" => "11.42AM"),
46                 array("db" => "15:00:00", "tf" => "ha", "display" => "03pm"),
47                 array("db" => "15:00:00", "tf" => "H", "display" => "15"),
48                 // FIXME: is this a valid format? it doesn't allow roundtrip
49                 array("db" => "03:00:00", "tf" => "h", "display" => "03"),
50                 // weirdo
51                 array("db" => "16:42:34", "tf" => "s:i:H", "display" => "34:42:16"),
52                 );
53
54         public function setUp()
55         {
56         $GLOBALS['current_user'] = SugarTestUserUtilities::createAnonymousUser();
57                 $this->time_date = new TimeDate();
58                 $this->_noUserCache();
59         }
60
61         public function tearDown()
62         {
63                 SugarTestUserUtilities::removeAllCreatedAnonymousUsers();
64         unset($GLOBALS['current_user']);
65         unset($this->time_date);
66         }
67
68         protected function _noUserCache()
69         {
70                 $this->time_date->allow_cache = false;
71         }
72
73         protected function _setPrefs($datef, $timef, $tz) {
74                         $GLOBALS['current_user']->setPreference('datef', $datef);
75                         $GLOBALS['current_user']->setPreference('timef', $timef);
76                         $GLOBALS['current_user']->setPreference('timezone', $tz);
77                         // new object to avoid TZ caching
78                         $this->time_date = new TimeDate();
79                         $this->_noUserCache();
80         }
81
82         protected function _dateOnly($datetime)
83         {
84                 // FIXME: assumes dates have no spaces
85                 $dt = explode(' ', $datetime);
86                 return $dt[0];
87         }
88
89         protected function _timeOnly($datetime)
90         {
91                 // FIXME: assumes dates have no spaces
92                 $dt = explode(' ', $datetime);
93                 if(count($dt) > 1) {
94                         return $dt[1];
95                 }
96                 return $datetime;
97         }
98
99         /**
100          * test conversion from local datetime to DB datetime
101          */
102         public function testToDbFormats()
103         {
104                 foreach($this->date_tests as $datetest) {
105                         $tf = isset($datetest["tf"]) ? $datetest["tf"] : self::DEFAULT_TIME_FORMAT;
106                         $this->_setPrefs($datetest["df"], $tf, $datetest["tz"]);
107                         $this->assertEquals($datetest["db"],
108                                 $this->time_date->to_db($datetest["display"]),
109                                 "Broken conversion for '{$datetest["df"]} $tf' with date '{$datetest["display"]}' and TZ {$datetest["tz"]}");
110                 }
111         }
112
113         /**
114          * test conversion from full local datetime to DB date
115          */
116         public function testToDbDateFormatsWithOffset()
117         {
118                 foreach($this->date_tests as $datetest) {
119                         $tf = isset($datetest["tf"]) ? $datetest["tf"] : self::DEFAULT_TIME_FORMAT;
120                         $this->_setPrefs($datetest["df"], $tf, $datetest["tz"]);
121                         $this->assertEquals(
122                                 $this->_dateOnly($datetest["db"]),
123                                 $this->time_date->to_db_date($datetest["display"], true),
124                                 "Broken conversion for '{$datetest["df"]} $tf' with date '{$datetest["display"]}' and TZ {$datetest["tz"]}");
125                 }
126         }
127
128         /**
129          * test conversion from local date to DB date, no TZ handling
130          */
131         public function testToDbDateFormatsNoOffset()
132         {
133                 foreach($this->date_tests as $datetest) {
134                         $tf = isset($datetest["tf"]) ? $datetest["tf"] : self::DEFAULT_TIME_FORMAT;
135                         $this->_setPrefs($datetest["df"], $tf, $datetest["tz"]);
136                         $this->assertEquals(
137                                 $this->_dateOnly($datetest["dbdate"]),
138                                 $this->time_date->to_db_date($this->_dateOnly($datetest["display"]), false),
139                                 "Broken conversion for '{$datetest["df"]} $tf' with date '{$datetest["display"]}' and TZ {$datetest["tz"]}");
140                 }
141         }
142
143         /**
144          * test conversion from full local datetime to DB time
145          */
146         public function testToDbTimeFormatsWithTz()
147         {
148                 foreach($this->date_tests as $datetest) {
149                         $tf = isset($datetest["tf"]) ? $datetest["tf"] : self::DEFAULT_TIME_FORMAT;
150                         $this->_setPrefs($datetest["df"], $tf, $datetest["tz"]);
151                         $this->assertEquals(
152                                 $this->_timeOnly($datetest["db"]),
153                                 $this->time_date->to_db_time($datetest["display"], true),
154                                 "Broken conversion for '{$datetest["df"]} $tf' with date '{$datetest["display"]}' and TZ {$datetest["tz"]}");
155                 }
156         }
157
158         /**
159          * test conversion from local time to DB time, no TZ handling
160          */
161         public function testToDbTimeFormatsNoTz()
162         {
163                 foreach($this->time_tests as $datetest) {
164                         $tf = isset($datetest["tf"]) ? $datetest["tf"] : self::DEFAULT_TIME_FORMAT;
165                         $this->_setPrefs('Y-m-d', $tf, '');
166                         $this->assertEquals(
167                                 $datetest["db"],
168                                 $this->time_date->to_db_time($datetest["display"], false),
169                                 "Broken conversion for '$tf' with date '{$datetest["display"]}'");
170                 }
171         }
172
173         /**
174          * test conversion from local date+time to DB date+time, no TZ handling
175          */
176         public function testToDbDateTimeFormats()
177         {
178                 foreach($this->date_tests as $datetest) {
179                         $tf = isset($datetest["tf"]) ? $datetest["tf"] : self::DEFAULT_TIME_FORMAT;
180                         $this->_setPrefs($datetest["df"], $tf, $datetest["tz"]);
181                         $dt = explode(' ', $datetest["display"]);
182                         if(count($dt) > 1) {
183                                 list($date, $time) = $dt;
184                         } else {
185                                 $date = $dt[0];
186                                 $z = new DateTime("@0", new DateTimeZone("GMT"));
187                                 $time = $z->format($tf);
188                         }
189                         $this->assertEquals(
190                                 explode(' ',$datetest["dbdate"]),
191                                 $this->time_date->to_db_date_time($date, $time),
192                                 "Broken conversion for '{$datetest["df"]} $tf' with date '{$datetest["display"]}' and TZ {$datetest["tz"]}");
193                 }
194         }
195
196
197         /**
198          * test conversion from DB date+time to local date+time with TZ handling
199          */
200         public function testToDisplayDateTimeFormats()
201         {
202                 foreach($this->date_tests as $datetest) {
203                         if(!isset($datetest["tf"])) {
204                                 $tf = null;
205                         } else {
206                                 $tf = $datetest["tf"];
207                         }
208                         $df = $datetest["df"]." ".$tf;
209                         $this->_setPrefs($datetest["df"], $tf, $datetest["tz"]);
210                         $result = $this->time_date->to_display_date_time($datetest["db"], true, true, $GLOBALS['current_user']);
211                         if(!isset($datetest["tf"])) {
212                                 $result = $this->_dateOnly($result);
213                         }
214                         $this->assertEquals(
215                                 $datetest["display"],
216                                 $result,
217                                 "Broken conversion for '$df' with date '{$datetest["db"]}' and TZ {$datetest["tz"]}");
218                 }
219         }
220
221         /**
222          * test conversion from DB date+time to local date+time without TZ handling
223          */
224         public function testToDisplayFormatsNoTz()
225         {
226                 foreach($this->date_tests as $datetest) {
227                         if(!isset($datetest["tf"])) {
228                                 $tf = null;
229                         } else {
230                                 $tf = $datetest["tf"];
231                         }
232                         $df = $datetest["df"]." ".$tf;
233                         $this->_setPrefs($datetest["df"], $tf, $datetest["tz"]);
234                         $result = $this->time_date->to_display($datetest["dbdate"], $this->time_date->get_db_date_time_format(), $df);
235                         if(!isset($datetest["tf"])) {
236                                 $result = $this->_dateOnly($result);
237                         }
238                         $this->assertEquals(
239                                 $datetest["display"],
240                                 $result,
241                                 "Broken conversion for '$df' with date '{$datetest["db"]}' and TZ {$datetest["tz"]}");
242                 }
243         }
244
245         /**
246          * test conversion from DB time to local time without TZ conversion
247          */
248         public function testToDisplayTimeFormatsNoTZ()
249         {
250                 foreach($this->time_tests as $datetest) {
251                         $tf = isset($datetest["tf"]) ? $datetest["tf"] : self::DEFAULT_TIME_FORMAT;
252                         $this->_setPrefs('Y-m-d', $tf, '');
253                         $this->assertEquals(
254                                 $datetest["display"],
255                                 $this->time_date->to_display_time($datetest["db"], true, false),
256                                 "Broken conversion for '$tf' with date '{$datetest["db"]}'");
257                 }
258         }
259
260         /**
261          * test conversion from DB time to local time with TZ conversion
262          */
263         public function testToDisplayTimeFormatsWithTZ()
264         {
265                 foreach($this->date_tests as $datetest) {
266                         if(!isset($datetest["tf"])) continue;
267                         $this->_setPrefs($datetest["df"], $datetest["tf"], $datetest["tz"]);
268                         $result = $this->time_date->to_display_time($datetest["db"], true, true);
269                         $result = $this->_timeOnly($result);
270                         $this->assertEquals(
271                                 $this->_timeOnly($datetest["display"]),
272                                 $result,
273                                 "Broken conversion for '{$datetest["tf"]}' with date '{$datetest["db"]}' and TZ {$datetest["tz"]}");
274                 }
275         }
276
277
278         /**
279          * test conversion from DB date to local date without TZ handling
280          */
281         public function testToDisplayDateFormatsNoTz()
282         {
283                 foreach($this->date_tests as $datetest) {
284                         if(!isset($datetest["tf"])) {
285                                 $tf = null;
286                         } else {
287                                 $tf = $datetest["tf"];
288                         }
289                         $df = $datetest["df"]." ".$tf;
290                         $this->_setPrefs($datetest["df"], $tf, $datetest["tz"]);
291                         $result = $this->time_date->to_display_date($this->_dateOnly($datetest["dbdate"]), false);
292                         $this->assertEquals(
293                                 $this->_dateOnly($datetest["display"]),
294                                 $this->_dateOnly($result),
295                                 "Broken conversion for '{$datetest["df"]}' with date '{$datetest["dbdate"]}' and TZ {$datetest["tz"]}");
296                 }
297         }
298
299         /**
300          * test conversion from DB date to local date with TZ handling
301          */
302         public function testToDisplayDateFormatsWithTz()
303         {
304                 foreach($this->date_tests as $datetest) {
305                         if(!isset($datetest["tf"])) {
306                                 $tf = null;
307                         } else {
308                                 $tf = $datetest["tf"];
309                         }
310                         $df = $datetest["df"]." ".$tf;
311                         $this->_setPrefs($datetest["df"], $tf, $datetest["tz"]);
312                         $result = $this->time_date->to_display_date($datetest["db"], true);
313                         $this->assertEquals(
314                                 $this->_dateOnly($datetest["display"]),
315                                 $this->_dateOnly($result),
316                                 "Broken conversion for '{$datetest["df"]}' with date '{$datetest["dbdate"]}' and TZ {$datetest["tz"]}");
317                 }
318         }
319
320         /**
321          * test midnight formatting
322          */
323         public function testGetMidnight()
324         {
325                 if(!is_callable(array($this->time_date, "get_default_midnight"))) {
326                         $this->markTestSkipped("Method is no longer public");
327                 }
328                 $times = array(
329                         array("tf" => "H:i", "time" => "00:00"),
330                         array("tf" => "H:i:s", "time" => "00:00:00"),
331                         array("tf" => "h:i", "time" => "12:00"),
332                         array("tf" => "h:i:s", "time" => "12:00:00"),
333                         array("tf" => "h`iA", "time" => "12`00AM"),
334                         array("tf" => "h`i`sa", "time" => "12`00`00am"),
335                 );
336                 foreach($times as $timetest) {
337                         $this->_setPrefs('', $timetest["tf"], "America/Los_Angeles");
338                         $this->assertEquals($timetest["time"],  $this->time_date->get_default_midnight(true),
339                                 "Bad midnight value for {$timetest["time"]} format {$timetest["tf"]}");
340                 }
341         }
342
343         public function testSwapFormatsWithTheSameDateFormat()
344         {
345                 $original_date = '2005-12-25';
346                 $original_format = 'Y-m-d';
347                 $new_format = $original_format;
348                 $expected_new_date = $original_date;
349
350                 $new_date = $this->time_date->swap_formats($original_date,
351                         $original_format, $new_format);
352
353                 $this->assertEquals($expected_new_date, $new_date);
354         }
355
356         public function testSwapFormatsFromMdyFormatToDmyFormat()
357         {
358                 $original_date = '12-25-2005';
359                 $original_format = 'm-d-Y';
360                 $new_format = 'd-m-Y';
361                 $expected_new_date = '25-12-2005';
362
363                 $new_date = $this->time_date->swap_formats($original_date,
364                         $original_format, $new_format);
365
366                 $this->assertEquals($expected_new_date, $new_date,
367                         "Convert from $original_format to $new_format failed.");
368         }
369
370         public function testSwapFormatsWithTheSameDatetimeFormat()
371         {
372                 $original_date = '2005-12-25 12:55:35';
373                 $original_format = 'Y-m-d H:i:s';
374                 $new_format = $original_format;
375                 $expected_new_date = $original_date;
376
377                 $new_date = $this->time_date->swap_formats($original_date,
378                         $original_format, $new_format);
379
380                 $this->assertEquals($expected_new_date, $new_date,
381                         'Same datetime format not returned.');
382         }
383
384         public function testSwapFormatsFromYmdhiFormatToYmdhisFormat()
385         {
386                 $original_date = '2005-12-25 12:55';
387                 $original_format = 'Y-m-d H:i';
388                 $new_format = 'Y-m-d H:i:s';
389                 $expected_new_date = '2005-12-25 12:55:00';
390
391                 $new_date = $this->time_date->swap_formats($original_date,
392                         $original_format, $new_format);
393
394                 $this->assertEquals($expected_new_date, $new_date);
395         }
396
397         public function testSwapFormatsFromYmdhiFormatToYmdhiaFormat()
398         {
399                 $original = '2005-12-25 13:55';
400                 $original_format = 'Y-m-d H:i';
401                 $new_format = 'Y-m-d h:ia';
402                 $expected = '2005-12-25 01:55pm';
403
404                 $new = $this->time_date->swap_formats($original,
405                         $original_format, $new_format);
406
407                 $this->assertEquals($expected, $new);
408         }
409
410         public function testAllDateFormatSwappingCombinations()
411         {
412                 $orig_formats_and_dates = array(
413                         'Y-m-d' => '2006-12-23',
414                         'm-d-Y' => '12-23-2006',
415                         'd-m-Y' => '23-12-2006',
416                         'Y/m/d' => '2006/12/23',
417                         'm/d/Y' => '12/23/2006',
418                         'd/m/Y' => '23/12/2006');
419
420                 $new_formats_and_dates = $orig_formats_and_dates;
421
422                 foreach($orig_formats_and_dates as $orig_format => $orig_date)
423                 {
424                         foreach($new_formats_and_dates as $new_format => $expected_date)
425                         {
426                                 $new_date = $this->time_date->swap_formats($orig_date,
427                                         $orig_format, $new_format);
428
429                                 $this->assertEquals($expected_date, $new_date,
430                                         "Convert from $orig_format to $new_format failed.");
431
432                                 if($expected_date != $new_date)
433                                 {
434                                         return;
435                                 }
436                         }
437                 }
438         }
439
440     /**
441      * @group bug17528
442      */
443         public function testSwapDatetimeFormatToDbFormat()
444         {
445                 $date = '10-25-2007 12:00am';
446                 $format = $this->time_date->get_date_time_format();
447                 $db_format = $this->time_date->get_db_date_time_format();
448
449                 $this->assertEquals(
450                         $this->time_date->swap_formats(
451                                 $date,
452                                 'm-d-Y h:ia',
453                                 $this->time_date->get_db_date_time_format()
454                         ),
455                         '2007-10-25 00:00:00'
456                 );
457         }
458
459         /**
460      * @group bug17528
461      */
462         public function testTodbCanHandleDdmmyyyyFormats()
463         {
464                 $old_pattern = $GLOBALS['current_user']->getPreference('datef');
465                 $GLOBALS['current_user']->setPreference('datef','d-m-Y');
466                 $db_date_pattern = '/2007-10-25 [0-9]{2}:[0-9]{2}:[0-9]{2}/';
467                 $this->assertRegExp(
468                         $db_date_pattern,
469                         $this->time_date->to_db('25-10-2007')
470                 );
471
472                 $this->_noUserCache();
473                 $GLOBALS['current_user']->setPreference('datef','m-d-Y');
474                 $this->assertRegExp(
475                         $db_date_pattern,
476                         $this->time_date->to_db('10-25-2007')
477                 );
478                 $GLOBALS['current_user']->setPreference('datef',$old_pattern);
479         }
480
481         /**
482      * @group bug17528
483      */
484         public function testTodbCanHandleMmddyyyyFormats()
485         {
486                 $old_date = $GLOBALS['current_user']->getPreference('datef');
487
488                 $GLOBALS['current_user']->setPreference('datef','m-d-Y');
489                 $db_date_pattern = '/2007-10-25 [0-9]{2}:[0-9]{2}:[0-9]{2}/';
490                 $this->assertRegExp(
491                         $db_date_pattern,
492                         $this->time_date->to_db('10-25-2007')
493                 );
494
495                 $GLOBALS['current_user']->setPreference('datef',$old_date);
496         }
497
498         /**
499      * @group bug17528
500      */
501         public function testTodbdateCanHandleDdmmyyyyFormats()
502         {
503                 $old_date = $GLOBALS['current_user']->getPreference('datef');
504
505                 $GLOBALS['current_user']->setPreference('datef','d-m-Y');
506                 $this->assertEquals(
507                         $this->time_date->to_db_date('25-10-2007'),
508                         '2007-10-25'
509                 );
510
511                 $GLOBALS['current_user']->setPreference('datef',$old_date);
512         }
513
514         /**
515      * @group bug17528
516      */
517         public function testTodbdateCanHandleMmddyyyyFormats()
518         {
519                 $old_date = $GLOBALS['current_user']->getPreference('datef');
520                 $GLOBALS['current_user']->setPreference('datef','m-d-Y');
521                 $this->assertEquals(
522                         '2007-10-25',
523                         $this->time_date->to_db_date('10-25-2007')
524                 );
525
526                 $GLOBALS['current_user']->setPreference('datef',$old_date);
527         }
528
529         public function testConvertMmddyyyyFormatToYyyymmdd()
530         {
531                 $this->assertEquals(
532                         '2007-11-02',
533                         $this->time_date->swap_formats(
534                                 '11-02-2007',
535                                 'm-d-Y',
536                                 'Y-m-d'
537                         )
538                 );
539         }
540
541         public function testGeneratingDefaultMidnight()
542         {
543                 if(!is_callable(array($this->time_date, "get_default_midnight"))) {
544                         $this->markTestSkipped("Method is no longer public");
545                 }
546                 $old_time = $GLOBALS['current_user']->getPreference('timef');
547
548                 $GLOBALS['current_user']->setPreference('timef','H:i:s');
549                 $this->assertEquals(
550                         '00:00:00',
551                         $this->time_date->get_default_midnight(true)
552                 );
553
554                 $GLOBALS['current_user']->setPreference('timef','h:ia');
555                 $this->assertEquals(
556                         '12:00am',
557                         $this->time_date->get_default_midnight(true)
558                 );
559
560                 $GLOBALS['current_user']->setPreference('timef',$old_time);
561         }
562
563         public function providerGetDateFromRules()
564         {
565             return array(
566                 array('2009',10,1,0,7200,"2009-10-04 02:00:00"),
567                 array('2009',4,1,0,7200,"2009-04-05 02:00:00"),
568                 array('2010',3,24,5,7200,"2010-03-26 02:00:00"),
569                 array('2010',9,12,0,7200,"2010-09-12 02:00:00"),
570                 );
571         }
572
573         /**
574          * @dataProvider providerGetDateFromRules
575          */
576         public function testGetDateFromRules(
577             $year,
578             $startMonth,
579             $startDate,
580             $weekday,
581             $startTime,
582             $returnValue
583             )
584         {
585                 if(!is_callable(array($this->time_date, "getDateFromRules"))) {
586                         $this->markTestSkipped("Method is no longer public");
587                 }
588                 $this->assertEquals(
589                 $this->time_date->getDateFromRules($year, $startMonth, $startDate, $weekday, $startTime),
590                 $returnValue
591                 );
592         }
593
594         /**
595          * tests for check_matching_format
596          */
597         public function testCheckMatchingFormats()
598         {
599                 foreach($this->date_tests as $datetest) {
600                         if(isset($datetest["tf"])) {
601                                 $df = $this->time_date->merge_date_time($df = $datetest["df"], $datetest["tf"]);
602                         } else {
603                                 $df = $datetest["df"];
604                         }
605                         $this->assertTrue($this->time_date->check_matching_format($datetest["display"], $df),
606                                 "Broken match for '$df' with date '{$datetest["display"]}'");
607                 }
608
609                 // Some bad dates not detected by current code, it's too lenient
610                 $badtests = array(
611                         array("format" => "Y-m-d", "date" => ""),
612                         array("format" => "Y-m-d", "date" => "blah-blah-blah"),
613                         array("format" => "Y-m-d", "date" => "1-2"),
614                         array("format" => "Y-m-d", "date" => "2007-10"),
615                         //FIXME: array("format" => "Y-m-d", "date" => "200-10-25"),
616                         array("format" => "Y-m-d", "date" => "2007-101-25"),
617                         array("format" => "Y-m-d", "date" => "2007-Oct-25"),
618                         //FIXME: array("format" => "Y-m-d", "date" => "2007-10-250"),
619                         array("format" => "d-m-Y", "date" => "2007-10-25"),
620                         array("format" => "d-m-Y", "date" => "10/25/2007"),
621                         //FIXME: array("format" => "Y-m-d", "date" => "here: 2007-20-25"),
622                         //FIXME: array("format" => "Y-m-d", "date" => "2007-20-25 here"),
623                 );
624                 foreach($badtests as $datetest) {
625                         $this->assertFalse($this->time_date->check_matching_format($datetest["date"], $datetest["format"]),
626                         "Broken match for '{$datetest["format"]}' with date '{$datetest["date"]}'");
627                 }
628         }
629
630         /**
631          * test fetching user settings
632          */
633         public function testGetUserSettings()
634         {
635                 $this->_setPrefs('d/m/Y', 'h:i:sA', "America/Lima");
636                 $this->assertEquals('dd/mm/yyyy', $this->time_date->get_user_date_format());
637                 //FIXME: $this->assertEquals('11:00:00PM', $this->time_date->get_user_time_format());
638                 $tz = $this->time_date->getUserTimeZone();
639                 $this->assertEquals(-300, $tz["gmtOffset"]);
640 //              $this->assertEquals(60, $tz["dstOffset"]);
641         }
642
643         /**
644          * test getting GMT dates
645          */
646         public function testGetGMT()
647         {
648                 $gmt = $this->time_date->get_gmt_db_datetime();
649                 $dt = strptime($gmt, "%Y-%m-%d %H:%M:%S");
650                 $this->assertEquals($dt['tm_year']+1900, gmdate("Y"));
651                 $this->assertEquals($dt['tm_mon']+1, gmdate("m"));
652                 $this->assertEquals($dt['tm_mday'], gmdate("d"));
653
654                 $gmt = $this->time_date->get_gmt_db_date();
655                 $dt = strptime($gmt, "%Y-%m-%d");
656                 $this->assertEquals($dt['tm_year']+1900, gmdate("Y"));
657                 $this->assertEquals($dt['tm_mon']+1, gmdate("m"));
658                 $this->assertEquals($dt['tm_mday'], gmdate("d"));
659         }
660
661         /**
662          * test getting DB date formats indifferent ways
663          */
664         public function testGetDB()
665         {
666                 $this->assertEquals(gmdate($this->time_date->merge_date_time($this->time_date->get_db_date_format(),
667                                                                                                                                                 $this->time_date->get_db_time_format())),
668                          $this->time_date->get_gmt_db_datetime());
669         }
670
671         public function testGetCalFormats()
672         {
673                 $cal_tests = array(
674                         array("df" => "Y-m-d", "caldf" => "%Y-%m-%d", "tf" => "H:i:s", "caltf" => "%H:%M:%S"),
675                         array("df" => "d/m/Y", "caldf" => "%d/%m/%Y", "tf" => "h:i:sa", "caltf" => "%I:%M:%S%P"),
676                         array("df" => "m/d/Y", "caldf" => "%m/%d/%Y", "tf" => "H:i", "caltf" => "%H:%M"),
677                         array("df" => "Y-m-d", "caldf" => "%Y-%m-%d", "tf" => "h:iA", "caltf" => "%I:%M%p"),
678                 );
679                 foreach($cal_tests as $datetest) {
680                         $this->_setPrefs($datetest["df"], $datetest["tf"], "America/Los_Angeles");
681                         $this->assertEquals(
682                                 $datetest["caldf"],
683                                 $this->time_date->get_cal_date_format(),
684                                 "Bad cal date format for '{$datetest["df"]}'");
685                         $this->assertEquals(
686                                 $datetest["caltf"],
687                                 $this->time_date->get_cal_time_format(),
688                                 "Bad cal time format for '{$datetest["tf"]}'");
689                         $this->assertEquals(
690                                 $this->time_date->merge_date_time($datetest["caldf"], $datetest["caltf"]),
691                                 $this->time_date->get_cal_date_time_format(),
692                                 "Bad cal datetime format for '{$datetest["df"]} {$datetest["tf"]}'");
693                 }
694         }
695
696         /**
697          * test for handleOffsetMax
698          */
699         public function testDayMinMax()
700         {
701                 $day_tests = array(
702                         array("date" => "2010-05-19", "start" => "2010-05-19 07:00:00", "end" => "2010-05-20 06:59:59", 'tz' => 'America/Los_Angeles'),
703                         array("date" => "2010-01-19", "start" => "2010-01-19 08:00:00", "end" => "2010-01-20 07:59:59", 'tz' => 'America/Los_Angeles'),
704                         array("date" => "2010-05-19", "start" => "2010-05-18 23:00:00", "end" => "2010-05-19 22:59:59", 'tz' => 'Europe/London'),
705                         array("date" => "2010-01-19", "start" => "2010-01-19 00:00:00", "end" => "2010-01-19 23:59:59", 'tz' => 'Europe/London'),
706                         array("date" => "2010-05-19", "start" => "2010-05-18 22:00:00", "end" => "2010-05-19 21:59:59", 'tz' => 'Europe/Oslo'),
707                 );
708                 foreach($day_tests as $datetest) {
709                         $this->_setPrefs('', '', $datetest["tz"]);
710                         $dates = $this->time_date->handleOffsetMax($datetest["date"], '');
711                         $this->assertEquals($datetest["start"], $dates["min"],
712                                 "Bad min result for {$datetest["date"]} tz {$datetest["tz"]}");
713                         $this->assertEquals($datetest["end"], $dates["max"],
714                                 "Bad max result for {$datetest["date"]} tz {$datetest["tz"]}");
715                 }
716         }
717
718         /**
719          * test for getDayStartEndGMT
720          */
721         public function testGetDayStartEnd()
722         {
723                 $day_tests = array(
724                         array("date" => "05/19/2010", "start" => "2010-05-19 07:00:00", "end" => "2010-05-20 06:59:59", 'tz' => 'America/Los_Angeles'),
725                         array("date" => "01/19/2010", "start" => "2010-01-19 08:00:00", "end" => "2010-01-20 07:59:59", 'tz' => 'America/Los_Angeles'),
726                         array("date" => "05/19/2010", "start" => "2010-05-18 23:00:00", "end" => "2010-05-19 22:59:59", 'tz' => 'Europe/London'),
727                         array("date" => "01/19/2010", "start" => "2010-01-19 00:00:00", "end" => "2010-01-19 23:59:59", 'tz' => 'Europe/London'),
728                         array("date" => "05/19/2010", "start" => "2010-05-18 22:00:00", "end" => "2010-05-19 21:59:59", 'tz' => 'Europe/Oslo'),
729                 );
730                 foreach($day_tests as $datetest) {
731                         $this->_setPrefs('m/d/Y', '', $datetest["tz"]);
732                         $dates = $this->time_date->getDayStartEndGMT($datetest["date"], '');
733                         $this->assertEquals($datetest["start"], $dates["start"],
734                                 "Bad min result for {$datetest["date"]} tz {$datetest["tz"]}");
735                         $this->assertEquals($datetest["end"], $dates["end"],
736                                 "Bad max result for {$datetest["date"]} tz {$datetest["tz"]}");
737                 }
738         }
739
740         /**
741          * test for merge_time_meridiem
742          */
743         public function testMergeAmPm()
744         {
745                 $ampm_tests = array(
746                         array("date" => "05:17:28", "mer" => "am", "tf" => "H:i:s", "display" => "05:17:28"),
747                         array("date" => "05:17:28", "mer" => "am", "tf" => "h:i:sa", "display" => "05:17:28am"),
748                         // short ones
749                         array("date" => "17:34", "mer" => "pm", "tf" => "H:i", "display" => "17:34"),
750                         array("date" => "11:42", "mer" => "PM", "tf" => "h:iA", "display" => "11:42PM"),
751                         array("date" => "11:42", "mer" => "pm", "tf" => "h:iA", "display" => "11:42pm"),
752                         array("date" => "03", "mer" => "AM", "tf" => "ha", "display" => "03AM"),
753                         array("date" => "15", "mer" => "AM", "tf" => "H", "display" => "15"),
754                 );
755         foreach($ampm_tests as $datetest) {
756                         $amdate = $this->time_date->merge_time_meridiem($datetest["date"], $datetest["tf"], $datetest["mer"]);
757                         $this->assertEquals($datetest["display"], $amdate,
758                                 "Bad min result for {$datetest["date"]} format {$datetest["tf"]}");
759                 }
760         }
761         
762         public function providerSplitDateTime()
763         {
764             return array(
765                 array("2009-10-04 02:00:00","2009-10-04","02:00:00"),
766                 array("10/04/2010 2:00pm","10/04/2010","2:00pm"),
767                 array("10-04-2010 2:00","10-04-2010","2:00"),
768                 );
769         }
770         
771         /**
772          * @dataProvider providerSplitDateTime
773          */
774         public function testSplitDateTime(
775             $datetime,
776             $date,
777             $time
778             )
779         {
780             $this->assertEquals($date,$this->time_date->getDatePart($datetime));
781             $this->assertEquals($time,$this->time_date->getTimePart($datetime));
782         }
783 }