| 1 |
<?php |
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
namespace MediaWiki\Tests\Unit\Content\Renderer; |
| 23 |
|
| 24 |
use MediaWiki\Content\IContentHandlerFactory; |
| 25 |
use MediaWiki\Content\Renderer\ContentRenderer; |
| 26 |
use MediaWiki\Content\WikitextContent; |
| 27 |
use MediaWiki\Content\WikitextContentHandler; |
| 28 |
use MediaWiki\DAO\WikiAwareEntity; |
| 29 |
use MediaWiki\Page\PageIdentityValue; |
| 30 |
use MediaWiki\Parser\ParserOptions; |
| 31 |
use MediaWiki\Parser\ParserOutput; |
| 32 |
use MediaWiki\Revision\MutableRevisionRecord; |
| 33 |
use MediaWikiUnitTestCase; |
| 34 |
use Wikimedia\UUID\GlobalIdGenerator; |
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
class ContentRendererTest extends MediaWikiUnitTestCase { |
| 41 |
|
| 42 |
protected IContentHandlerFactory $contentHandlerFactory; |
| 43 |
protected GlobalIdGenerator $globalIdGenerator; |
| 44 |
|
| 45 |
protected function setUp(): void { |
| 46 |
parent::setUp(); |
| 47 |
$this->contentHandlerFactory = $this->createMock( IContentHandlerFactory::class ); |
| 48 |
$this->globalIdGenerator = $this->createMock( GlobalIdGenerator::class ); |
| 49 |
} |
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
public function testGetParserOutput() { |
| 56 |
$page = new PageIdentityValue( 1, NS_MAIN, 'TestPage', WikiAwareEntity::LOCAL ); |
| 57 |
$parserOptions = $this->createMock( ParserOptions::class ); |
| 58 |
$revision = new MutableRevisionRecord( $page ); |
| 59 |
$revision->setTimestamp( '20230418000000' ); |
| 60 |
|
| 61 |
$contentHandler = $this->createMock( WikitextContentHandler::class ); |
| 62 |
|
| 63 |
$content = $this->createMock( WikitextContent::class ); |
| 64 |
$content->method( 'getModel' )->willReturn( CONTENT_MODEL_WIKITEXT ); |
| 65 |
|
| 66 |
$this->contentHandlerFactory->expects( $this->once() ) |
| 67 |
->method( 'getContentHandler' ) |
| 68 |
->with( $content->getModel() ) |
| 69 |
->willReturn( $contentHandler ); |
| 70 |
|
| 71 |
$expectedParserOutput = new ParserOutput(); |
| 72 |
$expectedParserOutput->setRenderId( '12345' ); |
| 73 |
$expectedParserOutput->setCacheRevisionId( 123 ); |
| 74 |
|
| 75 |
$contentHandler->expects( $this->once() ) |
| 76 |
->method( 'getParserOutput' ) |
| 77 |
->with( $content, $this->anything() ) |
| 78 |
->willReturn( $expectedParserOutput ); |
| 79 |
|
| 80 |
$renderer = new ContentRenderer( $this->contentHandlerFactory, $this->globalIdGenerator ); |
| 81 |
$parserOutput = $renderer->getParserOutput( $content, $page, $revision, $parserOptions ); |
| 82 |
|
| 83 |
$this->assertInstanceOf( ParserOutput::class, $parserOutput ); |
| 84 |
if ( $parserOutput->getCacheRevisionId() !== null ) { |
| 85 |
$this->assertEquals( 123, $parserOutput->getCacheRevisionId() ); |
| 86 |
} else { |
| 87 |
$this->fail( 'Cache revision ID is null' ); |
| 88 |
} |
| 89 |
if ( $parserOutput->getRenderId() !== null ) { |
| 90 |
$this->assertSame( '12345', $parserOutput->getRenderId() ); |
| 91 |
} else { |
| 92 |
$this->fail( 'Render ID is null' ); |
| 93 |
} |
| 94 |
if ( $parserOutput->getRevisionTimestamp() !== null ) { |
| 95 |
$this->assertSame( '20230418000000', $parserOutput->getRevisionTimestamp() ); |
| 96 |
} else { |
| 97 |
$this->fail( 'Revision timestamp is null' ); |
| 98 |
} |
| 99 |
} |
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
public function testGetParserOutputWithNullRevision() { |
| 106 |
$page = new PageIdentityValue( 1, NS_MAIN, 'TestPage', WikiAwareEntity::LOCAL ); |
| 107 |
$parserOptions = $this->createMock( ParserOptions::class ); |
| 108 |
|
| 109 |
$contentHandler = $this->createMock( WikitextContentHandler::class ); |
| 110 |
|
| 111 |
$content = $this->createMock( WikitextContent::class ); |
| 112 |
$content->method( 'getModel' )->willReturn( CONTENT_MODEL_WIKITEXT ); |
| 113 |
|
| 114 |
$this->contentHandlerFactory->expects( $this->once() ) |
| 115 |
->method( 'getContentHandler' ) |
| 116 |
->with( $content->getModel() ) |
| 117 |
->willReturn( $contentHandler ); |
| 118 |
|
| 119 |
$expectedParserOutput = new ParserOutput(); |
| 120 |
$expectedParserOutput->setRenderId( '12345' ); |
| 121 |
|
| 122 |
$contentHandler->expects( $this->once() ) |
| 123 |
->method( 'getParserOutput' ) |
| 124 |
->with( $content, $this->anything() ) |
| 125 |
->willReturn( $expectedParserOutput ); |
| 126 |
|
| 127 |
$renderer = new ContentRenderer( $this->contentHandlerFactory, $this->globalIdGenerator ); |
| 128 |
$parserOutput = $renderer->getParserOutput( $content, $page, null, $parserOptions ); |
| 129 |
|
| 130 |
$this->assertInstanceOf( ParserOutput::class, $parserOutput ); |
| 131 |
if ( $parserOutput->getRenderId() !== null ) { |
| 132 |
$this->assertSame( '12345', $parserOutput->getRenderId() ); |
| 133 |
} else { |
| 134 |
$this->fail( 'Render ID is null' ); |
| 135 |
} |
| 136 |
} |
| 137 |
} |