MediaWiki REL1_30
ParserOptionsTest.php
Go to the documentation of this file.
1<?php
2
3use Wikimedia\TestingAccessWrapper;
4use Wikimedia\ScopedCallback;
5
7
8 private static function clearCache() {
9 $wrap = TestingAccessWrapper::newFromClass( ParserOptions::class );
10 $wrap->defaults = null;
11 $wrap->lazyOptions = [
12 'dateformat' => [ ParserOptions::class, 'initDateFormat' ],
13 ];
14 $wrap->inCacheKey = [
15 'dateformat' => true,
16 'numberheadings' => true,
17 'thumbsize' => true,
18 'stubthreshold' => true,
19 'printable' => true,
20 'userlang' => true,
21 'wrapclass' => true,
22 ];
23 }
24
25 protected function setUp() {
27
28 parent::setUp();
30
31 $this->setMwGlobals( [
32 'wgRenderHashAppend' => '',
33 'wgHooks' => [
34 'PageRenderingHash' => [],
35 ] + $wgHooks,
36 ] );
37 }
38
39 protected function tearDown() {
41 parent::tearDown();
42 }
43
49 public function testIsSafeToCache( $expect, $options ) {
50 $popt = ParserOptions::newCanonical();
51 foreach ( $options as $name => $value ) {
52 $popt->setOption( $name, $value );
53 }
54 $this->assertSame( $expect, $popt->isSafeToCache() );
55 }
56
57 public static function provideIsSafeToCache() {
58 return [
59 'No overrides' => [ true, [] ],
60 'In-key options are ok' => [ true, [
61 'thumbsize' => 1e100,
62 'wrapclass' => false,
63 ] ],
64 'Non-in-key options are not ok' => [ false, [
65 'removeComments' => false,
66 ] ],
67 'Canonical override, not default (1)' => [ true, [
68 'tidy' => true,
69 ] ],
70 'Canonical override, not default (2)' => [ false, [
71 'tidy' => false,
72 ] ],
73 ];
74 }
75
83 public function testOptionsHash( $usedOptions, $expect, $options, $globals = [] ) {
85
86 $globals += [
87 'wgHooks' => [],
88 ];
89 $globals['wgHooks'] += [
90 'PageRenderingHash' => [],
91 ] + $wgHooks;
92 $this->setMwGlobals( $globals );
93
94 $popt = ParserOptions::newCanonical();
95 foreach ( $options as $name => $value ) {
96 $popt->setOption( $name, $value );
97 }
98 $this->assertSame( $expect, $popt->optionsHash( $usedOptions ) );
99 }
100
101 public static function provideOptionsHash() {
102 $used = [ 'wrapclass', 'printable' ];
103
104 $classWrapper = TestingAccessWrapper::newFromClass( ParserOptions::class );
105 $classWrapper->getDefaults();
106 $allUsableOptions = array_diff(
107 array_keys( $classWrapper->inCacheKey ),
108 array_keys( $classWrapper->lazyOptions )
109 );
110
111 return [
112 'Canonical options, nothing used' => [ [], 'canonical', [] ],
113 'Canonical options, used some options' => [ $used, 'canonical', [] ],
114 'Used some options, non-default values' => [
115 $used,
116 'printable=1!wrapclass=foobar',
117 [
118 'wrapclass' => 'foobar',
119 'printable' => true,
120 ]
121 ],
122 'Canonical options, used all non-lazy options' => [ $allUsableOptions, 'canonical', [] ],
123 'Canonical options, nothing used, but with hooks and $wgRenderHashAppend' => [
124 [],
125 'canonical!wgRenderHashAppend!onPageRenderingHash',
126 [],
127 [
128 'wgRenderHashAppend' => '!wgRenderHashAppend',
129 'wgHooks' => [ 'PageRenderingHash' => [ [ __CLASS__ . '::onPageRenderingHash' ] ] ],
130 ]
131 ],
132 ];
133 }
134
135 public static function onPageRenderingHash( &$confstr ) {
136 $confstr .= '!onPageRenderingHash';
137 }
138
139 // Test weird historical behavior is still weird
140 public function testOptionsHashEditSection() {
141 $popt = ParserOptions::newCanonical();
142 $popt->registerWatcher( function ( $name ) {
143 $this->assertNotEquals( 'editsection', $name );
144 } );
145
146 $this->assertTrue( $popt->getEditSection() );
147 $this->assertSame( 'canonical', $popt->optionsHash( [] ) );
148 $this->assertSame( 'canonical', $popt->optionsHash( [ 'editsection' ] ) );
149
150 $popt->setEditSection( false );
151 $this->assertFalse( $popt->getEditSection() );
152 $this->assertSame( 'canonical', $popt->optionsHash( [] ) );
153 $this->assertSame( 'editsection=0', $popt->optionsHash( [ 'editsection' ] ) );
154 }
155
160 public function testGetInvalidOption() {
161 $popt = ParserOptions::newCanonical();
162 $popt->getOption( 'bogus' );
163 }
164
169 public function testSetInvalidOption() {
170 $popt = ParserOptions::newCanonical();
171 $popt->setOption( 'bogus', true );
172 }
173
174 public function testMatches() {
175 $classWrapper = TestingAccessWrapper::newFromClass( ParserOptions::class );
176 $oldDefaults = $classWrapper->defaults;
177 $oldLazy = $classWrapper->lazyOptions;
178 $reset = new ScopedCallback( function () use ( $classWrapper, $oldDefaults, $oldLazy ) {
179 $classWrapper->defaults = $oldDefaults;
180 $classWrapper->lazyOptions = $oldLazy;
181 } );
182
183 $popt1 = ParserOptions::newCanonical();
184 $popt2 = ParserOptions::newCanonical();
185 $this->assertTrue( $popt1->matches( $popt2 ) );
186
187 $popt1->enableLimitReport( true );
188 $popt2->enableLimitReport( false );
189 $this->assertTrue( $popt1->matches( $popt2 ) );
190
191 $popt2->setTidy( !$popt2->getTidy() );
192 $this->assertFalse( $popt1->matches( $popt2 ) );
193
194 $ctr = 0;
195 $classWrapper->defaults += [ __METHOD__ => null ];
196 $classWrapper->lazyOptions += [ __METHOD__ => function () use ( &$ctr ) {
197 return ++$ctr;
198 } ];
199 $popt1 = ParserOptions::newCanonical();
200 $popt2 = ParserOptions::newCanonical();
201 $this->assertFalse( $popt1->matches( $popt2 ) );
202
203 ScopedCallback::consume( $reset );
204 }
205
206 public function testAllCacheVaryingOptions() {
208
209 // $wgHooks is already saved in self::setUp(), so we can modify it freely here
210 $wgHooks['ParserOptionsRegister'] = [];
211 $this->assertSame( [
212 'dateformat', 'numberheadings', 'printable', 'stubthreshold',
213 'thumbsize', 'userlang', 'wrapclass',
214 ], ParserOptions::allCacheVaryingOptions() );
215
217
218 $wgHooks['ParserOptionsRegister'][] = function ( &$defaults, &$inCacheKey ) {
219 $defaults += [
220 'foo' => 'foo',
221 'bar' => 'bar',
222 'baz' => 'baz',
223 ];
224 $inCacheKey += [
225 'foo' => true,
226 'bar' => false,
227 ];
228 };
229 $this->assertSame( [
230 'dateformat', 'foo', 'numberheadings', 'printable', 'stubthreshold',
231 'thumbsize', 'userlang', 'wrapclass',
232 ], ParserOptions::allCacheVaryingOptions() );
233 }
234
235}
Apache License January AND DISTRIBUTION Definitions License shall mean the terms and conditions for use
setMwGlobals( $pairs, $value=null)
Sets a global, maintaining a stashed version of the previous global to be restored in 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=[])
provideOptionsHash
when a variable name is used in a it is silently declared as a new local masking the global
Definition design.txt:95
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:1971
$wgHooks['ArticleShow'][]
Definition hooks.txt:108
Allows to change the fields on the form that will be generated $name
Definition hooks.txt:302
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