MediaWiki  1.23.12
wfTimestampTest.php
Go to the documentation of this file.
1 <?php
2 /*
3  * @covers ::wfTimestamp
4  */
9  public function testNormalTimestamps( $input, $format, $output, $desc ) {
10  $this->assertEquals( $output, wfTimestamp( $format, $input ), $desc );
11  }
12 
13  public static function provideNormalTimestamps() {
14  $t = gmmktime( 12, 34, 56, 1, 15, 2001 );
15 
16  return array(
17  // TS_UNIX
18  array( $t, TS_MW, '20010115123456', 'TS_UNIX to TS_MW' ),
19  array( -30281104, TS_MW, '19690115123456', 'Negative TS_UNIX to TS_MW' ),
20  array( $t, TS_UNIX, 979562096, 'TS_UNIX to TS_UNIX' ),
21  array( $t, TS_DB, '2001-01-15 12:34:56', 'TS_UNIX to TS_DB' ),
22 
23  array( $t, TS_ISO_8601_BASIC, '20010115T123456Z', 'TS_ISO_8601_BASIC to TS_DB' ),
24 
25  // TS_MW
26  array( '20010115123456', TS_MW, '20010115123456', 'TS_MW to TS_MW' ),
27  array( '20010115123456', TS_UNIX, 979562096, 'TS_MW to TS_UNIX' ),
28  array( '20010115123456', TS_DB, '2001-01-15 12:34:56', 'TS_MW to TS_DB' ),
29  array( '20010115123456', TS_ISO_8601_BASIC, '20010115T123456Z', 'TS_MW to TS_ISO_8601_BASIC' ),
30 
31  // TS_DB
32  array( '2001-01-15 12:34:56', TS_MW, '20010115123456', 'TS_DB to TS_MW' ),
33  array( '2001-01-15 12:34:56', TS_UNIX, 979562096, 'TS_DB to TS_UNIX' ),
34  array( '2001-01-15 12:34:56', TS_DB, '2001-01-15 12:34:56', 'TS_DB to TS_DB' ),
35  array( '2001-01-15 12:34:56', TS_ISO_8601_BASIC, '20010115T123456Z', 'TS_DB to TS_ISO_8601_BASIC' ),
36 
37  # rfc2822 section 3.3
38  array( '20010115123456', TS_RFC2822, 'Mon, 15 Jan 2001 12:34:56 GMT', 'TS_MW to TS_RFC2822' ),
39  array( 'Mon, 15 Jan 2001 12:34:56 GMT', TS_MW, '20010115123456', 'TS_RFC2822 to TS_MW' ),
40  array( ' Mon, 15 Jan 2001 12:34:56 GMT', TS_MW, '20010115123456', 'TS_RFC2822 with leading space to TS_MW' ),
41  array( '15 Jan 2001 12:34:56 GMT', TS_MW, '20010115123456', 'TS_RFC2822 without optional day-of-week to TS_MW' ),
42 
43  # FWS = ([*WSP CRLF] 1*WSP) / obs-FWS ; Folding white space
44  # obs-FWS = 1*WSP *(CRLF 1*WSP) ; Section 4.2
45  array( 'Mon, 15 Jan 2001 12:34:56 GMT', TS_MW, '20010115123456', 'TS_RFC2822 to TS_MW' ),
46 
47  # WSP = SP / HTAB ; rfc2234
48  array( "Mon, 15 Jan\x092001 12:34:56 GMT", TS_MW, '20010115123456', 'TS_RFC2822 with HTAB to TS_MW' ),
49  array( "Mon, 15 Jan\x09 \x09 2001 12:34:56 GMT", TS_MW, '20010115123456', 'TS_RFC2822 with HTAB and SP to TS_MW' ),
50  array( 'Sun, 6 Nov 94 08:49:37 GMT', TS_MW, '19941106084937', 'TS_RFC2822 with obsolete year to TS_MW' ),
51  );
52  }
53 
60  public function testOldTimestamps( $input, $format, $output, $desc ) {
61  $this->assertEquals( $output, wfTimestamp( $format, $input ), $desc );
62  }
63 
64  public static function provideOldTimestamps() {
65  return array(
66  array( '19011213204554', TS_RFC2822, 'Fri, 13 Dec 1901 20:45:54 GMT', 'Earliest time according to php documentation' ),
67  array( '20380119031407', TS_RFC2822, 'Tue, 19 Jan 2038 03:14:07 GMT', 'Latest 32 bit time' ),
68  array( '19011213204552', TS_UNIX, '-2147483648', 'Earliest 32 bit unix time' ),
69  array( '20380119031407', TS_UNIX, '2147483647', 'Latest 32 bit unix time' ),
70  array( '19011213204552', TS_RFC2822, 'Fri, 13 Dec 1901 20:45:52 GMT', 'Earliest 32 bit time' ),
71  array( '19011213204551', TS_RFC2822, 'Fri, 13 Dec 1901 20:45:51 GMT', 'Earliest 32 bit time - 1' ),
72  array( '20380119031408', TS_RFC2822, 'Tue, 19 Jan 2038 03:14:08 GMT', 'Latest 32 bit time + 1' ),
73  array( '19011212000000', TS_MW, '19011212000000', 'Convert to itself r74778#c10645' ),
74  array( '19011213204551', TS_UNIX, '-2147483649', 'Earliest 32 bit unix time - 1' ),
75  array( '20380119031408', TS_UNIX, '2147483648', 'Latest 32 bit unix time + 1' ),
76  array( '-2147483649', TS_MW, '19011213204551', '1901 negative unix time to MediaWiki' ),
77  array( '-5331871504', TS_MW, '18010115123456', '1801 negative unix time to MediaWiki' ),
78  array( '0117-08-09 12:34:56', TS_RFC2822, 'Tue, 09 Aug 0117 12:34:56 GMT', 'Death of Roman Emperor [[Trajan]]' ),
79 
80  /* @todo FIXME: 00 to 101 years are taken as being in [1970-2069] */
81  array( '-58979923200', TS_RFC2822, 'Sun, 01 Jan 0101 00:00:00 GMT', '1/1/101' ),
82  array( '-62135596800', TS_RFC2822, 'Mon, 01 Jan 0001 00:00:00 GMT', 'Year 1' ),
83 
84  /* It is not clear if we should generate a year 0 or not
85  * We are completely off RFC2822 requirement of year being
86  * 1900 or later.
87  */
88  array( '-62142076800', TS_RFC2822, 'Wed, 18 Oct 0000 00:00:00 GMT', 'ISO 8601:2004 [[year 0]], also called [[1 BC]]' ),
89  );
90  }
91 
99  public function testHttpDate( $input, $output, $desc ) {
100  $this->assertEquals( $output, wfTimestamp( TS_MW, $input ), $desc );
101  }
102 
103  public static function provideHttpDates() {
104  return array(
105  array( 'Sun, 06 Nov 1994 08:49:37 GMT', '19941106084937', 'RFC 822 date' ),
106  array( 'Sunday, 06-Nov-94 08:49:37 GMT', '19941106084937', 'RFC 850 date' ),
107  array( 'Sun Nov 6 08:49:37 1994', '19941106084937', "ANSI C's asctime() format" ),
108  // See http://www.squid-cache.org/mail-archive/squid-users/200307/0122.html and r77171
109  array( 'Mon, 22 Nov 2010 14:12:42 GMT; length=52626', '20101122141242', 'Netscape extension to HTTP/1.0' ),
110  );
111  }
112 
117  public function testTimestampParameter() {
118  $now = wfTimestamp( TS_UNIX );
119  // We check that wfTimestamp doesn't return false (error) and use a LessThan assert
120  // for the cases where the test is run in a second boundary.
121 
122  $zero = wfTimestamp( TS_UNIX, 0 );
123  $this->assertNotEquals( false, $zero );
124  $this->assertLessThan( 5, $zero - $now );
125 
126  $empty = wfTimestamp( TS_UNIX, '' );
127  $this->assertNotEquals( false, $empty );
128  $this->assertLessThan( 5, $empty - $now );
129 
130  $null = wfTimestamp( TS_UNIX, null );
131  $this->assertNotEquals( false, $null );
132  $this->assertLessThan( 5, $null - $now );
133  }
134 }
WfTimestampTest\provideHttpDates
static provideHttpDates()
Definition: wfTimestampTest.php:103
WfTimestampTest\testTimestampParameter
testTimestampParameter()
There are a number of assumptions in our codebase where wfTimestamp() should give the current date bu...
Definition: wfTimestampTest.php:117
php
skin txt MediaWiki includes four core it has been set as the default in MediaWiki since the replacing Monobook it had been been the default skin since before being replaced by Vector largely rewritten in while keeping its appearance Several legacy skins were removed in the as the burden of supporting them became too heavy to bear Those in etc for skin dependent CSS etc for skin dependent JavaScript These can also be customised on a per user by etc This feature has led to a wide variety of user styles becoming that gallery is a good place to ending in php
Definition: skin.txt:62
wfTimestamp
wfTimestamp( $outputtype=TS_UNIX, $ts=0)
Get a timestamp string in one of various formats.
Definition: GlobalFunctions.php:2530
TS_DB
const TS_DB
MySQL DATETIME (YYYY-MM-DD HH:MM:SS)
Definition: GlobalFunctions.php:2483
WfTimestampTest\testOldTimestamps
testOldTimestamps( $input, $format, $output, $desc)
This test checks wfTimestamp() with values outside.
Definition: wfTimestampTest.php:60
WfTimestampTest
Definition: wfTimestampTest.php:5
MediaWikiTestCase
Definition: MediaWikiTestCase.php:6
array
the array() calling protocol came about after MediaWiki 1.4rc1.
List of Api Query prop modules.
WfTimestampTest\provideNormalTimestamps
static provideNormalTimestamps()
Definition: wfTimestampTest.php:13
TS_MW
const TS_MW
MediaWiki concatenated string timestamp (YYYYMMDDHHMMSS)
Definition: GlobalFunctions.php:2478
WfTimestampTest\testNormalTimestamps
testNormalTimestamps( $input, $format, $output, $desc)
@dataProvider provideNormalTimestamps
Definition: wfTimestampTest.php:9
TS_ISO_8601_BASIC
const TS_ISO_8601_BASIC
ISO 8601 basic format with no timezone: 19860209T200000Z.
Definition: GlobalFunctions.php:2519
WfTimestampTest\testHttpDate
testHttpDate( $input, $output, $desc)
The Resource Loader uses wfTimestamp() to convert timestamps from If-Modified-Since header.
Definition: wfTimestampTest.php:99
WfTimestampTest\provideOldTimestamps
static provideOldTimestamps()
Definition: wfTimestampTest.php:64
TS_UNIX
const TS_UNIX
Unix time - the number of seconds since 1970-01-01 00:00:00 UTC.
Definition: GlobalFunctions.php:2473
$output
& $output
Definition: hooks.txt:375
$t
$t
Definition: testCompression.php:65
TS_RFC2822
const TS_RFC2822
RFC 2822 format, for E-mail and HTTP headers.
Definition: GlobalFunctions.php:2488
section
section
Definition: parserTests.txt:378