Merge "mediawiki.content.json: Remove file and author annotations"
[mediawiki.git] / tests / phpunit / includes / utils / MWTimestampTest.php
blobaec0b1c9143e3be985492838d6a6d77da8a563f8
1 <?php
3 use MediaWiki\MainConfigNames;
4 use MediaWiki\User\Options\StaticUserOptionsLookup;
5 use MediaWiki\User\UserIdentityValue;
6 use MediaWiki\Utils\MWTimestamp;
8 /**
9 * @covers \MediaWiki\Utils\MWTimestamp
11 class MWTimestampTest extends MediaWikiLangTestCase {
13 private function setMockUserOptions( array $options ) {
14 $defaults = $this->getServiceContainer()->getMainConfig()->get( MainConfigNames::DefaultUserOptions );
16 // $options are set as the options for "Pamela", the name used in the tests
17 $userOptionsLookup = new StaticUserOptionsLookup(
18 [ 'Pamela' => $options ],
19 $defaults
22 $this->setService( 'UserOptionsLookup', $userOptionsLookup );
25 /**
26 * @dataProvider provideRelativeTimestampTests
28 public function testRelativeTimestamp(
29 $tsTime, // The timestamp to format
30 $currentTime, // The time to consider "now"
31 $timeCorrection, // The time offset to use
32 $dateFormat, // The date preference to use
33 $expectedOutput, // The expected output
34 $desc // Description
35 ) {
36 $this->setMockUserOptions( [
37 'timecorrection' => $timeCorrection,
38 'date' => $dateFormat
39 ] );
41 $user = new UserIdentityValue( 13, 'Pamela' );
43 $tsTime = new MWTimestamp( $tsTime );
44 $currentTime = new MWTimestamp( $currentTime );
46 $this->assertEquals(
47 $expectedOutput,
48 $tsTime->getRelativeTimestamp( $currentTime, $user ),
49 $desc
53 public static function provideRelativeTimestampTests() {
54 return [
56 '20111231170000',
57 '20120101000000',
58 'Offset|0',
59 'mdy',
60 '7 hours ago',
61 '"Yesterday" across years',
64 '20120717190900',
65 '20120717190929',
66 'Offset|0',
67 'mdy',
68 '29 seconds ago',
69 '"Just now"',
72 '20120717190900',
73 '20120717191530',
74 'Offset|0',
75 'mdy',
76 '6 minutes and 30 seconds ago',
77 'Combination of multiple units',
80 '20121006173100',
81 '20121006173200',
82 'Offset|0',
83 'mdy',
84 '1 minute ago',
85 '"1 minute ago"',
88 '19910130151500',
89 '20120716193700',
90 'Offset|0',
91 'mdy',
92 '2 decades, 1 year, 168 days, 2 hours, 8 minutes and 48 seconds ago',
93 'A long time ago',
96 '20120101050000',
97 '20120101080000',
98 'Offset|-360',
99 'mdy',
100 '3 hours ago',
101 '"Yesterday" across years with time correction',
104 '20120714184300',
105 '20120716184300',
106 'Offset|-420',
107 'mdy',
108 '2 days ago',
109 'Recent weekday with time correction',
112 '20120714184300',
113 '20120715040000',
114 'Offset|-420',
115 'mdy',
116 '9 hours and 17 minutes ago',
117 'Today at another time with time correction',