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,
27 protected function setUp() {
31 $this->setMwGlobals( [
32 'wgRenderHashAppend' => '',
35 // This is crazy, but registering false, null, or other falsey values
36 // as a hook callback "works".
37 $this->setTemporaryHook( 'PageRenderingHash', null );
40 protected function tearDown() {
46 * @dataProvider provideIsSafeToCache
47 * @param bool $expect Expected value
48 * @param array $options Options to set
50 public function testIsSafeToCache( $expect, $options ) {
51 $popt = ParserOptions
::newCanonical();
52 foreach ( $options as $name => $value ) {
53 $popt->setOption( $name, $value );
55 $this->assertSame( $expect, $popt->isSafeToCache() );
58 public static function provideIsSafeToCache() {
60 'No overrides' => [ true, [] ],
61 'In-key options are ok' => [ true, [
65 'Non-in-key options are not ok' => [ false, [
66 'removeComments' => false,
68 'Non-in-key options are not ok (2)' => [ false, [
69 'wrapclass' => 'foobar',
71 'Canonical override, not default (1)' => [ true, [
74 'Canonical override, not default (2)' => [ false, [
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
88 public function testOptionsHash(
89 $usedOptions, $expect, $options, $globals = [], $hookFunc = null
91 $this->setMwGlobals( $globals );
92 $this->setTemporaryHook( 'PageRenderingHash', $hookFunc );
94 $popt = ParserOptions
::newCanonical();
95 foreach ( $options as $name => $value ) {
96 $popt->setOption( $name, $value );
98 $this->assertSame( $expect, $popt->optionsHash( $usedOptions ) );
101 public static function provideOptionsHash() {
102 $used = [ 'thumbsize', 'printable' ];
104 $classWrapper = TestingAccessWrapper
::newFromClass( ParserOptions
::class );
105 $classWrapper->getDefaults();
106 $allUsableOptions = array_diff(
107 array_keys( $classWrapper->inCacheKey
),
108 array_keys( $classWrapper->lazyOptions
)
112 'Canonical options, nothing used' => [ [], 'canonical', [] ],
113 'Canonical options, used some options' => [ $used, 'canonical', [] ],
114 'Used some options, non-default values' => [
116 'printable=1!thumbsize=200',
122 'Canonical options, used all non-lazy options' => [ $allUsableOptions, 'canonical', [] ],
123 'Canonical options, nothing used, but with hooks and $wgRenderHashAppend' => [
125 'canonical!wgRenderHashAppend!onPageRenderingHash',
127 [ 'wgRenderHashAppend' => '!wgRenderHashAppend' ],
128 [ __CLASS__
. '::onPageRenderingHash' ],
133 public function testUsedLazyOptionsInHash() {
134 $this->setTemporaryHook( 'ParserOptionsRegister',
135 function ( &$defaults, &$inCacheKey, &$lazyOptions ) {
136 $lazyFuncs = $this->getMockBuilder( stdClass
::class )
137 ->setMethods( [ 'neverCalled', 'calledOnce' ] )
139 $lazyFuncs->expects( $this->never() )->method( 'neverCalled' );
140 $lazyFuncs->expects( $this->once() )->method( 'calledOnce' )->willReturn( 'value' );
152 'opt1' => [ $lazyFuncs, 'calledOnce' ],
153 'opt2' => [ $lazyFuncs, 'neverCalled' ],
154 'opt3' => [ $lazyFuncs, 'neverCalled' ],
161 $popt = ParserOptions
::newCanonical();
162 $popt->registerWatcher( function () {
163 $this->fail( 'Watcher should not have been called' );
165 $this->assertSame( 'opt1=value', $popt->optionsHash( [ 'opt1', 'opt3' ] ) );
167 // Second call to see that opt1 isn't resolved a second time
168 $this->assertSame( 'opt1=value', $popt->optionsHash( [ 'opt1', 'opt3' ] ) );
171 public static function onPageRenderingHash( &$confstr ) {
172 $confstr .= '!onPageRenderingHash';
176 * @expectedException InvalidArgumentException
177 * @expectedExceptionMessage Unknown parser option bogus
179 public function testGetInvalidOption() {
180 $popt = ParserOptions
::newCanonical();
181 $popt->getOption( 'bogus' );
185 * @expectedException InvalidArgumentException
186 * @expectedExceptionMessage Unknown parser option bogus
188 public function testSetInvalidOption() {
189 $popt = ParserOptions
::newCanonical();
190 $popt->setOption( 'bogus', true );
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;
202 $popt1 = ParserOptions
::newCanonical();
203 $popt2 = ParserOptions
::newCanonical();
204 $this->assertTrue( $popt1->matches( $popt2 ) );
206 $popt1->enableLimitReport( true );
207 $popt2->enableLimitReport( false );
208 $this->assertTrue( $popt1->matches( $popt2 ) );
210 $popt2->setTidy( !$popt2->getTidy() );
211 $this->assertFalse( $popt1->matches( $popt2 ) );
214 $classWrapper->defaults +
= [ __METHOD__
=> null ];
215 $classWrapper->lazyOptions +
= [ __METHOD__
=> function () use ( &$ctr ) {
218 $popt1 = ParserOptions
::newCanonical();
219 $popt2 = ParserOptions
::newCanonical();
220 $this->assertFalse( $popt1->matches( $popt2 ) );
222 ScopedCallback
::consume( $reset );
225 public function testAllCacheVaryingOptions() {
226 $this->setTemporaryHook( 'ParserOptionsRegister', null );
228 'dateformat', 'numberheadings', 'printable', 'stubthreshold',
229 'thumbsize', 'userlang'
230 ], ParserOptions
::allCacheVaryingOptions() );
234 $this->setTemporaryHook( 'ParserOptionsRegister', function ( &$defaults, &$inCacheKey ) {
246 'dateformat', 'foo', 'numberheadings', 'printable', 'stubthreshold',
247 'thumbsize', 'userlang'
248 ], ParserOptions
::allCacheVaryingOptions() );