MediaWiki  REL1_31
MWTimestampTest.php
Go to the documentation of this file.
1 <?php
2 
7  protected function setUp() {
8  parent::setUp();
9 
10  // Avoid 'GetHumanTimestamp' hook and others
11  $this->setMwGlobals( 'wgHooks', [] );
12  }
13 
18  public function testHumanTimestamp(
19  $tsTime, // The timestamp to format
20  $currentTime, // The time to consider "now"
21  $timeCorrection, // The time offset to use
22  $dateFormat, // The date preference to use
23  $expectedOutput, // The expected output
24  $desc // Description
25  ) {
26  $user = $this->createMock( User::class );
27  $user->expects( $this->any() )
28  ->method( 'getOption' )
29  ->with( 'timecorrection' )
30  ->will( $this->returnValue( $timeCorrection ) );
31 
32  $user->expects( $this->any() )
33  ->method( 'getDatePreference' )
34  ->will( $this->returnValue( $dateFormat ) );
35 
36  $tsTime = new MWTimestamp( $tsTime );
37  $currentTime = new MWTimestamp( $currentTime );
38 
39  $this->assertEquals(
40  $expectedOutput,
41  $tsTime->getHumanTimestamp( $currentTime, $user ),
42  $desc
43  );
44  }
45 
46  public static function provideHumanTimestampTests() {
47  return [
48  [
49  '20111231170000',
50  '20120101000000',
51  'Offset|0',
52  'mdy',
53  'Yesterday at 17:00',
54  '"Yesterday" across years',
55  ],
56  [
57  '20120717190900',
58  '20120717190929',
59  'Offset|0',
60  'mdy',
61  'just now',
62  '"Just now"',
63  ],
64  [
65  '20120717190900',
66  '20120717191530',
67  'Offset|0',
68  'mdy',
69  '6 minutes ago',
70  'X minutes ago',
71  ],
72  [
73  '20121006173100',
74  '20121006173200',
75  'Offset|0',
76  'mdy',
77  '1 minute ago',
78  '"1 minute ago"',
79  ],
80  [
81  '20120617190900',
82  '20120717190900',
83  'Offset|0',
84  'mdy',
85  'June 17',
86  'Another month'
87  ],
88  [
89  '19910130151500',
90  '20120716193700',
91  'Offset|0',
92  'mdy',
93  '15:15, January 30, 1991',
94  'Different year',
95  ],
96  [
97  '20120101050000',
98  '20120101080000',
99  'Offset|-360',
100  'mdy',
101  'Yesterday at 23:00',
102  '"Yesterday" across years with time correction',
103  ],
104  [
105  '20120714184300',
106  '20120716184300',
107  'Offset|-420',
108  'mdy',
109  'Saturday at 11:43',
110  'Recent weekday with time correction',
111  ],
112  [
113  '20120714184300',
114  '20120715040000',
115  'Offset|-420',
116  'mdy',
117  '11:43',
118  'Today at another time with time correction',
119  ],
120  [
121  '20120617190900',
122  '20120717190900',
123  'Offset|0',
124  'dmy',
125  '17 June',
126  'Another month with dmy'
127  ],
128  [
129  '20120617190900',
130  '20120717190900',
131  'Offset|0',
132  'ISO 8601',
133  '06-17',
134  'Another month with ISO-8601'
135  ],
136  [
137  '19910130151500',
138  '20120716193700',
139  'Offset|0',
140  'ISO 8601',
141  '1991-01-30T15:15:00',
142  'Different year with ISO-8601',
143  ],
144  ];
145  }
146 
151  public function testRelativeTimestamp(
152  $tsTime, // The timestamp to format
153  $currentTime, // The time to consider "now"
154  $timeCorrection, // The time offset to use
155  $dateFormat, // The date preference to use
156  $expectedOutput, // The expected output
157  $desc // Description
158  ) {
159  $user = $this->createMock( User::class );
160  $user->expects( $this->any() )
161  ->method( 'getOption' )
162  ->with( 'timecorrection' )
163  ->will( $this->returnValue( $timeCorrection ) );
164 
165  $tsTime = new MWTimestamp( $tsTime );
166  $currentTime = new MWTimestamp( $currentTime );
167 
168  $this->assertEquals(
169  $expectedOutput,
170  $tsTime->getRelativeTimestamp( $currentTime, $user ),
171  $desc
172  );
173  }
174 
175  public static function provideRelativeTimestampTests() {
176  return [
177  [
178  '20111231170000',
179  '20120101000000',
180  'Offset|0',
181  'mdy',
182  '7 hours ago',
183  '"Yesterday" across years',
184  ],
185  [
186  '20120717190900',
187  '20120717190929',
188  'Offset|0',
189  'mdy',
190  '29 seconds ago',
191  '"Just now"',
192  ],
193  [
194  '20120717190900',
195  '20120717191530',
196  'Offset|0',
197  'mdy',
198  '6 minutes and 30 seconds ago',
199  'Combination of multiple units',
200  ],
201  [
202  '20121006173100',
203  '20121006173200',
204  'Offset|0',
205  'mdy',
206  '1 minute ago',
207  '"1 minute ago"',
208  ],
209  [
210  '19910130151500',
211  '20120716193700',
212  'Offset|0',
213  'mdy',
214  '2 decades, 1 year, 168 days, 2 hours, 8 minutes and 48 seconds ago',
215  'A long time ago',
216  ],
217  [
218  '20120101050000',
219  '20120101080000',
220  'Offset|-360',
221  'mdy',
222  '3 hours ago',
223  '"Yesterday" across years with time correction',
224  ],
225  [
226  '20120714184300',
227  '20120716184300',
228  'Offset|-420',
229  'mdy',
230  '2 days ago',
231  'Recent weekday with time correction',
232  ],
233  [
234  '20120714184300',
235  '20120715040000',
236  'Offset|-420',
237  'mdy',
238  '9 hours and 17 minutes ago',
239  'Today at another time with time correction',
240  ],
241  ];
242  }
243 }
MWTimestampTest\provideRelativeTimestampTests
static provideRelativeTimestampTests()
Definition: MWTimestampTest.php:175
$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:247
MWTimestamp
Library for creating and parsing MW-style timestamps.
Definition: MWTimestamp.php:32
MWTimestampTest\setUp
setUp()
Definition: MWTimestampTest.php:7
MWTimestampTest\testHumanTimestamp
testHumanTimestamp( $tsTime, $currentTime, $timeCorrection, $dateFormat, $expectedOutput, $desc)
provideHumanTimestampTests MWTimestamp::getHumanTimestamp
Definition: MWTimestampTest.php:18
MWTimestampTest
Tests timestamp parsing and output.
Definition: MWTimestampTest.php:6
php
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:37
MediaWikiTestCase\setMwGlobals
setMwGlobals( $pairs, $value=null)
Sets a global, maintaining a stashed version of the previous global to be restored in tearDown.
Definition: MediaWikiTestCase.php:678
MediaWikiLangTestCase
Base class that store and restore the Language objects.
Definition: MediaWikiLangTestCase.php:6
MWTimestampTest\provideHumanTimestampTests
static provideHumanTimestampTests()
Definition: MWTimestampTest.php:46
class
you have access to all of the normal MediaWiki so you can get a DB use the etc For full docs on the Maintenance class
Definition: maintenance.txt:56
MWTimestampTest\testRelativeTimestamp
testRelativeTimestamp( $tsTime, $currentTime, $timeCorrection, $dateFormat, $expectedOutput, $desc)
provideRelativeTimestampTests MWTimestamp::getRelativeTimestamp
Definition: MWTimestampTest.php:151