MediaWiki  master
ParserOptionsTest.php
Go to the documentation of this file.
1 <?php
2 
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();
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' );
61  $context->setUser( $user );
62  $context->setLanguage( $lang );
63 
64  // No parameters picks up $wgUser and $wgLang
66  $this->assertSame( $wgUser, $popt->getUser() );
67  $this->assertSame( $wgLang, $popt->getUserLangObj() );
68 
69  // Just a user uses $wgLang
71  $this->assertSame( $user, $popt->getUser() );
72  $this->assertSame( $wgLang, $popt->getUserLangObj() );
73 
74  // Just a language uses $wgUser
76  $this->assertSame( $wgUser, $popt->getUser() );
77  $this->assertSame( $lang, $popt->getUserLangObj() );
78 
79  // Passing both works
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
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 
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 
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 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'
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'
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 }
setTemporaryHook( $hookName, $handler)
Create a temporary hook handler which will be reset by tearDown.
testOptionsHash( $usedOptions, $expect, $options, $globals=[], $hookFunc=null)
provideOptionsHash
Apache License January AND DISTRIBUTION Definitions License shall mean the terms and conditions for use
if(!isset( $args[0])) $lang
An IContextSource implementation which will inherit context from another source but allow individual ...
static newFromUserAndLang(User $user, Language $lang)
Get a ParserOptions object from a given user and language.
$value
testIsSafeToCache( $expect, $options)
provideIsSafeToCache
static newCanonical( $context=null, $userLang=null)
Creates a "canonical" ParserOptions object.
$wgLang
Definition: Setup.php:931
static getMain()
Get the RequestContext object associated with the main request.
static getMutableTestUser( $groups=[])
Convenience method for getting a mutable test user.
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:1982
ParserOptions.
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:780
static factory( $code)
Get a cached or new language object for a given language code.
Definition: Language.php:216
setMwGlobals( $pairs, $value=null)
Sets a global, maintaining a stashed version of the previous global to be restored in tearDown...
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
Definition: distributors.txt:9
static newFromAnon()
Get a ParserOptions object for an anonymous user.
static allCacheVaryingOptions()
Return all option keys that vary the options hash.
testGetInvalidOption()
InvalidArgumentException Unknown parser option bogus.
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:35
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:52
testSetInvalidOption()
InvalidArgumentException Unknown parser option bogus.
static newFromUser( $user)
Get a ParserOptions object from a given user.
static onPageRenderingHash(&$confstr)
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:2633
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:1473