Debian Codegraph @

tests/phpunit/unit/includes/content/Renderer/ContentRendererTest.php

raw symbols SCIP
1 <?php
2 /**
3 * This program is free software; you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License as published by
5 * the Free Software Foundation; either version 2 of the License, or
6 * (at your option) any later version.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License along
14 * with this program; if not, write to the Free Software Foundation, Inc.,
15 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
16 * http://www.gnu.org/copyleft/gpl.html
17 *
18 * @file
19 * @since 1.42
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 * @group Renderer
38 * @covers \MediaWiki\Content\Renderer\ContentRenderer
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 * This method tests the getParserOutput method. It is expected that the method will return a ParserOutput
53 * object with a render ID, cache revision ID, and revision timestamp.
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 * This method tests the getParserOutput method with a null revision. It is expected that the method will
103 * return a ParserOutput object with a render ID.
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 }