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, [
28 'Non-in-key options are not ok' => [ false, [
29 'removeComments' => false,
31 'Canonical override, not default (1)' => [ true, [
34 'Canonical override, not default (2)' => [ false, [
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
47 public function testOptionsHash( $usedOptions, $expect, $options, $globals = [] ) {
51 'wgRenderHashAppend' => '',
54 $globals['wgHooks'] +
= [
55 'PageRenderingHash' => [],
57 $this->setMwGlobals( $globals );
59 $popt = ParserOptions
::newCanonical();
60 foreach ( $options as $name => $value ) {
61 $popt->setOption( $name, $value );
63 $this->assertSame( $expect, $popt->optionsHash( $usedOptions ) );
66 public static function provideOptionsHash() {
67 $used = [ 'wrapclass', 'printable' ];
69 $classWrapper = TestingAccessWrapper
::newFromClass( ParserOptions
::class );
70 $classWrapper->getDefaults();
71 $allUsableOptions = array_diff(
72 array_keys( $classWrapper->inCacheKey
),
73 array_keys( $classWrapper->lazyOptions
)
77 'Canonical options, nothing used' => [ [], 'canonical', [] ],
78 'Canonical options, used some options' => [ $used, 'canonical', [] ],
79 'Used some options, non-default values' => [
81 'printable=1!wrapclass=foobar',
83 'wrapclass' => 'foobar',
87 'Canonical options, used all non-lazy options' => [ $allUsableOptions, 'canonical', [] ],
88 'Canonical options, nothing used, but with hooks and $wgRenderHashAppend' => [
90 'canonical!wgRenderHashAppend!onPageRenderingHash',
93 'wgRenderHashAppend' => '!wgRenderHashAppend',
94 'wgHooks' => [ 'PageRenderingHash' => [ [ __CLASS__
. '::onPageRenderingHash' ] ] ],
100 public static function onPageRenderingHash( &$confstr ) {
101 $confstr .= '!onPageRenderingHash';
104 // Test weird historical behavior is still weird
105 public function testOptionsHashEditSection() {
108 $this->setMwGlobals( [
109 'wgRenderHashAppend' => '',
110 'wgHooks' => [ 'PageRenderingHash' => [] ] +
$wgHooks,
113 $popt = ParserOptions
::newCanonical();
114 $popt->registerWatcher( function ( $name ) {
115 $this->assertNotEquals( 'editsection', $name );
118 $this->assertTrue( $popt->getEditSection() );
119 $this->assertSame( 'canonical', $popt->optionsHash( [] ) );
120 $this->assertSame( 'canonical', $popt->optionsHash( [ 'editsection' ] ) );
122 $popt->setEditSection( false );
123 $this->assertFalse( $popt->getEditSection() );
124 $this->assertSame( 'canonical', $popt->optionsHash( [] ) );
125 $this->assertSame( 'editsection=0', $popt->optionsHash( [ 'editsection' ] ) );
129 * @expectedException InvalidArgumentException
130 * @expectedExceptionMessage Unknown parser option bogus
132 public function testGetInvalidOption() {
133 $popt = ParserOptions
::newCanonical();
134 $popt->getOption( 'bogus' );
138 * @expectedException InvalidArgumentException
139 * @expectedExceptionMessage Unknown parser option bogus
141 public function testSetInvalidOption() {
142 $popt = ParserOptions
::newCanonical();
143 $popt->setOption( 'bogus', true );
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;
155 $popt1 = ParserOptions
::newCanonical();
156 $popt2 = ParserOptions
::newCanonical();
157 $this->assertTrue( $popt1->matches( $popt2 ) );
159 $popt1->enableLimitReport( true );
160 $popt2->enableLimitReport( false );
161 $this->assertTrue( $popt1->matches( $popt2 ) );
163 $popt2->setTidy( !$popt2->getTidy() );
164 $this->assertFalse( $popt1->matches( $popt2 ) );
167 $classWrapper->defaults +
= [ __METHOD__
=> null ];
168 $classWrapper->lazyOptions +
= [ __METHOD__
=> function () use ( &$ctr ) {
171 $popt1 = ParserOptions
::newCanonical();
172 $popt2 = ParserOptions
::newCanonical();
173 $this->assertFalse( $popt1->matches( $popt2 ) );
175 ScopedCallback
::consume( $reset );