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