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
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
|
<?php
use MediaWiki\MediaWikiServices;
use MediaWiki\User\UserIdentityValue;
use Wikimedia\ScopedCallback;
/**
* @covers ParserOptions
*/
class ParserOptionsTest extends MediaWikiLangTestCase {
protected function setUp() : void {
parent::setUp();
$this->setMwGlobals( [
'wgRenderHashAppend' => '',
] );
// This is crazy, but registering false, null, or other falsey values
// as a hook callback "works".
$this->setTemporaryHook( 'PageRenderingHash', null );
}
protected function tearDown() : void {
ParserOptions::clearStaticCache();
parent::tearDown();
}
public function testNewCanonical() {
$user = $this->getMutableTestUser()->getUser();
$userLang = MediaWikiServices::getInstance()->getLanguageFactory()->getLanguage( 'fr' );
$contLang = MediaWikiServices::getInstance()->getLanguageFactory()->getLanguage( 'qqx' );
$this->setContentLang( $contLang );
$this->setMwGlobals( [
'wgLang' => $userLang,
] );
$lang = $this->getServiceContainer()->getLanguageFactory()->getLanguage( 'de' );
$lang2 = $this->getServiceContainer()->getLanguageFactory()->getLanguage( 'bug' );
$context = new DerivativeContext( RequestContext::getMain() );
$context->setUser( $user );
$context->setLanguage( $lang );
// Just a user uses $wgLang
$popt = ParserOptions::newCanonical( $user );
$this->assertSame( $user, $popt->getUser() );
$this->assertSame( $userLang, $popt->getUserLangObj() );
// Passing both works
$popt = ParserOptions::newCanonical( $user, $lang );
$this->assertSame( $user, $popt->getUser() );
$this->assertSame( $lang, $popt->getUserLangObj() );
// Passing 'canonical' uses an anon and $contLang, and ignores any passed $userLang
$popt = ParserOptions::newCanonical( 'canonical' );
$this->assertTrue( $popt->getUserIdentity()->isAnon() );
$this->assertSame( $contLang, $popt->getUserLangObj() );
$popt = ParserOptions::newCanonical( 'canonical', $lang2 );
$this->assertSame( $contLang, $popt->getUserLangObj() );
// Passing an IContextSource uses the user and lang from it, and ignores
// any passed $userLang
$popt = ParserOptions::newCanonical( $context );
$this->assertSame( $user, $popt->getUser() );
$this->assertSame( $lang, $popt->getUserLangObj() );
$popt = ParserOptions::newCanonical( $context, $lang2 );
$this->assertSame( $lang, $popt->getUserLangObj() );
// Passing something else raises an exception
try {
$popt = ParserOptions::newCanonical( 'bogus' );
$this->fail( 'Excpected exception not thrown' );
} catch ( InvalidArgumentException $ex ) {
}
}
/**
* @dataProvider provideIsSafeToCache
* @param bool $expect Expected value
* @param array $options Options to set
* @param array|null $usedOptions
*/
public function testIsSafeToCache( bool $expect, array $options, array $usedOptions = null ) {
$popt = ParserOptions::newCanonical( 'canonical' );
foreach ( $options as $name => $value ) {
$popt->setOption( $name, $value );
}
$this->assertSame( $expect, $popt->isSafeToCache( $usedOptions ) );
}
public static function provideIsSafeToCache() {
global $wgEnableParserLimitReporting;
$seven = static function () {
return 7;
};
return [
'No overrides' => [ true, [] ],
'No overrides, some used' => [ true, [], [ 'thumbsize', 'removeComments' ] ],
'In-key options are ok' => [ true, [
'thumbsize' => 1e100,
'printable' => false,
] ],
'In-key options are ok, some used' => [ true, [
'thumbsize' => 1e100,
'printable' => false,
], [ 'thumbsize', 'removeComments' ] ],
'Non-in-key options are not ok' => [ false, [
'removeComments' => false,
] ],
'Non-in-key options are not ok, used' => [ false, [
'removeComments' => false,
], [ 'removeComments' ] ],
'Non-in-key options are ok if other used' => [ true, [
'removeComments' => false,
], [ 'thumbsize' ] ],
'Non-in-key options are ok if nothing used' => [ true, [
'removeComments' => false,
], [] ],
'Unknown used options do not crash' => [ true, [
], [ 'unknown' ] ],
'Non-in-key options are not ok (2)' => [ false, [
'wrapclass' => 'foobar',
] ],
'Callback not default' => [ true, [
'speculativeRevIdCallback' => $seven,
] ],
'Canonical override, not default (1)' => [ true, [
'enableLimitReport' => $wgEnableParserLimitReporting,
] ],
'Canonical override, not default (2)' => [ false, [
'enableLimitReport' => !$wgEnableParserLimitReporting,
] ],
];
}
/**
* @dataProvider provideOptionsHash
* @param array $usedOptions Used options
* @param string $expect Expected value
* @param array $options Options to set
* @param array $globals Globals to set
* @param callable|null $hookFunc PageRenderingHash hook function
*/
public function testOptionsHash(
$usedOptions, $expect, $options, $globals = [], $hookFunc = null
) {
$this->setMwGlobals( $globals );
$this->setTemporaryHook( 'PageRenderingHash', $hookFunc );
$popt = ParserOptions::newCanonical( 'canonical' );
foreach ( $options as $name => $value ) {
$popt->setOption( $name, $value );
}
$this->assertSame( $expect, $popt->optionsHash( $usedOptions ) );
}
public static function provideOptionsHash() {
$used = [ 'thumbsize', 'printable' ];
$allUsableOptions = array_diff(
ParserOptions::allCacheVaryingOptions(),
array_keys( ParserOptions::getLazyOptions() )
);
return [
'Canonical options, nothing used' => [ [], 'canonical', [] ],
'Canonical options, used some options' => [ $used, 'canonical', [] ],
'Canonical options, used some more options' => [ array_merge( $used, [ 'wrapclass' ] ), 'canonical', [] ],
'Used some options, non-default values' => [
$used,
'printable=1!thumbsize=200',
[
'thumbsize' => 200,
'printable' => true,
]
],
'Canonical options, used all non-lazy options' => [ $allUsableOptions, 'canonical', [] ],
'Canonical options, nothing used, but with hooks and $wgRenderHashAppend' => [
[],
'canonical!wgRenderHashAppend!onPageRenderingHash',
[],
[ 'wgRenderHashAppend' => '!wgRenderHashAppend' ],
[ __CLASS__ . '::onPageRenderingHash' ],
],
];
}
public function testUsedLazyOptionsInHash() {
$this->setTemporaryHook( 'ParserOptionsRegister',
function ( &$defaults, &$inCacheKey, &$lazyOptions ) {
$lazyFuncs = $this->getMockBuilder( stdClass::class )
->addMethods( [ 'neverCalled', 'calledOnce' ] )
->getMock();
$lazyFuncs->expects( $this->never() )->method( 'neverCalled' );
$lazyFuncs->expects( $this->once() )->method( 'calledOnce' )->willReturn( 'value' );
$defaults += [
'opt1' => null,
'opt2' => null,
'opt3' => null,
];
$inCacheKey += [
'opt1' => true,
'opt2' => true,
];
$lazyOptions += [
'opt1' => [ $lazyFuncs, 'calledOnce' ],
'opt2' => [ $lazyFuncs, 'neverCalled' ],
'opt3' => [ $lazyFuncs, 'neverCalled' ],
];
}
);
ParserOptions::clearStaticCache();
$popt = ParserOptions::newCanonical( 'canonical' );
$popt->registerWatcher( function () {
$this->fail( 'Watcher should not have been called' );
} );
$this->assertSame( 'opt1=value', $popt->optionsHash( [ 'opt1', 'opt3' ] ) );
// Second call to see that opt1 isn't resolved a second time
$this->assertSame( 'opt1=value', $popt->optionsHash( [ 'opt1', 'opt3' ] ) );
}
public static function onPageRenderingHash( &$confstr ) {
$confstr .= '!onPageRenderingHash';
}
public function testGetInvalidOption() {
$popt = ParserOptions::newCanonical( 'canonical' );
$this->expectException( InvalidArgumentException::class );
$this->expectExceptionMessage( "Unknown parser option bogus" );
$popt->getOption( 'bogus' );
}
public function testSetInvalidOption() {
$popt = ParserOptions::newCanonical( 'canonical' );
$this->expectException( InvalidArgumentException::class );
$this->expectExceptionMessage( "Unknown parser option bogus" );
$popt->setOption( 'bogus', true );
}
public function testMatches() {
$popt1 = ParserOptions::newCanonical( 'canonical' );
$popt2 = ParserOptions::newCanonical( 'canonical' );
$this->assertTrue( $popt1->matches( $popt2 ) );
$popt1->enableLimitReport( true );
$popt2->enableLimitReport( false );
$this->assertTrue( $popt1->matches( $popt2 ) );
$popt2->setInterfaceMessage( !$popt2->getInterfaceMessage() );
$this->assertFalse( $popt1->matches( $popt2 ) );
$ctr = 0;
$this->setTemporaryHook( 'ParserOptionsRegister',
static function ( &$defaults, &$inCacheKey, &$lazyOptions ) use ( &$ctr ) {
$defaults['testMatches'] = null;
$lazyOptions['testMatches'] = static function () use ( &$ctr ) {
return ++$ctr;
};
}
);
ParserOptions::clearStaticCache();
$popt1 = ParserOptions::newCanonical( 'canonical' );
$popt2 = ParserOptions::newCanonical( 'canonical' );
$this->assertFalse( $popt1->matches( $popt2 ) );
ScopedCallback::consume( $reset );
}
/**
* This test fails if tearDown() does not call ParserOptions::clearStaticCache(),
* because the lazy option from the hook in the previous test remains active.
*/
public function testTeardownClearedCache() {
$popt1 = ParserOptions::newCanonical( 'canonical' );
$popt2 = ParserOptions::newCanonical( 'canonical' );
$this->assertTrue( $popt1->matches( $popt2 ) );
}
public function testMatchesForCacheKey() {
$user = new UserIdentityValue( 0, '127.0.0.1' );
$cOpts = ParserOptions::newCanonical(
$user,
$this->getServiceContainer()->getLanguageFactory()->getLanguage( 'en' )
);
$uOpts = ParserOptions::newFromAnon();
$this->assertTrue( $cOpts->matchesForCacheKey( $uOpts ) );
$uOpts = ParserOptions::newFromUser( $user );
$this->assertTrue( $cOpts->matchesForCacheKey( $uOpts ) );
$this->getServiceContainer()
->getUserOptionsManager()
->setOption( $user, 'thumbsize', 251 );
$uOpts = ParserOptions::newFromUser( $user );
$this->assertFalse( $cOpts->matchesForCacheKey( $uOpts ) );
$this->getServiceContainer()
->getUserOptionsManager()
->setOption( $user, 'stubthreshold', 800 );
$uOpts = ParserOptions::newFromUser( $user );
$this->assertFalse( $cOpts->matchesForCacheKey( $uOpts ) );
$uOpts = ParserOptions::newFromUserAndLang(
$user,
$this->getServiceContainer()->getLanguageFactory()->getLanguage( 'zh' )
);
$this->assertFalse( $cOpts->matchesForCacheKey( $uOpts ) );
}
public function testAllCacheVaryingOptions() {
$this->setTemporaryHook( 'ParserOptionsRegister', null );
$this->assertSame( [
'dateformat', 'numberheadings', 'printable',
'thumbsize', 'userlang'
], ParserOptions::allCacheVaryingOptions() );
ParserOptions::clearStaticCache();
$this->setTemporaryHook( 'ParserOptionsRegister', static function ( &$defaults, &$inCacheKey ) {
$defaults += [
'foo' => 'foo',
'bar' => 'bar',
'baz' => 'baz',
];
$inCacheKey += [
'foo' => true,
'bar' => false,
];
} );
$this->assertSame( [
'dateformat', 'foo', 'numberheadings', 'printable',
'thumbsize', 'userlang'
], ParserOptions::allCacheVaryingOptions() );
}
public function testGetSpeculativeRevid() {
$options = ParserOptions::newFromAnon();
$this->assertFalse( $options->getSpeculativeRevId() );
$counter = 0;
$options->setSpeculativeRevIdCallback( static function () use( &$counter ) {
return ++$counter;
} );
// make sure the same value is re-used once it is determined
$this->assertSame( 1, $options->getSpeculativeRevId() );
$this->assertSame( 1, $options->getSpeculativeRevId() );
}
}
|