MediaWiki  1.23.6
MWTimestampTest.php
Go to the documentation of this file.
1 <?php
2 
7 
8  protected function setUp() {
9  parent::setUp();
10 
11  RequestContext::getMain()->setLanguage( Language::factory( 'en' ) );
12  }
13 
17  public function testConstructWithNoTimestamp() {
18  $timestamp = new MWTimestamp();
19  $this->assertInternalType( 'string', $timestamp->getTimestamp() );
20  $this->assertNotEmpty( $timestamp->getTimestamp() );
21  $this->assertNotEquals( false, strtotime( $timestamp->getTimestamp( TS_MW ) ) );
22  }
23 
27  public function testToString() {
28  $timestamp = new MWTimestamp( '1406833268' ); // Equivalent to 20140731190108
29  $this->assertEquals( '1406833268', $timestamp->__toString() );
30  }
31 
33  return array(
34  array( '1406833268', '1406833269', '00 00 00 01' ),
35  array( '1406833268', '1406833329', '00 00 01 01' ),
36  array( '1406833268', '1406836929', '00 01 01 01' ),
37  array( '1406833268', '1406923329', '01 01 01 01' ),
38  );
39  }
40 
45  public function testDiff( $timestamp1, $timestamp2, $expected ) {
46  $timestamp1 = new MWTimestamp( $timestamp1 );
47  $timestamp2 = new MWTimestamp( $timestamp2 );
48  $diff = $timestamp1->diff( $timestamp2 );
49  $this->assertEquals( $expected, $diff->format( '%D %H %I %S' ) );
50  }
51 
57  public function testValidParse( $format, $original, $expected ) {
58  $timestamp = new MWTimestamp( $original );
59  $this->assertEquals( $expected, $timestamp->getTimestamp( TS_MW ) );
60  }
61 
67  public function testValidOutput( $format, $expected, $original ) {
68  $timestamp = new MWTimestamp( $original );
69  $this->assertEquals( $expected, (string)$timestamp->getTimestamp( $format ) );
70  }
71 
77  public function testInvalidParse() {
78  new MWTimestamp( "This is not a timestamp." );
79  }
80 
86  public function testInvalidOutput() {
87  $timestamp = new MWTimestamp( '1343761268' );
88  $timestamp->getTimestamp( 98 );
89  }
90 
95  public static function provideValidTimestamps() {
96  return array(
97  // Various formats
98  array( TS_UNIX, '1343761268', '20120731190108' ),
99  array( TS_MW, '20120731190108', '20120731190108' ),
100  array( TS_DB, '2012-07-31 19:01:08', '20120731190108' ),
101  array( TS_ISO_8601, '2012-07-31T19:01:08Z', '20120731190108' ),
102  array( TS_ISO_8601_BASIC, '20120731T190108Z', '20120731190108' ),
103  array( TS_EXIF, '2012:07:31 19:01:08', '20120731190108' ),
104  array( TS_RFC2822, 'Tue, 31 Jul 2012 19:01:08 GMT', '20120731190108' ),
105  array( TS_ORACLE, '31-07-2012 19:01:08.000000', '20120731190108' ),
106  array( TS_POSTGRES, '2012-07-31 19:01:08 GMT', '20120731190108' ),
107  // Some extremes and weird values
108  array( TS_ISO_8601, '9999-12-31T23:59:59Z', '99991231235959' ),
109  array( TS_UNIX, '-62135596801', '00001231235959' )
110  );
111  }
112 
117  public function testHumanTimestamp(
118  $tsTime, // The timestamp to format
119  $currentTime, // The time to consider "now"
120  $timeCorrection, // The time offset to use
121  $dateFormat, // The date preference to use
122  $expectedOutput, // The expected output
123  $desc // Description
124  ) {
125  $user = $this->getMock( 'User' );
126  $user->expects( $this->any() )
127  ->method( 'getOption' )
128  ->with( 'timecorrection' )
129  ->will( $this->returnValue( $timeCorrection ) );
130 
131  $user->expects( $this->any() )
132  ->method( 'getDatePreference' )
133  ->will( $this->returnValue( $dateFormat ) );
134 
135  $tsTime = new MWTimestamp( $tsTime );
136  $currentTime = new MWTimestamp( $currentTime );
137 
138  $this->assertEquals(
139  $expectedOutput,
140  $tsTime->getHumanTimestamp( $currentTime, $user ),
141  $desc
142  );
143  }
144 
145  public static function provideHumanTimestampTests() {
146  return array(
147  array(
148  '20111231170000',
149  '20120101000000',
150  'Offset|0',
151  'mdy',
152  'Yesterday at 17:00',
153  '"Yesterday" across years',
154  ),
155  array(
156  '20120717190900',
157  '20120717190929',
158  'Offset|0',
159  'mdy',
160  'just now',
161  '"Just now"',
162  ),
163  array(
164  '20120717190900',
165  '20120717191530',
166  'Offset|0',
167  'mdy',
168  '6 minutes ago',
169  'X minutes ago',
170  ),
171  array(
172  '20121006173100',
173  '20121006173200',
174  'Offset|0',
175  'mdy',
176  '1 minute ago',
177  '"1 minute ago"',
178  ),
179  array(
180  '20120617190900',
181  '20120717190900',
182  'Offset|0',
183  'mdy',
184  'June 17',
185  'Another month'
186  ),
187  array(
188  '19910130151500',
189  '20120716193700',
190  'Offset|0',
191  'mdy',
192  '15:15, January 30, 1991',
193  'Different year',
194  ),
195  array(
196  '20120101050000',
197  '20120101080000',
198  'Offset|-360',
199  'mdy',
200  'Yesterday at 23:00',
201  '"Yesterday" across years with time correction',
202  ),
203  array(
204  '20120714184300',
205  '20120716184300',
206  'Offset|-420',
207  'mdy',
208  'Saturday at 11:43',
209  'Recent weekday with time correction',
210  ),
211  array(
212  '20120714184300',
213  '20120715040000',
214  'Offset|-420',
215  'mdy',
216  '11:43',
217  'Today at another time with time correction',
218  ),
219  array(
220  '20120617190900',
221  '20120717190900',
222  'Offset|0',
223  'dmy',
224  '17 June',
225  'Another month with dmy'
226  ),
227  array(
228  '20120617190900',
229  '20120717190900',
230  'Offset|0',
231  'ISO 8601',
232  '06-17',
233  'Another month with ISO-8601'
234  ),
235  array(
236  '19910130151500',
237  '20120716193700',
238  'Offset|0',
239  'ISO 8601',
240  '1991-01-30T15:15:00',
241  'Different year with ISO-8601',
242  ),
243  );
244  }
245 
250  public function testRelativeTimestamp(
251  $tsTime, // The timestamp to format
252  $currentTime, // The time to consider "now"
253  $timeCorrection, // The time offset to use
254  $dateFormat, // The date preference to use
255  $expectedOutput, // The expected output
256  $desc // Description
257  ) {
258  $user = $this->getMock( 'User' );
259  $user->expects( $this->any() )
260  ->method( 'getOption' )
261  ->with( 'timecorrection' )
262  ->will( $this->returnValue( $timeCorrection ) );
263 
264  $tsTime = new MWTimestamp( $tsTime );
265  $currentTime = new MWTimestamp( $currentTime );
266 
267  $this->assertEquals(
268  $expectedOutput,
269  $tsTime->getRelativeTimestamp( $currentTime, $user ),
270  $desc
271  );
272  }
273 
274  public static function provideRelativeTimestampTests() {
275  return array(
276  array(
277  '20111231170000',
278  '20120101000000',
279  'Offset|0',
280  'mdy',
281  '7 hours ago',
282  '"Yesterday" across years',
283  ),
284  array(
285  '20120717190900',
286  '20120717190929',
287  'Offset|0',
288  'mdy',
289  '29 seconds ago',
290  '"Just now"',
291  ),
292  array(
293  '20120717190900',
294  '20120717191530',
295  'Offset|0',
296  'mdy',
297  '6 minutes and 30 seconds ago',
298  'Combination of multiple units',
299  ),
300  array(
301  '20121006173100',
302  '20121006173200',
303  'Offset|0',
304  'mdy',
305  '1 minute ago',
306  '"1 minute ago"',
307  ),
308  array(
309  '19910130151500',
310  '20120716193700',
311  'Offset|0',
312  'mdy',
313  '2 decades, 1 year, 168 days, 2 hours, 8 minutes and 48 seconds ago',
314  'A long time ago',
315  ),
316  array(
317  '20120101050000',
318  '20120101080000',
319  'Offset|-360',
320  'mdy',
321  '3 hours ago',
322  '"Yesterday" across years with time correction',
323  ),
324  array(
325  '20120714184300',
326  '20120716184300',
327  'Offset|-420',
328  'mdy',
329  '2 days ago',
330  'Recent weekday with time correction',
331  ),
332  array(
333  '20120714184300',
334  '20120715040000',
335  'Offset|-420',
336  'mdy',
337  '9 hours and 17 minutes ago',
338  'Today at another time with time correction',
339  ),
340  );
341  }
342 }
MWTimestampTest\provideRelativeTimestampTests
static provideRelativeTimestampTests()
Definition: MWTimestampTest.php:274
MWTimestamp
Library for creating and parsing MW-style timestamps.
Definition: MWTimestamp.php:31
MWTimestampTest\setUp
setUp()
Definition: MWTimestampTest.php:8
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
MWTimestampTest\testHumanTimestamp
testHumanTimestamp( $tsTime, $currentTime, $timeCorrection, $dateFormat, $expectedOutput, $desc)
@dataProvider provideHumanTimestampTests @covers MWTimestamp::getHumanTimestamp
Definition: MWTimestampTest.php:117
$timestamp
if( $limit) $timestamp
Definition: importImages.php:104
TS_EXIF
const TS_EXIF
An Exif timestamp (YYYY:MM:DD HH:MM:SS)
Definition: GlobalFunctions.php:2457
MWTimestampTest
Tests timestamp parsing and output.
Definition: MWTimestampTest.php:6
TS_DB
const TS_DB
MySQL DATETIME (YYYY-MM-DD HH:MM:SS)
Definition: GlobalFunctions.php:2436
TS_ORACLE
const TS_ORACLE
Oracle format time.
Definition: GlobalFunctions.php:2462
TS_ISO_8601
const TS_ISO_8601
ISO 8601 format with no timezone: 1986-02-09T20:00:00Z.
Definition: GlobalFunctions.php:2448
MWTimestampTest\testDiff
testDiff( $timestamp1, $timestamp2, $expected)
@dataProvider provideValidTimestampDifferences @covers MWTimestamp::diff
Definition: MWTimestampTest.php:45
array
the array() calling protocol came about after MediaWiki 1.4rc1.
List of Api Query prop modules.
TS_MW
const TS_MW
MediaWiki concatenated string timestamp (YYYYMMDDHHMMSS)
Definition: GlobalFunctions.php:2431
TS_ISO_8601_BASIC
const TS_ISO_8601_BASIC
ISO 8601 basic format with no timezone: 19860209T200000Z.
Definition: GlobalFunctions.php:2472
MediaWikiLangTestCase
Base class that store and restore the Language objects.
Definition: MediaWikiLangTestCase.php:6
RequestContext\getMain
static getMain()
Static methods.
Definition: RequestContext.php:420
$user
please add to it if you re going to add events to the MediaWiki code where normally authentication against an external auth plugin would be creating a account $user
Definition: hooks.txt:237
MWTimestampTest\testInvalidParse
testInvalidParse()
Test an invalid timestamp.
Definition: MWTimestampTest.php:77
TS_UNIX
const TS_UNIX
Unix time - the number of seconds since 1970-01-01 00:00:00 UTC.
Definition: GlobalFunctions.php:2426
MWTimestampTest\testConstructWithNoTimestamp
testConstructWithNoTimestamp()
@covers MWTimestamp::__construct
Definition: MWTimestampTest.php:17
MWTimestampTest\provideHumanTimestampTests
static provideHumanTimestampTests()
Definition: MWTimestampTest.php:145
MWTimestampTest\testToString
testToString()
@covers MWTimestamp::__toString
Definition: MWTimestampTest.php:27
TS_POSTGRES
const TS_POSTGRES
Postgres format time.
Definition: GlobalFunctions.php:2467
Language\factory
static factory( $code)
Get a cached or new language object for a given language code.
Definition: Language.php:184
MWTimestampTest\provideValidTimestampDifferences
provideValidTimestampDifferences()
Definition: MWTimestampTest.php:32
MWTimestampTest\testInvalidOutput
testInvalidOutput()
Test requesting an invalid output format.
Definition: MWTimestampTest.php:86
MWTimestampTest\testValidParse
testValidParse( $format, $original, $expected)
Test parsing of valid timestamps and outputing to MW format.
Definition: MWTimestampTest.php:57
MWTimestampTest\provideValidTimestamps
static provideValidTimestamps()
Returns a list of valid timestamps in the format: array( type, timestamp_of_type, timestamp_in_MW )
Definition: MWTimestampTest.php:95
TS_RFC2822
const TS_RFC2822
RFC 2822 format, for E-mail and HTTP headers.
Definition: GlobalFunctions.php:2441
MWTimestampTest\testRelativeTimestamp
testRelativeTimestamp( $tsTime, $currentTime, $timeCorrection, $dateFormat, $expectedOutput, $desc)
@dataProvider provideRelativeTimestampTests @covers MWTimestamp::getRelativeTimestamp
Definition: MWTimestampTest.php:250
MWTimestampTest\testValidOutput
testValidOutput( $format, $expected, $original)
Test outputting valid timestamps to different formats.
Definition: MWTimestampTest.php:67