3 use Wikimedia\TestingAccessWrapper
;
4 use Wikimedia\ScopedCallback
;
7 * @covers ParserOptions
9 class ParserOptionsTest
extends MediaWikiTestCase
{
11 private static function clearCache() {
12 $wrap = TestingAccessWrapper
::newFromClass( ParserOptions
::class );
13 $wrap->defaults
= null;
14 $wrap->lazyOptions
= [
15 'dateformat' => [ ParserOptions
::class, 'initDateFormat' ],
19 'numberheadings' => true,
21 'stubthreshold' => true,
28 protected function setUp() {
34 $this->setMwGlobals( [
35 'wgRenderHashAppend' => '',
37 'PageRenderingHash' => [],
42 protected function tearDown() {
48 * @dataProvider provideIsSafeToCache
49 * @param bool $expect Expected value
50 * @param array $options Options to set
52 public function testIsSafeToCache( $expect, $options ) {
53 $popt = ParserOptions
::newCanonical();
54 foreach ( $options as $name => $value ) {
55 $popt->setOption( $name, $value );
57 $this->assertSame( $expect, $popt->isSafeToCache() );
60 public static function provideIsSafeToCache() {
62 'No overrides' => [ true, [] ],
63 'In-key options are ok' => [ true, [
67 'Non-in-key options are not ok' => [ false, [
68 'removeComments' => false,
70 'Canonical override, not default (1)' => [ true, [
73 'Canonical override, not default (2)' => [ false, [
80 * @dataProvider provideOptionsHash
81 * @param array $usedOptions Used options
82 * @param string $expect Expected value
83 * @param array $options Options to set
84 * @param array $globals Globals to set
86 public function testOptionsHash( $usedOptions, $expect, $options, $globals = [] ) {
92 $globals['wgHooks'] +
= [
93 'PageRenderingHash' => [],
95 $this->setMwGlobals( $globals );
97 $popt = ParserOptions
::newCanonical();
98 foreach ( $options as $name => $value ) {
99 $popt->setOption( $name, $value );
101 $this->assertSame( $expect, $popt->optionsHash( $usedOptions ) );
104 public static function provideOptionsHash() {
105 $used = [ 'thumbsize', 'printable' ];
107 $classWrapper = TestingAccessWrapper
::newFromClass( ParserOptions
::class );
108 $classWrapper->getDefaults();
109 $allUsableOptions = array_diff(
110 array_keys( $classWrapper->inCacheKey
),
111 array_keys( $classWrapper->lazyOptions
)
115 'Canonical options, nothing used' => [ [], 'canonical', [] ],
116 'Canonical options, used some options' => [ $used, 'canonical', [] ],
117 'Used some options, non-default values' => [
119 'printable=1!thumbsize=200',
125 'Canonical options, used all non-lazy options' => [ $allUsableOptions, 'canonical', [] ],
126 'Canonical options, nothing used, but with hooks and $wgRenderHashAppend' => [
128 'canonical!wgRenderHashAppend!onPageRenderingHash',
131 'wgRenderHashAppend' => '!wgRenderHashAppend',
132 'wgHooks' => [ 'PageRenderingHash' => [ [ __CLASS__
. '::onPageRenderingHash' ] ] ],
138 public static function onPageRenderingHash( &$confstr ) {
139 $confstr .= '!onPageRenderingHash';
142 // Test weird historical behavior is still weird
143 public function testOptionsHashEditSection() {
144 $popt = ParserOptions
::newCanonical();
145 $popt->registerWatcher( function ( $name ) {
146 $this->assertNotEquals( 'editsection', $name );
149 $this->assertTrue( $popt->getEditSection() );
150 $this->assertSame( 'canonical', $popt->optionsHash( [] ) );
151 $this->assertSame( 'canonical', $popt->optionsHash( [ 'editsection' ] ) );
153 $popt->setEditSection( false );
154 $this->assertFalse( $popt->getEditSection() );
155 $this->assertSame( 'canonical', $popt->optionsHash( [] ) );
156 $this->assertSame( 'editsection=0', $popt->optionsHash( [ 'editsection' ] ) );
160 * @expectedException InvalidArgumentException
161 * @expectedExceptionMessage Unknown parser option bogus
163 public function testGetInvalidOption() {
164 $popt = ParserOptions
::newCanonical();
165 $popt->getOption( 'bogus' );
169 * @expectedException InvalidArgumentException
170 * @expectedExceptionMessage Unknown parser option bogus
172 public function testSetInvalidOption() {
173 $popt = ParserOptions
::newCanonical();
174 $popt->setOption( 'bogus', true );
177 public function testMatches() {
178 $classWrapper = TestingAccessWrapper
::newFromClass( ParserOptions
::class );
179 $oldDefaults = $classWrapper->defaults
;
180 $oldLazy = $classWrapper->lazyOptions
;
181 $reset = new ScopedCallback( function () use ( $classWrapper, $oldDefaults, $oldLazy ) {
182 $classWrapper->defaults
= $oldDefaults;
183 $classWrapper->lazyOptions
= $oldLazy;
186 $popt1 = ParserOptions
::newCanonical();
187 $popt2 = ParserOptions
::newCanonical();
188 $this->assertTrue( $popt1->matches( $popt2 ) );
190 $popt1->enableLimitReport( true );
191 $popt2->enableLimitReport( false );
192 $this->assertTrue( $popt1->matches( $popt2 ) );
194 $popt2->setTidy( !$popt2->getTidy() );
195 $this->assertFalse( $popt1->matches( $popt2 ) );
198 $classWrapper->defaults +
= [ __METHOD__
=> null ];
199 $classWrapper->lazyOptions +
= [ __METHOD__
=> function () use ( &$ctr ) {
202 $popt1 = ParserOptions
::newCanonical();
203 $popt2 = ParserOptions
::newCanonical();
204 $this->assertFalse( $popt1->matches( $popt2 ) );
206 ScopedCallback
::consume( $reset );
209 public function testAllCacheVaryingOptions() {
212 // $wgHooks is already saved in self::setUp(), so we can modify it freely here
213 $wgHooks['ParserOptionsRegister'] = [];
215 'dateformat', 'numberheadings', 'printable', 'stubthreshold',
216 'thumbsize', 'userlang', 'wrapclass',
217 ], ParserOptions
::allCacheVaryingOptions() );
221 $wgHooks['ParserOptionsRegister'][] = function ( &$defaults, &$inCacheKey ) {
233 'dateformat', 'foo', 'numberheadings', 'printable', 'stubthreshold',
234 'thumbsize', 'userlang', 'wrapclass',
235 ], ParserOptions
::allCacheVaryingOptions() );