use Wikimedia\TestingAccessWrapper;
use Wikimedia\ScopedCallback;
+/**
+ * @covers ParserOptions
+ */
class ParserOptionsTest extends MediaWikiTestCase {
+ private static function clearCache() {
+ $wrap = TestingAccessWrapper::newFromClass( ParserOptions::class );
+ $wrap->defaults = null;
+ $wrap->lazyOptions = [
+ 'dateformat' => [ ParserOptions::class, 'initDateFormat' ],
+ ];
+ $wrap->inCacheKey = [
+ 'dateformat' => true,
+ 'numberheadings' => true,
+ 'thumbsize' => true,
+ 'stubthreshold' => true,
+ 'printable' => true,
+ 'userlang' => true,
+ 'wrapclass' => true,
+ ];
+ }
+
+ protected function setUp() {
+ global $wgHooks;
+
+ parent::setUp();
+ self::clearCache();
+
+ $this->setMwGlobals( [
+ 'wgRenderHashAppend' => '',
+ 'wgHooks' => [
+ 'PageRenderingHash' => [],
+ ] + $wgHooks,
+ ] );
+ }
+
+ protected function tearDown() {
+ self::clearCache();
+ parent::tearDown();
+ }
+
/**
* @dataProvider provideIsSafeToCache
* @param bool $expect Expected value
return [
'No overrides' => [ true, [] ],
'In-key options are ok' => [ true, [
- 'editsection' => false,
'thumbsize' => 1e100,
'wrapclass' => false,
] ],
];
}
- /**
- * @dataProvider provideOptionsHashPre30
- * @param array $usedOptions Used options
- * @param string $expect Expected value
- * @param array $options Options to set
- * @param array $globals Globals to set
- */
- public function testOptionsHashPre30( $usedOptions, $expect, $options, $globals = [] ) {
- global $wgHooks;
-
- $globals += [
- 'wgRenderHashAppend' => '',
- 'wgHooks' => [],
- ];
- $globals['wgHooks'] += [
- 'PageRenderingHash' => [],
- ] + $wgHooks;
- $this->setMwGlobals( $globals );
-
- $popt = new ParserOptions();
- foreach ( $options as $setter => $value ) {
- $popt->$setter( $value );
- }
- $this->assertSame( $expect, $popt->optionsHashPre30( $usedOptions ) );
- }
-
- public static function provideOptionsHashPre30() {
- $used = [ 'wrapclass', 'editsection', 'printable' ];
-
- return [
- 'Canonical options, nothing used' => [ [], '*!*!*!*!*!*', [] ],
- 'Canonical options, used some options' => [ $used, '*!*!*!*!*', [] ],
- 'Used some options, non-default values' => [
- $used,
- '*!*!*!*!*!printable=1!wrapclass=foobar',
- [
- 'setWrapOutputClass' => 'foobar',
- 'setIsPrintable' => true,
- ]
- ],
- 'Canonical options, nothing used, but with hooks and $wgRenderHashAppend' => [
- [],
- '*!*!*!*!*!wgRenderHashAppend!*!onPageRenderingHash',
- [],
- [
- 'wgRenderHashAppend' => '!wgRenderHashAppend',
- 'wgHooks' => [ 'PageRenderingHash' => [ [ __CLASS__ . '::onPageRenderingHash' ] ] ],
- ]
- ],
- ];
- }
-
/**
* @dataProvider provideOptionsHash
* @param array $usedOptions Used options
global $wgHooks;
$globals += [
- 'wgRenderHashAppend' => '',
'wgHooks' => [],
];
$globals['wgHooks'] += [
}
public static function provideOptionsHash() {
- $used = [ 'wrapclass', 'editsection', 'printable' ];
+ $used = [ 'wrapclass', 'printable' ];
$classWrapper = TestingAccessWrapper::newFromClass( ParserOptions::class );
$classWrapper->getDefaults();
$confstr .= '!onPageRenderingHash';
}
+ // Test weird historical behavior is still weird
+ public function testOptionsHashEditSection() {
+ $popt = ParserOptions::newCanonical();
+ $popt->registerWatcher( function ( $name ) {
+ $this->assertNotEquals( 'editsection', $name );
+ } );
+
+ $this->assertTrue( $popt->getEditSection() );
+ $this->assertSame( 'canonical', $popt->optionsHash( [] ) );
+ $this->assertSame( 'canonical', $popt->optionsHash( [ 'editsection' ] ) );
+
+ $popt->setEditSection( false );
+ $this->assertFalse( $popt->getEditSection() );
+ $this->assertSame( 'canonical', $popt->optionsHash( [] ) );
+ $this->assertSame( 'editsection=0', $popt->optionsHash( [ 'editsection' ] ) );
+ }
+
/**
* @expectedException InvalidArgumentException
* @expectedExceptionMessage Unknown parser option bogus
ScopedCallback::consume( $reset );
}
+ public function testAllCacheVaryingOptions() {
+ global $wgHooks;
+
+ // $wgHooks is already saved in self::setUp(), so we can modify it freely here
+ $wgHooks['ParserOptionsRegister'] = [];
+ $this->assertSame( [
+ 'dateformat', 'numberheadings', 'printable', 'stubthreshold',
+ 'thumbsize', 'userlang', 'wrapclass',
+ ], ParserOptions::allCacheVaryingOptions() );
+
+ self::clearCache();
+
+ $wgHooks['ParserOptionsRegister'][] = function ( &$defaults, &$inCacheKey ) {
+ $defaults += [
+ 'foo' => 'foo',
+ 'bar' => 'bar',
+ 'baz' => 'baz',
+ ];
+ $inCacheKey += [
+ 'foo' => true,
+ 'bar' => false,
+ ];
+ };
+ $this->assertSame( [
+ 'dateformat', 'foo', 'numberheadings', 'printable', 'stubthreshold',
+ 'thumbsize', 'userlang', 'wrapclass',
+ ], ParserOptions::allCacheVaryingOptions() );
+ }
+
}