| 1 |
<?php |
| 2 |
|
| 3 |
namespace MediaWiki\Tests\Rest\Handler\Helper; |
| 4 |
|
| 5 |
use Exception; |
| 6 |
use MediaWiki\Content\CssContent; |
| 7 |
use MediaWiki\Content\WikitextContent; |
| 8 |
use MediaWiki\Deferred\DeferredUpdates; |
| 9 |
use MediaWiki\Edit\ParsoidRenderID; |
| 10 |
use MediaWiki\Edit\SimpleParsoidOutputStash; |
| 11 |
use MediaWiki\Hook\ParserLogLinterDataHook; |
| 12 |
use MediaWiki\Logger\Spi as LoggerSpi; |
| 13 |
use MediaWiki\MainConfigNames; |
| 14 |
use MediaWiki\Page\PageIdentity; |
| 15 |
use MediaWiki\Page\PageIdentityValue; |
| 16 |
use MediaWiki\Page\PageRecord; |
| 17 |
use MediaWiki\Page\PageReference; |
| 18 |
use MediaWiki\Page\ParserOutputAccess; |
| 19 |
use MediaWiki\Parser\ParserCache; |
| 20 |
use MediaWiki\Parser\ParserCacheFactory; |
| 21 |
use MediaWiki\Parser\ParserOptions; |
| 22 |
use MediaWiki\Parser\ParserOutput; |
| 23 |
use MediaWiki\Parser\Parsoid\HtmlTransformFactory; |
| 24 |
use MediaWiki\Parser\Parsoid\LanguageVariantConverter; |
| 25 |
use MediaWiki\Parser\Parsoid\PageBundleParserOutputConverter; |
| 26 |
use MediaWiki\Parser\Parsoid\ParsoidParser; |
| 27 |
use MediaWiki\Parser\Parsoid\ParsoidParserFactory; |
| 28 |
use MediaWiki\Parser\RevisionOutputCache; |
| 29 |
use MediaWiki\Permissions\Authority; |
| 30 |
use MediaWiki\Permissions\PermissionStatus; |
| 31 |
use MediaWiki\Rest\Handler\Helper\HtmlOutputRendererHelper; |
| 32 |
use MediaWiki\Rest\LocalizedHttpException; |
| 33 |
use MediaWiki\Rest\Response; |
| 34 |
use MediaWiki\Rest\ResponseInterface; |
| 35 |
use MediaWiki\Revision\MutableRevisionRecord; |
| 36 |
use MediaWiki\Revision\RevisionAccessException; |
| 37 |
use MediaWiki\Revision\RevisionRecord; |
| 38 |
use MediaWiki\Revision\SlotRecord; |
| 39 |
use MediaWiki\Status\Status; |
| 40 |
use MediaWiki\Utils\MWTimestamp; |
| 41 |
use MediaWikiIntegrationTestCase; |
| 42 |
use PHPUnit\Framework\MockObject\MockObject; |
| 43 |
use PHPUnit\Framework\MockObject\Rule\InvocationOrder; |
| 44 |
use Psr\Log\LoggerInterface; |
| 45 |
use Psr\Log\NullLogger; |
| 46 |
use Wikimedia\Bcp47Code\Bcp47Code; |
| 47 |
use Wikimedia\Bcp47Code\Bcp47CodeValue; |
| 48 |
use Wikimedia\Message\MessageValue; |
| 49 |
use Wikimedia\ObjectCache\EmptyBagOStuff; |
| 50 |
use Wikimedia\ObjectCache\HashBagOStuff; |
| 51 |
use Wikimedia\Parsoid\Core\ClientError; |
| 52 |
use Wikimedia\Parsoid\Core\PageBundle; |
| 53 |
use Wikimedia\Parsoid\Core\ResourceLimitExceededException; |
| 54 |
use Wikimedia\Parsoid\Parsoid; |
| 55 |
use Wikimedia\Stats\StatsFactory; |
| 56 |
use Wikimedia\TestingAccessWrapper; |
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
class HtmlOutputRendererHelperTest extends MediaWikiIntegrationTestCase { |
| 63 |
private const CACHE_EPOCH = '20001111010101'; |
| 64 |
|
| 65 |
private const TIMESTAMP_OLD = '20200101112233'; |
| 66 |
private const TIMESTAMP = '20200101223344'; |
| 67 |
private const TIMESTAMP_LATER = '20200101234200'; |
| 68 |
|
| 69 |
private const WIKITEXT_OLD = 'Hello \'\'\'Goat\'\'\''; |
| 70 |
private const WIKITEXT = 'Hello \'\'\'World\'\'\''; |
| 71 |
|
| 72 |
private const HTML_OLD = '>Goat<'; |
| 73 |
private const HTML = '>World<'; |
| 74 |
|
| 75 |
private const PARAM_DEFAULTS = [ |
| 76 |
'stash' => false, |
| 77 |
'flavor' => 'view', |
| 78 |
]; |
| 79 |
|
| 80 |
private const MOCK_HTML = 'mocked HTML'; |
| 81 |
private const MOCK_HTML_VARIANT = 'ockedmay HTML'; |
| 82 |
|
| 83 |
private function exactlyOrAny( ?int $count ): InvocationOrder { |
| 84 |
return $count === null ? $this->any() : $this->exactly( $count ); |
| 85 |
} |
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
private function getLoggerSpi( $logger = null ) { |
| 93 |
$spi = $this->createNoOpMock( LoggerSpi::class, [ 'getLogger' ] ); |
| 94 |
$spi->method( 'getLogger' )->willReturn( $logger ?? new NullLogger() ); |
| 95 |
return $spi; |
| 96 |
} |
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
public function newMockParserOutputAccess( ?string $expectedHtml ): ParserOutputAccess { |
| 102 |
$expectedCalls = [ |
| 103 |
'getParserOutput' => null, |
| 104 |
]; |
| 105 |
$access = $this->createNoOpMock( ParserOutputAccess::class, array_keys( $expectedCalls ) ); |
| 106 |
$access->expects( $this->exactlyOrAny( $expectedCalls[ 'getParserOutput' ] ) ) |
| 107 |
->method( 'getParserOutput' ) |
| 108 |
->willReturnCallback( function ( |
| 109 |
PageRecord $page, |
| 110 |
ParserOptions $parserOpts, |
| 111 |
?RevisionRecord $rev = null, |
| 112 |
int $options = 0 |
| 113 |
) use ( $expectedHtml ) { |
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
$pout = $this->makeParserOutput( |
| 118 |
$parserOpts, |
| 119 |
$expectedHtml ?? $this->getMockHtml( $rev ), |
| 120 |
$rev, |
| 121 |
$page |
| 122 |
); |
| 123 |
return Status::newGood( $pout ); |
| 124 |
} ); |
| 125 |
return $access; |
| 126 |
} |
| 127 |
|
| 128 |
private function getMockHtml( $rev ) { |
| 129 |
if ( $rev instanceof RevisionRecord ) { |
| 130 |
$html = '<p>' . $rev->getContent( SlotRecord::MAIN )->getText() . '</p>'; |
| 131 |
} elseif ( is_int( $rev ) ) { |
| 132 |
$html = '<p>rev:' . $rev . '</p>'; |
| 133 |
} else { |
| 134 |
$html = self::MOCK_HTML; |
| 135 |
} |
| 136 |
|
| 137 |
return $html; |
| 138 |
} |
| 139 |
|
| 140 |
|
| 141 |
|
| 142 |
|
| 143 |
|
| 144 |
|
| 145 |
|
| 146 |
|
| 147 |
|
| 148 |
|
| 149 |
private function makeParserOutput( |
| 150 |
ParserOptions $parserOpts, |
| 151 |
string $html, |
| 152 |
$rev, |
| 153 |
PageIdentity $page, |
| 154 |
?string $version = null |
| 155 |
): ParserOutput { |
| 156 |
static $counter = 0; |
| 157 |
$lang = $parserOpts->getTargetLanguage(); |
| 158 |
$lang = $lang ? $lang->getCode() : 'en'; |
| 159 |
$version ??= Parsoid::defaultHTMLVersion(); |
| 160 |
|
| 161 |
$html = "<!DOCTYPE html><html lang=\"$lang\"><body><div id='t3s7'>$html</div></body></html>"; |
| 162 |
|
| 163 |
$revTimestamp = null; |
| 164 |
if ( $rev instanceof RevisionRecord ) { |
| 165 |
$revTimestamp = $rev->getTimestamp(); |
| 166 |
$rev = $rev->getId() ?? 0; |
| 167 |
} |
| 168 |
|
| 169 |
$pout = new ParserOutput( $html ); |
| 170 |
$pout->setCacheRevisionId( $rev ?? $page->getLatest() ); |
| 171 |
$pout->setCacheTime( wfTimestampNow() ); |
| 172 |
if ( $revTimestamp ) { |
| 173 |
$pout->setRevisionTimestamp( $revTimestamp ); |
| 174 |
} |
| 175 |
|
| 176 |
$pout->setRenderId( 'bogus-uuid-' . strval( $counter++ ) ); |
| 177 |
$pout->setExtensionData( PageBundleParserOutputConverter::PARSOID_PAGE_BUNDLE_KEY, [ |
| 178 |
'parsoid' => [ 'ids' => [ |
| 179 |
't3s7' => [ 'dsr' => [ 0, 0, 0, 0 ] ], |
| 180 |
] ], |
| 181 |
'mw' => [ 'ids' => [] ], |
| 182 |
'version' => $version, |
| 183 |
'headers' => [ |
| 184 |
'content-language' => $lang |
| 185 |
] |
| 186 |
] ); |
| 187 |
|
| 188 |
$pout->setLanguage( new Bcp47CodeValue( $lang ) ); |
| 189 |
return $pout; |
| 190 |
} |
| 191 |
|
| 192 |
protected function setUp(): void { |
| 193 |
parent::setUp(); |
| 194 |
|
| 195 |
$this->overrideConfigValue( MainConfigNames::CacheEpoch, self::CACHE_EPOCH ); |
| 196 |
} |
| 197 |
|
| 198 |
|
| 199 |
|
| 200 |
|
| 201 |
private function newAuthority(): MockObject { |
| 202 |
$authority = $this->createNoOpMock( Authority::class, [ 'authorizeWrite' ] ); |
| 203 |
$authority->method( 'authorizeWrite' )->willReturn( true ); |
| 204 |
return $authority; |
| 205 |
} |
| 206 |
|
| 207 |
|
| 208 |
|
| 209 |
|
| 210 |
private function newAuthorityWhoCantStash(): MockObject { |
| 211 |
$authority = $this->createNoOpMock( Authority::class, [ 'authorizeWrite' ] ); |
| 212 |
$authority->method( 'authorizeWrite' )->willReturnCallback( |
| 213 |
static function ( $action, $target, PermissionStatus $status ) { |
| 214 |
if ( $action === 'stashbasehtml' ) { |
| 215 |
$status->setRateLimitExceeded(); |
| 216 |
$status->setPermission( $action ); |
| 217 |
return false; |
| 218 |
} |
| 219 |
|
| 220 |
return true; |
| 221 |
} |
| 222 |
); |
| 223 |
return $authority; |
| 224 |
} |
| 225 |
|
| 226 |
private function newHelper( |
| 227 |
array $options, |
| 228 |
PageIdentity $page, |
| 229 |
array $parameters = [], |
| 230 |
?Authority $authority = null, |
| 231 |
$revision = null, |
| 232 |
bool $lenientRevHandling = false |
| 233 |
): HtmlOutputRendererHelper { |
| 234 |
$jsonCodec = $this->getServiceContainer()->getJsonCodec(); |
| 235 |
$cache = $options['cache'] ?? new EmptyBagOStuff(); |
| 236 |
$stash = new SimpleParsoidOutputStash( $jsonCodec, $cache, 1 ); |
| 237 |
|
| 238 |
$services = $this->getServiceContainer(); |
| 239 |
|
| 240 |
if ( isset( $options['ParsoidParserFactory'] ) ) { |
| 241 |
$this->resetServicesWithMockedParsoidParserFactory( $options['ParsoidParserFactory'] ); |
| 242 |
} |
| 243 |
|
| 244 |
return new HtmlOutputRendererHelper( |
| 245 |
$stash, |
| 246 |
StatsFactory::newNull(), |
| 247 |
$options['ParserOutputAccess'] ?? $this->newMockParserOutputAccess( |
| 248 |
$options['expectedHtml'] ?? null |
| 249 |
), |
| 250 |
$services->getPageStore(), |
| 251 |
$services->getRevisionLookup(), |
| 252 |
$services->getRevisionRenderer(), |
| 253 |
$services->getParsoidSiteConfig(), |
| 254 |
$options['HtmlTransformFactory'] ?? $services->getHtmlTransformFactory(), |
| 255 |
$services->getContentHandlerFactory(), |
| 256 |
$services->getLanguageFactory(), |
| 257 |
$page, $parameters, $authority, $revision, $lenientRevHandling |
| 258 |
); |
| 259 |
} |
| 260 |
|
| 261 |
private function getExistingPageWithRevisions( |
| 262 |
$name, $wikitext = self::WIKITEXT, $wikitextOld = self::WIKITEXT_OLD |
| 263 |
) { |
| 264 |
$page = $this->getNonexistingTestPage( $name ); |
| 265 |
|
| 266 |
MWTimestamp::setFakeTime( self::TIMESTAMP_OLD ); |
| 267 |
$this->editPage( $page, $wikitextOld ); |
| 268 |
$revisions['first'] = $page->getRevisionRecord(); |
| 269 |
|
| 270 |
MWTimestamp::setFakeTime( self::TIMESTAMP ); |
| 271 |
$this->editPage( $page, $wikitext ); |
| 272 |
$revisions['latest'] = $page->getRevisionRecord(); |
| 273 |
|
| 274 |
MWTimestamp::setFakeTime( self::TIMESTAMP_LATER ); |
| 275 |
return [ $page, $revisions ]; |
| 276 |
} |
| 277 |
|
| 278 |
private function getNonExistingPageWithFakeRevision( $name ) { |
| 279 |
$page = $this->getNonexistingTestPage( $name ); |
| 280 |
MWTimestamp::setFakeTime( self::TIMESTAMP_OLD ); |
| 281 |
|
| 282 |
$content = new WikitextContent( self::WIKITEXT_OLD ); |
| 283 |
$rev = new MutableRevisionRecord( $page->getTitle() ); |
| 284 |
$rev->setPageId( $page->getId() ); |
| 285 |
$rev->setContent( SlotRecord::MAIN, $content ); |
| 286 |
|
| 287 |
return [ $page, $rev ]; |
| 288 |
} |
| 289 |
|
| 290 |
public static function provideRevisionReferences() { |
| 291 |
return [ |
| 292 |
'current' => [ null, [ 'html' => self::HTML, 'timestamp' => self::TIMESTAMP ] ], |
| 293 |
'old' => [ 'first', [ 'html' => self::HTML_OLD, 'timestamp' => self::TIMESTAMP_OLD ] ], |
| 294 |
]; |
| 295 |
} |
| 296 |
|
| 297 |
|
| 298 |
|
| 299 |
|
| 300 |
public function testGetHtml( $revRef ) { |
| 301 |
[ $page, $revisions ] = $this->getExistingPageWithRevisions( __METHOD__ ); |
| 302 |
|
| 303 |
|
| 304 |
$revId = $revRef ? $revisions[ $revRef ]->getId() : null; |
| 305 |
|
| 306 |
$helper = $this->newHelper( [ 'expectedHtml' => $this->getMockHtml( $revId ) ], $page, self::PARAM_DEFAULTS, $this->newAuthority() ); |
| 307 |
|
| 308 |
if ( $revId ) { |
| 309 |
$helper->setRevision( $revId ); |
| 310 |
$this->assertSame( $revId, $helper->getRevisionId() ); |
| 311 |
} else { |
| 312 |
|
| 313 |
$this->assertSame( 0, $helper->getRevisionId() ); |
| 314 |
} |
| 315 |
|
| 316 |
$htmlresult = $helper->getHtml()->getRawText(); |
| 317 |
|
| 318 |
$this->assertStringContainsString( $this->getMockHtml( $revId ), $htmlresult ); |
| 319 |
} |
| 320 |
|
| 321 |
public function testGetHtmlWithVariant() { |
| 322 |
$this->overrideConfigValue( MainConfigNames::UsePigLatinVariant, true ); |
| 323 |
$page = $this->getExistingTestPage( __METHOD__ ); |
| 324 |
|
| 325 |
$helper = $this->newHelper( [ 'expectedHtml' => self::MOCK_HTML ], $page, self::PARAM_DEFAULTS, $this->newAuthority() ); |
| 326 |
$helper->setVariantConversionLanguage( new Bcp47CodeValue( 'en-x-piglatin' ) ); |
| 327 |
|
| 328 |
$htmlResult = $helper->getHtml()->getRawText(); |
| 329 |
$this->assertStringContainsString( self::MOCK_HTML_VARIANT, $htmlResult ); |
| 330 |
$this->assertStringContainsString( 'en-x-piglatin', $helper->getETag() ); |
| 331 |
|
| 332 |
$pbResult = $helper->getPageBundle(); |
| 333 |
$this->assertStringContainsString( self::MOCK_HTML_VARIANT, $pbResult->html ); |
| 334 |
$this->assertStringContainsString( 'en-x-piglatin', $pbResult->headers['content-language'] ); |
| 335 |
} |
| 336 |
|
| 337 |
public function testGetHtmlWillLint() { |
| 338 |
$this->overrideConfigValue( MainConfigNames::ParsoidSettings, [ |
| 339 |
'linting' => true |
| 340 |
] ); |
| 341 |
|
| 342 |
$page = $this->getExistingTestPage( __METHOD__ ); |
| 343 |
|
| 344 |
$mockHandler = $this->createMock( ParserLogLinterDataHook::class ); |
| 345 |
$mockHandler->expects( $this->once() ) |
| 346 |
->method( 'onParserLogLinterData' ); |
| 347 |
|
| 348 |
$this->setTemporaryHook( |
| 349 |
'ParserLogLinterData', |
| 350 |
$mockHandler |
| 351 |
); |
| 352 |
|
| 353 |
|
| 354 |
$this->resetServices(); |
| 355 |
|
| 356 |
$access = $this->getServiceContainer()->getParserOutputAccess(); |
| 357 |
|
| 358 |
$helper = $this->newHelper( [ 'ParserOutputAccess' => $access ], $page, self::PARAM_DEFAULTS, $this->newAuthority() ); |
| 359 |
|
| 360 |
|
| 361 |
$helper->getHtml(); |
| 362 |
} |
| 363 |
|
| 364 |
public function testGetPageBundleWithOptions() { |
| 365 |
$this->markTestSkipped( 'T347426: Support for non-default output content major version has been disabled.' ); |
| 366 |
$page = $this->getExistingTestPage( __METHOD__ ); |
| 367 |
|
| 368 |
$helper = $this->newHelper( [], $page, self::PARAM_DEFAULTS, $this->newAuthority() ); |
| 369 |
|
| 370 |
|
| 371 |
$helper->setOutputProfileVersion( '999.0.0' ); |
| 372 |
|
| 373 |
$pb = $helper->getPageBundle(); |
| 374 |
|
| 375 |
|
| 376 |
|
| 377 |
$this->assertStringContainsString( '"outputContentVersion":"999.0.0"', $pb->html ); |
| 378 |
$this->assertStringContainsString( '"offsetType":"byte"', $pb->html ); |
| 379 |
|
| 380 |
$response = new Response(); |
| 381 |
$helper->putHeaders( $response, true ); |
| 382 |
$this->assertStringContainsString( 'private', $response->getHeaderLine( 'Cache-Control' ) ); |
| 383 |
} |
| 384 |
|
| 385 |
public function testGetPreviewHtml_setContent() { |
| 386 |
$page = $this->getNonexistingTestPage(); |
| 387 |
|
| 388 |
$helper = $this->newHelper( [], $page, self::PARAM_DEFAULTS, $this->newAuthority() ); |
| 389 |
$helper->setContent( new WikitextContent( 'text to preview' ) ); |
| 390 |
|
| 391 |
|
| 392 |
$this->assertNull( $helper->getRevisionId() ); |
| 393 |
|
| 394 |
$htmlresult = $helper->getHtml()->getRawText(); |
| 395 |
|
| 396 |
$this->assertStringContainsString( 'text to preview', $htmlresult ); |
| 397 |
} |
| 398 |
|
| 399 |
public function testGetPreviewHtml_setContentSource() { |
| 400 |
$page = $this->getNonexistingTestPage(); |
| 401 |
|
| 402 |
$helper = $this->newHelper( [], $page, self::PARAM_DEFAULTS, $this->newAuthority() ); |
| 403 |
$helper->setContentSource( 'text to preview', CONTENT_MODEL_WIKITEXT ); |
| 404 |
|
| 405 |
$htmlresult = $helper->getHtml()->getRawText(); |
| 406 |
|
| 407 |
$this->assertStringContainsString( 'text to preview', $htmlresult ); |
| 408 |
} |
| 409 |
|
| 410 |
public function testHtmlIsStashedForExistingPage() { |
| 411 |
[ $page, ] = $this->getExistingPageWithRevisions( __METHOD__ ); |
| 412 |
|
| 413 |
$cache = new HashBagOStuff(); |
| 414 |
|
| 415 |
$helper = $this->newHelper( |
| 416 |
[ 'cache' => $cache, 'expectedHtml' => self::MOCK_HTML ], $page, self::PARAM_DEFAULTS, $this->newAuthority() |
| 417 |
); |
| 418 |
$helper->setStashingEnabled( true ); |
| 419 |
|
| 420 |
$htmlresult = $helper->getHtml()->getRawText(); |
| 421 |
$this->assertStringContainsString( self::MOCK_HTML, $htmlresult ); |
| 422 |
|
| 423 |
$eTag = $helper->getETag(); |
| 424 |
$parsoidStashKey = ParsoidRenderID::newFromETag( $eTag ); |
| 425 |
|
| 426 |
$jsonCodec = $this->getServiceContainer()->getJsonCodec(); |
| 427 |
$stash = new SimpleParsoidOutputStash( $jsonCodec, $cache, 1 ); |
| 428 |
$this->assertNotNull( $stash->get( $parsoidStashKey ) ); |
| 429 |
} |
| 430 |
|
| 431 |
public function testHtmlIsStashedForFakeRevision() { |
| 432 |
$page = $this->getNonexistingTestPage(); |
| 433 |
|
| 434 |
$cache = new HashBagOStuff(); |
| 435 |
$text = 'just some wikitext'; |
| 436 |
|
| 437 |
$helper = $this->newHelper( [ 'cache' => $cache ], $page, self::PARAM_DEFAULTS, $this->newAuthority() ); |
| 438 |
$helper->setContent( new WikitextContent( $text ) ); |
| 439 |
$helper->setStashingEnabled( true ); |
| 440 |
|
| 441 |
$htmlresult = $helper->getHtml()->getRawText(); |
| 442 |
$this->assertStringContainsString( $text, $htmlresult ); |
| 443 |
|
| 444 |
$eTag = $helper->getETag(); |
| 445 |
$parsoidStashKey = ParsoidRenderID::newFromETag( $eTag ); |
| 446 |
|
| 447 |
$jsonCodec = $this->getServiceContainer()->getJsonCodec(); |
| 448 |
$stash = new SimpleParsoidOutputStash( $jsonCodec, $cache, 1 ); |
| 449 |
|
| 450 |
$selserContext = $stash->get( $parsoidStashKey ); |
| 451 |
$this->assertNotNull( $selserContext ); |
| 452 |
|
| 453 |
|
| 454 |
$stashedContent = $selserContext->getContent(); |
| 455 |
$this->assertNotNull( $stashedContent ); |
| 456 |
$this->assertInstanceOf( WikitextContent::class, $stashedContent ); |
| 457 |
$this->assertSame( $text, $stashedContent->getText() ); |
| 458 |
} |
| 459 |
|
| 460 |
public function testStashRateLimit() { |
| 461 |
$page = $this->getExistingTestPage( __METHOD__ ); |
| 462 |
|
| 463 |
$authority = $this->newAuthorityWhoCantStash(); |
| 464 |
$helper = $this->newHelper( [], $page, self::PARAM_DEFAULTS, $authority ); |
| 465 |
$helper->setStashingEnabled( true ); |
| 466 |
|
| 467 |
$this->expectException( LocalizedHttpException::class ); |
| 468 |
$this->expectExceptionCode( 429 ); |
| 469 |
$helper->getHtml(); |
| 470 |
} |
| 471 |
|
| 472 |
public function testInteractionOfStashAndFlavor() { |
| 473 |
$page = $this->getExistingTestPage( __METHOD__ ); |
| 474 |
|
| 475 |
$authority = $this->newAuthorityWhoCantStash(); |
| 476 |
$helper = $this->newHelper( [], $page, self::PARAM_DEFAULTS, $authority ); |
| 477 |
|
| 478 |
|
| 479 |
$this->assertSame( 'view', $helper->getFlavor() ); |
| 480 |
|
| 481 |
|
| 482 |
$helper->setFlavor( 'edit' ); |
| 483 |
$this->assertSame( 'edit', $helper->getFlavor() ); |
| 484 |
|
| 485 |
|
| 486 |
$helper->setStashingEnabled( true ); |
| 487 |
$this->assertSame( 'stash', $helper->getFlavor() ); |
| 488 |
|
| 489 |
|
| 490 |
$helper->setStashingEnabled( false ); |
| 491 |
$this->assertSame( 'view', $helper->getFlavor() ); |
| 492 |
|
| 493 |
|
| 494 |
$helper->setStashingEnabled( true ); |
| 495 |
$helper->setFlavor( 'view' ); |
| 496 |
$this->assertSame( 'stash', $helper->getFlavor() ); |
| 497 |
} |
| 498 |
|
| 499 |
public function testGetHtmlFragment() { |
| 500 |
$page = $this->getExistingTestPage(); |
| 501 |
|
| 502 |
$expectedHtml = '<html><body><section data-mw-section-id=0><p>Contents</p></section></body></html>'; |
| 503 |
$helper = $this->newHelper( [ |
| 504 |
'ParsoidParserFactory' => $this->newMockParsoidParserFactory( [ |
| 505 |
'expectedHtml' => $expectedHtml |
| 506 |
] ), |
| 507 |
'expectedHtml' => $expectedHtml, |
| 508 |
|
| 509 |
], $page, self::PARAM_DEFAULTS, $this->newAuthority() ); |
| 510 |
$helper->setFlavor( 'fragment' ); |
| 511 |
$helper->setContentSource( 'Contents', CONTENT_MODEL_WIKITEXT ); |
| 512 |
|
| 513 |
$htmlresult = $helper->getHtml()->getRawText(); |
| 514 |
|
| 515 |
$this->assertStringContainsString( 'fragment', $helper->getETag() ); |
| 516 |
$this->assertStringContainsString( '<p>Contents</p>', $htmlresult ); |
| 517 |
$this->assertStringNotContainsString( "<body", $htmlresult ); |
| 518 |
$this->assertStringNotContainsString( "<section", $htmlresult ); |
| 519 |
} |
| 520 |
|
| 521 |
public function testGetHtmlForEdit() { |
| 522 |
$page = $this->getExistingTestPage(); |
| 523 |
|
| 524 |
$helper = $this->newHelper( [], $page, self::PARAM_DEFAULTS, $this->newAuthority() ); |
| 525 |
$helper->setContentSource( 'hello {{world}}', CONTENT_MODEL_WIKITEXT ); |
| 526 |
$helper->setFlavor( 'edit' ); |
| 527 |
|
| 528 |
$htmlresult = $helper->getHtml()->getRawText(); |
| 529 |
|
| 530 |
$this->assertStringContainsString( 'edit', $helper->getETag() ); |
| 531 |
|
| 532 |
$this->assertStringContainsString( 'hello', $htmlresult ); |
| 533 |
$this->assertStringContainsString( 'data-parsoid=', $htmlresult ); |
| 534 |
$this->assertStringContainsString( '"dsr":', $htmlresult ); |
| 535 |
} |
| 536 |
|
| 537 |
|
| 538 |
|
| 539 |
|
| 540 |
public function testETagLastModified( $revRef ) { |
| 541 |
[ $page, $revisions ] = $this->getExistingPageWithRevisions( __METHOD__ ); |
| 542 |
$rev = $revRef ? $revisions[ $revRef ] : null; |
| 543 |
|
| 544 |
$cache = new HashBagOStuff(); |
| 545 |
|
| 546 |
|
| 547 |
$helper = $this->newHelper( [ 'cache' => $cache ], $page, self::PARAM_DEFAULTS, $this->newAuthority(), $rev ); |
| 548 |
|
| 549 |
|
| 550 |
$pout = $helper->getHtml(); |
| 551 |
|
| 552 |
$renderId = ParsoidRenderID::newFromParserOutput( $pout ); |
| 553 |
$lastModified = $pout->getCacheTime(); |
| 554 |
|
| 555 |
if ( $rev ) { |
| 556 |
$this->assertSame( $rev->getId(), $helper->getRevisionId() ); |
| 557 |
} else { |
| 558 |
|
| 559 |
$this->assertSame( 0, $helper->getRevisionId() ); |
| 560 |
} |
| 561 |
|
| 562 |
|
| 563 |
$this->assertStringContainsString( $renderId->getKey(), $helper->getETag() ); |
| 564 |
$this->assertSame( |
| 565 |
MWTimestamp::convert( TS_MW, $lastModified ), |
| 566 |
MWTimestamp::convert( TS_MW, $helper->getLastModified() ) |
| 567 |
); |
| 568 |
|
| 569 |
|
| 570 |
$now = MWTimestamp::convert( TS_UNIX, self::TIMESTAMP_LATER ) + 10000; |
| 571 |
MWTimestamp::setFakeTime( $now ); |
| 572 |
$this->assertTrue( |
| 573 |
$page->getTitle()->invalidateCache( MWTimestamp::convert( TS_MW, $now ) ), |
| 574 |
'Cannot invalidate cache' |
| 575 |
); |
| 576 |
DeferredUpdates::doUpdates(); |
| 577 |
$page->clear(); |
| 578 |
|
| 579 |
$helper = $this->newHelper( [ 'cache' => $cache ], $page, self::PARAM_DEFAULTS, $this->newAuthority(), $rev ); |
| 580 |
|
| 581 |
$this->assertStringNotContainsString( $renderId->getKey(), $helper->getETag() ); |
| 582 |
$this->assertSame( |
| 583 |
MWTimestamp::convert( TS_MW, $now ), |
| 584 |
MWTimestamp::convert( TS_MW, $helper->getLastModified() ) |
| 585 |
); |
| 586 |
} |
| 587 |
|
| 588 |
|
| 589 |
|
| 590 |
|
| 591 |
|
| 592 |
public function testETagLastModifiedWithPageIdentity() { |
| 593 |
[ $fakePage, $fakeRevision ] = $this->getNonExistingPageWithFakeRevision( __METHOD__ ); |
| 594 |
$pp = $this->createMock( ParsoidParser::class ); |
| 595 |
$pp->expects( $this->once() ) |
| 596 |
->method( 'parse' ) |
| 597 |
->willReturnCallback( function ( |
| 598 |
$text, |
| 599 |
PageReference $page, |
| 600 |
ParserOptions $parserOpts, |
| 601 |
bool $linestart = true, |
| 602 |
bool $clearState = true, |
| 603 |
?int $revId = null |
| 604 |
) use ( $fakePage, $fakeRevision ) { |
| 605 |
self::assertTrue( $page->isSamePageAs( $fakePage ), '$page and $fakePage should be the same' ); |
| 606 |
self::assertSame( $revId, $fakeRevision->getId(), '$rev and $fakeRevision should be the same' ); |
| 607 |
|
| 608 |
$html = $this->getMockHtml( $fakeRevision ); |
| 609 |
$pout = $this->makeParserOutput( $parserOpts, $html, $fakeRevision, $page ); |
| 610 |
return $pout; |
| 611 |
} ); |
| 612 |
$options['ParsoidParser'] = $pp; |
| 613 |
$options['ParsoidParserFactory'] = $this->newMockParsoidParserFactory( |
| 614 |
$options |
| 615 |
); |
| 616 |
|
| 617 |
$helper = $this->newHelper( $options, $fakePage, self::PARAM_DEFAULTS, $this->newAuthority() ); |
| 618 |
$helper->setRevision( $fakeRevision ); |
| 619 |
|
| 620 |
$this->assertNull( $helper->getRevisionId() ); |
| 621 |
|
| 622 |
$pout = $helper->getHtml(); |
| 623 |
$renderId = ParsoidRenderID::newFromParserOutput( $pout ); |
| 624 |
$lastModified = $pout->getCacheTime(); |
| 625 |
|
| 626 |
$this->assertStringContainsString( $renderId->getKey(), $helper->getETag() ); |
| 627 |
$this->assertSame( |
| 628 |
MWTimestamp::convert( TS_MW, $lastModified ), |
| 629 |
MWTimestamp::convert( TS_MW, $helper->getLastModified() ) |
| 630 |
); |
| 631 |
} |
| 632 |
|
| 633 |
public static function provideETagSuffix() { |
| 634 |
yield 'stash + html' => |
| 635 |
[ [ 'stash' => true ], 'html', '/stash/html' ]; |
| 636 |
|
| 637 |
yield 'view html' => |
| 638 |
[ [], 'html', '/view/html' ]; |
| 639 |
|
| 640 |
yield 'stash + wrapped' => |
| 641 |
[ [ 'stash' => true ], 'with_html', '/stash/with_html' ]; |
| 642 |
|
| 643 |
yield 'view wrapped' => |
| 644 |
[ [], 'with_html', '/view/with_html' ]; |
| 645 |
|
| 646 |
yield 'stash' => |
| 647 |
[ [ 'stash' => true ], '', '/stash' ]; |
| 648 |
|
| 649 |
yield 'flavor = fragment' => |
| 650 |
[ [ 'flavor' => 'fragment' ], '', '/fragment' ]; |
| 651 |
|
| 652 |
yield 'flavor = fragment + stash = true: stash should take over' => |
| 653 |
[ [ 'stash' => true, 'flavor' => 'fragment' ], '', '/stash' ]; |
| 654 |
|
| 655 |
yield 'nothing' => |
| 656 |
[ [], '', '/view' ]; |
| 657 |
} |
| 658 |
|
| 659 |
|
| 660 |
|
| 661 |
|
| 662 |
public function testETagSuffix( array $params, string $mode, string $suffix ) { |
| 663 |
$page = $this->getExistingTestPage( __METHOD__ ); |
| 664 |
|
| 665 |
$cache = new HashBagOStuff(); |
| 666 |
|
| 667 |
|
| 668 |
$helper = $this->newHelper( [ |
| 669 |
'cache' => $cache, |
| 670 |
], $page, $params + self::PARAM_DEFAULTS, $this->newAuthority() ); |
| 671 |
if ( ( $params['flavor'] ?? null ) === 'fragment' ) { |
| 672 |
$helper->setContentSource( "fragment test", CONTENT_MODEL_WIKITEXT ); |
| 673 |
} |
| 674 |
|
| 675 |
$etag = $helper->getETag( $mode ); |
| 676 |
$etag = trim( $etag, '"' ); |
| 677 |
$this->assertStringEndsWith( $suffix, $etag ); |
| 678 |
} |
| 679 |
|
| 680 |
public static function provideVariantConversionLanguage() { |
| 681 |
yield 'simple code' |
| 682 |
=> [ 'en', new Bcp47CodeValue( 'en' ) ]; |
| 683 |
|
| 684 |
yield 'code with dashes' |
| 685 |
=> [ 'en-x-piglatin', new Bcp47CodeValue( 'en-x-piglatin' ) ]; |
| 686 |
|
| 687 |
yield 'obsolete alias' |
| 688 |
=> [ 'zh-min-nan', new Bcp47CodeValue( 'nan' ) ]; |
| 689 |
|
| 690 |
yield 'obsolete alias in source language' |
| 691 |
=> [ 'en', new Bcp47CodeValue( 'en' ), |
| 692 |
'zh-min-nan', new Bcp47CodeValue( 'nan' ) ]; |
| 693 |
|
| 694 |
yield 'target and source given as objects' |
| 695 |
=> [ new Bcp47CodeValue( 'x y z' ), new Bcp47CodeValue( 'x y z' ), |
| 696 |
new Bcp47CodeValue( 'a,b,c' ), new Bcp47CodeValue( 'a,b,c' ) ]; |
| 697 |
|
| 698 |
yield 'complex accept-language header (T350852)' |
| 699 |
=> [ 'da, en-gb;q=0.8, en;q=0.7', new Bcp47CodeValue( 'da' ) ]; |
| 700 |
} |
| 701 |
|
| 702 |
|
| 703 |
|
| 704 |
|
| 705 |
public function testSetVariantConversionLanguage( |
| 706 |
$target, |
| 707 |
Bcp47Code $expectedTarget, |
| 708 |
$source = null, |
| 709 |
?Bcp47Code $expectedSource = null |
| 710 |
) { |
| 711 |
$converter = $this->createNoOpMock( |
| 712 |
LanguageVariantConverter::class, |
| 713 |
[ 'convertPageBundleVariant' ] |
| 714 |
); |
| 715 |
|
| 716 |
|
| 717 |
$converter->expects( $this->once() ) |
| 718 |
->method( 'convertPageBundleVariant' )->with( |
| 719 |
$this->anything(), |
| 720 |
$expectedTarget, |
| 721 |
$expectedSource |
| 722 |
); |
| 723 |
|
| 724 |
$transformFactory = $this->createNoOpMock( |
| 725 |
HtmlTransformFactory::class, |
| 726 |
[ 'getLanguageVariantConverter' ] |
| 727 |
); |
| 728 |
$transformFactory->method( 'getLanguageVariantConverter' ) |
| 729 |
->willReturn( $converter ); |
| 730 |
|
| 731 |
$this->overrideConfigValue( MainConfigNames::UsePigLatinVariant, true ); |
| 732 |
$page = $this->getExistingTestPage( __METHOD__ ); |
| 733 |
|
| 734 |
$helper = $this->newHelper( [ 'HtmlTransformFactory' => $transformFactory ], $page, self::PARAM_DEFAULTS, $this->newAuthority() ); |
| 735 |
|
| 736 |
|
| 737 |
$helper->setVariantConversionLanguage( $target, $source ); |
| 738 |
|
| 739 |
|
| 740 |
$this->assertStringEndsWith( "+lang:$expectedTarget\"", $helper->getETag() ); |
| 741 |
|
| 742 |
$helper->getPageBundle(); |
| 743 |
} |
| 744 |
|
| 745 |
public static function provideHandlesParsoidError() { |
| 746 |
yield 'ClientError' => [ |
| 747 |
new ClientError( 'TEST_TEST' ), |
| 748 |
new LocalizedHttpException( |
| 749 |
new MessageValue( 'rest-html-backend-error' ), |
| 750 |
400, |
| 751 |
[ |
| 752 |
'reason' => 'TEST_TEST' |
| 753 |
] |
| 754 |
) |
| 755 |
]; |
| 756 |
yield 'ResourceLimitExceededException' => [ |
| 757 |
new ResourceLimitExceededException( 'TEST_TEST' ), |
| 758 |
new LocalizedHttpException( |
| 759 |
new MessageValue( 'rest-resource-limit-exceeded' ), |
| 760 |
413, |
| 761 |
[ |
| 762 |
'reason' => 'TEST_TEST' |
| 763 |
] |
| 764 |
) |
| 765 |
]; |
| 766 |
yield 'RevisionAccessException' => [ |
| 767 |
new RevisionAccessException( 'TEST_TEST' ), |
| 768 |
new LocalizedHttpException( |
| 769 |
new MessageValue( 'rest-nonexistent-title' ), |
| 770 |
404, |
| 771 |
[ |
| 772 |
'reason' => 'TEST_TEST' |
| 773 |
] |
| 774 |
) |
| 775 |
]; |
| 776 |
} |
| 777 |
|
| 778 |
private function newMockParsoidParserFactory( array $options = [] ) { |
| 779 |
if ( isset( $options['Parsoid'] ) ) { |
| 780 |
$mockParsoid = $options['Parsoid']; |
| 781 |
} else { |
| 782 |
$mockParsoid = $this->createNoOpMock( Parsoid::class, [ |
| 783 |
'wikitext2html', |
| 784 |
] ); |
| 785 |
$mockParsoid |
| 786 |
->method( 'wikitext2html' ) |
| 787 |
->willReturn( new PageBundle( |
| 788 |
$options['expectedHtml'] ?? 'This is HTML' |
| 789 |
) ); |
| 790 |
} |
| 791 |
|
| 792 |
|
| 793 |
if ( isset( $options['ParsoidParser'] ) ) { |
| 794 |
$parsoidParser = $options['ParsoidParser']; |
| 795 |
} else { |
| 796 |
$services = $this->getServiceContainer(); |
| 797 |
$parsoidParser = new ParsoidParser( |
| 798 |
$mockParsoid, |
| 799 |
$services->getParsoidPageConfigFactory(), |
| 800 |
$services->getLanguageConverterFactory(), |
| 801 |
$services->getParserFactory(), |
| 802 |
$services->getGlobalIdGenerator() |
| 803 |
); |
| 804 |
} |
| 805 |
|
| 806 |
|
| 807 |
|
| 808 |
$mockParsoidParserFactory = $this->createNoOpMock( ParsoidParserFactory::class, [ 'create' ] ); |
| 809 |
$mockParsoidParserFactory->method( 'create' )->willReturn( $parsoidParser ); |
| 810 |
return $mockParsoidParserFactory; |
| 811 |
} |
| 812 |
|
| 813 |
private function resetServicesWithMockedParsoid( ?Parsoid $mockParsoid = null ): void { |
| 814 |
$services = $this->getServiceContainer(); |
| 815 |
$mockParsoidParserFactory = $this->newMockParsoidParserFactory( [ |
| 816 |
'Parsoid' => $mockParsoid, |
| 817 |
] ); |
| 818 |
$this->resetServicesWithMockedParsoidParserFactory( $mockParsoidParserFactory ); |
| 819 |
} |
| 820 |
|
| 821 |
private function resetServicesWithMockedParsoidParserFactory( ?ParsoidParserFactory $mockParsoidParserFactory = null ): void { |
| 822 |
$this->setService( 'ParsoidParserFactory', $mockParsoidParserFactory ); |
| 823 |
} |
| 824 |
|
| 825 |
private function newRealParserOutputAccess( $overrides = [] ): array { |
| 826 |
$services = $this->getServiceContainer(); |
| 827 |
|
| 828 |
if ( isset( $overrides['parserCache'] ) ) { |
| 829 |
$parserCache = $overrides['parserCache']; |
| 830 |
} else { |
| 831 |
$parserCache = $this->createNoOpMock( |
| 832 |
ParserCache::class, |
| 833 |
[ 'get', 'save', 'makeParserOutputKey', ] |
| 834 |
); |
| 835 |
$parserCache->method( 'get' )->willReturn( false ); |
| 836 |
$parserCache->method( 'save' )->willReturn( null ); |
| 837 |
$parserCache->method( 'makeParserOutputKey' )->willReturn( 'test-key' ); |
| 838 |
} |
| 839 |
|
| 840 |
if ( isset( $overrides['revisionCache'] ) ) { |
| 841 |
$revisionCache = $overrides['revisionCache']; |
| 842 |
} else { |
| 843 |
$revisionCache = $this->createNoOpMock( RevisionOutputCache::class, [ 'get', 'save' ] ); |
| 844 |
$revisionCache->method( 'get' )->willReturn( false ); |
| 845 |
$revisionCache->method( 'save' )->willReturn( null ); |
| 846 |
} |
| 847 |
|
| 848 |
$parserCacheFactory = $this->createNoOpMock( |
| 849 |
ParserCacheFactory::class, |
| 850 |
[ 'getParserCache', 'getRevisionOutputCache' ] |
| 851 |
); |
| 852 |
$parserCacheFactory->method( 'getParserCache' )->willReturn( $parserCache ); |
| 853 |
$parserCacheFactory->method( 'getRevisionOutputCache' )->willReturn( $revisionCache ); |
| 854 |
$parserOutputAccess = new ParserOutputAccess( |
| 855 |
$parserCacheFactory, |
| 856 |
$services->getRevisionLookup(), |
| 857 |
$services->getRevisionRenderer(), |
| 858 |
StatsFactory::newNull(), |
| 859 |
$services->getDBLoadBalancerFactory(), |
| 860 |
$services->getChronologyProtector(), |
| 861 |
$this->getLoggerSpi(), |
| 862 |
$services->getWikiPageFactory(), |
| 863 |
$services->getTitleFormatter() |
| 864 |
); |
| 865 |
return [ |
| 866 |
'ParserOutputAccess' => $parserOutputAccess, |
| 867 |
]; |
| 868 |
} |
| 869 |
|
| 870 |
|
| 871 |
|
| 872 |
|
| 873 |
public function testHandlesParsoidError( |
| 874 |
Exception $parsoidException, |
| 875 |
Exception $expectedException |
| 876 |
) { |
| 877 |
$page = $this->getExistingTestPage( __METHOD__ ); |
| 878 |
|
| 879 |
$parsoid = $this->createNoOpMock( Parsoid::class, [ 'wikitext2html' ] ); |
| 880 |
$parsoid->method( 'wikitext2html' ) |
| 881 |
->willThrowException( $parsoidException ); |
| 882 |
|
| 883 |
$parserCache = $this->createNoOpMock( ParserCache::class, [ 'get', 'getDirty', 'makeParserOutputKey' ] ); |
| 884 |
$parserCache->method( 'get' )->willReturn( false ); |
| 885 |
$parserCache->method( 'getDirty' )->willReturn( false ); |
| 886 |
$parserCache->expects( $this->atLeastOnce() )->method( 'makeParserOutputKey' ); |
| 887 |
|
| 888 |
$this->resetServicesWithMockedParsoid( $parsoid ); |
| 889 |
$access = $this->newRealParserOutputAccess( [ 'parserCache' => $parserCache ] ); |
| 890 |
|
| 891 |
$helper = $this->newHelper( $access, $page, self::PARAM_DEFAULTS, $this->newAuthority() ); |
| 892 |
|
| 893 |
$this->expectExceptionObject( $expectedException ); |
| 894 |
$helper->getHtml(); |
| 895 |
} |
| 896 |
|
| 897 |
public static function provideParsoidOutputStatus() { |
| 898 |
yield 'parsoid-client-error' => [ |
| 899 |
Status::newFatal( 'parsoid-client-error' ), |
| 900 |
new LocalizedHttpException( |
| 901 |
new MessageValue( 'rest-html-backend-error' ), |
| 902 |
400, |
| 903 |
[ |
| 904 |
'reason' => 'parsoid-client-error' |
| 905 |
] |
| 906 |
) |
| 907 |
]; |
| 908 |
yield 'parsoid-resource-limit-exceeded' => [ |
| 909 |
Status::newFatal( 'parsoid-resource-limit-exceeded' ), |
| 910 |
new LocalizedHttpException( |
| 911 |
new MessageValue( 'rest-resource-limit-exceeded' ), |
| 912 |
413, |
| 913 |
[ |
| 914 |
'reason' => 'TEST_TEST' |
| 915 |
] |
| 916 |
) |
| 917 |
]; |
| 918 |
yield 'missing-revision-permission' => [ |
| 919 |
Status::newFatal( 'missing-revision-permission' ), |
| 920 |
new LocalizedHttpException( |
| 921 |
new MessageValue( 'rest-permission-denied-revision' ), |
| 922 |
403, |
| 923 |
[ |
| 924 |
'reason' => 'missing-revision-permission' |
| 925 |
] |
| 926 |
) |
| 927 |
]; |
| 928 |
yield 'parsoid-revision-access' => [ |
| 929 |
Status::newFatal( 'parsoid-revision-access' ), |
| 930 |
new LocalizedHttpException( |
| 931 |
new MessageValue( 'rest-specified-revision-unavailable' ), |
| 932 |
404, |
| 933 |
[ |
| 934 |
'reason' => 'parsoid-revision-access' |
| 935 |
] |
| 936 |
) |
| 937 |
]; |
| 938 |
} |
| 939 |
|
| 940 |
|
| 941 |
|
| 942 |
|
| 943 |
public function testParsoidOutputStatus( |
| 944 |
Status $parserOutputStatus, |
| 945 |
Exception $expectedException |
| 946 |
) { |
| 947 |
$page = $this->getExistingTestPage( __METHOD__ ); |
| 948 |
|
| 949 |
$parserAccess = $this->createNoOpMock( ParserOutputAccess::class, [ 'getParserOutput' ] ); |
| 950 |
$parserAccess->method( 'getParserOutput' ) |
| 951 |
->willReturn( $parserOutputStatus ); |
| 952 |
|
| 953 |
$helper = $this->newHelper( [ |
| 954 |
'ParserOutputAccess' => $parserAccess, |
| 955 |
], $page, self::PARAM_DEFAULTS, $this->newAuthority() ); |
| 956 |
|
| 957 |
$this->expectExceptionObject( $expectedException ); |
| 958 |
$helper->getHtml(); |
| 959 |
} |
| 960 |
|
| 961 |
public function testWillUseParserCache() { |
| 962 |
$page = $this->getExistingTestPage( __METHOD__ ); |
| 963 |
|
| 964 |
|
| 965 |
|
| 966 |
$page = PageIdentityValue::localIdentity( $page->getId(), $page->getNamespace(), $page->getDBkey() ); |
| 967 |
|
| 968 |
|
| 969 |
$parserCache = $this->createNoOpMock( ParserCache::class, [ 'get', 'getDirty', 'save', 'makeParserOutputKey' ] ); |
| 970 |
$parserCache->expects( $this->once() )->method( 'get' )->willReturn( false ); |
| 971 |
$parserCache->method( 'getDirty' )->willReturn( false ); |
| 972 |
$parserCache->expects( $this->once() )->method( 'save' ); |
| 973 |
$parserCache->expects( $this->atLeastOnce() )->method( 'makeParserOutputKey' ); |
| 974 |
|
| 975 |
$this->resetServicesWithMockedParsoid(); |
| 976 |
$access = $this->newRealParserOutputAccess( [ |
| 977 |
'parserCache' => $parserCache, |
| 978 |
'revisionCache' => $this->createNoOpMock( RevisionOutputCache::class ) |
| 979 |
] ); |
| 980 |
|
| 981 |
$helper = $this->newHelper( $access, $page, self::PARAM_DEFAULTS, $this->newAuthority() ); |
| 982 |
|
| 983 |
$helper->getHtml(); |
| 984 |
} |
| 985 |
|
| 986 |
public function testGetParserOutputWithLanguageOverride() { |
| 987 |
[ $page, [ 'latest' => $revision ] ] = $this->getExistingPageWithRevisions( __METHOD__, '{{PAGELANGUAGE}}' ); |
| 988 |
|
| 989 |
$options = [ |
| 990 |
|
| 991 |
'ParserOutputAccess' => $this->getServiceContainer()->getParserOutputAccess(), |
| 992 |
]; |
| 993 |
$helper = $this->newHelper( $options, $page, [], $this->newAuthority(), $revision ); |
| 994 |
$helper->setPageLanguage( 'ar' ); |
| 995 |
|
| 996 |
|
| 997 |
$this->assertSame( 'ar', $helper->getHtmlOutputContentLanguage()->toBcp47Code() ); |
| 998 |
|
| 999 |
|
| 1000 |
$output = $helper->getHtml(); |
| 1001 |
$html = $output->getRawText(); |
| 1002 |
$this->assertStringContainsString( 'lang="ar"', $html ); |
| 1003 |
$this->assertStringContainsString( '>ar<', $html ); |
| 1004 |
|
| 1005 |
|
| 1006 |
$helper = $this->newHelper( $options, $page, [], $this->newAuthority(), $revision ); |
| 1007 |
$helper->setPageLanguage( 'en' ); |
| 1008 |
$this->assertSame( 'en', $helper->getHtmlOutputContentLanguage()->toBcp47Code() ); |
| 1009 |
$output = $helper->getHtml(); |
| 1010 |
$html = $output->getRawText(); |
| 1011 |
$this->assertStringContainsString( 'lang="en"', $html ); |
| 1012 |
$this->assertStringContainsString( '>en<', $html ); |
| 1013 |
} |
| 1014 |
|
| 1015 |
public function testGetParserOutputWithRedundantPageLanguage() { |
| 1016 |
$poa = $this->createMock( ParserOutputAccess::class ); |
| 1017 |
$poa->expects( $this->once() ) |
| 1018 |
->method( 'getParserOutput' ) |
| 1019 |
->willReturnCallback( function ( |
| 1020 |
PageIdentity $page, |
| 1021 |
ParserOptions $parserOpts, |
| 1022 |
$revision = null, |
| 1023 |
int $options = 0 |
| 1024 |
) { |
| 1025 |
$usedOptions = [ 'targetLanguage' ]; |
| 1026 |
self::assertNull( $parserOpts->getTargetLanguage(), 'No target language should be set in ParserOptions' ); |
| 1027 |
self::assertTrue( $parserOpts->isSafeToCache( $usedOptions ) ); |
| 1028 |
|
| 1029 |
$html = $this->getMockHtml( $revision ); |
| 1030 |
$pout = $this->makeParserOutput( $parserOpts, $html, $revision, $page ); |
| 1031 |
return Status::newGood( $pout ); |
| 1032 |
} ); |
| 1033 |
|
| 1034 |
$page = $this->getExistingTestPage(); |
| 1035 |
|
| 1036 |
$helper = $this->newHelper( [ 'ParserOutputAccess' => $poa ], $page, [], $this->newAuthority() ); |
| 1037 |
|
| 1038 |
|
| 1039 |
$pageLanguage = $page->getTitle()->getPageLanguage(); |
| 1040 |
$helper->setPageLanguage( $pageLanguage ); |
| 1041 |
|
| 1042 |
|
| 1043 |
$helper->getHtml(); |
| 1044 |
} |
| 1045 |
|
| 1046 |
public function provideInit() { |
| 1047 |
$page = PageIdentityValue::localIdentity( 7, NS_MAIN, 'Köfte' ); |
| 1048 |
$authority = $this->createNoOpMock( Authority::class ); |
| 1049 |
|
| 1050 |
yield 'Minimal' => [ |
| 1051 |
$page, |
| 1052 |
[], |
| 1053 |
$authority, |
| 1054 |
null, |
| 1055 |
[ |
| 1056 |
'page' => $page, |
| 1057 |
'authority' => $authority, |
| 1058 |
'revisionOrId' => null, |
| 1059 |
'stash' => false, |
| 1060 |
'flavor' => 'view', |
| 1061 |
] |
| 1062 |
]; |
| 1063 |
|
| 1064 |
$rev = $this->createNoOpMock( RevisionRecord::class, [ 'getId' ] ); |
| 1065 |
$rev->method( 'getId' )->willReturn( 7 ); |
| 1066 |
|
| 1067 |
yield 'Revision and Language' => [ |
| 1068 |
$page, |
| 1069 |
[], |
| 1070 |
$authority, |
| 1071 |
$rev, |
| 1072 |
[ |
| 1073 |
'revisionOrId' => $rev, |
| 1074 |
] |
| 1075 |
]; |
| 1076 |
|
| 1077 |
yield 'revid and stash' => [ |
| 1078 |
$page, |
| 1079 |
[ 'stash' => true ], |
| 1080 |
$authority, |
| 1081 |
8, |
| 1082 |
[ |
| 1083 |
'stash' => true, |
| 1084 |
'flavor' => 'stash', |
| 1085 |
'revisionOrId' => 8, |
| 1086 |
] |
| 1087 |
]; |
| 1088 |
|
| 1089 |
yield 'flavor' => [ |
| 1090 |
$page, |
| 1091 |
[ 'flavor' => 'fragment' ], |
| 1092 |
$authority, |
| 1093 |
8, |
| 1094 |
[ |
| 1095 |
'flavor' => 'fragment', |
| 1096 |
] |
| 1097 |
]; |
| 1098 |
|
| 1099 |
yield 'stash winds over flavor' => [ |
| 1100 |
$page, |
| 1101 |
[ 'flavor' => 'fragment', 'stash' => true ], |
| 1102 |
$authority, |
| 1103 |
8, |
| 1104 |
[ |
| 1105 |
'flavor' => 'stash', |
| 1106 |
] |
| 1107 |
]; |
| 1108 |
} |
| 1109 |
|
| 1110 |
|
| 1111 |
|
| 1112 |
|
| 1113 |
|
| 1114 |
|
| 1115 |
|
| 1116 |
|
| 1117 |
|
| 1118 |
|
| 1119 |
|
| 1120 |
|
| 1121 |
|
| 1122 |
public function testInit( |
| 1123 |
PageIdentity $page, |
| 1124 |
array $parameters, |
| 1125 |
Authority $authority, |
| 1126 |
$revision, |
| 1127 |
array $expected |
| 1128 |
) { |
| 1129 |
$helper = $this->newHelper( [], $page, $parameters, $authority, $revision ); |
| 1130 |
|
| 1131 |
$wrapper = TestingAccessWrapper::newFromObject( $helper ); |
| 1132 |
foreach ( $expected as $name => $value ) { |
| 1133 |
$this->assertSame( $value, $wrapper->$name ); |
| 1134 |
} |
| 1135 |
} |
| 1136 |
|
| 1137 |
|
| 1138 |
|
| 1139 |
|
| 1140 |
public function testPutHeaders( ?string $targetLanguage, bool $setContentLanguageHeader ) { |
| 1141 |
$this->overrideConfigValue( MainConfigNames::UsePigLatinVariant, true ); |
| 1142 |
$page = $this->getExistingTestPage( __METHOD__ ); |
| 1143 |
$expectedCalls = []; |
| 1144 |
|
| 1145 |
$helper = $this->newHelper( [], $page, self::PARAM_DEFAULTS, $this->newAuthority() ); |
| 1146 |
|
| 1147 |
if ( $targetLanguage ) { |
| 1148 |
$helper->setVariantConversionLanguage( new Bcp47CodeValue( $targetLanguage ) ); |
| 1149 |
$expectedCalls['addHeader'] = [ [ 'Vary', 'Accept-Language' ] ]; |
| 1150 |
} |
| 1151 |
|
| 1152 |
if ( $setContentLanguageHeader ) { |
| 1153 |
$expectedCalls['setHeader'][] = [ 'Content-Language', $targetLanguage ?: 'en' ]; |
| 1154 |
|
| 1155 |
$version = Parsoid::defaultHTMLVersion(); |
| 1156 |
$expectedCalls['setHeader'][] = [ |
| 1157 |
'Content-Type', |
| 1158 |
'text/html; charset=utf-8; profile="https://www.mediawiki.org/wiki/Specs/HTML/' . $version . '"', |
| 1159 |
]; |
| 1160 |
} |
| 1161 |
|
| 1162 |
$responseInterface = $this->getResponseInterfaceMock( $expectedCalls ); |
| 1163 |
$helper->putHeaders( $responseInterface, $setContentLanguageHeader ); |
| 1164 |
} |
| 1165 |
|
| 1166 |
public static function providePutHeaders() { |
| 1167 |
yield 'no target variant language' => [ null, true ]; |
| 1168 |
yield 'target language is set but setContentLanguageHeader is false' => [ 'en-x-piglatin', false ]; |
| 1169 |
yield 'target language and setContentLanguageHeader flag is true' => |
| 1170 |
[ 'en-x-piglatin', true ]; |
| 1171 |
} |
| 1172 |
|
| 1173 |
private function getResponseInterfaceMock( array $expectedCalls ) { |
| 1174 |
$responseInterface = $this->createNoOpMock( ResponseInterface::class, array_keys( $expectedCalls ) ); |
| 1175 |
foreach ( $expectedCalls as $method => $arguments ) { |
| 1176 |
$responseInterface |
| 1177 |
->expects( $this->exactly( count( $arguments ) ) ) |
| 1178 |
->method( $method ) |
| 1179 |
->willReturnCallback( function ( ...$actualArgs ) use ( $arguments ) { |
| 1180 |
static $expectedArgs; |
| 1181 |
$expectedArgs ??= $arguments; |
| 1182 |
$this->assertContains( $actualArgs, $expectedArgs ); |
| 1183 |
$argIdx = array_search( $actualArgs, $expectedArgs, true ); |
| 1184 |
unset( $expectedArgs[$argIdx] ); |
| 1185 |
} ); |
| 1186 |
} |
| 1187 |
|
| 1188 |
return $responseInterface; |
| 1189 |
} |
| 1190 |
|
| 1191 |
public static function provideFlavorsForBadModelOutput() { |
| 1192 |
yield 'view' => [ 'view' ]; |
| 1193 |
yield 'edit' => [ 'edit' ]; |
| 1194 |
|
| 1195 |
|
| 1196 |
|
| 1197 |
|
| 1198 |
} |
| 1199 |
|
| 1200 |
|
| 1201 |
|
| 1202 |
|
| 1203 |
public function testNonParsoidOutput( string $flavor ) { |
| 1204 |
$this->resetServicesWithMockedParsoid(); |
| 1205 |
|
| 1206 |
$page = $this->getNonexistingTestPage( __METHOD__ ); |
| 1207 |
$this->editPage( $page, new CssContent( '"not wikitext"' ) ); |
| 1208 |
|
| 1209 |
$helper = $this->newHelper( [ |
| 1210 |
'cache' => new HashBagOStuff(), |
| 1211 |
] + $this->newRealParserOutputAccess(), $page, self::PARAM_DEFAULTS, $this->newAuthority() ); |
| 1212 |
$helper->setFlavor( $flavor ); |
| 1213 |
|
| 1214 |
$output = $helper->getHtml(); |
| 1215 |
$this->assertStringContainsString( 'not wikitext', $output->getRawText() ); |
| 1216 |
$this->assertNotNull( ParsoidRenderID::newFromParserOutput( $output )->getKey() ); |
| 1217 |
} |
| 1218 |
|
| 1219 |
} |