Merge "Avoid unnecessary WaitConditionLoop delays in ChronologyProtector"
[lhc/web/wiklou.git] / tests / phpunit / includes / parser / ParserOptionsTest.php
1 <?php
2
3 use Wikimedia\TestingAccessWrapper;
4 use Wikimedia\ScopedCallback;
5
6 /**
7 * @covers ParserOptions
8 */
9 class ParserOptionsTest extends MediaWikiTestCase {
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 ];
17 $wrap->inCacheKey = [
18 'dateformat' => true,
19 'numberheadings' => true,
20 'thumbsize' => true,
21 'stubthreshold' => true,
22 'printable' => true,
23 'userlang' => true,
24 ];
25 }
26
27 protected function setUp() {
28 parent::setUp();
29 self::clearCache();
30
31 $this->setMwGlobals( [
32 'wgRenderHashAppend' => '',
33 ] );
34
35 // This is crazy, but registering false, null, or other falsey values
36 // as a hook callback "works".
37 $this->setTemporaryHook( 'PageRenderingHash', null );
38 }
39
40 protected function tearDown() {
41 self::clearCache();
42 parent::tearDown();
43 }
44
45 /**
46 * @dataProvider provideIsSafeToCache
47 * @param bool $expect Expected value
48 * @param array $options Options to set
49 */
50 public function testIsSafeToCache( $expect, $options ) {
51 $popt = ParserOptions::newCanonical();
52 foreach ( $options as $name => $value ) {
53 $popt->setOption( $name, $value );
54 }
55 $this->assertSame( $expect, $popt->isSafeToCache() );
56 }
57
58 public static function provideIsSafeToCache() {
59 return [
60 'No overrides' => [ true, [] ],
61 'In-key options are ok' => [ true, [
62 'thumbsize' => 1e100,
63 'printable' => false,
64 ] ],
65 'Non-in-key options are not ok' => [ false, [
66 'removeComments' => false,
67 ] ],
68 'Non-in-key options are not ok (2)' => [ false, [
69 'wrapclass' => 'foobar',
70 ] ],
71 'Canonical override, not default (1)' => [ true, [
72 'tidy' => true,
73 ] ],
74 'Canonical override, not default (2)' => [ false, [
75 'tidy' => false,
76 ] ],
77 ];
78 }
79
80 /**
81 * @dataProvider provideOptionsHash
82 * @param array $usedOptions Used options
83 * @param string $expect Expected value
84 * @param array $options Options to set
85 * @param array $globals Globals to set
86 * @param callable|null $hookFunc PageRenderingHash hook function
87 */
88 public function testOptionsHash(
89 $usedOptions, $expect, $options, $globals = [], $hookFunc = null
90 ) {
91 $this->setMwGlobals( $globals );
92 $this->setTemporaryHook( 'PageRenderingHash', $hookFunc );
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 = [ 'thumbsize', '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!thumbsize=200',
117 [
118 'thumbsize' => 200,
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 [ 'wgRenderHashAppend' => '!wgRenderHashAppend' ],
128 [ __CLASS__ . '::onPageRenderingHash' ],
129 ],
130 ];
131 }
132
133 public function testUsedLazyOptionsInHash() {
134 $this->setTemporaryHook( 'ParserOptionsRegister',
135 function ( &$defaults, &$inCacheKey, &$lazyOptions ) {
136 $lazyFuncs = $this->getMockBuilder( stdClass::class )
137 ->setMethods( [ 'neverCalled', 'calledOnce' ] )
138 ->getMock();
139 $lazyFuncs->expects( $this->never() )->method( 'neverCalled' );
140 $lazyFuncs->expects( $this->once() )->method( 'calledOnce' )->willReturn( 'value' );
141
142 $defaults += [
143 'opt1' => null,
144 'opt2' => null,
145 'opt3' => null,
146 ];
147 $inCacheKey += [
148 'opt1' => true,
149 'opt2' => true,
150 ];
151 $lazyOptions += [
152 'opt1' => [ $lazyFuncs, 'calledOnce' ],
153 'opt2' => [ $lazyFuncs, 'neverCalled' ],
154 'opt3' => [ $lazyFuncs, 'neverCalled' ],
155 ];
156 }
157 );
158
159 self::clearCache();
160
161 $popt = ParserOptions::newCanonical();
162 $popt->registerWatcher( function () {
163 $this->fail( 'Watcher should not have been called' );
164 } );
165 $this->assertSame( 'opt1=value', $popt->optionsHash( [ 'opt1', 'opt3' ] ) );
166
167 // Second call to see that opt1 isn't resolved a second time
168 $this->assertSame( 'opt1=value', $popt->optionsHash( [ 'opt1', 'opt3' ] ) );
169 }
170
171 public static function onPageRenderingHash( &$confstr ) {
172 $confstr .= '!onPageRenderingHash';
173 }
174
175 /**
176 * @expectedException InvalidArgumentException
177 * @expectedExceptionMessage Unknown parser option bogus
178 */
179 public function testGetInvalidOption() {
180 $popt = ParserOptions::newCanonical();
181 $popt->getOption( 'bogus' );
182 }
183
184 /**
185 * @expectedException InvalidArgumentException
186 * @expectedExceptionMessage Unknown parser option bogus
187 */
188 public function testSetInvalidOption() {
189 $popt = ParserOptions::newCanonical();
190 $popt->setOption( 'bogus', true );
191 }
192
193 public function testMatches() {
194 $classWrapper = TestingAccessWrapper::newFromClass( ParserOptions::class );
195 $oldDefaults = $classWrapper->defaults;
196 $oldLazy = $classWrapper->lazyOptions;
197 $reset = new ScopedCallback( function () use ( $classWrapper, $oldDefaults, $oldLazy ) {
198 $classWrapper->defaults = $oldDefaults;
199 $classWrapper->lazyOptions = $oldLazy;
200 } );
201
202 $popt1 = ParserOptions::newCanonical();
203 $popt2 = ParserOptions::newCanonical();
204 $this->assertTrue( $popt1->matches( $popt2 ) );
205
206 $popt1->enableLimitReport( true );
207 $popt2->enableLimitReport( false );
208 $this->assertTrue( $popt1->matches( $popt2 ) );
209
210 $popt2->setTidy( !$popt2->getTidy() );
211 $this->assertFalse( $popt1->matches( $popt2 ) );
212
213 $ctr = 0;
214 $classWrapper->defaults += [ __METHOD__ => null ];
215 $classWrapper->lazyOptions += [ __METHOD__ => function () use ( &$ctr ) {
216 return ++$ctr;
217 } ];
218 $popt1 = ParserOptions::newCanonical();
219 $popt2 = ParserOptions::newCanonical();
220 $this->assertFalse( $popt1->matches( $popt2 ) );
221
222 ScopedCallback::consume( $reset );
223 }
224
225 public function testAllCacheVaryingOptions() {
226 $this->setTemporaryHook( 'ParserOptionsRegister', null );
227 $this->assertSame( [
228 'dateformat', 'numberheadings', 'printable', 'stubthreshold',
229 'thumbsize', 'userlang'
230 ], ParserOptions::allCacheVaryingOptions() );
231
232 self::clearCache();
233
234 $this->setTemporaryHook( 'ParserOptionsRegister', function ( &$defaults, &$inCacheKey ) {
235 $defaults += [
236 'foo' => 'foo',
237 'bar' => 'bar',
238 'baz' => 'baz',
239 ];
240 $inCacheKey += [
241 'foo' => true,
242 'bar' => false,
243 ];
244 } );
245 $this->assertSame( [
246 'dateformat', 'foo', 'numberheadings', 'printable', 'stubthreshold',
247 'thumbsize', 'userlang'
248 ], ParserOptions::allCacheVaryingOptions() );
249 }
250
251 }