Merge "Special:AllPages: Overriding the title for form submission"
[lhc/web/wiklou.git] / tests / phpunit / includes / parser / ParserOptionsTest.php
index 93ab35c..8c61b03 100644 (file)
@@ -21,22 +21,20 @@ class ParserOptionsTest extends MediaWikiTestCase {
                        'stubthreshold' => true,
                        'printable' => true,
                        'userlang' => true,
-                       'wrapclass' => true,
                ];
        }
 
        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() {
@@ -62,11 +60,14 @@ class ParserOptionsTest extends MediaWikiTestCase {
                        'No overrides' => [ true, [] ],
                        'In-key options are ok' => [ true, [
                                'thumbsize' => 1e100,
-                               'wrapclass' => false,
+                               'printable' => false,
                        ] ],
                        'Non-in-key options are not ok' => [ false, [
                                'removeComments' => false,
                        ] ],
+                       'Non-in-key options are not ok (2)' => [ false, [
+                               'wrapclass' => 'foobar',
+                       ] ],
                        'Canonical override, not default (1)' => [ true, [
                                'tidy' => true,
                        ] ],
@@ -82,17 +83,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 ) {
@@ -102,7 +99,7 @@ class ParserOptionsTest extends MediaWikiTestCase {
        }
 
        public static function provideOptionsHash() {
-               $used = [ 'wrapclass', 'printable' ];
+               $used = [ 'thumbsize', 'printable' ];
 
                $classWrapper = TestingAccessWrapper::newFromClass( ParserOptions::class );
                $classWrapper->getDefaults();
@@ -116,9 +113,9 @@ class ParserOptionsTest extends MediaWikiTestCase {
                        'Canonical options, used some options' => [ $used, 'canonical', [] ],
                        'Used some options, non-default values' => [
                                $used,
-                               'printable=1!wrapclass=foobar',
+                               'printable=1!thumbsize=200',
                                [
-                                       'wrapclass' => 'foobar',
+                                       'thumbsize' => 200,
                                        'printable' => true,
                                ]
                        ],
@@ -127,33 +124,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';
        }
 
        /**
@@ -207,18 +223,15 @@ 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', 'wrapclass',
+                       'thumbsize', 'userlang'
                ], ParserOptions::allCacheVaryingOptions() );
 
                self::clearCache();
 
-               $wgHooks['ParserOptionsRegister'][] = function ( &$defaults, &$inCacheKey ) {
+               $this->setTemporaryHook( 'ParserOptionsRegister', function ( &$defaults, &$inCacheKey ) {
                        $defaults += [
                                'foo' => 'foo',
                                'bar' => 'bar',
@@ -228,10 +241,10 @@ class ParserOptionsTest extends MediaWikiTestCase {
                                'foo' => true,
                                'bar' => false,
                        ];
-               };
+               } );
                $this->assertSame( [
                        'dateformat', 'foo', 'numberheadings', 'printable', 'stubthreshold',
-                       'thumbsize', 'userlang', 'wrapclass',
+                       'thumbsize', 'userlang'
                ], ParserOptions::allCacheVaryingOptions() );
        }