1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
|
<?php
/**
* This file is intended to test magic variables in the parser
* It was inspired by Raymond & Matěj Grabovský commenting about r66200
*
* As of february 2011, it only tests some revisions and date related
* magic variables.
*
* @author Antoine Musso
* @copyright Copyright © 2011, Antoine Musso
* @file
*/
namespace MediaWiki\Tests\Parser;
use MediaWiki\MainConfigNames;
use MediaWiki\Parser\Parser;
use MediaWiki\Parser\ParserOptions;
use MediaWiki\Title\Title;
use MediaWiki\User\User;
use MediaWikiIntegrationTestCase;
use Wikimedia\TestingAccessWrapper;
/**
* @group Database
* @covers \MediaWiki\Parser\Parser::expandMagicVariable
*/
class MagicVariableTest extends MediaWikiIntegrationTestCase {
/**
* @var Parser
*/
private $testParser = null;
/** setup a basic parser object */
protected function setUp(): void {
parent::setUp();
$services = $this->getServiceContainer();
$contLang = $services->getLanguageFactory()->getLanguage( 'en' );
$this->setService( 'ContentLanguage', $contLang );
$this->overrideConfigValues( [
MainConfigNames::LanguageCode => $contLang->getCode(),
// NOTE: Europe/Stockholm DST applies Sun, Mar 26, 2023 2:00 - Sun, Oct 29, 2023 3:00AM
MainConfigNames::Localtimezone => 'Europe/Stockholm',
MainConfigNames::MiserMode => false,
MainConfigNames::ParserCacheExpireTime => 86400 * 7,
] );
$this->testParser = $services->getParserFactory()->create();
$this->testParser->setOptions( ParserOptions::newFromUserAndLang( new User, $contLang ) );
# initialize parser output
$this->testParser->clearState();
# Needs a title to do magic word stuff
$title = Title::makeTitle( NS_MAIN, 'Tests' );
# Else it needs a db connection just to check if it's a redirect
# (when deciding the page language).
$title->mRedirect = false;
$this->testParser->setTitle( $title );
}
/**
* @param int $num Upper limit for numbers
* @return array Array of strings naming numbers from 1 up to $num
*/
private static function createProviderUpTo( $num ) {
$ret = [];
for ( $i = 1; $i <= $num; $i++ ) {
$ret[] = [ strval( $i ) ];
}
return $ret;
}
/**
* @return array Array of months numbers (as an integer)
*/
public static function provideMonths() {
return self::createProviderUpTo( 12 );
}
/**
* @return array Array of days numbers (as an integer)
*/
public static function provideDays() {
return self::createProviderUpTo( 31 );
}
# ############## TESTS #############################################
# @todo FIXME:
# - those got copy pasted, we can probably make them cleaner
# - tests are lacking useful messages
# day
/** @dataProvider provideDays */
public function testCurrentdayIsUnPadded( $day ) {
$this->assertUnPadded( 'currentday', $day );
}
/** @dataProvider provideDays */
public function testCurrentdaytwoIsZeroPadded( $day ) {
$this->assertZeroPadded( 'currentday2', $day );
}
/** @dataProvider provideDays */
public function testLocaldayIsUnPadded( $day ) {
$this->assertUnPadded( 'localday', $day );
}
/** @dataProvider provideDays */
public function testLocaldaytwoIsZeroPadded( $day ) {
$this->assertZeroPadded( 'localday2', $day );
}
# month
/** @dataProvider provideMonths */
public function testCurrentmonthIsZeroPadded( $month ) {
$this->assertZeroPadded( 'currentmonth', $month );
}
/** @dataProvider provideMonths */
public function testCurrentmonthoneIsUnPadded( $month ) {
$this->assertUnPadded( 'currentmonth1', $month );
}
/** @dataProvider provideMonths */
public function testLocalmonthIsZeroPadded( $month ) {
$this->assertZeroPadded( 'localmonth', $month );
}
/** @dataProvider provideMonths */
public function testLocalmonthoneIsUnPadded( $month ) {
$this->assertUnPadded( 'localmonth1', $month );
}
# revision day
/** @dataProvider provideDays */
public function testRevisiondayIsUnPadded( $day ) {
$this->assertUnPadded( 'revisionday', $day );
}
/** @dataProvider provideDays */
public function testRevisiondaytwoIsZeroPadded( $day ) {
$this->assertZeroPadded( 'revisionday2', $day );
}
# revision month
/** @dataProvider provideMonths */
public function testRevisionmonthIsZeroPadded( $month ) {
$this->assertZeroPadded( 'revisionmonth', $month );
}
/** @dataProvider provideMonths */
public function testRevisionmonthoneIsUnPadded( $month ) {
$this->assertUnPadded( 'revisionmonth1', $month );
}
public static function provideCurrentUnitTimestampWords() {
return [
// Afternoon
[ 'currentmonth', '20200208153011', '02', 604800 ],
[ 'currentmonth1', '20200208153011', '2', 604800 ],
[ 'currentmonthname', '20200208153011', 'February', 604800 ],
[ 'currentmonthnamegen', '20200208153011', 'February', 604800 ],
[ 'currentmonthabbrev', '20200208153011', 'Feb', 604800 ],
[ 'currentday', '20200208153011', '8', 30601 ],
[ 'currentday2', '20200208153011', '08', 30601 ],
[ 'currentdayname', '20200208153011', 'Saturday', 30601 ],
[ 'currentyear', '20200208153011', '2020', 604800 ],
[ 'currenthour', '20200208153011', '15', 1801 ],
[ 'currentweek', '20200208153011', '6', 30601 ],
[ 'currentdow', '20200208153011', '6', 30601 ],
[ 'currenttime', '20200208153011', '15:30', 3600 ],
// Night
[ 'currentmonth', '20200208223011', '02', 604800 ],
[ 'currentmonth1', '20200208223011', '2', 604800 ],
[ 'currentmonthname', '20200208223011', 'February', 604800 ],
[ 'currentmonthnamegen', '20200208223011', 'February', 604800 ],
[ 'currentmonthabbrev', '20200208223011', 'Feb', 604800 ],
[ 'currentday', '20200208223011', '8', 5401 ],
[ 'currentday2', '20200208223011', '08', 5401 ],
[ 'currentdayname', '20200208223011', 'Saturday', 5401 ],
[ 'currentyear', '20200208223011', '2020', 604800 ],
[ 'currenthour', '20200208223011', '22', 1801 ],
[ 'currentweek', '20200208223011', '6', 5401 ],
[ 'currentdow', '20200208223011', '6', 5401 ],
[ 'currenttime', '20200208223011', '22:30', 3600 ]
];
}
public static function provideLocalUnitTimestampWords() {
// NOTE: Europe/Stockholm DST applies Sun, Mar 26, 2023 2:00 - Sun, Oct 29, 2023 3:00AM
return [
// Afternoon
[ 'localmonth', '20200208153011', '02', 604800 ],
[ 'localmonth1', '20200208153011', '2', 604800 ],
[ 'localmonthname', '20200208153011', 'February', 604800 ],
[ 'localmonthnamegen', '20200208153011', 'February', 604800 ],
[ 'localmonthabbrev', '20200208153011', 'Feb', 604800 ],
[ 'localday', '20200208153011', '8', 27001 ],
[ 'localday2', '20200208153011', '08', 27001 ],
[ 'localdayname', '20200208153011', 'Saturday', 27001 ],
[ 'localyear', '20200208153011', '2020', 604800 ],
[ 'localhour', '20200208153011', '16', 1801 ],
[ 'localweek', '20200208153011', '6', 27001 ],
[ 'localdow', '20200208153011', '6', 27001 ],
[ 'localtime', '20200208153011', '16:30', 3600 ],
// Night
[ 'localmonth', '20200208223011', '02', 604800 ],
[ 'localmonth1', '20200208223011', '2', 604800 ],
[ 'localmonthname', '20200208223011', 'February', 604800 ],
[ 'localmonthnamegen', '20200208223011', 'February', 604800 ],
[ 'localmonthabbrev', '20200208223011', 'Feb', 604800 ],
[ 'localday', '20200208223011', '8', 1801 ],
[ 'localday2', '20200208223011', '08', 1801 ],
[ 'localdayname', '20200208223011', 'Saturday', 1801 ],
[ 'localyear', '20200208223011', '2020', 604800 ],
[ 'localhour', '20200208223011', '23', 1801 ],
[ 'localweek', '20200208223011', '6', 1801 ],
[ 'localdow', '20200208223011', '6', 1801 ],
[ 'localtime', '20200208223011', '23:30', 3600 ],
// Late night / early morning
[ 'localmonth', '20200208233011', '02', 604800 ],
[ 'localmonth1', '20200208233011', '2', 604800 ],
[ 'localmonthname', '20200208233011', 'February', 604800 ],
[ 'localmonthnamegen', '20200208233011', 'February', 604800 ],
[ 'localmonthabbrev', '20200208233011', 'Feb', 604800 ],
[ 'localday', '20200208233011', '9', 84601 ],
[ 'localday2', '20200208233011', '09', 84601 ],
[ 'localdayname', '20200208233011', 'Sunday', 84601 ],
[ 'localyear', '20200208233011', '2020', 604800 ],
[ 'localhour', '20200208233011', '00', 1801 ],
[ 'localweek', '20200208233011', '6', 84601 ],
[ 'localdow', '20200208233011', '0', 84601 ],
[ 'localtime', '20200208233011', '00:30', 3600 ]
];
}
/**
* @param string $word
* @param string $ts
* @param string $expOutput
* @param int $expTTL
* @dataProvider provideCurrentUnitTimestampWords
* @dataProvider provideLocalUnitTimestampWords
*/
public function testCurrentUnitTimestampExpiry( $word, $ts, $expOutput, $expTTL ) {
$this->setParserTimestamp( $ts );
$this->assertMagic( $expOutput, $word );
$this->assertSame( $expTTL, $this->testParser->getOutput()->getCacheExpiry() );
}
# ############## HELPERS ############################################
/**
* assertion helper expecting a magic output which is zero padded
* @param string $magic
* @param string $value
*/
public function assertZeroPadded( $magic, $value ) {
$this->assertMagicPadding( $magic, $value, '%02d' );
}
/**
* assertion helper expecting a magic output which is unpadded
* @param string $magic
* @param string $value
*/
public function assertUnPadded( $magic, $value ) {
$this->assertMagicPadding( $magic, $value, '%d' );
}
/**
* Main assertion helper for magic variables padding
* @param string $magic Magic variable name
* @param mixed $value Month or day
* @param string $format Sprintf format for $value
*/
private function assertMagicPadding( $magic, $value, $format ) {
# Initialize parser timestamp as year 2010 at 12h34 56s.
# month and day are given by the caller ($value). Month < 12!
if ( $value > 12 ) {
$month = $value % 12;
} else {
$month = $value;
}
$this->setParserTimestamp(
sprintf( '2010%02d%02d123456', $month, $value )
);
# please keep the following commented line of code. It helps debugging.
// print "\nDEBUG (value $value):" . sprintf( '2010%02d%02d123456', $value, $value ) . "\n";
# format expectation and test it
$expected = sprintf( $format, $value );
$this->assertMagic( $expected, $magic );
}
/**
* helper to set the parser timestamp and revision timestamp
* @param string $ts
*/
private function setParserTimestamp( $ts ) {
$this->testParser->getOptions()->setTimestamp( $ts );
TestingAccessWrapper::newFromObject( $this->testParser )->mRevisionTimestamp = $ts;
}
/**
* Assertion helper to test a magic variable output
* @param string|int $expected
* @param string $magic
*/
private function assertMagic( $expected, $magic ) {
# Generate a message for the assertion
$msg = sprintf( "Magic %s should be <%s:%s>",
$magic,
$expected,
get_debug_type( $expected )
);
$this->assertSame(
$expected,
TestingAccessWrapper::newFromObject( $this->testParser )->expandMagicVariable( $magic ),
$msg
);
}
}
|