Merge "Parser: Hard deprecate getConverterLanguage" into REL1_34
[lhc/web/wiklou.git] / tests / phpunit / includes / ExtraParserTest.php
index 94de088..6e699bc 100644 (file)
@@ -1,6 +1,7 @@
 <?php
 
 use MediaWiki\MediaWikiServices;
+use Wikimedia\TestingAccessWrapper;
 
 /**
  * Parser-related tests that don't suit for parserTests.txt
@@ -46,6 +47,25 @@ class ExtraParserTest extends MediaWikiTestCase {
                        $this->parser->parse( $longLine, $title, $options )->getText( [ 'unwrap' => true ] ) );
        }
 
+       /**
+        * @covers Parser::braceSubstitution
+        * @covers SpecialPageFactory::capturePath
+        */
+       public function testSpecialPageTransclusionRestoresGlobalState() {
+               $text = "{{Special:ApiHelp/help}}";
+               $title = Title::newFromText( 'testSpecialPageTransclusionRestoresGlobalState' );
+               $options = ParserOptions::newFromUser( new User() );
+
+               RequestContext::getMain()->setTitle( $title );
+               RequestContext::getMain()->getWikiPage()->CustomTestProp = true;
+
+               $parsed = $this->parser->parse( $text, $title, $options )->getText();
+               $this->assertContains( 'apihelp-header', $parsed );
+
+               // Verify that this property wasn't wiped out by the parse
+               $this->assertTrue( RequestContext::getMain()->getWikiPage()->CustomTestProp );
+       }
+
        /**
         * Test the parser entry points
         * @covers Parser::parse
@@ -215,4 +235,81 @@ class ExtraParserTest extends MediaWikiTestCase {
                $result = $parserOutput->getCategoryLinks();
                $this->assertEmpty( $result );
        }
+
+       /**
+        * @covers Parser::parseLinkParameterPrivate
+        * @dataProvider provideParseLinkParameter
+        */
+       public function testParseLinkParameter( $input, $expected, $expectedLinks, $desc ) {
+               $this->parser->startExternalParse( Title::newFromText( __FUNCTION__ ),
+                       $this->options, Parser::OT_HTML );
+               $output = TestingAccessWrapper::newFromObject( $this->parser )
+                       ->parseLinkParameterPrivate( $input );
+
+               $this->assertEquals( $expected[0], $output[0], "$desc (type)" );
+
+               if ( $expected[0] === 'link-title' ) {
+                       $this->assertTrue( $expected[1]->equals( $output[1] ), "$desc (target)" );
+               } else {
+                       $this->assertEquals( $expected[1], $output[1], "$desc (target)" );
+               }
+
+               foreach ( $expectedLinks as $func => $expected ) {
+                       $output = $this->parser->getOutput()->$func();
+                       $this->assertEquals( $expected, $output, "$desc ($func)" );
+               }
+       }
+
+       public static function provideParseLinkParameter() {
+               return [
+                       [
+                               '',
+                               [ 'no-link', false ],
+                               [],
+                               'Return no link when requested',
+                       ],
+                       [
+                               'https://example.com/',
+                               [ 'link-url', 'https://example.com/' ],
+                               [ 'getExternalLinks' => [ 'https://example.com/' => 1 ] ],
+                               'External link',
+                       ],
+                       [
+                               '//example.com/',
+                               [ 'link-url', '//example.com/' ],
+                               [ 'getExternalLinks' => [ '//example.com/' => 1 ] ],
+                               'External link',
+                       ],
+                       [
+                               'Test',
+                               [ 'link-title', Title::newFromText( 'Test' ) ],
+                               [ 'getLinks' => [ 0 => [ 'Test' => 0 ] ] ],
+                               'Internal link',
+                       ],
+                       [
+                               'mw:Test',
+                               [ 'link-title', Title::newFromText( 'mw:Test' ) ],
+                               [ 'getInterwikiLinks' => [ 'mw' => [ 'Test' => 1 ] ] ],
+                               'Internal link (interwiki)',
+                       ],
+                       [
+                               'https://',
+                               [ null, false ],
+                               [],
+                               'Invalid link target',
+                       ],
+                       [
+                               '<>',
+                               [ null, false ],
+                               [],
+                               'Invalid link target',
+                       ],
+                       [
+                               ' ',
+                               [ null, false ],
+                               [],
+                               'Invalid link target',
+                       ],
+               ];
+       }
 }