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