Hard deprecate unused OutputPage::addWikiText* methods
[lhc/web/wiklou.git] / tests / phpunit / includes / OutputPageTest.php
index e7b7c76..8a4b5c3 100644 (file)
@@ -12,6 +12,16 @@ class OutputPageTest extends MediaWikiTestCase {
        const SCREEN_MEDIA_QUERY = 'screen and (min-width: 982px)';
        const SCREEN_ONLY_MEDIA_QUERY = 'only screen and (min-width: 982px)';
 
+       // Ensure that we don't affect the global ResourceLoader state.
+       protected function setUp() {
+               parent::setUp();
+               ResourceLoader::clearCache();
+       }
+       protected function tearDown() {
+               parent::tearDown();
+               ResourceLoader::clearCache();
+       }
+
        /**
         * @dataProvider provideRedirect
         *
@@ -1010,9 +1020,14 @@ class OutputPageTest extends MediaWikiTestCase {
 
                $op = $this->getMockBuilder( OutputPage::class )
                        ->setConstructorArgs( [ new RequestContext() ] )
-                       ->setMethods( [ 'addCategoryLinksToLBAndGetResult' ] )
+                                  ->setMethods( [ 'addCategoryLinksToLBAndGetResult', 'getTitle' ] )
                        ->getMock();
 
+               $title = Title::newFromText( 'My test page' );
+               $op->expects( $this->any() )
+                       ->method( 'getTitle' )
+                       ->will( $this->returnValue( $title ) );
+
                $op->expects( $this->any() )
                        ->method( 'addCategoryLinksToLBAndGetResult' )
                        ->will( $this->returnCallback( function ( array $categories ) use ( $fakeResults ) {
@@ -1377,14 +1392,20 @@ class OutputPageTest extends MediaWikiTestCase {
        /**
         * @dataProvider provideAddWikiText
         * @covers OutputPage::addWikiText
+        * @covers OutputPage::addWikiTextAsInterface
+        * @covers OutputPage::addWikiTextAsContent
         * @covers OutputPage::addWikiTextWithTitle
         * @covers OutputPage::addWikiTextTitle
+        * @covers OutputPage::addWikiTextTidy
+        * @covers OutputPage::addWikiTextTitleTidy
         * @covers OutputPage::getHTML
         */
        public function testAddWikiText( $method, array $args, $expected ) {
                $op = $this->newInstance();
                $this->assertSame( '', $op->getHTML() );
 
+               $this->hideDeprecated( 'OutputPage::addWikiTextTitle' );
+               $this->hideDeprecated( 'OutputPage::addWikiTextWithTitle' );
                if ( in_array(
                        $method,
                        [ 'addWikiTextWithTitle', 'addWikiTextTitleTidy', 'addWikiTextTitle' ]
@@ -1392,6 +1413,13 @@ class OutputPageTest extends MediaWikiTestCase {
                        // Special placeholder because we can't get the actual title in the provider
                        $args[1] = $op->getTitle();
                }
+               if ( in_array(
+                       $method,
+                       [ 'addWikiTextAsInterface', 'addWikiTextAsContent' ]
+               ) && count( $args ) >= 3 && $args[2] === null ) {
+                       // Special placeholder because we can't get the actual title in the provider
+                       $args[2] = $op->getTitle();
+               }
 
                $op->$method( ...$args );
                $this->assertSame( $expected, $op->getHTML() );
@@ -1400,6 +1428,7 @@ class OutputPageTest extends MediaWikiTestCase {
        public function provideAddWikiText() {
                $tests = [
                        'addWikiText' => [
+                               // Not tidied; this API is deprecated.
                                'Simple wikitext' => [
                                        [ "'''Bold'''" ],
                                        "<p><b>Bold</b>\n</p>",
@@ -1411,19 +1440,70 @@ class OutputPageTest extends MediaWikiTestCase {
                                        '* Not a list',
                                ], 'Non-interface' => [
                                        [ "'''Bold'''", true, false ],
-                                       "<div class=\"mw-parser-output\"><p><b>Bold</b>\n</p></div>",
+                                       "<p><b>Bold</b>\n</p>",
                                ], 'No section edit links' => [
                                        [ '== Title ==' ],
                                        "<h2><span class=\"mw-headline\" id=\"Title\">Title</span></h2>\n",
                                ],
                        ],
                        'addWikiTextWithTitle' => [
+                               // Untidied; this API is deprecated
                                'With title at start' => [
                                        [ '* {{PAGENAME}}', Title::newFromText( 'Talk:Some page' ) ],
-                                       "<div class=\"mw-parser-output\"><ul><li>Some page</li></ul>\n</div>",
+                                       "<ul><li>Some page</li></ul>\n",
                                ], 'With title at start' => [
                                        [ '* {{PAGENAME}}', Title::newFromText( 'Talk:Some page' ), false ],
-                                       "<div class=\"mw-parser-output\">* Some page</div>",
+                                       "* Some page",
+                               ],
+                       ],
+                       'addWikiTextAsInterface' => [
+                               // Preferred interface: output is tidied
+                               'Simple wikitext' => [
+                                       [ "'''Bold'''" ],
+                                       "<p><b>Bold</b>\n</p>",
+                               ], 'Untidy wikitext' => [
+                                       [ "<b>Bold" ],
+                                       "<p><b>Bold\n</b></p>",
+                               ], 'List at start' => [
+                                       [ '* List' ],
+                                       "<ul><li>List</li></ul>\n",
+                               ], 'List not at start' => [
+                                       [ '* Not a list', false ],
+                                       '<p>* Not a list</p>',
+                               ], 'No section edit links' => [
+                                       [ '== Title ==' ],
+                                       "<h2><span class=\"mw-headline\" id=\"Title\">Title</span></h2>\n",
+                               ], 'With title at start' => [
+                                       [ '* {{PAGENAME}}', true, Title::newFromText( 'Talk:Some page' ) ],
+                                       "<ul><li>Some page</li></ul>\n",
+                               ], 'With title at start' => [
+                                       [ '* {{PAGENAME}}', false, Title::newFromText( 'Talk:Some page' ), false ],
+                                       "<p>* Some page</p>",
+                               ], 'Untidy input' => [
+                                       [ '<b>{{PAGENAME}}', true, Title::newFromText( 'Talk:Some page' ) ],
+                                       "<p><b>Some page\n</b></p>",
+                               ],
+                       ],
+                       'addWikiTextAsContent' => [
+                               // Preferred interface: output is tidied
+                               'SpecialNewimages' => [
+                                       [ "<p lang='en' dir='ltr'>\nMy message" ],
+                                       '<p lang="en" dir="ltr">' . "\nMy message\n</p>"
+                               ], 'List at start' => [
+                                       [ '* List' ],
+                                       "<ul><li>List</li></ul>\n",
+                               ], 'List not at start' => [
+                                       [ '* <b>Not a list', false ],
+                                       '<p>* <b>Not a list</b></p>',
+                               ], 'With title at start' => [
+                                       [ '* {{PAGENAME}}', true, Title::newFromText( 'Talk:Some page' ) ],
+                                       "<ul><li>Some page</li></ul>\n",
+                               ], 'With title at start' => [
+                                       [ '* {{PAGENAME}}', false, Title::newFromText( 'Talk:Some page' ), false ],
+                                       "<p>* Some page</p>",
+                               ], 'EditPage' => [
+                                       [ "<div class='mw-editintro'>{{PAGENAME}}", true, Title::newFromText( 'Talk:Some page' ) ],
+                                       '<div class="mw-editintro">' . "Some page\n</div>"
                                ],
                        ],
                ];
@@ -1439,6 +1519,29 @@ class OutputPageTest extends MediaWikiTestCase {
                        $tests['addWikiTextTitle']["$key (addWikiTextTitle)"] =
                                array_merge( [ $args ], array_slice( $val, 1 ) );
                }
+               foreach ( $tests['addWikiTextAsInterface'] as $key => $val ) {
+                       $args = [ $val[0][0], $val[0][2] ?? null, $val[0][1] ?? true, true, true ];
+                       $tests['addWikiTextTitle']["$key (addWikiTextTitle)"] =
+                               array_merge( [ $args ], array_slice( $val, 1 ) );
+               }
+               foreach ( $tests['addWikiTextAsContent'] as $key => $val ) {
+                       $args = [ $val[0][0], $val[0][2] ?? null, $val[0][1] ?? true, true, false ];
+                       $tests['addWikiTextTitle']["$key (addWikiTextTitle)"] =
+                               array_merge( [ $args ], array_slice( $val, 1 ) );
+               }
+               // addWikiTextTidy / addWikiTextTitleTidy were old aliases of
+               // addWikiTextAsContent
+               foreach ( $tests['addWikiTextAsContent'] as $key => $val ) {
+                       if ( count( $val[0] ) > 2 ) {
+                               $args = [ $val[0][0], $val[0][2], $val[0][1] ?? true ];
+                               $tests['addWikiTextTitleTidy']["$key (addWikiTextTitleTidy)"] =
+                                       array_merge( [ $args ], array_slice( $val, 1 ) );
+                       } else {
+                               $args = [ $val[0][0], $val[0][1] ?? true ];
+                               $tests['addWikiTextTidy']["$key (addWikiTextTidy)"] =
+                                       array_merge( [ $args ], array_slice( $val, 1 ) );
+                       }
+               }
 
                // We have to reformat our array to match what PHPUnit wants
                $ret = [];
@@ -1462,7 +1565,55 @@ class OutputPageTest extends MediaWikiTestCase {
                $op->addWikiText( 'a' );
        }
 
-       // @todo How should we cover the Tidy variants?
+       /**
+        * @covers OutputPage::addWikiTextAsInterface
+        */
+       public function testAddWikiTextAsInterfaceNoTitle() {
+               $this->setExpectedException( MWException::class, 'Title is null' );
+
+               $op = $this->newInstance( [], null, 'notitle' );
+               $op->addWikiTextAsInterface( 'a' );
+       }
+
+       /**
+        * @covers OutputPage::addWikiTextAsContent
+        */
+       public function testAddWikiTextAsContentNoTitle() {
+               $this->setExpectedException( MWException::class, 'Title is null' );
+
+               $op = $this->newInstance( [], null, 'notitle' );
+               $op->addWikiTextAsContent( 'a' );
+       }
+
+       /**
+        * @covers OutputPage::addWikiMsg
+        */
+       public function testAddWikiMsg() {
+               $msg = wfMessage( 'parentheses' );
+               $this->assertSame( '(a)', $msg->rawParams( 'a' )->plain() );
+
+               $op = $this->newInstance();
+               $this->assertSame( '', $op->getHTML() );
+               $op->addWikiMsg( 'parentheses', "<b>a" );
+               // This is known to be bad unbalanced HTML; this will be fixed
+               // by I743f4185a03403f8d9b9db010ff1ee4e9342e062 (T198214)
+               $this->assertSame( "<p>(<b>a)\n</p>", $op->getHTML() );
+       }
+
+       /**
+        * @covers OutputPage::wrapWikiMsg
+        */
+       public function testWrapWikiMsg() {
+               $msg = wfMessage( 'parentheses' );
+               $this->assertSame( '(a)', $msg->rawParams( 'a' )->plain() );
+
+               $op = $this->newInstance();
+               $this->assertSame( '', $op->getHTML() );
+               $op->wrapWikiMsg( '[$1]', [ 'parentheses', "<b>a" ] );
+               // This is known to be bad unbalanced HTML; this will be fixed
+               // by I743f4185a03403f8d9b9db010ff1ee4e9342e062 (T198214)
+               $this->assertSame( "<p>[(<b>a)]\n</p>", $op->getHTML() );
+       }
 
        /**
         * @covers OutputPage::addParserOutputMetadata