namespace MediaWiki\Tests\Revision;
+use CommentStoreComment;
+use Content;
use Language;
use LogicException;
+use MediaWiki\Revision\MutableRevisionRecord;
+use MediaWiki\Revision\RevisionRecord;
use MediaWiki\Revision\RevisionRenderer;
-use MediaWiki\Storage\MutableRevisionRecord;
-use MediaWiki\Storage\RevisionRecord;
-use MediaWiki\User\UserIdentityValue;
+use MediaWiki\Revision\SlotRecord;
use MediaWikiTestCase;
+use MediaWiki\User\UserIdentityValue;
use ParserOptions;
+use ParserOutput;
use PHPUnit\Framework\MockObject\MockObject;
use Title;
use User;
$rev = new MutableRevisionRecord( $title );
$rev->setUser( new UserIdentityValue( 9, 'Frank', 0 ) );
$rev->setTimestamp( '20180101000003' );
+ $rev->setComment( CommentStoreComment::newUnsavedComment( '' ) );
$text = "";
$text .= "* page:{{PAGENAME}}\n";
$text .= "* time:{{REVISIONTIMESTAMP}}\n";
$text .= "* [[Link It]]\n";
- $rev->setContent( 'main', new WikitextContent( $text ) );
+ $rev->setContent( SlotRecord::MAIN, new WikitextContent( $text ) );
$options = ParserOptions::newCanonical( 'canonical' );
$rr = $renderer->getRenderedRevision( $rev, $options );
$this->assertContains( 'user:Frank', $html );
$this->assertContains( 'time:20180101000003', $html );
- $this->assertSame( $html, $rr->getSlotParserOutput( 'main' )->getText() );
+ $this->assertSame( $html, $rr->getSlotParserOutput( SlotRecord::MAIN )->getText() );
}
public function testGetRenderedRevision_current() {
$rev->setId( 21 ); // current!
$rev->setUser( new UserIdentityValue( 9, 'Frank', 0 ) );
$rev->setTimestamp( '20180101000003' );
+ $rev->setComment( CommentStoreComment::newUnsavedComment( '' ) );
$text = "";
$text .= "* page:{{PAGENAME}}\n";
$text .= "* user:{{REVISIONUSER}}\n";
$text .= "* time:{{REVISIONTIMESTAMP}}\n";
- $rev->setContent( 'main', new WikitextContent( $text ) );
+ $rev->setContent( SlotRecord::MAIN, new WikitextContent( $text ) );
$options = ParserOptions::newCanonical( 'canonical' );
$rr = $renderer->getRenderedRevision( $rev, $options );
$this->assertContains( 'user:Frank', $html );
$this->assertContains( 'time:20180101000003', $html );
- $this->assertSame( $html, $rr->getSlotParserOutput( 'main' )->getText() );
+ $this->assertSame( $html, $rr->getSlotParserOutput( SlotRecord::MAIN )->getText() );
}
public function testGetRenderedRevision_master() {
$rev->setId( 21 ); // current!
$rev->setUser( new UserIdentityValue( 9, 'Frank', 0 ) );
$rev->setTimestamp( '20180101000003' );
+ $rev->setComment( CommentStoreComment::newUnsavedComment( '' ) );
$text = "";
$text .= "* page:{{PAGENAME}}\n";
$text .= "* user:{{REVISIONUSER}}\n";
$text .= "* time:{{REVISIONTIMESTAMP}}\n";
- $rev->setContent( 'main', new WikitextContent( $text ) );
+ $rev->setContent( SlotRecord::MAIN, new WikitextContent( $text ) );
$options = ParserOptions::newCanonical( 'canonical' );
$rr = $renderer->getRenderedRevision( $rev, $options, null, [ 'use-master' => true ] );
$this->assertContains( 'rev:21', $html );
- $this->assertSame( $html, $rr->getSlotParserOutput( 'main' )->getText() );
+ $this->assertSame( $html, $rr->getSlotParserOutput( SlotRecord::MAIN )->getText() );
+ }
+
+ public function testGetRenderedRevision_known() {
+ $renderer = $this->newRevisionRenderer( 100, true ); // use master
+ $title = $this->getMockTitle( 7, 21 );
+
+ $rev = new MutableRevisionRecord( $title );
+ $rev->setId( 21 ); // current!
+ $rev->setUser( new UserIdentityValue( 9, 'Frank', 0 ) );
+ $rev->setTimestamp( '20180101000003' );
+ $rev->setComment( CommentStoreComment::newUnsavedComment( '' ) );
+
+ $text = "uncached text";
+ $rev->setContent( SlotRecord::MAIN, new WikitextContent( $text ) );
+
+ $output = new ParserOutput( 'cached text' );
+
+ $options = ParserOptions::newCanonical( 'canonical' );
+ $rr = $renderer->getRenderedRevision(
+ $rev,
+ $options,
+ null,
+ [ 'known-revision-output' => $output ]
+ );
+
+ $this->assertSame( $output, $rr->getRevisionParserOutput() );
+ $this->assertSame( 'cached text', $rr->getRevisionParserOutput()->getText() );
+ $this->assertSame( 'cached text', $rr->getSlotParserOutput( SlotRecord::MAIN )->getText() );
}
public function testGetRenderedRevision_old() {
$rev->setId( 11 ); // old!
$rev->setUser( new UserIdentityValue( 9, 'Frank', 0 ) );
$rev->setTimestamp( '20180101000003' );
+ $rev->setComment( CommentStoreComment::newUnsavedComment( '' ) );
$text = "";
$text .= "* page:{{PAGENAME}}\n";
$text .= "* user:{{REVISIONUSER}}\n";
$text .= "* time:{{REVISIONTIMESTAMP}}\n";
- $rev->setContent( 'main', new WikitextContent( $text ) );
+ $rev->setContent( SlotRecord::MAIN, new WikitextContent( $text ) );
$options = ParserOptions::newCanonical( 'canonical' );
$rr = $renderer->getRenderedRevision( $rev, $options );
$rev->setVisibility( RevisionRecord::DELETED_TEXT ); // suppressed!
$rev->setUser( new UserIdentityValue( 9, 'Frank', 0 ) );
$rev->setTimestamp( '20180101000003' );
+ $rev->setComment( CommentStoreComment::newUnsavedComment( '' ) );
$text = "";
$text .= "* page:{{PAGENAME}}\n";
$text .= "* user:{{REVISIONUSER}}\n";
$text .= "* time:{{REVISIONTIMESTAMP}}\n";
- $rev->setContent( 'main', new WikitextContent( $text ) );
+ $rev->setContent( SlotRecord::MAIN, new WikitextContent( $text ) );
$options = ParserOptions::newCanonical( 'canonical' );
$rr = $renderer->getRenderedRevision( $rev, $options );
$rev->setVisibility( RevisionRecord::DELETED_TEXT ); // suppressed!
$rev->setUser( new UserIdentityValue( 9, 'Frank', 0 ) );
$rev->setTimestamp( '20180101000003' );
+ $rev->setComment( CommentStoreComment::newUnsavedComment( '' ) );
$text = "";
$text .= "* page:{{PAGENAME}}\n";
$text .= "* user:{{REVISIONUSER}}\n";
$text .= "* time:{{REVISIONTIMESTAMP}}\n";
- $rev->setContent( 'main', new WikitextContent( $text ) );
+ $rev->setContent( SlotRecord::MAIN, new WikitextContent( $text ) );
$options = ParserOptions::newCanonical( 'canonical' );
$sysop = $this->getTestUser( [ 'sysop' ] )->getUser(); // privileged!
$rev->setVisibility( RevisionRecord::DELETED_TEXT ); // suppressed!
$rev->setUser( new UserIdentityValue( 9, 'Frank', 0 ) );
$rev->setTimestamp( '20180101000003' );
+ $rev->setComment( CommentStoreComment::newUnsavedComment( '' ) );
$text = "";
$text .= "* page:{{PAGENAME}}\n";
$text .= "* user:{{REVISIONUSER}}\n";
$text .= "* time:{{REVISIONTIMESTAMP}}\n";
- $rev->setContent( 'main', new WikitextContent( $text ) );
+ $rev->setContent( SlotRecord::MAIN, new WikitextContent( $text ) );
$options = ParserOptions::newCanonical( 'canonical' );
$rr = $renderer->getRenderedRevision(
$rev = new MutableRevisionRecord( $title );
$rev->setUser( new UserIdentityValue( 9, 'Frank', 0 ) );
$rev->setTimestamp( '20180101000003' );
+ $rev->setComment( CommentStoreComment::newUnsavedComment( '' ) );
- $rev->setContent( 'main', new WikitextContent( '[[Kittens]]' ) );
+ $rev->setContent( SlotRecord::MAIN, new WikitextContent( '[[Kittens]]' ) );
$rev->setContent( 'aux', new WikitextContent( '[[Goats]]' ) );
$rr = $renderer->getRenderedRevision( $rev );
$combinedOutput = $rr->getRevisionParserOutput();
- $mainOutput = $rr->getSlotParserOutput( 'main' );
+ $mainOutput = $rr->getSlotParserOutput( SlotRecord::MAIN );
$auxOutput = $rr->getSlotParserOutput( 'aux' );
$combinedHtml = $combinedOutput->getText();
$this->assertFalse( isset( $auxLinks[NS_MAIN]['Kittens'] ), 'no main links in aux' );
}
+ public function testGetRenderedRevision_noHtml() {
+ /** @var MockObject|Content $mockContent */
+ $mockContent = $this->getMockBuilder( WikitextContent::class )
+ ->setMethods( [ 'getParserOutput' ] )
+ ->setConstructorArgs( [ 'Whatever' ] )
+ ->getMock();
+ $mockContent->method( 'getParserOutput' )
+ ->willReturnCallback( function ( Title $title, $revId = null,
+ ParserOptions $options = null, $generateHtml = true
+ ) {
+ if ( !$generateHtml ) {
+ return new ParserOutput( null );
+ } else {
+ $this->fail( 'Should not be called with $generateHtml == true' );
+ return null; // never happens, make analyzer happy
+ }
+ } );
+
+ $renderer = $this->newRevisionRenderer();
+ $title = $this->getMockTitle( 7, 21 );
+
+ $rev = new MutableRevisionRecord( $title );
+ $rev->setContent( SlotRecord::MAIN, $mockContent );
+ $rev->setContent( 'aux', $mockContent );
+
+ // NOTE: we are testing the private combineSlotOutput() callback here.
+ $rr = $renderer->getRenderedRevision( $rev );
+
+ $output = $rr->getSlotParserOutput( SlotRecord::MAIN, [ 'generate-html' => false ] );
+ $this->assertFalse( $output->hasText(), 'hasText' );
+
+ $output = $rr->getRevisionParserOutput( [ 'generate-html' => false ] );
+ $this->assertFalse( $output->hasText(), 'hasText' );
+ }
+
}