MediaWiki  1.33.0
ParserOptionsTest.php
Go to the documentation of this file.
1 <?php
2 
3 use Wikimedia\TestingAccessWrapper;
4 use 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' );
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
75  $popt = ParserOptions::newCanonical( null, $lang );
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 
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'
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'
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 }
ParserOptions
Set options of the Parser.
Definition: ParserOptions.php:42
$user
return true to allow those checks to and false if checking is done & $user
Definition: hooks.txt:1476
$context
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:2636
$lang
if(!isset( $args[0])) $lang
Definition: testCompression.php:33
ParserOptionsTest\testUsedLazyOptionsInHash
testUsedLazyOptionsInHash()
Definition: ParserOptionsTest.php:195
ParserOptionsTest\testIsSafeToCache
testIsSafeToCache( $expect, $options)
provideIsSafeToCache
Definition: ParserOptionsTest.php:112
php
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
DerivativeContext
An IContextSource implementation which will inherit context from another source but allow individual ...
Definition: DerivativeContext.php:30
MediaWikiTestCase\setMwGlobals
setMwGlobals( $pairs, $value=null)
Sets a global, maintaining a stashed version of the previous global to be restored in tearDown.
Definition: MediaWikiTestCase.php:709
ParserOptionsTest\provideOptionsHash
static provideOptionsHash()
Definition: ParserOptionsTest.php:163
MediaWikiTestCase
Definition: MediaWikiTestCase.php:17
$wgLang
$wgLang
Definition: Setup.php:875
ParserOptionsTest\testAllCacheVaryingOptions
testAllCacheVaryingOptions()
Definition: ParserOptionsTest.php:287
use
as see the revision history and available at free of to any person obtaining a copy of this software and associated documentation to deal in the Software without including without limitation the rights to use
Definition: MIT-LICENSE.txt:10
ParserOptionsTest\setUp
setUp()
Definition: ParserOptionsTest.php:28
ParserOptionsTest\testNewCanonical
testNewCanonical()
Definition: ParserOptionsTest.php:46
null
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
ParserOptionsTest\clearCache
static clearCache()
Definition: ParserOptionsTest.php:11
MediaWikiTestCase\setContentLang
setContentLang( $lang)
Definition: MediaWikiTestCase.php:1066
$name
Allows to change the fields on the form that will be generated $name
Definition: hooks.txt:271
$value
$value
Definition: styleTest.css.php:49
ParserOptionsTest\onPageRenderingHash
static onPageRenderingHash(&$confstr)
Definition: ParserOptionsTest.php:233
MediaWikiTestCase\getMutableTestUser
static getMutableTestUser( $groups=[])
Convenience method for getting a mutable test user.
Definition: MediaWikiTestCase.php:192
ParserOptionsTest\testGetSpeculativeRevid
testGetSpeculativeRevid()
Definition: ParserOptionsTest.php:313
ParserOptions\newCanonical
static newCanonical( $context=null, $userLang=null)
Creates a "canonical" ParserOptions object.
Definition: ParserOptions.php:1064
ParserOptionsTest\tearDown
tearDown()
Definition: ParserOptionsTest.php:41
RequestContext\getMain
static getMain()
Get the RequestContext object associated with the main request.
Definition: RequestContext.php:430
$options
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:1985
ParserOptionsTest\testSetInvalidOption
testSetInvalidOption()
InvalidArgumentException Unknown parser option bogus.
Definition: ParserOptionsTest.php:250
ParserOptions\allCacheVaryingOptions
static allCacheVaryingOptions()
Return all option keys that vary the options hash.
Definition: ParserOptions.php:1269
ParserOptionsTest\testMatches
testMatches()
Definition: ParserOptionsTest.php:255
as
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
ParserOptionsTest\testGetInvalidOption
testGetInvalidOption()
InvalidArgumentException Unknown parser option bogus.
Definition: ParserOptionsTest.php:241
ParserOptionsTest
ParserOptions.
Definition: ParserOptionsTest.php:9
Language\factory
static factory( $code)
Get a cached or new language object for a given language code.
Definition: Language.php:215
class
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
MediaWikiTestCase\setTemporaryHook
setTemporaryHook( $hookName, $handler)
Create a temporary hook handler which will be reset by tearDown.
Definition: MediaWikiTestCase.php:2325
ParserOptionsTest\provideIsSafeToCache
static provideIsSafeToCache()
Definition: ParserOptionsTest.php:120
ParserOptionsTest\testOptionsHash
testOptionsHash( $usedOptions, $expect, $options, $globals=[], $hookFunc=null)
provideOptionsHash
Definition: ParserOptionsTest.php:150