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() {
33 $this->setMwGlobals( [
34 'wgRenderHashAppend' => '',
36 'PageRenderingHash' => [],
41 protected function tearDown() {
47 * @dataProvider provideIsSafeToCache
48 * @param bool $expect Expected value
49 * @param array $options Options to set
51 public function testIsSafeToCache( $expect, $options ) {
52 $popt = ParserOptions
::newCanonical();
53 foreach ( $options as $name => $value ) {
54 $popt->setOption( $name, $value );
56 $this->assertSame( $expect, $popt->isSafeToCache() );
59 public static function provideIsSafeToCache() {
61 'No overrides' => [ true, [] ],
62 'In-key options are ok' => [ true, [
66 'Non-in-key options are not ok' => [ false, [
67 'removeComments' => false,
69 'Non-in-key options are not ok (2)' => [ false, [
70 'wrapclass' => 'foobar',
72 'Canonical override, not default (1)' => [ true, [
75 'Canonical override, not default (2)' => [ false, [
82 * @dataProvider provideOptionsHash
83 * @param array $usedOptions Used options
84 * @param string $expect Expected value
85 * @param array $options Options to set
86 * @param array $globals Globals to set
88 public function testOptionsHash( $usedOptions, $expect, $options, $globals = [] ) {
94 $globals['wgHooks'] +
= [
95 'PageRenderingHash' => [],
97 $this->setMwGlobals( $globals );
99 $popt = ParserOptions
::newCanonical();
100 foreach ( $options as $name => $value ) {
101 $popt->setOption( $name, $value );
103 $this->assertSame( $expect, $popt->optionsHash( $usedOptions ) );
106 public static function provideOptionsHash() {
107 $used = [ 'thumbsize', 'printable' ];
109 $classWrapper = TestingAccessWrapper
::newFromClass( ParserOptions
::class );
110 $classWrapper->getDefaults();
111 $allUsableOptions = array_diff(
112 array_keys( $classWrapper->inCacheKey
),
113 array_keys( $classWrapper->lazyOptions
)
117 'Canonical options, nothing used' => [ [], 'canonical', [] ],
118 'Canonical options, used some options' => [ $used, 'canonical', [] ],
119 'Used some options, non-default values' => [
121 'printable=1!thumbsize=200',
127 'Canonical options, used all non-lazy options' => [ $allUsableOptions, 'canonical', [] ],
128 'Canonical options, nothing used, but with hooks and $wgRenderHashAppend' => [
130 'canonical!wgRenderHashAppend!onPageRenderingHash',
133 'wgRenderHashAppend' => '!wgRenderHashAppend',
134 'wgHooks' => [ 'PageRenderingHash' => [ [ __CLASS__
. '::onPageRenderingHash' ] ] ],
140 public static function onPageRenderingHash( &$confstr ) {
141 $confstr .= '!onPageRenderingHash';
145 * @expectedException InvalidArgumentException
146 * @expectedExceptionMessage Unknown parser option bogus
148 public function testGetInvalidOption() {
149 $popt = ParserOptions
::newCanonical();
150 $popt->getOption( 'bogus' );
154 * @expectedException InvalidArgumentException
155 * @expectedExceptionMessage Unknown parser option bogus
157 public function testSetInvalidOption() {
158 $popt = ParserOptions
::newCanonical();
159 $popt->setOption( 'bogus', true );
162 public function testMatches() {
163 $classWrapper = TestingAccessWrapper
::newFromClass( ParserOptions
::class );
164 $oldDefaults = $classWrapper->defaults
;
165 $oldLazy = $classWrapper->lazyOptions
;
166 $reset = new ScopedCallback( function () use ( $classWrapper, $oldDefaults, $oldLazy ) {
167 $classWrapper->defaults
= $oldDefaults;
168 $classWrapper->lazyOptions
= $oldLazy;
171 $popt1 = ParserOptions
::newCanonical();
172 $popt2 = ParserOptions
::newCanonical();
173 $this->assertTrue( $popt1->matches( $popt2 ) );
175 $popt1->enableLimitReport( true );
176 $popt2->enableLimitReport( false );
177 $this->assertTrue( $popt1->matches( $popt2 ) );
179 $popt2->setTidy( !$popt2->getTidy() );
180 $this->assertFalse( $popt1->matches( $popt2 ) );
183 $classWrapper->defaults +
= [ __METHOD__
=> null ];
184 $classWrapper->lazyOptions +
= [ __METHOD__
=> function () use ( &$ctr ) {
187 $popt1 = ParserOptions
::newCanonical();
188 $popt2 = ParserOptions
::newCanonical();
189 $this->assertFalse( $popt1->matches( $popt2 ) );
191 ScopedCallback
::consume( $reset );
194 public function testAllCacheVaryingOptions() {
197 // $wgHooks is already saved in self::setUp(), so we can modify it freely here
198 $wgHooks['ParserOptionsRegister'] = [];
200 'dateformat', 'numberheadings', 'printable', 'stubthreshold',
201 'thumbsize', 'userlang'
202 ], ParserOptions
::allCacheVaryingOptions() );
206 $wgHooks['ParserOptionsRegister'][] = function ( &$defaults, &$inCacheKey ) {
218 'dateformat', 'foo', 'numberheadings', 'printable', 'stubthreshold',
219 'thumbsize', 'userlang'
220 ], ParserOptions
::allCacheVaryingOptions() );