91c23ec63838759d4e9d93e790e71bf85ffa0883
[lhc/web/wiklou.git] / tests / phpunit / includes / TimestampTest.php
1 <?php
2
3 /**
4 * Tests timestamp parsing and output.
5 */
6 class TimestampTest extends MediaWikiTestCase {
7
8 protected function setUp() {
9 parent::setUp();
10
11 $this->setMwGlobals( array(
12 'wgLanguageCode' => 'en',
13 'wgContLang' => Language::factory( 'en' ),
14 'wgLang' => Language::factory( 'en' ),
15 ) );
16 }
17
18 /**
19 * Test parsing of valid timestamps and outputing to MW format.
20 * @dataProvider provideValidTimestamps
21 */
22 function testValidParse( $format, $original, $expected ) {
23 $timestamp = new MWTimestamp( $original );
24 $this->assertEquals( $expected, $timestamp->getTimestamp( TS_MW ) );
25 }
26
27 /**
28 * Test outputting valid timestamps to different formats.
29 * @dataProvider provideValidTimestamps
30 */
31 function testValidOutput( $format, $expected, $original ) {
32 $timestamp = new MWTimestamp( $original );
33 $this->assertEquals( $expected, (string)$timestamp->getTimestamp( $format ) );
34 }
35
36 /**
37 * Test an invalid timestamp.
38 * @expectedException TimestampException
39 */
40 function testInvalidParse() {
41 $timestamp = new MWTimestamp( "This is not a timestamp." );
42 }
43
44 /**
45 * Test requesting an invalid output format.
46 * @expectedException TimestampException
47 */
48 function testInvalidOutput() {
49 $timestamp = new MWTimestamp( '1343761268' );
50 $timestamp->getTimestamp( 98 );
51 }
52
53 /**
54 * Test human readable timestamp format.
55 */
56 function testHumanOutput() {
57 $timestamp = new MWTimestamp( time() - 3600 );
58 $this->assertEquals( "1 hour ago", $timestamp->getHumanTimestamp()->inLanguage( 'en' )->text() );
59 $timestamp = new MWTimestamp( time() - 5184000 );
60 $this->assertEquals( "2 months ago", $timestamp->getHumanTimestamp()->inLanguage( 'en' )->text() );
61 $timestamp = new MWTimestamp( time() - 31536000 );
62 $this->assertEquals( "1 year ago", $timestamp->getHumanTimestamp()->inLanguage( 'en' )->text() );
63 }
64
65 /**
66 * Returns a list of valid timestamps in the format:
67 * array( type, timestamp_of_type, timestamp_in_MW )
68 */
69 public static function provideValidTimestamps() {
70 return array(
71 // Various formats
72 array( TS_UNIX, '1343761268', '20120731190108' ),
73 array( TS_MW, '20120731190108', '20120731190108' ),
74 array( TS_DB, '2012-07-31 19:01:08', '20120731190108' ),
75 array( TS_ISO_8601, '2012-07-31T19:01:08Z', '20120731190108' ),
76 array( TS_ISO_8601_BASIC, '20120731T190108Z', '20120731190108' ),
77 array( TS_EXIF, '2012:07:31 19:01:08', '20120731190108' ),
78 array( TS_RFC2822, 'Tue, 31 Jul 2012 19:01:08 GMT', '20120731190108' ),
79 array( TS_ORACLE, '31-07-2012 19:01:08.000000', '20120731190108' ),
80 array( TS_POSTGRES, '2012-07-31 19:01:08 GMT', '20120731190108' ),
81 array( TS_DB2, '2012-07-31 19:01:08', '20120731190108' ),
82 // Some extremes and weird values
83 array( TS_ISO_8601, '9999-12-31T23:59:59Z', '99991231235959' ),
84 array( TS_UNIX, '-62135596801', '00001231235959' )
85 );
86 }
87 }