Debian Codegraph @

tests/phpunit/unit/includes/content/WikitextContentHandlerTest.php

raw symbols SCIP
1 <?php
2
3 namespace MediaWiki\Tests\Unit;
4
5 use MediaWiki\Content\Renderer\ContentParseParams;
6 use MediaWiki\Content\WikitextContent;
7 use MediaWiki\Content\WikitextContentHandler;
8 use MediaWiki\Language\Language;
9 use MediaWiki\Languages\LanguageNameUtils;
10 use MediaWiki\Linker\LinkRenderer;
11 use MediaWiki\Parser\MagicWord;
12 use MediaWiki\Parser\MagicWordFactory;
13 use MediaWiki\Parser\Parser;
14 use MediaWiki\Parser\ParserFactory;
15 use MediaWiki\Parser\ParserOptions;
16 use MediaWiki\Parser\ParserOutput;
17 use MediaWiki\Parser\Parsoid\ParsoidParser;
18 use MediaWiki\Parser\Parsoid\ParsoidParserFactory;
19 use MediaWiki\Revision\SlotRecord;
20 use MediaWiki\Revision\SlotRenderingProvider;
21 use MediaWiki\Title\Title;
22 use MediaWiki\Title\TitleFactory;
23 use MediaWikiUnitTestCase;
24 use MockTitleTrait;
25 use MWException;
26 use Wikimedia\TestingAccessWrapper;
27 use Wikimedia\UUID\GlobalIdGenerator;
28
29 /**
30 * Split from \WikitextContentHandlerTest integration tests
31 *
32 * @group ContentHandler
33 * @covers \MediaWiki\Content\WikitextContentHandler
34 */
35 class WikitextContentHandlerTest extends MediaWikiUnitTestCase {
36 use MockTitleTrait;
37
38 private function newWikitextContentHandler( $overrides = [] ): WikitextContentHandler {
39 return new WikitextContentHandler(
40 CONTENT_MODEL_WIKITEXT,
41 $overrides[TitleFactory::class] ?? $this->createMock( TitleFactory::class ),
42 $overrides[ParserFactory::class] ?? $this->createMock( ParserFactory::class ),
43 $overrides[GlobalIdGenerator::class] ?? $this->createMock( GlobalIdGenerator::class ),
44 $overrides[LanguageNameUtils::class] ?? $this->createMock( LanguageNameUtils::class ),
45 $overrides[LinkRenderer::class] ?? $this->createMock( LinkRenderer::class ),
46 $overrides[MagicWordFactory::class] ?? $this->createMock( MagicWordFactory::class ),
47 $overrides[ParsoidParserFactory::class] ?? $this->createMock( ParsoidParserFactory::class )
48 );
49 }
50
51 public function testSerializeContent() {
52 $content = new WikitextContent( 'hello world' );
53 $handler = $this->newWikitextContentHandler();
54
55 $this->assertEquals( 'hello world', $handler->serializeContent( $content ) );
56 $this->assertEquals(
57 'hello world',
58 $handler->serializeContent( $content, CONTENT_FORMAT_WIKITEXT )
59 );
60
61 $this->expectException( MWException::class );
62 $handler->serializeContent( $content, 'dummy/foo' );
63 }
64
65 public function testUnserializeContent() {
66 $handler = $this->newWikitextContentHandler();
67
68 $content = $handler->unserializeContent( 'hello world' );
69 $this->assertEquals( 'hello world', $content->getText() );
70
71 $content = $handler->unserializeContent( 'hello world', CONTENT_FORMAT_WIKITEXT );
72 $this->assertEquals( 'hello world', $content->getText() );
73
74 $this->expectException( MWException::class );
75 $handler->unserializeContent( 'hello world', 'dummy/foo' );
76 }
77
78 public function testMakeEmptyContent() {
79 $handler = $this->newWikitextContentHandler();
80 $content = $handler->makeEmptyContent();
81
82 $this->assertTrue( $content->isEmpty() );
83 $this->assertSame( '', $content->getText() );
84 }
85
86 public function dataIsSupportedFormat() {
87 return [
88 [ null, true ],
89 [ CONTENT_FORMAT_WIKITEXT, true ],
90 [ 99887766, false ],
91 ];
92 }
93
94 /**
95 * @dataProvider dataIsSupportedFormat
96 */
97 public function testIsSupportedFormat( $format, $supported ) {
98 $handler = $this->newWikitextContentHandler();
99 $this->assertEquals( $supported, $handler->isSupportedFormat( $format ) );
100 }
101
102 public function testSupportsDirectEditing() {
103 $handler = $this->newWikiTextContentHandler();
104 $this->assertTrue( $handler->supportsDirectEditing(), 'direct editing is supported' );
105 }
106
107 public function testGetSecondaryDataUpdates() {
108 $title = $this->createMock( Title::class );
109 $content = new WikitextContent( '' );
110 $srp = $this->createMock( SlotRenderingProvider::class );
111 $handler = $this->newWikitextContentHandler();
112
113 $updates = $handler->getSecondaryDataUpdates( $title, $content, SlotRecord::MAIN, $srp );
114
115 $this->assertEquals( [], $updates );
116 }
117
118 public function testGetDeletionUpdates() {
119 $title = $this->createMock( Title::class );
120 $handler = $this->newWikitextContentHandler();
121
122 $updates = $handler->getDeletionUpdates( $title, SlotRecord::MAIN );
123 $this->assertEquals( [], $updates );
124 }
125
126 /**
127 * @dataProvider provideFillParserOutput
128 */
129 public function testFillParserOutput( $useParsoid = true, $testRedirect = false ) {
130 $parserOptions = $this->createMock( ParserOptions::class );
131 $parserOptions
132 ->method( 'getUseParsoid' )
133 ->willReturn( $useParsoid );
134
135 $parserOutput = $this->createMock( ParserOutput::class );
136
137 // This is the core of the test: if the useParsoid option is NOT
138 // present, we expect ParserFactory->getInstance()->parse()
139 // to be called exactly once, otherwise never.
140 $parser = $this->createMock( Parser::class );
141 $parser
142 ->expects( $useParsoid ? $this->never() : $this->once() )
143 ->method( 'parse' )
144 ->willReturn( $parserOutput );
145 $parserFactory = $this->createMock( ParserFactory::class );
146 $parserFactory
147 ->expects( $useParsoid ? $this->never() : $this->once() )
148 ->method( 'getInstance' )
149 ->willReturn( $parser );
150
151 // If the useParsoid option is present, we expect
152 // ParsoidParserFactory()->create()->parse() to be called
153 // exactly once, otherwise never.
154 $parsoidParser = $this->createMock( ParsoidParser::class );
155 $parsoidParser
156 ->expects( $useParsoid ? $this->once() : $this->never() )
157 ->method( 'parse' )
158 ->willReturn( $parserOutput );
159 $parsoidParserFactory = $this->createMock( ParsoidParserFactory::class );
160 $parsoidParserFactory
161 ->expects( $useParsoid ? $this->once() : $this->never() )
162 ->method( 'create' )
163 ->willReturn( $parsoidParser );
164
165 $linkRenderer = $this->createMock( LinkRenderer::class );
166 // For a redirect test, we expect LinkRenderer::makeRedirectHeader() to
167 // be called once, and ParserOutput::setRedirectHeader() to be called
168 // with whatever it returns.
169 $linkRenderer
170 ->expects( $testRedirect ? $this->once() : $this->never() )
171 ->method( 'makeRedirectHeader' )
172 ->willReturn( 'ABCDEFG' );
173 $parserOutput
174 ->expects( $testRedirect ? $this->once() : $this->never() )
175 ->method( 'setRedirectHeader' )
176 ->with( 'ABCDEFG' );
177
178 // Set up the rest of the mocks
179 $magicWordRedirect = $this->createMock( MagicWord::class );
180 $magicWordRedirect
181 ->method( 'getSynonym' )
182 ->willReturnMap( [ [ 0, '#REDIRECT' ] ] );
183 $magicWordRedirect
184 ->method( 'matchStartAndRemove' )
185 ->willReturnCallback( static function ( string &$text ) use ( $testRedirect ) {
186 if ( $testRedirect ) {
187 $text = '[[SomeTitle]]';
188 return true;
189 } else {
190 return false;
191 }
192 } );
193 $magicWordFactory = $this->createMock( MagicWordFactory::class );
194 $magicWordFactory
195 ->method( 'get' )
196 ->willReturnMap( [ [ 'redirect', $magicWordRedirect ] ] );
197
198 $title = $this->makeMockTitle( 'SomeTitle', [
199 'language' => $this->createMock( Language::class ),
200 ] );
201 $titleFactory = $this->createMock( TitleFactory::class );
202 $titleFactory
203 ->method( 'newFromPageReference' )
204 ->willReturn( $title );
205 $titleFactory
206 ->method( 'newFromText' )
207 ->willReturnMap( [ [ 'SomeTitle', NS_MAIN, $title ] ] );
208
209 $cpoParams = new ContentParseParams( $title, 42, $parserOptions );
210
211 $handler = $this->newWikitextContentHandler( [
212 TitleFactory::class => $titleFactory,
213 LinkRenderer::class => $linkRenderer,
214 MagicWordFactory::class => $magicWordFactory,
215 ParserFactory::class => $parserFactory,
216 ParsoidParserFactory::class => $parsoidParserFactory,
217 ] );
218
219 if ( $testRedirect ) {
220 $content = $handler->makeRedirectContent( $title );
221 } else {
222 $content = new WikitextContent( '* Hello, world!' );
223 }
224
225 /** @var WikitextContentHandler $handler */
226 $handler = TestingAccessWrapper::newFromObject( $handler );
227 // Okay, invoke fillParserOutput() and verify that the assertions
228 // above about the parse() invocations are correct.
229 // TestingAccessWrapper cannot pass-by-ref directly - T287318
230 call_user_func_array( [ $handler, 'fillParserOutput' ], [ $content, $cpoParams, &$parserOutput ] );
231 }
232
233 public static function provideFillParserOutput() {
234 return [
235 [ false, false ],
236 [ false, true ],
237 [ true, false ],
238 [ true, true ],
239 ];
240 }
241 }