3 use Wikimedia\TestingAccessWrapper
;
4 use Wikimedia\ScopedCallback
;
7 * @covers ParserOptions
9 class ParserOptionsTest
extends MediaWikiTestCase
{
11 private static function clearCache() {
12 $wrap = TestingAccessWrapper
::newFromClass( ParserOptions
::class );
13 $wrap->defaults
= null;
14 $wrap->lazyOptions
= [
15 'dateformat' => [ ParserOptions
::class, 'initDateFormat' ],
19 'numberheadings' => true,
21 'stubthreshold' => true,
27 protected function setUp() {
31 $this->setMwGlobals( [
32 'wgRenderHashAppend' => '',
35 // This is crazy, but registering false, null, or other falsey values
36 // as a hook callback "works".
37 $this->setTemporaryHook( 'PageRenderingHash', null );
40 protected function tearDown() {
45 public function testNewCanonical() {
46 $wgUser = $this->getMutableTestUser()->getUser();
47 $wgLang = Language
::factory( 'fr' );
48 $wgContLang = Language
::factory( 'qqx' );
50 $this->setContentLang( $wgContLang );
51 $this->setMwGlobals( [
56 $user = $this->getMutableTestUser()->getUser();
57 $lang = Language
::factory( 'de' );
58 $lang2 = Language
::factory( 'bug' );
59 $context = new DerivativeContext( RequestContext
::getMain() );
60 $context->setUser( $user );
61 $context->setLanguage( $lang );
63 // No parameters picks up $wgUser and $wgLang
64 $popt = ParserOptions
::newCanonical();
65 $this->assertSame( $wgUser, $popt->getUser() );
66 $this->assertSame( $wgLang, $popt->getUserLangObj() );
68 // Just a user uses $wgLang
69 $popt = ParserOptions
::newCanonical( $user );
70 $this->assertSame( $user, $popt->getUser() );
71 $this->assertSame( $wgLang, $popt->getUserLangObj() );
73 // Just a language uses $wgUser
74 $popt = ParserOptions
::newCanonical( null, $lang );
75 $this->assertSame( $wgUser, $popt->getUser() );
76 $this->assertSame( $lang, $popt->getUserLangObj() );
79 $popt = ParserOptions
::newCanonical( $user, $lang );
80 $this->assertSame( $user, $popt->getUser() );
81 $this->assertSame( $lang, $popt->getUserLangObj() );
83 // Passing 'canonical' uses an anon and $wgContLang, and ignores
84 // any passed $userLang
85 $popt = ParserOptions
::newCanonical( 'canonical' );
86 $this->assertTrue( $popt->getUser()->isAnon() );
87 $this->assertSame( $wgContLang, $popt->getUserLangObj() );
88 $popt = ParserOptions
::newCanonical( 'canonical', $lang2 );
89 $this->assertSame( $wgContLang, $popt->getUserLangObj() );
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() );
99 // Passing something else raises an exception
101 $popt = ParserOptions
::newCanonical( 'bogus' );
102 $this->fail( 'Excpected exception not thrown' );
103 } catch ( InvalidArgumentException
$ex ) {
108 * @dataProvider provideIsSafeToCache
109 * @param bool $expect Expected value
110 * @param array $options Options to set
112 public function testIsSafeToCache( $expect, $options ) {
113 $popt = ParserOptions
::newCanonical();
114 foreach ( $options as $name => $value ) {
115 $popt->setOption( $name, $value );
117 $this->assertSame( $expect, $popt->isSafeToCache() );
120 public static function provideIsSafeToCache() {
122 'No overrides' => [ true, [] ],
123 'In-key options are ok' => [ true, [
124 'thumbsize' => 1e100
,
125 'printable' => false,
127 'Non-in-key options are not ok' => [ false, [
128 'removeComments' => false,
130 'Non-in-key options are not ok (2)' => [ false, [
131 'wrapclass' => 'foobar',
133 'Canonical override, not default (1)' => [ true, [
136 'Canonical override, not default (2)' => [ false, [
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
150 public function testOptionsHash(
151 $usedOptions, $expect, $options, $globals = [], $hookFunc = null
153 $this->setMwGlobals( $globals );
154 $this->setTemporaryHook( 'PageRenderingHash', $hookFunc );
156 $popt = ParserOptions
::newCanonical();
157 foreach ( $options as $name => $value ) {
158 $popt->setOption( $name, $value );
160 $this->assertSame( $expect, $popt->optionsHash( $usedOptions ) );
163 public static function provideOptionsHash() {
164 $used = [ 'thumbsize', 'printable' ];
166 $classWrapper = TestingAccessWrapper
::newFromClass( ParserOptions
::class );
167 $classWrapper->getDefaults();
168 $allUsableOptions = array_diff(
169 array_keys( $classWrapper->inCacheKey
),
170 array_keys( $classWrapper->lazyOptions
)
174 'Canonical options, nothing used' => [ [], 'canonical', [] ],
175 'Canonical options, used some options' => [ $used, 'canonical', [] ],
176 'Used some options, non-default values' => [
178 'printable=1!thumbsize=200',
184 'Canonical options, used all non-lazy options' => [ $allUsableOptions, 'canonical', [] ],
185 'Canonical options, nothing used, but with hooks and $wgRenderHashAppend' => [
187 'canonical!wgRenderHashAppend!onPageRenderingHash',
189 [ 'wgRenderHashAppend' => '!wgRenderHashAppend' ],
190 [ __CLASS__
. '::onPageRenderingHash' ],
195 public function testUsedLazyOptionsInHash() {
196 $this->setTemporaryHook( 'ParserOptionsRegister',
197 function ( &$defaults, &$inCacheKey, &$lazyOptions ) {
198 $lazyFuncs = $this->getMockBuilder( stdClass
::class )
199 ->setMethods( [ 'neverCalled', 'calledOnce' ] )
201 $lazyFuncs->expects( $this->never() )->method( 'neverCalled' );
202 $lazyFuncs->expects( $this->once() )->method( 'calledOnce' )->willReturn( 'value' );
214 'opt1' => [ $lazyFuncs, 'calledOnce' ],
215 'opt2' => [ $lazyFuncs, 'neverCalled' ],
216 'opt3' => [ $lazyFuncs, 'neverCalled' ],
223 $popt = ParserOptions
::newCanonical();
224 $popt->registerWatcher( function () {
225 $this->fail( 'Watcher should not have been called' );
227 $this->assertSame( 'opt1=value', $popt->optionsHash( [ 'opt1', 'opt3' ] ) );
229 // Second call to see that opt1 isn't resolved a second time
230 $this->assertSame( 'opt1=value', $popt->optionsHash( [ 'opt1', 'opt3' ] ) );
233 public static function onPageRenderingHash( &$confstr ) {
234 $confstr .= '!onPageRenderingHash';
238 * @expectedException InvalidArgumentException
239 * @expectedExceptionMessage Unknown parser option bogus
241 public function testGetInvalidOption() {
242 $popt = ParserOptions
::newCanonical();
243 $popt->getOption( 'bogus' );
247 * @expectedException InvalidArgumentException
248 * @expectedExceptionMessage Unknown parser option bogus
250 public function testSetInvalidOption() {
251 $popt = ParserOptions
::newCanonical();
252 $popt->setOption( 'bogus', true );
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;
264 $popt1 = ParserOptions
::newCanonical();
265 $popt2 = ParserOptions
::newCanonical();
266 $this->assertTrue( $popt1->matches( $popt2 ) );
268 $popt1->enableLimitReport( true );
269 $popt2->enableLimitReport( false );
270 $this->assertTrue( $popt1->matches( $popt2 ) );
272 $popt2->setTidy( !$popt2->getTidy() );
273 $this->assertFalse( $popt1->matches( $popt2 ) );
276 $classWrapper->defaults +
= [ __METHOD__
=> null ];
277 $classWrapper->lazyOptions +
= [ __METHOD__
=> function () use ( &$ctr ) {
280 $popt1 = ParserOptions
::newCanonical();
281 $popt2 = ParserOptions
::newCanonical();
282 $this->assertFalse( $popt1->matches( $popt2 ) );
284 ScopedCallback
::consume( $reset );
287 public function testAllCacheVaryingOptions() {
288 $this->setTemporaryHook( 'ParserOptionsRegister', null );
290 'dateformat', 'numberheadings', 'printable', 'stubthreshold',
291 'thumbsize', 'userlang'
292 ], ParserOptions
::allCacheVaryingOptions() );
296 $this->setTemporaryHook( 'ParserOptionsRegister', function ( &$defaults, &$inCacheKey ) {
308 'dateformat', 'foo', 'numberheadings', 'printable', 'stubthreshold',
309 'thumbsize', 'userlang'
310 ], ParserOptions
::allCacheVaryingOptions() );