Debian Codegraph @

tests/phpunit/integration/includes/Rest/Handler/ParsoidOutputAccessTest.php

raw symbols SCIP
1 <?php
2
3 use MediaWiki\Content\JavaScriptContent;
4 use MediaWiki\Content\WikitextContent;
5 use MediaWiki\Edit\ParsoidRenderID;
6 use MediaWiki\Language\Language;
7 use MediaWiki\Language\LanguageCode;
8 use MediaWiki\Page\ParserOutputAccess;
9 use MediaWiki\Parser\ParserOptions;
10 use MediaWiki\Parser\ParserOutput;
11 use MediaWiki\Parser\ParserOutputFlags;
12 use MediaWiki\Parser\Parsoid\PageBundleParserOutputConverter;
13 use MediaWiki\Parser\Parsoid\ParsoidOutputAccess;
14 use MediaWiki\Parser\Parsoid\ParsoidParser;
15 use MediaWiki\Parser\Parsoid\ParsoidParserFactory;
16 use MediaWiki\Revision\MutableRevisionRecord;
17 use MediaWiki\Revision\RevisionAccessException;
18 use MediaWiki\Revision\RevisionRecord;
19 use MediaWiki\Revision\SlotRecord;
20 use MediaWiki\Status\Status;
21 use MediaWiki\Title\Title;
22 use PHPUnit\Framework\MockObject\MockObject;
23 use Wikimedia\ObjectCache\BagOStuff;
24 use Wikimedia\ObjectCache\HashBagOStuff;
25 use Wikimedia\Parsoid\Config\PageConfig;
26 use Wikimedia\Parsoid\Core\ContentMetadataCollector;
27 use Wikimedia\Parsoid\Core\PageBundle;
28 use Wikimedia\Parsoid\Parsoid;
29 use Wikimedia\TestingAccessWrapper;
30
31 /**
32 * @covers \MediaWiki\Parser\Parsoid\ParsoidOutputAccess
33 * @group Database
34 */
35 class ParsoidOutputAccessTest extends MediaWikiIntegrationTestCase {
36 private const WIKITEXT = 'Hello \'\'\'Parsoid\'\'\'!';
37 private const MOCKED_HTML = 'mocked HTML';
38 private const ENV_OPTS = [ 'pageBundle' => true ];
39
40 protected function setUp(): void {
41 parent::setUp();
42 // This class is deprecated, as all are its methods.
43 $this->filterDeprecated( '/' . preg_quote( ParsoidOutputAccess::class, '/' ) . '/' );
44 }
45
46 /**
47 * @param int $expectedCalls
48 * @param string|null $version
49 *
50 * @return MockObject|Parsoid
51 */
52 private function newMockParsoid( int $expectedCalls = 1, ?string $version = null ) {
53 $parsoid = $this->createNoOpMock( Parsoid::class, [ 'wikitext2html' ] );
54 $parsoid->expects( $this->exactly( $expectedCalls ) )->method( 'wikitext2html' )->willReturnCallback(
55 static function (
56 PageConfig $pageConfig, $options, &$headers, ?ContentMetadataCollector $metadata = null
57 ) use ( $version ) {
58 $wikitext = $pageConfig->getRevisionContent()->getContent( SlotRecord::MAIN );
59 if ( $metadata !== null ) {
60 $metadata->setExtensionData( 'my-key', 'my-data' );
61 $metadata->setPageProperty( 'forcetoc', '' );
62 $metadata->setOutputFlag( ParserOutputFlags::NO_GALLERY );
63 }
64
65 return new PageBundle(
66 self::MOCKED_HTML . ' of ' . $wikitext,
67 [ 'parsoid-data' ],
68 [ 'mw-data' ],
69 $version ?? Parsoid::defaultHTMLVersion(),
70 [ 'content-language' => 'en' ],
71 $pageConfig->getContentModel()
72 );
73 }
74 );
75
76 return $parsoid;
77 }
78
79 /**
80 * @param int $expectedParses
81 * @param array $parsoidCacheConfig
82 * @param BagOStuff|null $parserCacheBag
83 * @param string|null $version
84 *
85 * @return ParsoidOutputAccess
86 * @throws Exception
87 */
88 private function resetServicesWithMockedParsoid(
89 $expectedParses,
90 $parsoidCacheConfig = [],
91 ?BagOStuff $parserCacheBag = null,
92 ?string $version = null
93 ): void {
94 $services = $this->getServiceContainer();
95
96 $mockParsoid = $this->newMockParsoid( $expectedParses, $version );
97 $parsoidParser = new ParsoidParser(
98 $mockParsoid,
99 $services->getParsoidPageConfigFactory(),
100 $services->getLanguageConverterFactory(),
101 $services->getParserFactory(),
102 $services->getGlobalIdGenerator()
103 );
104
105 // Create a mock Parsoid factory that returns the ParsoidParser object
106 // with the mocked Parsoid object.
107 $mockParsoidParserFactory = $this->createNoOpMock( ParsoidParserFactory::class, [ 'create' ] );
108 $mockParsoidParserFactory->expects( $this->exactly( $expectedParses ) )
109 ->method( 'create' )
110 ->willReturn( $parsoidParser );
111
112 $this->setService( 'ParsoidParserFactory', $mockParsoidParserFactory );
113 }
114
115 /**
116 * @param ?ParserOutputAccess $parserOutputAccess
117 * @return ParsoidOutputAccess
118 */
119 private function getParsoidOutputAccessWithCache(
120 ?ParserOutputAccess $parserOutputAccess = null
121 ): ParsoidOutputAccess {
122 $services = $this->getServiceContainer();
123 return new ParsoidOutputAccess(
124 $services->getParsoidParserFactory(),
125 $parserOutputAccess ?? $services->getParserOutputAccess(),
126 $services->getPageStore(),
127 $services->getRevisionLookup(),
128 $services->getParsoidSiteConfig(),
129 $services->getContentHandlerFactory()
130 );
131 }
132
133 /**
134 * @return ParserOptions
135 */
136 private function getParserOptions() {
137 return ParserOptions::newFromAnon();
138 }
139
140 private function getHtml( $value ) {
141 if ( $value instanceof StatusValue ) {
142 $value = $value->getValue();
143 }
144
145 if ( $value instanceof ParserOutput ) {
146 $value = $value->getRawText();
147 }
148
149 $html = preg_replace( '/<!--.*?-->/s', '', $value );
150 $html = trim( preg_replace( '/[\r\n]{2,}/', "\n", $html ) );
151 $html = trim( preg_replace( '/\s{2,}/', ' ', $html ) );
152 return $html;
153 }
154
155 private function assertContainsHtml( $needle, $actual, $msg = '' ) {
156 $this->assertNotNull( $actual );
157
158 if ( $actual instanceof StatusValue ) {
159 $this->assertStatusOK( $actual, 'isOK' );
160 }
161
162 $this->assertStringContainsString( $needle, $this->getHtml( $actual ), $msg );
163 }
164
165 /**
166 * @covers \MediaWiki\Parser\Parsoid\ParsoidOutputAccess::getParserOutput
167 */
168 public function testGetParserOutputThrowsIfRevisionNotFound() {
169 $this->resetServicesWithMockedParsoid( 0 );
170 $access = $this->getParsoidOutputAccessWithCache();
171 $parserOptions = $this->getParserOptions();
172
173 $page = $this->getNonexistingTestPage( __METHOD__ );
174
175 $this->expectException( RevisionAccessException::class );
176 $access->getParserOutput( $page, $parserOptions );
177 }
178
179 /**
180 * Tests that getParserOutput() will return output.
181 *
182 * @covers \MediaWiki\Parser\Parsoid\ParsoidOutputAccess::getParserOutput
183 */
184 public function testGetParserOutput() {
185 $this->resetServicesWithMockedParsoid( 1 );
186 $access = $this->getParsoidOutputAccessWithCache();
187 $parserOptions = $this->getParserOptions();
188
189 $page = $this->getNonexistingTestPage( __METHOD__ );
190 $this->editPage( $page, self::WIKITEXT );
191
192 $status = $access->getParserOutput( $page, $parserOptions );
193 $this->assertContainsHtml( self::MOCKED_HTML . ' of ' . self::WIKITEXT, $status );
194
195 $output = $status->getValue();
196
197 // check that ParsoidRenderID::newFromParserOutput() doesn't throw
198 $this->assertNotNull( ParsoidRenderID::newFromParserOutput( $output ) );
199
200 // Ensure that we can still create a valid instance of PageBundle from the ParserOutput
201 $pageBundle = PageBundleParserOutputConverter::pageBundleFromParserOutput( $output );
202 $this->assertSame( $output->getRawText(), $pageBundle->html );
203
204 // Ensure that the expected mw and parsoid fields are set in the PageBundle
205 $this->assertNotEmpty( $pageBundle->mw );
206 $this->assertNotEmpty( $pageBundle->parsoid );
207 $this->assertNotEmpty( $pageBundle->headers );
208 $this->assertNotEmpty( $pageBundle->version );
209
210 // Check that the metadata set by our mock parsoid is preserved
211 $this->checkMetadata( $output );
212 }
213
214 /**
215 * Tests that getParserOutput() will place the generated output for the latest revision
216 * in the parsoid parser cache.
217 *
218 * @covers \MediaWiki\Parser\Parsoid\ParsoidOutputAccess::getParserOutput
219 */
220 public function testLatestRevisionIsCached() {
221 $this->resetServicesWithMockedParsoid( 1 );
222 $access = $this->getParsoidOutputAccessWithCache();
223 $parserOptions = $this->getParserOptions();
224
225 $page = $this->getNonexistingTestPage( __METHOD__ );
226 $this->editPage( $page, self::WIKITEXT );
227
228 $status = $access->getParserOutput( $page, $parserOptions );
229 $this->assertContainsHtml( self::MOCKED_HTML . ' of ' . self::WIKITEXT, $status );
230 $this->checkMetadata( $status );
231
232 // Get the ParserOutput from cache
233 $status = $access->getCachedParserOutput( $page, $parserOptions );
234 $this->assertContainsHtml( self::MOCKED_HTML . ' of ' . self::WIKITEXT, $status );
235 $this->checkMetadata( $status );
236
237 // Get the ParserOutput from cache, without supplying a PageRecord
238 $status = $access->getCachedParserOutput( $page->getTitle(), $parserOptions );
239 $this->assertContainsHtml( self::MOCKED_HTML . ' of ' . self::WIKITEXT, $status );
240 $this->checkMetadata( $status );
241
242 // Get the ParserOutput again, this should not trigger a new parse.
243 $status = $access->getParserOutput( $page, $parserOptions );
244 $this->assertContainsHtml( self::MOCKED_HTML . ' of ' . self::WIKITEXT, $status );
245 $this->checkMetadata( $status );
246 }
247
248 /**
249 * Tests that getParserOutput() will force a parse since we know that
250 * the revision is not in the cache.
251 *
252 * @covers \MediaWiki\Parser\Parsoid\ParsoidOutputAccess::getParserOutput
253 */
254 public function testLatestRevisionWithForceParse() {
255 $this->resetServicesWithMockedParsoid( 2 );
256 $access = $this->getParsoidOutputAccessWithCache();
257 $parserOptions = $this->getParserOptions();
258
259 $page = $this->getNonexistingTestPage( __METHOD__ );
260 $this->editPage( $page, self::WIKITEXT );
261
262 $status = $access->getParserOutput( $page, $parserOptions );
263 $this->assertContainsHtml( self::MOCKED_HTML . ' of ' . self::WIKITEXT, $status );
264 $this->checkMetadata( $status );
265
266 // Get the ParserOutput again, this should trigger a new parse
267 // since we're forcing it to.
268 $status = $access->getParserOutput(
269 $page,
270 $parserOptions,
271 null,
272 ParserOutputAccess::OPT_FORCE_PARSE
273 );
274 $this->assertContainsHtml( self::MOCKED_HTML . ' of ' . self::WIKITEXT, $status );
275 $this->checkMetadata( $status );
276 }
277
278 /**
279 * Tests that getParserOutput() will force a parse since we know that
280 * the revision is not in the cache.
281 *
282 * @covers \MediaWiki\Parser\Parsoid\ParsoidOutputAccess::getParserOutput
283 */
284 public function testLatestRevisionWithNoUpdateCache() {
285 $cacheBag = $this->getMockBuilder( HashBagOStuff::class )
286 ->onlyMethods( [ 'set', 'setMulti' ] )
287 ->getMock();
288 $cacheBag->expects( $this->never() )->method( 'set' );
289 $cacheBag->expects( $this->never() )->method( 'setMulti' );
290
291 // ParserCache should not get anything stored in it.
292 $this->resetServicesWithMockedParsoid( 1, [], $cacheBag );
293 $access = $this->getParsoidOutputAccessWithCache();
294 $parserOptions = $this->getParserOptions();
295
296 $page = $this->getNonexistingTestPage( __METHOD__ );
297 $this->editPage( $page, self::WIKITEXT );
298
299 $status = $access->getParserOutput(
300 $page,
301 $parserOptions,
302 null,
303 ParserOutputAccess::OPT_NO_UPDATE_CACHE
304 );
305 $this->assertContainsHtml( self::MOCKED_HTML . ' of ' . self::WIKITEXT, $status );
306 $this->checkMetadata( $status );
307 }
308
309 /**
310 * Tests that getParserOutput() will not call Parsoid and will not write to ParserCache
311 * for unsupported content models.
312 *
313 * @covers \MediaWiki\Parser\Parsoid\ParsoidOutputAccess::getParserOutput
314 */
315 public function testNonParsoidOutput() {
316 // Expect no cache writes!
317 $cacheBag = $this->getMockBuilder( HashBagOStuff::class )
318 ->onlyMethods( [ 'set', 'setMulti' ] )
319 ->getMock();
320 $cacheBag->expects( $this->never() )->method( 'set' );
321 $cacheBag->expects( $this->never() )->method( 'setMulti' );
322
323 // Expect no calls to parsoid!
324 $this->resetServicesWithMockedParsoid( 0, [], $cacheBag );
325 $access = $this->getParsoidOutputAccessWithCache();
326 $parserOptions = $this->getParserOptions();
327
328 $page = $this->getNonexistingTestPage( __METHOD__ );
329 $this->editPage( $page, new JavaScriptContent( '"not wikitext"' ) );
330
331 $status = $access->getParserOutput( $page, $parserOptions );
332 $this->assertContainsHtml( 'not wikitext', $status );
333
334 /** @var ParserOutput $parserOutput */
335 $parserOutput = $status->getValue();
336 $this->assertNotNull(
337 ParsoidRenderID::newFromParserOutput( $parserOutput )->getKey()
338 );
339 }
340
341 public function testOldRevisionIsCached() {
342 $this->resetServicesWithMockedParsoid( 1 );
343 $access = $this->getParsoidOutputAccessWithCache();
344 $parserOptions = $this->getParserOptions();
345
346 $page = $this->getNonexistingTestPage( __METHOD__ );
347 $status1 = $this->editPage( $page, self::WIKITEXT );
348 $rev = $status1->getValue()['revision-record'];
349
350 // Make an edit so that the revision we're getting output
351 // for below is not the current revision.
352 $this->editPage( $page, 'Second revision' );
353
354 $access->getParserOutput( $page, $parserOptions, $rev );
355
356 // Get the ParserOutput from cache, using revision object
357 $status = $access->getCachedParserOutput( $page, $parserOptions, $rev );
358 $this->assertContainsHtml( self::MOCKED_HTML . ' of ' . self::WIKITEXT, $status );
359 $this->checkMetadata( $status );
360
361 // Get the ParserOutput from cache, using revision ID
362 $status = $access->getCachedParserOutput( $page->getTitle(), $parserOptions, $rev->getId() );
363 $this->assertContainsHtml( self::MOCKED_HTML . ' of ' . self::WIKITEXT, $status );
364 $this->checkMetadata( $status );
365
366 // Get the ParserOutput again, this should not trigger a new parse.
367 $status = $access->getParserOutput( $page, $parserOptions, $rev );
368 $this->assertContainsHtml( self::MOCKED_HTML . ' of ' . self::WIKITEXT, $status );
369 $this->checkMetadata( $status );
370 }
371
372 public function testGetParserOutputWithOldRevision() {
373 $this->resetServicesWithMockedParsoid( 2 );
374 $access = $this->getParsoidOutputAccessWithCache();
375 $parserOptions = $this->getParserOptions();
376
377 $page = $this->getNonexistingTestPage( __METHOD__ );
378 $status1 = $this->editPage( $page, self::WIKITEXT );
379 $rev1 = $status1->getValue()['revision-record'];
380
381 $this->editPage( $page, 'Second revision' );
382
383 $status2 = $access->getParserOutput( $page, $parserOptions );
384 $this->assertContainsHtml( self::MOCKED_HTML . ' of Second revision', $status2 );
385 $this->checkMetadata( $status2 );
386
387 $status1 = $access->getParserOutput( $page, $parserOptions, $rev1 );
388 $this->assertContainsHtml( self::MOCKED_HTML . ' of ' . self::WIKITEXT, $status1 );
389 $this->checkMetadata( $status1 );
390
391 // Again, using just the revision ID
392 $status1 = $access->getParserOutput( $page, $parserOptions, $rev1->getId() );
393 $this->assertContainsHtml( self::MOCKED_HTML . ' of ' . self::WIKITEXT, $status1 );
394 $this->checkMetadata( $status1 );
395
396 // check that ParsoidRenderID::newFromParserOutput() doesn't throw
397 $output1 = $status1->getValue();
398 $this->assertNotNull( ParsoidRenderID::newFromParserOutput( $output1 ) );
399 }
400
401 /**
402 * @covers \MediaWiki\Parser\Parsoid\ParsoidOutputAccess::getParserOutput
403 */
404 public function testParseWithPageRecordAndNoRevision() {
405 $pageRecord = $this->getExistingTestPage( __METHOD__ )->toPageRecord();
406 $pOpts = ParserOptions::newFromAnon();
407
408 $parsoidOutputAccess = $this->getServiceContainer()->getParsoidOutputAccess();
409 $status = $parsoidOutputAccess->getParserOutput( $pageRecord, $pOpts, null );
410
411 $this->assertInstanceOf( Status::class, $status );
412 $this->assertTrue( $status->isOK() );
413 $this->assertInstanceOf( ParserOutput::class, $status->getValue() );
414
415 /** @var ParserOutput $parserOutput */
416 $parserOutput = $status->getValue();
417 $this->assertStringContainsString( __METHOD__, $parserOutput->getRawText() );
418 $this->assertNotEmpty( $parserOutput->getRenderId() );
419 $this->assertNotEmpty( $parserOutput->getCacheRevisionId() );
420 $this->assertNotEmpty( $parserOutput->getCacheTime() );
421 }
422
423 private function checkMetadata( $output ) {
424 $parserOutput = $output instanceof StatusValue ? $output->getValue() : $output;
425
426 // Check the metadata added by ::newMockParsoid() is preserved
427 $this->assertSame( 'my-data', $parserOutput->getExtensionData( 'my-key' ) );
428 $this->assertSame( '', $parserOutput->getPageProperty( 'forcetoc' ) );
429 $this->assertSame( true, $parserOutput->getOutputFlag( ParserOutputFlags::NO_GALLERY ) );
430 }
431
432 /**
433 * @covers \MediaWiki\Parser\Parsoid\ParsoidOutputAccess::getParserOutput
434 */
435 public function testParseWithPageRecordAndRevision() {
436 $page = $this->getExistingTestPage( __METHOD__ );
437 $pageRecord = $page->toPageRecord();
438 $pOpts = ParserOptions::newFromAnon();
439 $revRecord = $page->getRevisionRecord();
440
441 $parsoidOutputAccess = $this->getServiceContainer()->getParsoidOutputAccess();
442 $status = $parsoidOutputAccess->getParserOutput( $pageRecord, $pOpts, $revRecord );
443
444 $this->assertInstanceOf( Status::class, $status );
445 $this->assertTrue( $status->isOK() );
446 $this->assertInstanceOf( ParserOutput::class, $status->getValue() );
447
448 /** @var ParserOutput $parserOutput */
449 $parserOutput = $status->getValue();
450 $this->assertStringContainsString( __METHOD__, $parserOutput->getRawText() );
451 $this->assertNotEmpty( $parserOutput->getRenderId() );
452 $this->assertNotEmpty( $parserOutput->getCacheRevisionId() );
453 $this->assertNotEmpty( $parserOutput->getCacheTime() );
454 }
455
456 /**
457 * @covers \MediaWiki\Parser\Parsoid\ParsoidOutputAccess::getParserOutput
458 */
459 public function testParseWithPageIdentityAndRevisionId() {
460 $page = $this->getExistingTestPage( __METHOD__ );
461 $pOpts = ParserOptions::newFromAnon();
462 $revId = $page->getLatest();
463
464 $parsoidOutputAccess = $this->getServiceContainer()->getParsoidOutputAccess();
465 $status = $parsoidOutputAccess->getParserOutput( $page->getTitle(), $pOpts, $revId );
466
467 $this->assertInstanceOf( Status::class, $status );
468 $this->assertTrue( $status->isOK() );
469 $this->assertInstanceOf( ParserOutput::class, $status->getValue() );
470
471 /** @var ParserOutput $parserOutput */
472 $parserOutput = $status->getValue();
473 $this->assertStringContainsString( __METHOD__, $parserOutput->getRawText() );
474 $this->assertNotEmpty( $parserOutput->getRenderId() );
475 $this->assertNotEmpty( $parserOutput->getCacheRevisionId() );
476 $this->assertNotEmpty( $parserOutput->getCacheTime() );
477 }
478
479 /**
480 * @covers \MediaWiki\Parser\Parsoid\ParsoidOutputAccess::parseUncacheable
481 */
482 public function testParseWithNonExistingPageAndFakeRevision() {
483 $page = $this->getNonexistingTestPage( __METHOD__ );
484 $pOpts = ParserOptions::newFromAnon();
485
486 // Create a fake revision record
487 $revRecord = new MutableRevisionRecord( $page->getTitle() );
488 $revRecord->setId( 0 );
489 $revRecord->setPageId( $page->getId() );
490 $revRecord->setContent(
491 SlotRecord::MAIN,
492 new WikitextContent( 'test' )
493 );
494
495 $parsoidOutputAccess = $this->getServiceContainer()->getParsoidOutputAccess();
496 $status = $parsoidOutputAccess->parseUncacheable( $page->getTitle(), $pOpts, $revRecord );
497
498 $this->assertInstanceOf( Status::class, $status );
499 $this->assertTrue( $status->isOK() );
500 $this->assertInstanceOf( ParserOutput::class, $status->getValue() );
501
502 /** @var ParserOutput $parserOutput */
503 $parserOutput = $status->getValue();
504 $this->assertStringContainsString( __METHOD__, $parserOutput->getRawText() );
505 $this->assertNotEmpty( $parserOutput->getRenderId() );
506 // The revision ID is set to 0, so that's what is in the cache.
507 $this->assertSame( 0, $parserOutput->getCacheRevisionId() );
508 $this->assertNotEmpty( $parserOutput->getCacheTime() );
509 }
510
511 /**
512 * @covers \MediaWiki\Parser\Parsoid\ParsoidOutputAccess::parseUncacheable
513 */
514 public function testParseDeletedRevision() {
515 $page = $this->getNonexistingTestPage( __METHOD__ );
516 $pOpts = ParserOptions::newFromAnon();
517
518 // Create a fake revision record
519 $revRecord = new MutableRevisionRecord( $page->getTitle() );
520 $revRecord->setId( 0 );
521 $revRecord->setPageId( $page->getId() );
522 $revRecord->setContent(
523 SlotRecord::MAIN,
524 new WikitextContent( 'test' )
525 );
526 // Induce a RevisionAccessException
527 $revRecord->setVisibility( RevisionRecord::DELETED_TEXT );
528
529 $parsoidOutputAccess = $this->getServiceContainer()->getParsoidOutputAccess();
530 $status = $parsoidOutputAccess->parseUncacheable( $page->getTitle(), $pOpts, $revRecord );
531
532 $this->assertStatusNotOK( $status );
533 $this->assertStatusMessagesExactly(
534 StatusValue::newFatal( 'parsoid-revision-access', 'Not an available content version.' ),
535 $status
536 );
537 }
538
539 /**
540 * Mock the language class based on a language code.
541 *
542 * @param string $langCode
543 *
544 * @return Language|Language&MockObject|MockObject
545 */
546 private function getLanguageMock( string $langCode ) {
547 $language = $this->createMock( Language::class );
548 $language->method( 'getCode' )->willReturn( $langCode );
549 $language->method( 'getDir' )->willReturn( 'ltr' );
550 $bcp47 = LanguageCode::bcp47( $langCode );
551 $language
552 ->method( 'getHtmlCode' )
553 ->willReturn( $bcp47 );
554 $language
555 ->method( 'toBcp47Code' )
556 ->willReturn( $bcp47 );
557 return $language;
558 }
559
560 /** @return Generator */
561 public function provideParserOptionsWithLanguageOverride() {
562 $parserOptions = $this->createMock( ParserOptions::class );
563 $parserOptions->method( 'optionsHash' )->willReturn( '' );
564 $parserOptions->method( 'getWrapOutputClass' )->willReturn( false );
565 $parserOptions->method( 'getUseParsoid' )->willReturn( true );
566 $parserOptions->method( 'getTargetLanguage' )
567 ->willReturn( null );
568 yield 'ParserOptions with no language' => [ $parserOptions, 'en' ];
569
570 $langCode = 'de';
571 $parserOptions = $this->createMock( ParserOptions::class );
572 $parserOptions->method( 'optionsHash' )->willReturn( '' );
573 $parserOptions->method( 'getWrapOutputClass' )->willReturn( false );
574 $parserOptions->method( 'getUseParsoid' )->willReturn( true );
575 $parserOptions->method( 'getTargetLanguage' )
576 ->willReturn( $this->getLanguageMock( $langCode ) );
577 yield 'ParserOptions for "de" language' => [ $parserOptions, $langCode ];
578
579 $langCode = 'ar';
580 $parserOptions = $this->createMock( ParserOptions::class );
581 $parserOptions->method( 'optionsHash' )->willReturn( '' );
582 $parserOptions->method( 'getWrapOutputClass' )->willReturn( false );
583 $parserOptions->method( 'getUseParsoid' )->willReturn( true );
584 $parserOptions->method( 'getTargetLanguage' )
585 ->willReturn( $this->getLanguageMock( $langCode ) );
586 yield 'ParserOptions for "ar" language' => [ $parserOptions, $langCode ];
587 }
588
589 /**
590 * @covers \MediaWiki\Rest\Handler\Helper\HtmlOutputRendererHelper::getParserOutput
591 * @dataProvider provideParserOptionsWithLanguageOverride
592 */
593 public function testGetParserOutputWithLanguageOverride( $parserOptions, $expectedLangCode ) {
594 $services = $this->getServiceContainer();
595 $parserOutputAccess = $services->getParsoidOutputAccess();
596
597 $content = 'Test content for ' . __METHOD__;
598 $page = Title::makeTitle( NS_MAIN, 'TestGetParserOutputWithLanguageOverride' );
599 $this->editPage( $page, $content );
600
601 $status = $parserOutputAccess->getParserOutput( $page, $parserOptions );
602
603 $this->assertTrue( $status->isOK() );
604
605 // assert dummy content in parsoid output HTML
606 $html = $status->getValue()->getRawText();
607 $this->assertStringContainsString( $content, $html );
608
609 if ( $parserOptions->getTargetLanguage() !== null ) {
610 $targetLanguage = $parserOptions->getTargetLanguage()->getCode();
611 $this->assertSame( $expectedLangCode, $targetLanguage );
612 $this->assertInstanceOf( Language::class, $parserOptions->getTargetLanguage() );
613 } else {
614 $this->assertNull( $parserOptions->getTargetLanguage() );
615 }
616
617 // assert the page language in parsoid output HTML
618 $this->assertStringContainsString( 'lang="' . $expectedLangCode . '"', $html );
619 $this->assertStringContainsString( 'content="' . $expectedLangCode . '"', $html );
620 }
621
622 /**
623 * @covers \MediaWiki\Parser\Parsoid\ParsoidOutputAccess::getParserOutput
624 */
625 public function testRerenderForNonDefaultVersion() {
626 // Rendering is asked for twice because version is not the Parsoid default
627 // so even though the output is found in the primary cache, it's obsolete.
628 $this->resetServicesWithMockedParsoid( 2, [], null, '1.1.1' );
629
630 $parserOutputAccess = $this->getServiceContainer()->getParserOutputAccess();
631 $access = $this->getParsoidOutputAccessWithCache( $parserOutputAccess );
632
633 $parserOptions = $this->getParserOptions();
634 $page = $this->getExistingTestPage();
635
636 $access->getParserOutput( $page, $parserOptions );
637
638 // Clear the localCache since that has priority and updating the Parsoid
639 // default version would require a process restart anyways.
640 $testingAccess = TestingAccessWrapper::newFromObject( $parserOutputAccess );
641 $testingAccess->localCache->clear();
642
643 $access->getParserOutput( $page, $parserOptions );
644 }
645
646 }