3 use Wikimedia\TestingAccessWrapper
;
4 use Wikimedia\ScopedCallback
;
6 class ParserOptionsTest
extends MediaWikiTestCase
{
9 * @dataProvider provideIsSafeToCache
10 * @param bool $expect Expected value
11 * @param array $options Options to set
13 public function testIsSafeToCache( $expect, $options ) {
14 $popt = ParserOptions
::newCanonical();
15 foreach ( $options as $name => $value ) {
16 $popt->setOption( $name, $value );
18 $this->assertSame( $expect, $popt->isSafeToCache() );
21 public static function provideIsSafeToCache() {
23 'No overrides' => [ true, [] ],
24 'In-key options are ok' => [ true, [
25 'editsection' => false,
29 'Non-in-key options are not ok' => [ false, [
30 'removeComments' => false,
32 'Canonical override, not default (1)' => [ true, [
35 'Canonical override, not default (2)' => [ false, [
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
48 public function testOptionsHashPre30( $usedOptions, $expect, $options, $globals = [] ) {
52 'wgRenderHashAppend' => '',
55 $globals['wgHooks'] +
= [
56 'PageRenderingHash' => [],
58 $this->setMwGlobals( $globals );
60 $popt = new ParserOptions();
61 foreach ( $options as $setter => $value ) {
62 $popt->$setter( $value );
64 $this->assertSame( $expect, $popt->optionsHashPre30( $usedOptions ) );
67 public static function provideOptionsHashPre30() {
68 $used = [ 'wrapclass', 'editsection', 'printable' ];
71 'Canonical options, nothing used' => [ [], '*!*!*!*!*!*', [] ],
72 'Canonical options, used some options' => [ $used, '*!*!*!*!*', [] ],
73 'Used some options, non-default values' => [
75 '*!*!*!*!*!printable=1!wrapclass=foobar',
77 'setWrapOutputClass' => 'foobar',
78 'setIsPrintable' => true,
81 'Canonical options, nothing used, but with hooks and $wgRenderHashAppend' => [
83 '*!*!*!*!*!wgRenderHashAppend!*!onPageRenderingHash',
86 'wgRenderHashAppend' => '!wgRenderHashAppend',
87 'wgHooks' => [ 'PageRenderingHash' => [ [ __CLASS__
. '::onPageRenderingHash' ] ] ],
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
100 public function testOptionsHash( $usedOptions, $expect, $options, $globals = [] ) {
104 'wgRenderHashAppend' => '',
107 $globals['wgHooks'] +
= [
108 'PageRenderingHash' => [],
110 $this->setMwGlobals( $globals );
112 $popt = ParserOptions
::newCanonical();
113 foreach ( $options as $name => $value ) {
114 $popt->setOption( $name, $value );
116 $this->assertSame( $expect, $popt->optionsHash( $usedOptions ) );
119 public static function provideOptionsHash() {
120 $used = [ 'wrapclass', 'editsection', 'printable' ];
122 $classWrapper = TestingAccessWrapper
::newFromClass( ParserOptions
::class );
123 $classWrapper->getDefaults();
124 $allUsableOptions = array_diff(
125 array_keys( $classWrapper->inCacheKey
),
126 array_keys( $classWrapper->lazyOptions
)
130 'Canonical options, nothing used' => [ [], 'canonical', [] ],
131 'Canonical options, used some options' => [ $used, 'canonical', [] ],
132 'Used some options, non-default values' => [
134 'printable=1!wrapclass=foobar',
136 'wrapclass' => 'foobar',
140 'Canonical options, used all non-lazy options' => [ $allUsableOptions, 'canonical', [] ],
141 'Canonical options, nothing used, but with hooks and $wgRenderHashAppend' => [
143 'canonical!wgRenderHashAppend!onPageRenderingHash',
146 'wgRenderHashAppend' => '!wgRenderHashAppend',
147 'wgHooks' => [ 'PageRenderingHash' => [ [ __CLASS__
. '::onPageRenderingHash' ] ] ],
153 public static function onPageRenderingHash( &$confstr ) {
154 $confstr .= '!onPageRenderingHash';
158 * @expectedException InvalidArgumentException
159 * @expectedExceptionMessage Unknown parser option bogus
161 public function testGetInvalidOption() {
162 $popt = ParserOptions
::newCanonical();
163 $popt->getOption( 'bogus' );
167 * @expectedException InvalidArgumentException
168 * @expectedExceptionMessage Unknown parser option bogus
170 public function testSetInvalidOption() {
171 $popt = ParserOptions
::newCanonical();
172 $popt->setOption( 'bogus', true );
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;
184 $popt1 = ParserOptions
::newCanonical();
185 $popt2 = ParserOptions
::newCanonical();
186 $this->assertTrue( $popt1->matches( $popt2 ) );
188 $popt1->enableLimitReport( true );
189 $popt2->enableLimitReport( false );
190 $this->assertTrue( $popt1->matches( $popt2 ) );
192 $popt2->setTidy( !$popt2->getTidy() );
193 $this->assertFalse( $popt1->matches( $popt2 ) );
196 $classWrapper->defaults +
= [ __METHOD__
=> null ];
197 $classWrapper->lazyOptions +
= [ __METHOD__
=> function () use ( &$ctr ) {
200 $popt1 = ParserOptions
::newCanonical();
201 $popt2 = ParserOptions
::newCanonical();
202 $this->assertFalse( $popt1->matches( $popt2 ) );
204 ScopedCallback
::consume( $reset );