X-Git-Url: https://git.heureux-cyclage.org/?a=blobdiff_plain;f=tests%2Fphpunit%2Fincludes%2Fparser%2FParserOptionsTest.php;h=48205f4456d2a780ee90ed5b95088c1f709d35fc;hb=32fd2342e5648ec7d37eecb00fc4044a3a7526a9;hp=d55372c61f70490ee405928f2c1010451f430adb;hpb=9527edfd59f889151e0334f1a729acf2f730cfde;p=lhc%2Fweb%2Fwiklou.git diff --git a/tests/phpunit/includes/parser/ParserOptionsTest.php b/tests/phpunit/includes/parser/ParserOptionsTest.php index d55372c61f..48205f4456 100644 --- a/tests/phpunit/includes/parser/ParserOptionsTest.php +++ b/tests/phpunit/includes/parser/ParserOptionsTest.php @@ -25,17 +25,16 @@ class ParserOptionsTest extends MediaWikiTestCase { } protected function setUp() { - global $wgHooks; - parent::setUp(); self::clearCache(); $this->setMwGlobals( [ 'wgRenderHashAppend' => '', - 'wgHooks' => [ - 'PageRenderingHash' => [], - ] + $wgHooks, ] ); + + // This is crazy, but registering false, null, or other falsey values + // as a hook callback "works". + $this->setTemporaryHook( 'PageRenderingHash', null ); } protected function tearDown() { @@ -43,6 +42,68 @@ class ParserOptionsTest extends MediaWikiTestCase { parent::tearDown(); } + public function testNewCanonical() { + $wgUser = $this->getMutableTestUser()->getUser(); + $wgLang = Language::factory( 'fr' ); + $wgContLang = Language::factory( 'qqx' ); + + $this->setMwGlobals( [ + 'wgUser' => $wgUser, + 'wgLang' => $wgLang, + 'wgContLang' => $wgContLang, + ] ); + + $user = $this->getMutableTestUser()->getUser(); + $lang = Language::factory( 'de' ); + $lang2 = Language::factory( 'bug' ); + $context = new DerivativeContext( RequestContext::getMain() ); + $context->setUser( $user ); + $context->setLanguage( $lang ); + + // No parameters picks up $wgUser and $wgLang + $popt = ParserOptions::newCanonical(); + $this->assertSame( $wgUser, $popt->getUser() ); + $this->assertSame( $wgLang, $popt->getUserLangObj() ); + + // Just a user uses $wgLang + $popt = ParserOptions::newCanonical( $user ); + $this->assertSame( $user, $popt->getUser() ); + $this->assertSame( $wgLang, $popt->getUserLangObj() ); + + // Just a language uses $wgUser + $popt = ParserOptions::newCanonical( null, $lang ); + $this->assertSame( $wgUser, $popt->getUser() ); + $this->assertSame( $lang, $popt->getUserLangObj() ); + + // Passing both works + $popt = ParserOptions::newCanonical( $user, $lang ); + $this->assertSame( $user, $popt->getUser() ); + $this->assertSame( $lang, $popt->getUserLangObj() ); + + // Passing 'canonical' uses an anon and $wgContLang, and ignores + // any passed $userLang + $popt = ParserOptions::newCanonical( 'canonical' ); + $this->assertTrue( $popt->getUser()->isAnon() ); + $this->assertSame( $wgContLang, $popt->getUserLangObj() ); + $popt = ParserOptions::newCanonical( 'canonical', $lang2 ); + $this->assertSame( $wgContLang, $popt->getUserLangObj() ); + + // Passing an IContextSource uses the user and lang from it, and ignores + // any passed $userLang + $popt = ParserOptions::newCanonical( $context ); + $this->assertSame( $user, $popt->getUser() ); + $this->assertSame( $lang, $popt->getUserLangObj() ); + $popt = ParserOptions::newCanonical( $context, $lang2 ); + $this->assertSame( $lang, $popt->getUserLangObj() ); + + // Passing something else raises an exception + try { + $popt = ParserOptions::newCanonical( 'bogus' ); + $this->fail( 'Excpected exception not thrown' ); + } catch ( InvalidArgumentException $ex ) { + } + } + /** * @dataProvider provideIsSafeToCache * @param bool $expect Expected value @@ -84,17 +145,13 @@ class ParserOptionsTest extends MediaWikiTestCase { * @param string $expect Expected value * @param array $options Options to set * @param array $globals Globals to set + * @param callable|null $hookFunc PageRenderingHash hook function */ - public function testOptionsHash( $usedOptions, $expect, $options, $globals = [] ) { - global $wgHooks; - - $globals += [ - 'wgHooks' => [], - ]; - $globals['wgHooks'] += [ - 'PageRenderingHash' => [], - ] + $wgHooks; + public function testOptionsHash( + $usedOptions, $expect, $options, $globals = [], $hookFunc = null + ) { $this->setMwGlobals( $globals ); + $this->setTemporaryHook( 'PageRenderingHash', $hookFunc ); $popt = ParserOptions::newCanonical(); foreach ( $options as $name => $value ) { @@ -129,33 +186,52 @@ class ParserOptionsTest extends MediaWikiTestCase { [], 'canonical!wgRenderHashAppend!onPageRenderingHash', [], - [ - 'wgRenderHashAppend' => '!wgRenderHashAppend', - 'wgHooks' => [ 'PageRenderingHash' => [ [ __CLASS__ . '::onPageRenderingHash' ] ] ], - ] + [ 'wgRenderHashAppend' => '!wgRenderHashAppend' ], + [ __CLASS__ . '::onPageRenderingHash' ], ], ]; } - public static function onPageRenderingHash( &$confstr ) { - $confstr .= '!onPageRenderingHash'; - } + public function testUsedLazyOptionsInHash() { + $this->setTemporaryHook( 'ParserOptionsRegister', + function ( &$defaults, &$inCacheKey, &$lazyOptions ) { + $lazyFuncs = $this->getMockBuilder( stdClass::class ) + ->setMethods( [ 'neverCalled', 'calledOnce' ] ) + ->getMock(); + $lazyFuncs->expects( $this->never() )->method( 'neverCalled' ); + $lazyFuncs->expects( $this->once() )->method( 'calledOnce' )->willReturn( 'value' ); + + $defaults += [ + 'opt1' => null, + 'opt2' => null, + 'opt3' => null, + ]; + $inCacheKey += [ + 'opt1' => true, + 'opt2' => true, + ]; + $lazyOptions += [ + 'opt1' => [ $lazyFuncs, 'calledOnce' ], + 'opt2' => [ $lazyFuncs, 'neverCalled' ], + 'opt3' => [ $lazyFuncs, 'neverCalled' ], + ]; + } + ); + + self::clearCache(); - // Test weird historical behavior is still weird - public function testOptionsHashEditSection() { $popt = ParserOptions::newCanonical(); - $popt->registerWatcher( function ( $name ) { - $this->assertNotEquals( 'editsection', $name ); + $popt->registerWatcher( function () { + $this->fail( 'Watcher should not have been called' ); } ); + $this->assertSame( 'opt1=value', $popt->optionsHash( [ 'opt1', 'opt3' ] ) ); - $this->assertTrue( $popt->getEditSection() ); - $this->assertSame( 'canonical', $popt->optionsHash( [] ) ); - $this->assertSame( 'canonical', $popt->optionsHash( [ 'editsection' ] ) ); + // Second call to see that opt1 isn't resolved a second time + $this->assertSame( 'opt1=value', $popt->optionsHash( [ 'opt1', 'opt3' ] ) ); + } - $popt->setEditSection( false ); - $this->assertFalse( $popt->getEditSection() ); - $this->assertSame( 'canonical', $popt->optionsHash( [] ) ); - $this->assertSame( 'editsection=0', $popt->optionsHash( [ 'editsection' ] ) ); + public static function onPageRenderingHash( &$confstr ) { + $confstr .= '!onPageRenderingHash'; } /** @@ -209,10 +285,7 @@ class ParserOptionsTest extends MediaWikiTestCase { } public function testAllCacheVaryingOptions() { - global $wgHooks; - - // $wgHooks is already saved in self::setUp(), so we can modify it freely here - $wgHooks['ParserOptionsRegister'] = []; + $this->setTemporaryHook( 'ParserOptionsRegister', null ); $this->assertSame( [ 'dateformat', 'numberheadings', 'printable', 'stubthreshold', 'thumbsize', 'userlang' @@ -220,7 +293,7 @@ class ParserOptionsTest extends MediaWikiTestCase { self::clearCache(); - $wgHooks['ParserOptionsRegister'][] = function ( &$defaults, &$inCacheKey ) { + $this->setTemporaryHook( 'ParserOptionsRegister', function ( &$defaults, &$inCacheKey ) { $defaults += [ 'foo' => 'foo', 'bar' => 'bar', @@ -230,7 +303,7 @@ class ParserOptionsTest extends MediaWikiTestCase { 'foo' => true, 'bar' => false, ]; - }; + } ); $this->assertSame( [ 'dateformat', 'foo', 'numberheadings', 'printable', 'stubthreshold', 'thumbsize', 'userlang'