Merge "Remove space after cast"
[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 'thumbsize' => 1e100,
26 'wrapclass' => false,
27 ] ],
28 'Non-in-key options are not ok' => [ false, [
29 'removeComments' => false,
30 ] ],
31 'Canonical override, not default (1)' => [ true, [
32 'tidy' => true,
33 ] ],
34 'Canonical override, not default (2)' => [ false, [
35 'tidy' => false,
36 ] ],
37 ];
38 }
39
40 /**
41 * @dataProvider provideOptionsHash
42 * @param array $usedOptions Used options
43 * @param string $expect Expected value
44 * @param array $options Options to set
45 * @param array $globals Globals to set
46 */
47 public function testOptionsHash( $usedOptions, $expect, $options, $globals = [] ) {
48 global $wgHooks;
49
50 $globals += [
51 'wgRenderHashAppend' => '',
52 'wgHooks' => [],
53 ];
54 $globals['wgHooks'] += [
55 'PageRenderingHash' => [],
56 ] + $wgHooks;
57 $this->setMwGlobals( $globals );
58
59 $popt = ParserOptions::newCanonical();
60 foreach ( $options as $name => $value ) {
61 $popt->setOption( $name, $value );
62 }
63 $this->assertSame( $expect, $popt->optionsHash( $usedOptions ) );
64 }
65
66 public static function provideOptionsHash() {
67 $used = [ 'wrapclass', 'printable' ];
68
69 $classWrapper = TestingAccessWrapper::newFromClass( ParserOptions::class );
70 $classWrapper->getDefaults();
71 $allUsableOptions = array_diff(
72 array_keys( $classWrapper->inCacheKey ),
73 array_keys( $classWrapper->lazyOptions )
74 );
75
76 return [
77 'Canonical options, nothing used' => [ [], 'canonical', [] ],
78 'Canonical options, used some options' => [ $used, 'canonical', [] ],
79 'Used some options, non-default values' => [
80 $used,
81 'printable=1!wrapclass=foobar',
82 [
83 'wrapclass' => 'foobar',
84 'printable' => true,
85 ]
86 ],
87 'Canonical options, used all non-lazy options' => [ $allUsableOptions, 'canonical', [] ],
88 'Canonical options, nothing used, but with hooks and $wgRenderHashAppend' => [
89 [],
90 'canonical!wgRenderHashAppend!onPageRenderingHash',
91 [],
92 [
93 'wgRenderHashAppend' => '!wgRenderHashAppend',
94 'wgHooks' => [ 'PageRenderingHash' => [ [ __CLASS__ . '::onPageRenderingHash' ] ] ],
95 ]
96 ],
97 ];
98 }
99
100 public static function onPageRenderingHash( &$confstr ) {
101 $confstr .= '!onPageRenderingHash';
102 }
103
104 // Test weird historical behavior is still weird
105 public function testOptionsHashEditSection() {
106 global $wgHooks;
107
108 $this->setMwGlobals( [
109 'wgRenderHashAppend' => '',
110 'wgHooks' => [ 'PageRenderingHash' => [] ] + $wgHooks,
111 ] );
112
113 $popt = ParserOptions::newCanonical();
114 $popt->registerWatcher( function ( $name ) {
115 $this->assertNotEquals( 'editsection', $name );
116 } );
117
118 $this->assertTrue( $popt->getEditSection() );
119 $this->assertSame( 'canonical', $popt->optionsHash( [] ) );
120 $this->assertSame( 'canonical', $popt->optionsHash( [ 'editsection' ] ) );
121
122 $popt->setEditSection( false );
123 $this->assertFalse( $popt->getEditSection() );
124 $this->assertSame( 'canonical', $popt->optionsHash( [] ) );
125 $this->assertSame( 'editsection=0', $popt->optionsHash( [ 'editsection' ] ) );
126 }
127
128 /**
129 * @expectedException InvalidArgumentException
130 * @expectedExceptionMessage Unknown parser option bogus
131 */
132 public function testGetInvalidOption() {
133 $popt = ParserOptions::newCanonical();
134 $popt->getOption( 'bogus' );
135 }
136
137 /**
138 * @expectedException InvalidArgumentException
139 * @expectedExceptionMessage Unknown parser option bogus
140 */
141 public function testSetInvalidOption() {
142 $popt = ParserOptions::newCanonical();
143 $popt->setOption( 'bogus', true );
144 }
145
146 public function testMatches() {
147 $classWrapper = TestingAccessWrapper::newFromClass( ParserOptions::class );
148 $oldDefaults = $classWrapper->defaults;
149 $oldLazy = $classWrapper->lazyOptions;
150 $reset = new ScopedCallback( function () use ( $classWrapper, $oldDefaults, $oldLazy ) {
151 $classWrapper->defaults = $oldDefaults;
152 $classWrapper->lazyOptions = $oldLazy;
153 } );
154
155 $popt1 = ParserOptions::newCanonical();
156 $popt2 = ParserOptions::newCanonical();
157 $this->assertTrue( $popt1->matches( $popt2 ) );
158
159 $popt1->enableLimitReport( true );
160 $popt2->enableLimitReport( false );
161 $this->assertTrue( $popt1->matches( $popt2 ) );
162
163 $popt2->setTidy( !$popt2->getTidy() );
164 $this->assertFalse( $popt1->matches( $popt2 ) );
165
166 $ctr = 0;
167 $classWrapper->defaults += [ __METHOD__ => null ];
168 $classWrapper->lazyOptions += [ __METHOD__ => function () use ( &$ctr ) {
169 return ++$ctr;
170 } ];
171 $popt1 = ParserOptions::newCanonical();
172 $popt2 = ParserOptions::newCanonical();
173 $this->assertFalse( $popt1->matches( $popt2 ) );
174
175 ScopedCallback::consume( $reset );
176 }
177
178 }