Merge "Add SPARQL client to core"
[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 global $wgHooks;
29
30 parent::setUp();
31 self::clearCache();
32
33 $this->setMwGlobals( [
34 'wgRenderHashAppend' => '',
35 'wgHooks' => [
36 'PageRenderingHash' => [],
37 ] + $wgHooks,
38 ] );
39 }
40
41 protected function tearDown() {
42 self::clearCache();
43 parent::tearDown();
44 }
45
46 /**
47 * @dataProvider provideIsSafeToCache
48 * @param bool $expect Expected value
49 * @param array $options Options to set
50 */
51 public function testIsSafeToCache( $expect, $options ) {
52 $popt = ParserOptions::newCanonical();
53 foreach ( $options as $name => $value ) {
54 $popt->setOption( $name, $value );
55 }
56 $this->assertSame( $expect, $popt->isSafeToCache() );
57 }
58
59 public static function provideIsSafeToCache() {
60 return [
61 'No overrides' => [ true, [] ],
62 'In-key options are ok' => [ true, [
63 'thumbsize' => 1e100,
64 'printable' => false,
65 ] ],
66 'Non-in-key options are not ok' => [ false, [
67 'removeComments' => false,
68 ] ],
69 'Non-in-key options are not ok (2)' => [ false, [
70 'wrapclass' => 'foobar',
71 ] ],
72 'Canonical override, not default (1)' => [ true, [
73 'tidy' => true,
74 ] ],
75 'Canonical override, not default (2)' => [ false, [
76 'tidy' => false,
77 ] ],
78 ];
79 }
80
81 /**
82 * @dataProvider provideOptionsHash
83 * @param array $usedOptions Used options
84 * @param string $expect Expected value
85 * @param array $options Options to set
86 * @param array $globals Globals to set
87 */
88 public function testOptionsHash( $usedOptions, $expect, $options, $globals = [] ) {
89 global $wgHooks;
90
91 $globals += [
92 'wgHooks' => [],
93 ];
94 $globals['wgHooks'] += [
95 'PageRenderingHash' => [],
96 ] + $wgHooks;
97 $this->setMwGlobals( $globals );
98
99 $popt = ParserOptions::newCanonical();
100 foreach ( $options as $name => $value ) {
101 $popt->setOption( $name, $value );
102 }
103 $this->assertSame( $expect, $popt->optionsHash( $usedOptions ) );
104 }
105
106 public static function provideOptionsHash() {
107 $used = [ 'thumbsize', 'printable' ];
108
109 $classWrapper = TestingAccessWrapper::newFromClass( ParserOptions::class );
110 $classWrapper->getDefaults();
111 $allUsableOptions = array_diff(
112 array_keys( $classWrapper->inCacheKey ),
113 array_keys( $classWrapper->lazyOptions )
114 );
115
116 return [
117 'Canonical options, nothing used' => [ [], 'canonical', [] ],
118 'Canonical options, used some options' => [ $used, 'canonical', [] ],
119 'Used some options, non-default values' => [
120 $used,
121 'printable=1!thumbsize=200',
122 [
123 'thumbsize' => 200,
124 'printable' => true,
125 ]
126 ],
127 'Canonical options, used all non-lazy options' => [ $allUsableOptions, 'canonical', [] ],
128 'Canonical options, nothing used, but with hooks and $wgRenderHashAppend' => [
129 [],
130 'canonical!wgRenderHashAppend!onPageRenderingHash',
131 [],
132 [
133 'wgRenderHashAppend' => '!wgRenderHashAppend',
134 'wgHooks' => [ 'PageRenderingHash' => [ [ __CLASS__ . '::onPageRenderingHash' ] ] ],
135 ]
136 ],
137 ];
138 }
139
140 public static function onPageRenderingHash( &$confstr ) {
141 $confstr .= '!onPageRenderingHash';
142 }
143
144 // Test weird historical behavior is still weird
145 public function testOptionsHashEditSection() {
146 $popt = ParserOptions::newCanonical();
147 $popt->registerWatcher( function ( $name ) {
148 $this->assertNotEquals( 'editsection', $name );
149 } );
150
151 $this->assertTrue( $popt->getEditSection() );
152 $this->assertSame( 'canonical', $popt->optionsHash( [] ) );
153 $this->assertSame( 'canonical', $popt->optionsHash( [ 'editsection' ] ) );
154
155 $popt->setEditSection( false );
156 $this->assertFalse( $popt->getEditSection() );
157 $this->assertSame( 'canonical', $popt->optionsHash( [] ) );
158 $this->assertSame( 'editsection=0', $popt->optionsHash( [ 'editsection' ] ) );
159 }
160
161 /**
162 * @expectedException InvalidArgumentException
163 * @expectedExceptionMessage Unknown parser option bogus
164 */
165 public function testGetInvalidOption() {
166 $popt = ParserOptions::newCanonical();
167 $popt->getOption( 'bogus' );
168 }
169
170 /**
171 * @expectedException InvalidArgumentException
172 * @expectedExceptionMessage Unknown parser option bogus
173 */
174 public function testSetInvalidOption() {
175 $popt = ParserOptions::newCanonical();
176 $popt->setOption( 'bogus', true );
177 }
178
179 public function testMatches() {
180 $classWrapper = TestingAccessWrapper::newFromClass( ParserOptions::class );
181 $oldDefaults = $classWrapper->defaults;
182 $oldLazy = $classWrapper->lazyOptions;
183 $reset = new ScopedCallback( function () use ( $classWrapper, $oldDefaults, $oldLazy ) {
184 $classWrapper->defaults = $oldDefaults;
185 $classWrapper->lazyOptions = $oldLazy;
186 } );
187
188 $popt1 = ParserOptions::newCanonical();
189 $popt2 = ParserOptions::newCanonical();
190 $this->assertTrue( $popt1->matches( $popt2 ) );
191
192 $popt1->enableLimitReport( true );
193 $popt2->enableLimitReport( false );
194 $this->assertTrue( $popt1->matches( $popt2 ) );
195
196 $popt2->setTidy( !$popt2->getTidy() );
197 $this->assertFalse( $popt1->matches( $popt2 ) );
198
199 $ctr = 0;
200 $classWrapper->defaults += [ __METHOD__ => null ];
201 $classWrapper->lazyOptions += [ __METHOD__ => function () use ( &$ctr ) {
202 return ++$ctr;
203 } ];
204 $popt1 = ParserOptions::newCanonical();
205 $popt2 = ParserOptions::newCanonical();
206 $this->assertFalse( $popt1->matches( $popt2 ) );
207
208 ScopedCallback::consume( $reset );
209 }
210
211 public function testAllCacheVaryingOptions() {
212 global $wgHooks;
213
214 // $wgHooks is already saved in self::setUp(), so we can modify it freely here
215 $wgHooks['ParserOptionsRegister'] = [];
216 $this->assertSame( [
217 'dateformat', 'numberheadings', 'printable', 'stubthreshold',
218 'thumbsize', 'userlang'
219 ], ParserOptions::allCacheVaryingOptions() );
220
221 self::clearCache();
222
223 $wgHooks['ParserOptionsRegister'][] = function ( &$defaults, &$inCacheKey ) {
224 $defaults += [
225 'foo' => 'foo',
226 'bar' => 'bar',
227 'baz' => 'baz',
228 ];
229 $inCacheKey += [
230 'foo' => true,
231 'bar' => false,
232 ];
233 };
234 $this->assertSame( [
235 'dateformat', 'foo', 'numberheadings', 'printable', 'stubthreshold',
236 'thumbsize', 'userlang'
237 ], ParserOptions::allCacheVaryingOptions() );
238 }
239
240 }