1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137
|
<?php
/**
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
* http://www.gnu.org/copyleft/gpl.html
*
* @file
* @since 1.42
*/
namespace MediaWiki\Tests\Unit\Content\Renderer;
use MediaWiki\Content\IContentHandlerFactory;
use MediaWiki\Content\Renderer\ContentRenderer;
use MediaWiki\Content\WikitextContent;
use MediaWiki\Content\WikitextContentHandler;
use MediaWiki\DAO\WikiAwareEntity;
use MediaWiki\Page\PageIdentityValue;
use MediaWiki\Parser\ParserOptions;
use MediaWiki\Parser\ParserOutput;
use MediaWiki\Revision\MutableRevisionRecord;
use MediaWikiUnitTestCase;
use Wikimedia\UUID\GlobalIdGenerator;
/**
* @group Renderer
* @covers \MediaWiki\Content\Renderer\ContentRenderer
*/
class ContentRendererTest extends MediaWikiUnitTestCase {
protected IContentHandlerFactory $contentHandlerFactory;
protected GlobalIdGenerator $globalIdGenerator;
protected function setUp(): void {
parent::setUp();
$this->contentHandlerFactory = $this->createMock( IContentHandlerFactory::class );
$this->globalIdGenerator = $this->createMock( GlobalIdGenerator::class );
}
/**
* This method tests the getParserOutput method. It is expected that the method will return a ParserOutput
* object with a render ID, cache revision ID, and revision timestamp.
*/
public function testGetParserOutput() {
$page = new PageIdentityValue( 1, NS_MAIN, 'TestPage', WikiAwareEntity::LOCAL );
$parserOptions = $this->createMock( ParserOptions::class );
$revision = new MutableRevisionRecord( $page );
$revision->setTimestamp( '20230418000000' );
$contentHandler = $this->createMock( WikitextContentHandler::class );
$content = $this->createMock( WikitextContent::class );
$content->method( 'getModel' )->willReturn( CONTENT_MODEL_WIKITEXT );
$this->contentHandlerFactory->expects( $this->once() )
->method( 'getContentHandler' )
->with( $content->getModel() )
->willReturn( $contentHandler );
$expectedParserOutput = new ParserOutput();
$expectedParserOutput->setRenderId( '12345' );
$expectedParserOutput->setCacheRevisionId( 123 );
$contentHandler->expects( $this->once() )
->method( 'getParserOutput' )
->with( $content, $this->anything() )
->willReturn( $expectedParserOutput );
$renderer = new ContentRenderer( $this->contentHandlerFactory, $this->globalIdGenerator );
$parserOutput = $renderer->getParserOutput( $content, $page, $revision, $parserOptions );
$this->assertInstanceOf( ParserOutput::class, $parserOutput );
if ( $parserOutput->getCacheRevisionId() !== null ) {
$this->assertEquals( 123, $parserOutput->getCacheRevisionId() );
} else {
$this->fail( 'Cache revision ID is null' );
}
if ( $parserOutput->getRenderId() !== null ) {
$this->assertSame( '12345', $parserOutput->getRenderId() );
} else {
$this->fail( 'Render ID is null' );
}
if ( $parserOutput->getRevisionTimestamp() !== null ) {
$this->assertSame( '20230418000000', $parserOutput->getRevisionTimestamp() );
} else {
$this->fail( 'Revision timestamp is null' );
}
}
/**
* This method tests the getParserOutput method with a null revision. It is expected that the method will
* return a ParserOutput object with a render ID.
*/
public function testGetParserOutputWithNullRevision() {
$page = new PageIdentityValue( 1, NS_MAIN, 'TestPage', WikiAwareEntity::LOCAL );
$parserOptions = $this->createMock( ParserOptions::class );
$contentHandler = $this->createMock( WikitextContentHandler::class );
$content = $this->createMock( WikitextContent::class );
$content->method( 'getModel' )->willReturn( CONTENT_MODEL_WIKITEXT );
$this->contentHandlerFactory->expects( $this->once() )
->method( 'getContentHandler' )
->with( $content->getModel() )
->willReturn( $contentHandler );
$expectedParserOutput = new ParserOutput();
$expectedParserOutput->setRenderId( '12345' );
$contentHandler->expects( $this->once() )
->method( 'getParserOutput' )
->with( $content, $this->anything() )
->willReturn( $expectedParserOutput );
$renderer = new ContentRenderer( $this->contentHandlerFactory, $this->globalIdGenerator );
$parserOutput = $renderer->getParserOutput( $content, $page, null, $parserOptions );
$this->assertInstanceOf( ParserOutput::class, $parserOutput );
if ( $parserOutput->getRenderId() !== null ) {
$this->assertSame( '12345', $parserOutput->getRenderId() );
} else {
$this->fail( 'Render ID is null' );
}
}
}
|