+
+ $text = '* user:{{REVISIONUSER}};time:{{REVISIONTIMESTAMP}};'
+ . 'user-subst:{{subst:REVISIONUSER}};time-subst:{{subst:REVISIONTIMESTAMP}};';
+
+ $newRevision = new MutableRevisionRecord( $title );
+ $newRevision->setUser( new UserIdentityValue( 9, 'NewAuthor', 0 ) );
+ $newRevision->setTimestamp( '20180808000000' );
+ $newRevision->setContent( SlotRecord::MAIN, new WikitextContent( 'NEW' ) );
+
+ $po = new ParserOptions( $frank );
+ $po->setIsPreview( true );
+ $po->setCurrentRevisionCallback( function () use ( $newRevision ) {
+ return new Revision( $newRevision );
+ } );
+
+ yield 'preview' => [
+ $text,
+ $po,
+ null,
+ 'user:NewAuthor;time:20180808000000;',
+ 'user-subst:NewAuthor;time-subst:20180808000000;',
+ ];
+
+ $po = new ParserOptions( $frank );
+ $po->setCurrentRevisionCallback( function () use ( $newRevision ) {
+ return new Revision( $newRevision );
+ } );
+
+ yield 'pre-save' => [
+ $text,
+ $po,
+ null,
+ 'user:NewAuthor;time:20180808000000;',
+ 'user-subst:NewAuthor;time-subst:20180808000000;',
+ ];
+
+ $text = "(ONE)<includeonly>(TWO)</includeonly>"
+ . "<noinclude>#{{:ParserRevisionAccessTest}}#</noinclude>";
+
+ $newRevision = new MutableRevisionRecord( $title );
+ $newRevision->setUser( new UserIdentityValue( 9, 'NewAuthor', 0 ) );
+ $newRevision->setTimestamp( '20180808000000' );
+ $newRevision->setContent( SlotRecord::MAIN, new WikitextContent( $text ) );
+
+ $po = new ParserOptions( $frank );
+ $po->setIsPreview( true );
+ $po->setCurrentRevisionCallback( function () use ( $newRevision ) {
+ return new Revision( $newRevision );
+ } );
+
+ yield 'preview with self-transclude' => [ $text, $po, null, '(ONE)#(ONE)(TWO)#' ];
+ }
+
+ /**
+ * @dataProvider provideRevisionAccess
+ */
+ public function testRevisionAccess(
+ $text,
+ ParserOptions $po,
+ $revId,
+ $expectedInHtml,
+ $expectedInPst = null
+ ) {
+ $title = $this->getMockTitle( 'ParserRevisionAccessTest' );
+
+ $po->enableLimitReport( false );
+
+ $oldRevision = new MutableRevisionRecord( $title );
+ $oldRevision->setId( 100 );
+ $oldRevision->setUser( new UserIdentityValue( 7, 'OldAuthor', 0 ) );
+ $oldRevision->setTimestamp( '20140404000000' );
+ $oldRevision->setContent( SlotRecord::MAIN, new WikitextContent( 'OLD' ) );
+
+ $currentRevision = new MutableRevisionRecord( $title );
+ $currentRevision->setId( 200 );
+ $currentRevision->setUser( new UserIdentityValue( 9, 'CurrentAuthor', 0 ) );
+ $currentRevision->setTimestamp( '20160606000000' );
+ $currentRevision->setContent( SlotRecord::MAIN, new WikitextContent( 'CURRENT' ) );
+
+ $revisionStore = $this->getMockBuilder( RevisionStore::class )
+ ->disableOriginalConstructor()
+ ->getMock();
+
+ $revisionStore
+ ->method( 'getKnownCurrentRevision' )
+ ->willReturnMap( [
+ [ $title, 100, $oldRevision ],
+ [ $title, 200, $currentRevision ],
+ [ $title, 0, $currentRevision ],
+ ] );
+
+ $revisionStore
+ ->method( 'getRevisionById' )
+ ->willReturnMap( [
+ [ 100, 0, $oldRevision ],
+ [ 200, 0, $currentRevision ],
+ ] );
+
+ $this->setService( 'RevisionStore', $revisionStore );
+
+ $parser = MediaWikiServices::getInstance()->getParser();
+ $parser->parse( $text, $title, $po, true, true, $revId );
+ $html = $parser->getOutput()->getText();
+
+ $this->assertContains( $expectedInHtml, $html, 'In HTML' );
+
+ if ( $expectedInPst !== null ) {
+ $pst = $parser->preSaveTransform( $text, $title, $po->getUser(), $po );
+ $this->assertContains( $expectedInPst, $pst, 'After Pre-Safe Transform' );
+ }
+ }
+
+ public static function provideGuessSectionNameFromWikiText() {
+ return [
+ [ '1/2', 'html5', '#1/2' ],
+ [ '1/2', 'legacy', '#1.2F2' ],
+ ];
+ }
+
+ /** @dataProvider provideGuessSectionNameFromWikiText */
+ public function testGuessSectionNameFromWikiText( $input, $mode, $expected ) {
+ $this->setMwGlobals( [ 'wgFragmentMode' => [ $mode ] ] );
+ $result = MediaWikiServices::getInstance()->getParser()
+ ->guessSectionNameFromWikiText( $input );
+ $this->assertEquals( $result, $expected );