MediaWiki REL1_33
ParserOptionsTest.php
Go to the documentation of this file.
1<?php
2
3use Wikimedia\TestingAccessWrapper;
4use Wikimedia\ScopedCallback;
5
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();
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() {
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
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
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
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
241 public function testGetInvalidOption() {
242 $popt = ParserOptions::newCanonical();
243 $popt->getOption( 'bogus' );
244 }
245
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 testAllCacheVaryingOptions() {
288 $this->setTemporaryHook( 'ParserOptionsRegister', null );
289 $this->assertSame( [
290 'dateformat', 'numberheadings', 'printable', 'stubthreshold',
291 'thumbsize', 'userlang'
292 ], ParserOptions::allCacheVaryingOptions() );
293
295
296 $this->setTemporaryHook( 'ParserOptionsRegister', function ( &$defaults, &$inCacheKey ) {
297 $defaults += [
298 'foo' => 'foo',
299 'bar' => 'bar',
300 'baz' => 'baz',
301 ];
302 $inCacheKey += [
303 'foo' => true,
304 'bar' => false,
305 ];
306 } );
307 $this->assertSame( [
308 'dateformat', 'foo', 'numberheadings', 'printable', 'stubthreshold',
309 'thumbsize', 'userlang'
310 ], ParserOptions::allCacheVaryingOptions() );
311 }
312
313 public function testGetSpeculativeRevid() {
314 $options = new ParserOptions();
315
316 $this->assertFalse( $options->getSpeculativeRevId() );
317
318 $counter = 0;
319 $options->setSpeculativeRevIdCallback( function () use( &$counter ) {
320 return ++$counter;
321 } );
322
323 // make sure the same value is re-used once it is determined
324 $this->assertSame( 1, $options->getSpeculativeRevId() );
325 $this->assertSame( 1, $options->getSpeculativeRevId() );
326 }
327
328}
Apache License January AND DISTRIBUTION Definitions License shall mean the terms and conditions for use
$wgLang
Definition Setup.php:875
An IContextSource implementation which will inherit context from another source but allow individual ...
static getMutableTestUser( $groups=[])
Convenience method for getting a mutable test user.
setMwGlobals( $pairs, $value=null)
Sets a global, maintaining a stashed version of the previous global to be restored in tearDown.
setTemporaryHook( $hookName, $handler)
Create a temporary hook handler which will be reset by tearDown.
testSetInvalidOption()
InvalidArgumentException Unknown parser option bogus.
testGetInvalidOption()
InvalidArgumentException Unknown parser option bogus.
testIsSafeToCache( $expect, $options)
provideIsSafeToCache
static onPageRenderingHash(&$confstr)
testOptionsHash( $usedOptions, $expect, $options, $globals=[], $hookFunc=null)
provideOptionsHash
Set options of the Parser.
This document is intended to provide useful advice for parties seeking to redistribute MediaWiki to end users It s targeted particularly at maintainers for Linux since it s been observed that distribution packages of MediaWiki often break We ve consistently had to recommend that users seeking support use official tarballs instead of their distribution s and this often solves whatever problem the user is having It would be nice if this could such as
null means default in associative array with keys and values unescaped Should be merged with default with a value of false meaning to suppress the attribute in associative array with keys and values unescaped & $options
Definition hooks.txt:1999
do that in ParserLimitReportFormat instead use this to modify the parameters of the image all existing parser cache entries will be invalid To avoid you ll need to handle that somehow(e.g. with the RejectParserCacheValue hook) because MediaWiki won 't do it for you. & $defaults also a ContextSource after deleting those rows but within the same transaction you ll probably need to make sure the header is varied on and they can depend only on the ResourceLoaderContext $context
Definition hooks.txt:2848
this hook is for auditing only or null if authentication failed before getting that far or null if we can t even determine that When $user is not null
Definition hooks.txt:783
Allows to change the fields on the form that will be generated $name
Definition hooks.txt:271
return true to allow those checks to and false if checking is done & $user
Definition hooks.txt:1510
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
if(!isset( $args[0])) $lang