Merge "Allow skins to control style of mediawiki.hlist"
[lhc/web/wiklou.git] / tests / phpunit / includes / parser / ParserOptionsTest.php
1 <?php
2
3 use Wikimedia\TestingAccessWrapper;
4 use Wikimedia\ScopedCallback;
5
6 class ParserOptionsTest extends MediaWikiTestCase {
7
8 /**
9 * @dataProvider provideIsSafeToCache
10 * @param bool $expect Expected value
11 * @param array $options Options to set
12 */
13 public function testIsSafeToCache( $expect, $options ) {
14 $popt = ParserOptions::newCanonical();
15 foreach ( $options as $name => $value ) {
16 $popt->setOption( $name, $value );
17 }
18 $this->assertSame( $expect, $popt->isSafeToCache() );
19 }
20
21 public static function provideIsSafeToCache() {
22 return [
23 'No overrides' => [ true, [] ],
24 'In-key options are ok' => [ true, [
25 'editsection' => false,
26 'thumbsize' => 1e100,
27 'wrapclass' => false,
28 ] ],
29 'Non-in-key options are not ok' => [ false, [
30 'removeComments' => false,
31 ] ],
32 'Canonical override, not default (1)' => [ true, [
33 'tidy' => true,
34 ] ],
35 'Canonical override, not default (2)' => [ false, [
36 'tidy' => false,
37 ] ],
38 ];
39 }
40
41 /**
42 * @dataProvider provideOptionsHashPre30
43 * @param array $usedOptions Used options
44 * @param string $expect Expected value
45 * @param array $options Options to set
46 * @param array $globals Globals to set
47 */
48 public function testOptionsHashPre30( $usedOptions, $expect, $options, $globals = [] ) {
49 global $wgHooks;
50
51 $globals += [
52 'wgRenderHashAppend' => '',
53 'wgHooks' => [],
54 ];
55 $globals['wgHooks'] += [
56 'PageRenderingHash' => [],
57 ] + $wgHooks;
58 $this->setMwGlobals( $globals );
59
60 $popt = new ParserOptions();
61 foreach ( $options as $setter => $value ) {
62 $popt->$setter( $value );
63 }
64 $this->assertSame( $expect, $popt->optionsHashPre30( $usedOptions ) );
65 }
66
67 public static function provideOptionsHashPre30() {
68 $used = [ 'wrapclass', 'editsection', 'printable' ];
69
70 return [
71 'Canonical options, nothing used' => [ [], '*!*!*!*!*!*', [] ],
72 'Canonical options, used some options' => [ $used, '*!*!*!*!*', [] ],
73 'Used some options, non-default values' => [
74 $used,
75 '*!*!*!*!*!printable=1!wrapclass=foobar',
76 [
77 'setWrapOutputClass' => 'foobar',
78 'setIsPrintable' => true,
79 ]
80 ],
81 'Canonical options, nothing used, but with hooks and $wgRenderHashAppend' => [
82 [],
83 '*!*!*!*!*!wgRenderHashAppend!*!onPageRenderingHash',
84 [],
85 [
86 'wgRenderHashAppend' => '!wgRenderHashAppend',
87 'wgHooks' => [ 'PageRenderingHash' => [ [ __CLASS__ . '::onPageRenderingHash' ] ] ],
88 ]
89 ],
90 ];
91 }
92
93 /**
94 * @dataProvider provideOptionsHash
95 * @param array $usedOptions Used options
96 * @param string $expect Expected value
97 * @param array $options Options to set
98 * @param array $globals Globals to set
99 */
100 public function testOptionsHash( $usedOptions, $expect, $options, $globals = [] ) {
101 global $wgHooks;
102
103 $globals += [
104 'wgRenderHashAppend' => '',
105 'wgHooks' => [],
106 ];
107 $globals['wgHooks'] += [
108 'PageRenderingHash' => [],
109 ] + $wgHooks;
110 $this->setMwGlobals( $globals );
111
112 $popt = ParserOptions::newCanonical();
113 foreach ( $options as $name => $value ) {
114 $popt->setOption( $name, $value );
115 }
116 $this->assertSame( $expect, $popt->optionsHash( $usedOptions ) );
117 }
118
119 public static function provideOptionsHash() {
120 $used = [ 'wrapclass', 'editsection', 'printable' ];
121
122 $classWrapper = TestingAccessWrapper::newFromClass( ParserOptions::class );
123 $classWrapper->getDefaults();
124 $allUsableOptions = array_diff(
125 array_keys( $classWrapper->inCacheKey ),
126 array_keys( $classWrapper->lazyOptions )
127 );
128
129 return [
130 'Canonical options, nothing used' => [ [], 'canonical', [] ],
131 'Canonical options, used some options' => [ $used, 'canonical', [] ],
132 'Used some options, non-default values' => [
133 $used,
134 'printable=1!wrapclass=foobar',
135 [
136 'wrapclass' => 'foobar',
137 'printable' => true,
138 ]
139 ],
140 'Canonical options, used all non-lazy options' => [ $allUsableOptions, 'canonical', [] ],
141 'Canonical options, nothing used, but with hooks and $wgRenderHashAppend' => [
142 [],
143 'canonical!wgRenderHashAppend!onPageRenderingHash',
144 [],
145 [
146 'wgRenderHashAppend' => '!wgRenderHashAppend',
147 'wgHooks' => [ 'PageRenderingHash' => [ [ __CLASS__ . '::onPageRenderingHash' ] ] ],
148 ]
149 ],
150 ];
151 }
152
153 public static function onPageRenderingHash( &$confstr ) {
154 $confstr .= '!onPageRenderingHash';
155 }
156
157 /**
158 * @expectedException InvalidArgumentException
159 * @expectedExceptionMessage Unknown parser option bogus
160 */
161 public function testGetInvalidOption() {
162 $popt = ParserOptions::newCanonical();
163 $popt->getOption( 'bogus' );
164 }
165
166 /**
167 * @expectedException InvalidArgumentException
168 * @expectedExceptionMessage Unknown parser option bogus
169 */
170 public function testSetInvalidOption() {
171 $popt = ParserOptions::newCanonical();
172 $popt->setOption( 'bogus', true );
173 }
174
175 public function testMatches() {
176 $classWrapper = TestingAccessWrapper::newFromClass( ParserOptions::class );
177 $oldDefaults = $classWrapper->defaults;
178 $oldLazy = $classWrapper->lazyOptions;
179 $reset = new ScopedCallback( function () use ( $classWrapper, $oldDefaults, $oldLazy ) {
180 $classWrapper->defaults = $oldDefaults;
181 $classWrapper->lazyOptions = $oldLazy;
182 } );
183
184 $popt1 = ParserOptions::newCanonical();
185 $popt2 = ParserOptions::newCanonical();
186 $this->assertTrue( $popt1->matches( $popt2 ) );
187
188 $popt1->enableLimitReport( true );
189 $popt2->enableLimitReport( false );
190 $this->assertTrue( $popt1->matches( $popt2 ) );
191
192 $popt2->setTidy( !$popt2->getTidy() );
193 $this->assertFalse( $popt1->matches( $popt2 ) );
194
195 $ctr = 0;
196 $classWrapper->defaults += [ __METHOD__ => null ];
197 $classWrapper->lazyOptions += [ __METHOD__ => function () use ( &$ctr ) {
198 return ++$ctr;
199 } ];
200 $popt1 = ParserOptions::newCanonical();
201 $popt2 = ParserOptions::newCanonical();
202 $this->assertFalse( $popt1->matches( $popt2 ) );
203
204 ScopedCallback::consume( $reset );
205 }
206
207 }