ParserOutput: Add stateless transforms to getText()
[lhc/web/wiklou.git] / tests / phpunit / includes / parser / ParserOutputTest.php
index fe46f2c..441d60d 100644 (file)
@@ -1,5 +1,7 @@
 <?php
 
+use Wikimedia\TestingAccessWrapper;
+
 /**
  * @group Database
  *        ^--- trigger DB shadowing because we are using Title magic
@@ -7,28 +9,28 @@
 class ParserOutputTest extends MediaWikiTestCase {
 
        public static function provideIsLinkInternal() {
-               return array(
+               return [
                        // Different domains
-                       array( false, 'http://example.org', 'http://mediawiki.org' ),
+                       [ false, 'http://example.org', 'http://mediawiki.org' ],
                        // Same domains
-                       array( true, 'http://example.org', 'http://example.org' ),
-                       array( true, 'https://example.org', 'https://example.org' ),
-                       array( true, '//example.org', '//example.org' ),
+                       [ true, 'http://example.org', 'http://example.org' ],
+                       [ true, 'https://example.org', 'https://example.org' ],
+                       [ true, '//example.org', '//example.org' ],
                        // Same domain different cases
-                       array( true, 'http://example.org', 'http://EXAMPLE.ORG' ),
+                       [ true, 'http://example.org', 'http://EXAMPLE.ORG' ],
                        // Paths, queries, and fragments are not relevant
-                       array( true, 'http://example.org', 'http://example.org/wiki/Main_Page' ),
-                       array( true, 'http://example.org', 'http://example.org?my=query' ),
-                       array( true, 'http://example.org', 'http://example.org#its-a-fragment' ),
+                       [ true, 'http://example.org', 'http://example.org/wiki/Main_Page' ],
+                       [ true, 'http://example.org', 'http://example.org?my=query' ],
+                       [ true, 'http://example.org', 'http://example.org#its-a-fragment' ],
                        // Different protocols
-                       array( false, 'http://example.org', 'https://example.org' ),
-                       array( false, 'https://example.org', 'http://example.org' ),
+                       [ false, 'http://example.org', 'https://example.org' ],
+                       [ false, 'https://example.org', 'http://example.org' ],
                        // Protocol relative servers always match http and https links
-                       array( true, '//example.org', 'http://example.org' ),
-                       array( true, '//example.org', 'https://example.org' ),
+                       [ true, '//example.org', 'http://example.org' ],
+                       [ true, '//example.org', 'https://example.org' ],
                        // But they don't match strange things like this
-                       array( false, '//example.org', 'irc://example.org' ),
-               );
+                       [ false, '//example.org', 'irc://example.org' ],
+               ];
        }
 
        /**
@@ -90,64 +92,230 @@ class ParserOutputTest extends MediaWikiTestCase {
        }
 
        /**
-        * @covers ParserOutput::hasCustomDataUpdates
-        * @covers ParserOutput::addSecondaryDataUpdate
+        * @covers ParserOutput::getText
+        * @dataProvider provideGetText
+        * @param array $options Options to getText()
+        * @param array $poState ParserOptions state fields to set
+        * @param string $text Parser text
+        * @param string $expect Expected output
         */
-       public function testHasCustomDataUpdates() {
-               $po = new ParserOutput();
-               $this->assertFalse( $po->hasCustomDataUpdates() );
+       public function testGetText( $options, $poState, $text, $expect ) {
+               $this->setMwGlobals( [
+                       'wgArticlePath' => '/wiki/$1',
+                       'wgScriptPath' => '/w',
+                       'wgScript' => '/w/index.php',
+               ] );
 
-               $dataUpdate = $this->getMock( 'DataUpdate' );
-               $po->addSecondaryDataUpdate( $dataUpdate );
-               $this->assertTrue( $po->hasCustomDataUpdates() );
-       }
+               $po = new ParserOutput( $text );
 
-       /**
-        * @covers ParserOutput::getSecondaryDataUpdates
-        * @covers ParserOutput::addSecondaryDataUpdate
-        */
-       public function testGetSecondaryDataUpdates() {
-               // NOTE: getSecondaryDataUpdates always returns a LinksUpdate object
-               // in addition to the DataUpdates registered via addSecondaryDataUpdate().
+               // Emulate Parser
+               $po->setEditSectionTokens( true );
 
-               $title = Title::makeTitle( NS_MAIN, 'Dummy' );
-               $title->resetArticleID( 7777777 );
+               if ( $poState ) {
+                       $wrap = TestingAccessWrapper::newFromObject( $po );
+                       foreach ( $poState as $key => $value ) {
+                               $wrap->$key = $value;
+                       }
+               }
 
-               $po = new ParserOutput();
-               $this->assertCount( 1, $po->getSecondaryDataUpdates( $title ) );
+               $actual = $po->getText( $options );
+               $this->assertSame( $expect, $actual );
+       }
 
-               $dataUpdate = $this->getMock( 'DataUpdate' );
-               $po->addSecondaryDataUpdate( $dataUpdate );
-               $this->assertCount( 2, $po->getSecondaryDataUpdates( $title ) );
+       public static function provideGetText() {
+               // @codingStandardsIgnoreStart Generic.Files.LineLength
+               $text = <<<EOF
+<p>Test document.
+</p>
+<mw:toc><div id="toc" class="toc"><div class="toctitle"><h2>Contents</h2></div>
+<ul>
+<li class="toclevel-1 tocsection-1"><a href="#Section_1"><span class="tocnumber">1</span> <span class="toctext">Section 1</span></a></li>
+<li class="toclevel-1 tocsection-2"><a href="#Section_2"><span class="tocnumber">2</span> <span class="toctext">Section 2</span></a>
+<ul>
+<li class="toclevel-2 tocsection-3"><a href="#Section_2.1"><span class="tocnumber">2.1</span> <span class="toctext">Section 2.1</span></a></li>
+</ul>
+</li>
+<li class="toclevel-1 tocsection-4"><a href="#Section_3"><span class="tocnumber">3</span> <span class="toctext">Section 3</span></a></li>
+</ul>
+</div>
+</mw:toc>
+<h2><span class="mw-headline" id="Section_1">Section 1</span><mw:editsection page="Test Page" section="1">Section 1</mw:editsection></h2>
+<p>One
+</p>
+<h2><span class="mw-headline" id="Section_2">Section 2</span><mw:editsection page="Test Page" section="2">Section 2</mw:editsection></h2>
+<p>Two
+</p>
+<h3><span class="mw-headline" id="Section_2.1">Section 2.1</span><mw:editsection page="Test Page" section="3">Section 2.1</mw:editsection></h3>
+<p>Two point one
+</p>
+<h2><span class="mw-headline" id="Section_3">Section 3</span><mw:editsection page="Test Page" section="4">Section 3</mw:editsection></h2>
+<p>Three
+</p>
+EOF;
 
-               // Test Fallback to getTitleText
-               $this->insertPage( 'Project:ParserOutputTestDummyPage' );
-               $po->setTitleText( 'Project:ParserOutputTestDummyPage' );
-               $this->assertCount( 2, $po->getSecondaryDataUpdates() );
-       }
+               return [
+                       'No stateless options, default state' => [
+                               [], [], $text, <<<EOF
+<p>Test document.
+</p>
+<div id="toc" class="toc"><div class="toctitle"><h2>Contents</h2></div>
+<ul>
+<li class="toclevel-1 tocsection-1"><a href="#Section_1"><span class="tocnumber">1</span> <span class="toctext">Section 1</span></a></li>
+<li class="toclevel-1 tocsection-2"><a href="#Section_2"><span class="tocnumber">2</span> <span class="toctext">Section 2</span></a>
+<ul>
+<li class="toclevel-2 tocsection-3"><a href="#Section_2.1"><span class="tocnumber">2.1</span> <span class="toctext">Section 2.1</span></a></li>
+</ul>
+</li>
+<li class="toclevel-1 tocsection-4"><a href="#Section_3"><span class="tocnumber">3</span> <span class="toctext">Section 3</span></a></li>
+</ul>
+</div>
 
-       /**
-        * @covers ParserOutput::getSecondaryDataUpdates
-        * @covers ParserOutput::__sleep
-        */
-       public function testGetSecondaryDataUpdates_serialization() {
-               $title = Title::makeTitle( NS_MAIN, 'Dummy' );
-               $title->resetArticleID( 7777777 );
+<h2><span class="mw-headline" id="Section_1">Section 1</span><span class="mw-editsection"><span class="mw-editsection-bracket">[</span><a href="/w/index.php?title=Test_Page&amp;action=edit&amp;section=1" title="Edit section: Section 1">edit</a><span class="mw-editsection-bracket">]</span></span></h2>
+<p>One
+</p>
+<h2><span class="mw-headline" id="Section_2">Section 2</span><span class="mw-editsection"><span class="mw-editsection-bracket">[</span><a href="/w/index.php?title=Test_Page&amp;action=edit&amp;section=2" title="Edit section: Section 2">edit</a><span class="mw-editsection-bracket">]</span></span></h2>
+<p>Two
+</p>
+<h3><span class="mw-headline" id="Section_2.1">Section 2.1</span><span class="mw-editsection"><span class="mw-editsection-bracket">[</span><a href="/w/index.php?title=Test_Page&amp;action=edit&amp;section=3" title="Edit section: Section 2.1">edit</a><span class="mw-editsection-bracket">]</span></span></h3>
+<p>Two point one
+</p>
+<h2><span class="mw-headline" id="Section_3">Section 3</span><span class="mw-editsection"><span class="mw-editsection-bracket">[</span><a href="/w/index.php?title=Test_Page&amp;action=edit&amp;section=4" title="Edit section: Section 3">edit</a><span class="mw-editsection-bracket">]</span></span></h2>
+<p>Three
+</p>
+EOF
+                       ],
+                       'No stateless options, TOC statefully disabled' => [
+                               [], [ 'mTOCEnabled' => false ], $text, <<<EOF
+<p>Test document.
+</p>
 
-               $po = new ParserOutput();
+<h2><span class="mw-headline" id="Section_1">Section 1</span><span class="mw-editsection"><span class="mw-editsection-bracket">[</span><a href="/w/index.php?title=Test_Page&amp;action=edit&amp;section=1" title="Edit section: Section 1">edit</a><span class="mw-editsection-bracket">]</span></span></h2>
+<p>One
+</p>
+<h2><span class="mw-headline" id="Section_2">Section 2</span><span class="mw-editsection"><span class="mw-editsection-bracket">[</span><a href="/w/index.php?title=Test_Page&amp;action=edit&amp;section=2" title="Edit section: Section 2">edit</a><span class="mw-editsection-bracket">]</span></span></h2>
+<p>Two
+</p>
+<h3><span class="mw-headline" id="Section_2.1">Section 2.1</span><span class="mw-editsection"><span class="mw-editsection-bracket">[</span><a href="/w/index.php?title=Test_Page&amp;action=edit&amp;section=3" title="Edit section: Section 2.1">edit</a><span class="mw-editsection-bracket">]</span></span></h3>
+<p>Two point one
+</p>
+<h2><span class="mw-headline" id="Section_3">Section 3</span><span class="mw-editsection"><span class="mw-editsection-bracket">[</span><a href="/w/index.php?title=Test_Page&amp;action=edit&amp;section=4" title="Edit section: Section 3">edit</a><span class="mw-editsection-bracket">]</span></span></h2>
+<p>Three
+</p>
+EOF
+                       ],
+                       'No stateless options, section edits statefully disabled' => [
+                               [], [ 'mEditSectionTokens' => false ], $text, <<<EOF
+<p>Test document.
+</p>
+<div id="toc" class="toc"><div class="toctitle"><h2>Contents</h2></div>
+<ul>
+<li class="toclevel-1 tocsection-1"><a href="#Section_1"><span class="tocnumber">1</span> <span class="toctext">Section 1</span></a></li>
+<li class="toclevel-1 tocsection-2"><a href="#Section_2"><span class="tocnumber">2</span> <span class="toctext">Section 2</span></a>
+<ul>
+<li class="toclevel-2 tocsection-3"><a href="#Section_2.1"><span class="tocnumber">2.1</span> <span class="toctext">Section 2.1</span></a></li>
+</ul>
+</li>
+<li class="toclevel-1 tocsection-4"><a href="#Section_3"><span class="tocnumber">3</span> <span class="toctext">Section 3</span></a></li>
+</ul>
+</div>
+
+<h2><span class="mw-headline" id="Section_1">Section 1</span></h2>
+<p>One
+</p>
+<h2><span class="mw-headline" id="Section_2">Section 2</span></h2>
+<p>Two
+</p>
+<h3><span class="mw-headline" id="Section_2.1">Section 2.1</span></h3>
+<p>Two point one
+</p>
+<h2><span class="mw-headline" id="Section_3">Section 3</span></h2>
+<p>Three
+</p>
+EOF
+                       ],
+                       'Stateless options override stateful settings' => [
+                               [ 'allowTOC' => true, 'enableSectionEditLinks' => true ],
+                               [ 'mTOCEnabled' => false, 'mEditSectionTokens' => false ],
+                               $text, <<<EOF
+<p>Test document.
+</p>
+<div id="toc" class="toc"><div class="toctitle"><h2>Contents</h2></div>
+<ul>
+<li class="toclevel-1 tocsection-1"><a href="#Section_1"><span class="tocnumber">1</span> <span class="toctext">Section 1</span></a></li>
+<li class="toclevel-1 tocsection-2"><a href="#Section_2"><span class="tocnumber">2</span> <span class="toctext">Section 2</span></a>
+<ul>
+<li class="toclevel-2 tocsection-3"><a href="#Section_2.1"><span class="tocnumber">2.1</span> <span class="toctext">Section 2.1</span></a></li>
+</ul>
+</li>
+<li class="toclevel-1 tocsection-4"><a href="#Section_3"><span class="tocnumber">3</span> <span class="toctext">Section 3</span></a></li>
+</ul>
+</div>
 
-               // Serializing is fine with no custom DataUpdates.
-               $po = unserialize( serialize( $po ) );
-               $this->assertCount( 1, $po->getSecondaryDataUpdates( $title ) );
+<h2><span class="mw-headline" id="Section_1">Section 1</span><span class="mw-editsection"><span class="mw-editsection-bracket">[</span><a href="/w/index.php?title=Test_Page&amp;action=edit&amp;section=1" title="Edit section: Section 1">edit</a><span class="mw-editsection-bracket">]</span></span></h2>
+<p>One
+</p>
+<h2><span class="mw-headline" id="Section_2">Section 2</span><span class="mw-editsection"><span class="mw-editsection-bracket">[</span><a href="/w/index.php?title=Test_Page&amp;action=edit&amp;section=2" title="Edit section: Section 2">edit</a><span class="mw-editsection-bracket">]</span></span></h2>
+<p>Two
+</p>
+<h3><span class="mw-headline" id="Section_2.1">Section 2.1</span><span class="mw-editsection"><span class="mw-editsection-bracket">[</span><a href="/w/index.php?title=Test_Page&amp;action=edit&amp;section=3" title="Edit section: Section 2.1">edit</a><span class="mw-editsection-bracket">]</span></span></h3>
+<p>Two point one
+</p>
+<h2><span class="mw-headline" id="Section_3">Section 3</span><span class="mw-editsection"><span class="mw-editsection-bracket">[</span><a href="/w/index.php?title=Test_Page&amp;action=edit&amp;section=4" title="Edit section: Section 3">edit</a><span class="mw-editsection-bracket">]</span></span></h2>
+<p>Three
+</p>
+EOF
+                       ],
+                       'Statelessly disable section edit links' => [
+                               [ 'enableSectionEditLinks' => false ], [], $text, <<<EOF
+<p>Test document.
+</p>
+<div id="toc" class="toc"><div class="toctitle"><h2>Contents</h2></div>
+<ul>
+<li class="toclevel-1 tocsection-1"><a href="#Section_1"><span class="tocnumber">1</span> <span class="toctext">Section 1</span></a></li>
+<li class="toclevel-1 tocsection-2"><a href="#Section_2"><span class="tocnumber">2</span> <span class="toctext">Section 2</span></a>
+<ul>
+<li class="toclevel-2 tocsection-3"><a href="#Section_2.1"><span class="tocnumber">2.1</span> <span class="toctext">Section 2.1</span></a></li>
+</ul>
+</li>
+<li class="toclevel-1 tocsection-4"><a href="#Section_3"><span class="tocnumber">3</span> <span class="toctext">Section 3</span></a></li>
+</ul>
+</div>
 
-               // If there are custom DataUpdates, getSecondaryDataUpdates
-               // should fail after serialization.
-               $dataUpdate = $this->getMock( 'DataUpdate' );
-               $po->addSecondaryDataUpdate( $dataUpdate );
-               $po = unserialize( serialize( $po ) );
+<h2><span class="mw-headline" id="Section_1">Section 1</span></h2>
+<p>One
+</p>
+<h2><span class="mw-headline" id="Section_2">Section 2</span></h2>
+<p>Two
+</p>
+<h3><span class="mw-headline" id="Section_2.1">Section 2.1</span></h3>
+<p>Two point one
+</p>
+<h2><span class="mw-headline" id="Section_3">Section 3</span></h2>
+<p>Three
+</p>
+EOF
+                       ],
+                       'Statelessly disable TOC' => [
+                               [ 'allowTOC' => false ], [], $text, <<<EOF
+<p>Test document.
+</p>
 
-               $this->setExpectedException( 'MWException' );
-               $po->getSecondaryDataUpdates( $title );
+<h2><span class="mw-headline" id="Section_1">Section 1</span><span class="mw-editsection"><span class="mw-editsection-bracket">[</span><a href="/w/index.php?title=Test_Page&amp;action=edit&amp;section=1" title="Edit section: Section 1">edit</a><span class="mw-editsection-bracket">]</span></span></h2>
+<p>One
+</p>
+<h2><span class="mw-headline" id="Section_2">Section 2</span><span class="mw-editsection"><span class="mw-editsection-bracket">[</span><a href="/w/index.php?title=Test_Page&amp;action=edit&amp;section=2" title="Edit section: Section 2">edit</a><span class="mw-editsection-bracket">]</span></span></h2>
+<p>Two
+</p>
+<h3><span class="mw-headline" id="Section_2.1">Section 2.1</span><span class="mw-editsection"><span class="mw-editsection-bracket">[</span><a href="/w/index.php?title=Test_Page&amp;action=edit&amp;section=3" title="Edit section: Section 2.1">edit</a><span class="mw-editsection-bracket">]</span></span></h3>
+<p>Two point one
+</p>
+<h2><span class="mw-headline" id="Section_3">Section 3</span><span class="mw-editsection"><span class="mw-editsection-bracket">[</span><a href="/w/index.php?title=Test_Page&amp;action=edit&amp;section=4" title="Edit section: Section 3">edit</a><span class="mw-editsection-bracket">]</span></span></h2>
+<p>Three
+</p>
+EOF
+                       ],
+               ];
+               // @codingStandardsIgnoreEnd
        }
 
 }