Merge branch 'Wikidata' into master.
[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 * Test parsing of valid timestamps and outputing to MW format.
19 * @dataProvider provideValidTimestamps
20 */
21 function testValidParse( $format, $original, $expected ) {
22 $timestamp = new MWTimestamp( $original );
23 $this->assertEquals( $expected, $timestamp->getTimestamp( TS_MW ) );
24 }
25
26 /**
27 * Test outputting valid timestamps to different formats.
28 * @dataProvider provideValidTimestamps
29 */
30 function testValidOutput( $format, $expected, $original ) {
31 $timestamp = new MWTimestamp( $original );
32 $this->assertEquals( $expected, (string) $timestamp->getTimestamp( $format ) );
33 }
34
35 /**
36 * Test an invalid timestamp.
37 * @expectedException TimestampException
38 */
39 function testInvalidParse() {
40 $timestamp = new MWTimestamp( "This is not a timestamp." );
41 }
42
43 /**
44 * Test requesting an invalid output format.
45 * @expectedException TimestampException
46 */
47 function testInvalidOutput() {
48 $timestamp = new MWTimestamp( '1343761268' );
49 $timestamp->getTimestamp( 98 );
50 }
51
52 /**
53 * Test human readable timestamp format.
54 */
55 function testHumanOutput() {
56 $timestamp = new MWTimestamp( time() - 3600 );
57 $this->assertEquals( "1 hour ago", $timestamp->getHumanTimestamp()->inLanguage( 'en' )->text() );
58 }
59
60 /**
61 * Returns a list of valid timestamps in the format:
62 * array( type, timestamp_of_type, timestamp_in_MW )
63 */
64 public static function provideValidTimestamps() {
65 return array(
66 // Various formats
67 array( TS_UNIX, '1343761268', '20120731190108' ),
68 array( TS_MW, '20120731190108', '20120731190108' ),
69 array( TS_DB, '2012-07-31 19:01:08', '20120731190108' ),
70 array( TS_ISO_8601, '2012-07-31T19:01:08Z', '20120731190108' ),
71 array( TS_ISO_8601_BASIC, '20120731T190108Z', '20120731190108' ),
72 array( TS_EXIF, '2012:07:31 19:01:08', '20120731190108' ),
73 array( TS_RFC2822, 'Tue, 31 Jul 2012 19:01:08 GMT', '20120731190108' ),
74 array( TS_ORACLE, '31-07-2012 19:01:08.000000', '20120731190108' ),
75 array( TS_POSTGRES, '2012-07-31 19:01:08 GMT', '20120731190108' ),
76 array( TS_DB2, '2012-07-31 19:01:08', '20120731190108' ),
77 // Some extremes and weird values
78 array( TS_ISO_8601, '9999-12-31T23:59:59Z', '99991231235959' ),
79 array( TS_UNIX, '-62135596801', '00001231235959' )
80 );
81 }
82 }