MediaWiki  1.27.2
MWTimestampTest.php
Go to the documentation of this file.
1 <?php
2 
7 
8  protected function setUp() {
9  parent::setUp();
10 
11  // Avoid 'GetHumanTimestamp' hook and others
12  $this->setMwGlobals( 'wgHooks', [] );
13  }
14 
18  public function testConstructWithNoTimestamp() {
19  $timestamp = new MWTimestamp();
20  $this->assertInternalType( 'string', $timestamp->getTimestamp() );
21  $this->assertNotEmpty( $timestamp->getTimestamp() );
22  $this->assertNotEquals( false, strtotime( $timestamp->getTimestamp( TS_MW ) ) );
23  }
24 
28  public function testToString() {
29  $timestamp = new MWTimestamp( '1406833268' ); // Equivalent to 20140731190108
30  $this->assertEquals( '1406833268', $timestamp->__toString() );
31  }
32 
33  public static function provideValidTimestampDifferences() {
34  return [
35  [ '1406833268', '1406833269', '00 00 00 01' ],
36  [ '1406833268', '1406833329', '00 00 01 01' ],
37  [ '1406833268', '1406836929', '00 01 01 01' ],
38  [ '1406833268', '1406923329', '01 01 01 01' ],
39  ];
40  }
41 
46  public function testDiff( $timestamp1, $timestamp2, $expected ) {
47  $timestamp1 = new MWTimestamp( $timestamp1 );
48  $timestamp2 = new MWTimestamp( $timestamp2 );
49  $diff = $timestamp1->diff( $timestamp2 );
50  $this->assertEquals( $expected, $diff->format( '%D %H %I %S' ) );
51  }
52 
58  public function testValidParse( $format, $original, $expected ) {
59  $timestamp = new MWTimestamp( $original );
60  $this->assertEquals( $expected, $timestamp->getTimestamp( TS_MW ) );
61  }
62 
68  public function testValidOutput( $format, $expected, $original ) {
69  $timestamp = new MWTimestamp( $original );
70  $this->assertEquals( $expected, (string)$timestamp->getTimestamp( $format ) );
71  }
72 
78  public function testInvalidParse() {
79  new MWTimestamp( "This is not a timestamp." );
80  }
81 
88  public function testOutOfRangeTimestamps( $format, $input ) {
89  $timestamp = new MWTimestamp( $input );
90  $timestamp->getTimestamp( $format );
91  }
92 
98  public function testInvalidOutput() {
99  $timestamp = new MWTimestamp( '1343761268' );
100  $timestamp->getTimestamp( 98 );
101  }
102 
107  public static function provideValidTimestamps() {
108  return [
109  // Various formats
110  [ TS_UNIX, '1343761268', '20120731190108' ],
111  [ TS_MW, '20120731190108', '20120731190108' ],
112  [ TS_DB, '2012-07-31 19:01:08', '20120731190108' ],
113  [ TS_ISO_8601, '2012-07-31T19:01:08Z', '20120731190108' ],
114  [ TS_ISO_8601_BASIC, '20120731T190108Z', '20120731190108' ],
115  [ TS_EXIF, '2012:07:31 19:01:08', '20120731190108' ],
116  [ TS_RFC2822, 'Tue, 31 Jul 2012 19:01:08 GMT', '20120731190108' ],
117  [ TS_ORACLE, '31-07-2012 19:01:08.000000', '20120731190108' ],
118  [ TS_POSTGRES, '2012-07-31 19:01:08 GMT', '20120731190108' ],
119  // Some extremes and weird values
120  [ TS_ISO_8601, '9999-12-31T23:59:59Z', '99991231235959' ],
121  [ TS_UNIX, '-62135596801', '00001231235959' ]
122  ];
123  }
124 
129  public static function provideOutOfRangeTimestamps() {
130  return [
131  // Various formats
132  [ TS_MW, '-62167219201' ], // -0001-12-31T23:59:59Z
133  [ TS_MW, '253402300800' ], // 10000-01-01T00:00:00Z
134  ];
135  }
136 
141  public function testHumanTimestamp(
142  $tsTime, // The timestamp to format
143  $currentTime, // The time to consider "now"
144  $timeCorrection, // The time offset to use
145  $dateFormat, // The date preference to use
146  $expectedOutput, // The expected output
147  $desc // Description
148  ) {
149  $user = $this->getMock( 'User' );
150  $user->expects( $this->any() )
151  ->method( 'getOption' )
152  ->with( 'timecorrection' )
153  ->will( $this->returnValue( $timeCorrection ) );
154 
155  $user->expects( $this->any() )
156  ->method( 'getDatePreference' )
157  ->will( $this->returnValue( $dateFormat ) );
158 
159  $tsTime = new MWTimestamp( $tsTime );
160  $currentTime = new MWTimestamp( $currentTime );
161 
162  $this->assertEquals(
163  $expectedOutput,
164  $tsTime->getHumanTimestamp( $currentTime, $user ),
165  $desc
166  );
167  }
168 
169  public static function provideHumanTimestampTests() {
170  return [
171  [
172  '20111231170000',
173  '20120101000000',
174  'Offset|0',
175  'mdy',
176  'Yesterday at 17:00',
177  '"Yesterday" across years',
178  ],
179  [
180  '20120717190900',
181  '20120717190929',
182  'Offset|0',
183  'mdy',
184  'just now',
185  '"Just now"',
186  ],
187  [
188  '20120717190900',
189  '20120717191530',
190  'Offset|0',
191  'mdy',
192  '6 minutes ago',
193  'X minutes ago',
194  ],
195  [
196  '20121006173100',
197  '20121006173200',
198  'Offset|0',
199  'mdy',
200  '1 minute ago',
201  '"1 minute ago"',
202  ],
203  [
204  '20120617190900',
205  '20120717190900',
206  'Offset|0',
207  'mdy',
208  'June 17',
209  'Another month'
210  ],
211  [
212  '19910130151500',
213  '20120716193700',
214  'Offset|0',
215  'mdy',
216  '15:15, January 30, 1991',
217  'Different year',
218  ],
219  [
220  '20120101050000',
221  '20120101080000',
222  'Offset|-360',
223  'mdy',
224  'Yesterday at 23:00',
225  '"Yesterday" across years with time correction',
226  ],
227  [
228  '20120714184300',
229  '20120716184300',
230  'Offset|-420',
231  'mdy',
232  'Saturday at 11:43',
233  'Recent weekday with time correction',
234  ],
235  [
236  '20120714184300',
237  '20120715040000',
238  'Offset|-420',
239  'mdy',
240  '11:43',
241  'Today at another time with time correction',
242  ],
243  [
244  '20120617190900',
245  '20120717190900',
246  'Offset|0',
247  'dmy',
248  '17 June',
249  'Another month with dmy'
250  ],
251  [
252  '20120617190900',
253  '20120717190900',
254  'Offset|0',
255  'ISO 8601',
256  '06-17',
257  'Another month with ISO-8601'
258  ],
259  [
260  '19910130151500',
261  '20120716193700',
262  'Offset|0',
263  'ISO 8601',
264  '1991-01-30T15:15:00',
265  'Different year with ISO-8601',
266  ],
267  ];
268  }
269 
274  public function testRelativeTimestamp(
275  $tsTime, // The timestamp to format
276  $currentTime, // The time to consider "now"
277  $timeCorrection, // The time offset to use
278  $dateFormat, // The date preference to use
279  $expectedOutput, // The expected output
280  $desc // Description
281  ) {
282  $user = $this->getMock( 'User' );
283  $user->expects( $this->any() )
284  ->method( 'getOption' )
285  ->with( 'timecorrection' )
286  ->will( $this->returnValue( $timeCorrection ) );
287 
288  $tsTime = new MWTimestamp( $tsTime );
289  $currentTime = new MWTimestamp( $currentTime );
290 
291  $this->assertEquals(
292  $expectedOutput,
293  $tsTime->getRelativeTimestamp( $currentTime, $user ),
294  $desc
295  );
296  }
297 
298  public static function provideRelativeTimestampTests() {
299  return [
300  [
301  '20111231170000',
302  '20120101000000',
303  'Offset|0',
304  'mdy',
305  '7 hours ago',
306  '"Yesterday" across years',
307  ],
308  [
309  '20120717190900',
310  '20120717190929',
311  'Offset|0',
312  'mdy',
313  '29 seconds ago',
314  '"Just now"',
315  ],
316  [
317  '20120717190900',
318  '20120717191530',
319  'Offset|0',
320  'mdy',
321  '6 minutes and 30 seconds ago',
322  'Combination of multiple units',
323  ],
324  [
325  '20121006173100',
326  '20121006173200',
327  'Offset|0',
328  'mdy',
329  '1 minute ago',
330  '"1 minute ago"',
331  ],
332  [
333  '19910130151500',
334  '20120716193700',
335  'Offset|0',
336  'mdy',
337  '2 decades, 1 year, 168 days, 2 hours, 8 minutes and 48 seconds ago',
338  'A long time ago',
339  ],
340  [
341  '20120101050000',
342  '20120101080000',
343  'Offset|-360',
344  'mdy',
345  '3 hours ago',
346  '"Yesterday" across years with time correction',
347  ],
348  [
349  '20120714184300',
350  '20120716184300',
351  'Offset|-420',
352  'mdy',
353  '2 days ago',
354  'Recent weekday with time correction',
355  ],
356  [
357  '20120714184300',
358  '20120715040000',
359  'Offset|-420',
360  'mdy',
361  '9 hours and 17 minutes ago',
362  'Today at another time with time correction',
363  ],
364  ];
365  }
366 }
const TS_RFC2822
RFC 2822 format, for E-mail and HTTP headers.
testInvalidParse()
Test an invalid timestamp.
static provideHumanTimestampTests()
static provideValidTimestamps()
Returns a list of valid timestamps in the format: array( type, timestamp_of_type, timestamp_in_MW ) ...
testToString()
MWTimestamp::__toString.
testOutOfRangeTimestamps($format, $input)
Test an out of range timestamp provideOutOfRangeTimestamps TimestampException MWTimestamp.
static provideOutOfRangeTimestamps()
Returns a list of out of range timestamps in the format: array( type, timestamp_of_type ) ...
testValidOutput($format, $expected, $original)
Test outputting valid timestamps to different formats.
const TS_EXIF
An Exif timestamp (YYYY:MM:DD HH:MM:SS)
testHumanTimestamp($tsTime, $currentTime, $timeCorrection, $dateFormat, $expectedOutput, $desc)
provideHumanTimestampTests MWTimestamp::getHumanTimestamp
if($limit) $timestamp
const TS_ISO_8601
ISO 8601 format with no timezone: 1986-02-09T20:00:00Z.
const TS_ORACLE
Oracle format time.
Base class that store and restore the Language objects.
const TS_DB
MySQL DATETIME (YYYY-MM-DD HH:MM:SS)
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 local account $user
Definition: hooks.txt:242
static provideValidTimestampDifferences()
const TS_MW
MediaWiki concatenated string timestamp (YYYYMMDDHHMMSS)
testValidParse($format, $original, $expected)
Test parsing of valid timestamps and outputing to MW format.
injection txt This is an overview of how MediaWiki makes use of dependency injection The design described here grew from the discussion of RFC T384 The term dependency this means that anything an object needs to operate should be injected from the the object itself should only know narrow no concrete implementation of the logic it relies on The requirement to inject everything typically results in an architecture that based on two main types of and essentially stateless service objects that use other service objects to operate on the value objects As of the beginning MediaWiki is only starting to use the DI approach Much of the code still relies on global state or direct resulting in a highly cyclical dependency which acts as the top level factory for services in MediaWiki which can be used to gain access to default instances of various services MediaWikiServices however also allows new services to be defined and default services to be redefined Services are defined or redefined by providing a callback the instantiator that will return a new instance of the service When it will create an instance of MediaWikiServices and populate it with the services defined in the files listed by thereby bootstrapping the DI framework Per $wgServiceWiringFiles lists includes ServiceWiring php
Definition: injection.txt:35
testDiff($timestamp1, $timestamp2, $expected)
provideValidTimestampDifferences MWTimestamp::diff
const TS_UNIX
Unix time - the number of seconds since 1970-01-01 00:00:00 UTC.
setMwGlobals($pairs, $value=null)
testRelativeTimestamp($tsTime, $currentTime, $timeCorrection, $dateFormat, $expectedOutput, $desc)
provideRelativeTimestampTests MWTimestamp::getRelativeTimestamp
const TS_POSTGRES
Postgres format time.
Library for creating and parsing MW-style timestamps.
Definition: MWTimestamp.php:31
testInvalidOutput()
Test requesting an invalid output format.
testConstructWithNoTimestamp()
MWTimestamp::__construct.
static provideRelativeTimestampTests()
Tests timestamp parsing and output.
const TS_ISO_8601_BASIC
ISO 8601 basic format with no timezone: 19860209T200000Z.