SECURITY: blacklist CSS var()
[lhc/web/wiklou.git] / tests / phpunit / includes / parser / ParserOptionsTest.php
1 <?php
2
3 use Wikimedia\TestingAccessWrapper;
4 use Wikimedia\ScopedCallback;
5
6 /**
7 * @covers ParserOptions
8 */
9 class ParserOptionsTest extends MediaWikiTestCase {
10
11 private static function clearCache() {
12 $wrap = TestingAccessWrapper::newFromClass( ParserOptions::class );
13 $wrap->defaults = null;
14 $wrap->lazyOptions = [
15 'dateformat' => [ ParserOptions::class, 'initDateFormat' ],
16 'speculativeRevId' => [ ParserOptions::class, 'initSpeculativeRevId' ],
17 ];
18 $wrap->inCacheKey = [
19 'dateformat' => true,
20 'numberheadings' => true,
21 'thumbsize' => true,
22 'stubthreshold' => true,
23 'printable' => true,
24 'userlang' => true,
25 ];
26 }
27
28 protected function setUp() {
29 parent::setUp();
30 self::clearCache();
31
32 $this->setMwGlobals( [
33 'wgRenderHashAppend' => '',
34 ] );
35
36 // This is crazy, but registering false, null, or other falsey values
37 // as a hook callback "works".
38 $this->setTemporaryHook( 'PageRenderingHash', null );
39 }
40
41 protected function tearDown() {
42 self::clearCache();
43 parent::tearDown();
44 }
45
46 public function testNewCanonical() {
47 $wgUser = $this->getMutableTestUser()->getUser();
48 $wgLang = Language::factory( 'fr' );
49 $contLang = Language::factory( 'qqx' );
50
51 $this->setContentLang( $contLang );
52 $this->setMwGlobals( [
53 'wgUser' => $wgUser,
54 'wgLang' => $wgLang,
55 ] );
56
57 $user = $this->getMutableTestUser()->getUser();
58 $lang = Language::factory( 'de' );
59 $lang2 = Language::factory( 'bug' );
60 $context = new DerivativeContext( RequestContext::getMain() );
61 $context->setUser( $user );
62 $context->setLanguage( $lang );
63
64 // No parameters picks up $wgUser and $wgLang
65 $popt = ParserOptions::newCanonical();
66 $this->assertSame( $wgUser, $popt->getUser() );
67 $this->assertSame( $wgLang, $popt->getUserLangObj() );
68
69 // Just a user uses $wgLang
70 $popt = ParserOptions::newCanonical( $user );
71 $this->assertSame( $user, $popt->getUser() );
72 $this->assertSame( $wgLang, $popt->getUserLangObj() );
73
74 // Just a language uses $wgUser
75 $popt = ParserOptions::newCanonical( null, $lang );
76 $this->assertSame( $wgUser, $popt->getUser() );
77 $this->assertSame( $lang, $popt->getUserLangObj() );
78
79 // Passing both works
80 $popt = ParserOptions::newCanonical( $user, $lang );
81 $this->assertSame( $user, $popt->getUser() );
82 $this->assertSame( $lang, $popt->getUserLangObj() );
83
84 // Passing 'canonical' uses an anon and $contLang, and ignores any passed $userLang
85 $popt = ParserOptions::newCanonical( 'canonical' );
86 $this->assertTrue( $popt->getUser()->isAnon() );
87 $this->assertSame( $contLang, $popt->getUserLangObj() );
88 $popt = ParserOptions::newCanonical( 'canonical', $lang2 );
89 $this->assertSame( $contLang, $popt->getUserLangObj() );
90
91 // Passing an IContextSource uses the user and lang from it, and ignores
92 // any passed $userLang
93 $popt = ParserOptions::newCanonical( $context );
94 $this->assertSame( $user, $popt->getUser() );
95 $this->assertSame( $lang, $popt->getUserLangObj() );
96 $popt = ParserOptions::newCanonical( $context, $lang2 );
97 $this->assertSame( $lang, $popt->getUserLangObj() );
98
99 // Passing something else raises an exception
100 try {
101 $popt = ParserOptions::newCanonical( 'bogus' );
102 $this->fail( 'Excpected exception not thrown' );
103 } catch ( InvalidArgumentException $ex ) {
104 }
105 }
106
107 /**
108 * @dataProvider provideIsSafeToCache
109 * @param bool $expect Expected value
110 * @param array $options Options to set
111 */
112 public function testIsSafeToCache( $expect, $options ) {
113 $popt = ParserOptions::newCanonical();
114 foreach ( $options as $name => $value ) {
115 $popt->setOption( $name, $value );
116 }
117 $this->assertSame( $expect, $popt->isSafeToCache() );
118 }
119
120 public static function provideIsSafeToCache() {
121 return [
122 'No overrides' => [ true, [] ],
123 'In-key options are ok' => [ true, [
124 'thumbsize' => 1e100,
125 'printable' => false,
126 ] ],
127 'Non-in-key options are not ok' => [ false, [
128 'removeComments' => false,
129 ] ],
130 'Non-in-key options are not ok (2)' => [ false, [
131 'wrapclass' => 'foobar',
132 ] ],
133 'Canonical override, not default (1)' => [ true, [
134 'tidy' => true,
135 ] ],
136 'Canonical override, not default (2)' => [ false, [
137 'tidy' => false,
138 ] ],
139 ];
140 }
141
142 /**
143 * @dataProvider provideOptionsHash
144 * @param array $usedOptions Used options
145 * @param string $expect Expected value
146 * @param array $options Options to set
147 * @param array $globals Globals to set
148 * @param callable|null $hookFunc PageRenderingHash hook function
149 */
150 public function testOptionsHash(
151 $usedOptions, $expect, $options, $globals = [], $hookFunc = null
152 ) {
153 $this->setMwGlobals( $globals );
154 $this->setTemporaryHook( 'PageRenderingHash', $hookFunc );
155
156 $popt = ParserOptions::newCanonical();
157 foreach ( $options as $name => $value ) {
158 $popt->setOption( $name, $value );
159 }
160 $this->assertSame( $expect, $popt->optionsHash( $usedOptions ) );
161 }
162
163 public static function provideOptionsHash() {
164 $used = [ 'thumbsize', 'printable' ];
165
166 $classWrapper = TestingAccessWrapper::newFromClass( ParserOptions::class );
167 $classWrapper->getDefaults();
168 $allUsableOptions = array_diff(
169 array_keys( $classWrapper->inCacheKey ),
170 array_keys( $classWrapper->lazyOptions )
171 );
172
173 return [
174 'Canonical options, nothing used' => [ [], 'canonical', [] ],
175 'Canonical options, used some options' => [ $used, 'canonical', [] ],
176 'Used some options, non-default values' => [
177 $used,
178 'printable=1!thumbsize=200',
179 [
180 'thumbsize' => 200,
181 'printable' => true,
182 ]
183 ],
184 'Canonical options, used all non-lazy options' => [ $allUsableOptions, 'canonical', [] ],
185 'Canonical options, nothing used, but with hooks and $wgRenderHashAppend' => [
186 [],
187 'canonical!wgRenderHashAppend!onPageRenderingHash',
188 [],
189 [ 'wgRenderHashAppend' => '!wgRenderHashAppend' ],
190 [ __CLASS__ . '::onPageRenderingHash' ],
191 ],
192 ];
193 }
194
195 public function testUsedLazyOptionsInHash() {
196 $this->setTemporaryHook( 'ParserOptionsRegister',
197 function ( &$defaults, &$inCacheKey, &$lazyOptions ) {
198 $lazyFuncs = $this->getMockBuilder( stdClass::class )
199 ->setMethods( [ 'neverCalled', 'calledOnce' ] )
200 ->getMock();
201 $lazyFuncs->expects( $this->never() )->method( 'neverCalled' );
202 $lazyFuncs->expects( $this->once() )->method( 'calledOnce' )->willReturn( 'value' );
203
204 $defaults += [
205 'opt1' => null,
206 'opt2' => null,
207 'opt3' => null,
208 ];
209 $inCacheKey += [
210 'opt1' => true,
211 'opt2' => true,
212 ];
213 $lazyOptions += [
214 'opt1' => [ $lazyFuncs, 'calledOnce' ],
215 'opt2' => [ $lazyFuncs, 'neverCalled' ],
216 'opt3' => [ $lazyFuncs, 'neverCalled' ],
217 ];
218 }
219 );
220
221 self::clearCache();
222
223 $popt = ParserOptions::newCanonical();
224 $popt->registerWatcher( function () {
225 $this->fail( 'Watcher should not have been called' );
226 } );
227 $this->assertSame( 'opt1=value', $popt->optionsHash( [ 'opt1', 'opt3' ] ) );
228
229 // Second call to see that opt1 isn't resolved a second time
230 $this->assertSame( 'opt1=value', $popt->optionsHash( [ 'opt1', 'opt3' ] ) );
231 }
232
233 public static function onPageRenderingHash( &$confstr ) {
234 $confstr .= '!onPageRenderingHash';
235 }
236
237 /**
238 * @expectedException InvalidArgumentException
239 * @expectedExceptionMessage Unknown parser option bogus
240 */
241 public function testGetInvalidOption() {
242 $popt = ParserOptions::newCanonical();
243 $popt->getOption( 'bogus' );
244 }
245
246 /**
247 * @expectedException InvalidArgumentException
248 * @expectedExceptionMessage Unknown parser option bogus
249 */
250 public function testSetInvalidOption() {
251 $popt = ParserOptions::newCanonical();
252 $popt->setOption( 'bogus', true );
253 }
254
255 public function testMatches() {
256 $classWrapper = TestingAccessWrapper::newFromClass( ParserOptions::class );
257 $oldDefaults = $classWrapper->defaults;
258 $oldLazy = $classWrapper->lazyOptions;
259 $reset = new ScopedCallback( function () use ( $classWrapper, $oldDefaults, $oldLazy ) {
260 $classWrapper->defaults = $oldDefaults;
261 $classWrapper->lazyOptions = $oldLazy;
262 } );
263
264 $popt1 = ParserOptions::newCanonical();
265 $popt2 = ParserOptions::newCanonical();
266 $this->assertTrue( $popt1->matches( $popt2 ) );
267
268 $popt1->enableLimitReport( true );
269 $popt2->enableLimitReport( false );
270 $this->assertTrue( $popt1->matches( $popt2 ) );
271
272 $popt2->setTidy( !$popt2->getTidy() );
273 $this->assertFalse( $popt1->matches( $popt2 ) );
274
275 $ctr = 0;
276 $classWrapper->defaults += [ __METHOD__ => null ];
277 $classWrapper->lazyOptions += [ __METHOD__ => function () use ( &$ctr ) {
278 return ++$ctr;
279 } ];
280 $popt1 = ParserOptions::newCanonical();
281 $popt2 = ParserOptions::newCanonical();
282 $this->assertFalse( $popt1->matches( $popt2 ) );
283
284 ScopedCallback::consume( $reset );
285 }
286
287 public function testMatchesForCacheKey() {
288 $cOpts = ParserOptions::newCanonical( null, 'en' );
289
290 $uOpts = ParserOptions::newFromAnon();
291 $this->assertTrue( $cOpts->matchesForCacheKey( $uOpts ) );
292
293 $user = new User();
294 $uOpts = ParserOptions::newFromUser( $user );
295 $this->assertTrue( $cOpts->matchesForCacheKey( $uOpts ) );
296
297 $user = new User();
298 $user->setOption( 'thumbsize', 251 );
299 $uOpts = ParserOptions::newFromUser( $user );
300 $this->assertFalse( $cOpts->matchesForCacheKey( $uOpts ) );
301
302 $user = new User();
303 $user->setOption( 'stubthreshold', 800 );
304 $uOpts = ParserOptions::newFromUser( $user );
305 $this->assertFalse( $cOpts->matchesForCacheKey( $uOpts ) );
306
307 $user = new User();
308 $uOpts = ParserOptions::newFromUserAndLang( $user, Language::factory( 'zh' ) );
309 $this->assertFalse( $cOpts->matchesForCacheKey( $uOpts ) );
310 }
311
312 public function testAllCacheVaryingOptions() {
313 $this->setTemporaryHook( 'ParserOptionsRegister', null );
314 $this->assertSame( [
315 'dateformat', 'numberheadings', 'printable', 'stubthreshold',
316 'thumbsize', 'userlang'
317 ], ParserOptions::allCacheVaryingOptions() );
318
319 self::clearCache();
320
321 $this->setTemporaryHook( 'ParserOptionsRegister', function ( &$defaults, &$inCacheKey ) {
322 $defaults += [
323 'foo' => 'foo',
324 'bar' => 'bar',
325 'baz' => 'baz',
326 ];
327 $inCacheKey += [
328 'foo' => true,
329 'bar' => false,
330 ];
331 } );
332 $this->assertSame( [
333 'dateformat', 'foo', 'numberheadings', 'printable', 'stubthreshold',
334 'thumbsize', 'userlang'
335 ], ParserOptions::allCacheVaryingOptions() );
336 }
337
338 public function testGetSpeculativeRevid() {
339 $options = new ParserOptions();
340
341 $this->assertFalse( $options->getSpeculativeRevId() );
342
343 $counter = 0;
344 $options->setSpeculativeRevIdCallback( function () use( &$counter ) {
345 return ++$counter;
346 } );
347
348 // make sure the same value is re-used once it is determined
349 $this->assertSame( 1, $options->getSpeculativeRevId() );
350 $this->assertSame( 1, $options->getSpeculativeRevId() );
351 }
352
353 }