| 1 |
<?php |
| 2 |
|
| 3 |
namespace MediaWiki\Tests\Rest\Handler\Helper; |
| 4 |
|
| 5 |
use Exception; |
| 6 |
use LogicException; |
| 7 |
use MediaWiki\Content\TextContent; |
| 8 |
use MediaWiki\Content\WikitextContent; |
| 9 |
use MediaWiki\Edit\ParsoidRenderID; |
| 10 |
use MediaWiki\Edit\SelserContext; |
| 11 |
use MediaWiki\MainConfigNames; |
| 12 |
use MediaWiki\MainConfigSchema; |
| 13 |
use MediaWiki\Message\TextFormatter; |
| 14 |
use MediaWiki\Page\PageIdentity; |
| 15 |
use MediaWiki\Page\PageIdentityValue; |
| 16 |
use MediaWiki\Parser\ParserOptions; |
| 17 |
use MediaWiki\Parser\ParserOutput; |
| 18 |
use MediaWiki\Parser\Parsoid\HtmlToContentTransform; |
| 19 |
use MediaWiki\Parser\Parsoid\HtmlTransformFactory; |
| 20 |
use MediaWiki\Parser\Parsoid\PageBundleParserOutputConverter; |
| 21 |
use MediaWiki\Rest\Handler\Helper\HtmlInputTransformHelper; |
| 22 |
use MediaWiki\Rest\Handler\Helper\ParsoidFormatHelper; |
| 23 |
use MediaWiki\Rest\HttpException; |
| 24 |
use MediaWiki\Rest\LocalizedHttpException; |
| 25 |
use MediaWiki\Rest\ResponseFactory; |
| 26 |
use MediaWiki\Revision\MutableRevisionRecord; |
| 27 |
use MediaWiki\Revision\RevisionRecord; |
| 28 |
use MediaWiki\Revision\SlotRecord; |
| 29 |
use MediaWikiIntegrationTestCase; |
| 30 |
use PHPUnit\Framework\MockObject\MockObject; |
| 31 |
use Psr\Log\NullLogger; |
| 32 |
use Wikimedia\Bcp47Code\Bcp47Code; |
| 33 |
use Wikimedia\Message\MessageValue; |
| 34 |
use Wikimedia\Parsoid\Core\ClientError; |
| 35 |
use Wikimedia\Parsoid\Core\PageBundle; |
| 36 |
use Wikimedia\Parsoid\Core\ResourceLimitExceededException; |
| 37 |
use Wikimedia\Parsoid\Parsoid; |
| 38 |
use Wikimedia\Parsoid\Utils\ContentUtils; |
| 39 |
use Wikimedia\Stats\BufferingStatsdDataFactory; |
| 40 |
use Wikimedia\Stats\Emitters\NullEmitter; |
| 41 |
use Wikimedia\Stats\StatsCache; |
| 42 |
use Wikimedia\Stats\StatsFactory; |
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
class HtmlInputTransformHelperTest extends MediaWikiIntegrationTestCase { |
| 49 |
private const CACHE_EPOCH = '20001111010101'; |
| 50 |
|
| 51 |
protected function setUp(): void { |
| 52 |
parent::setUp(); |
| 53 |
|
| 54 |
$this->overrideConfigValue( MainConfigNames::CacheEpoch, self::CACHE_EPOCH ); |
| 55 |
} |
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
public function newMockHtmlTransformFactory( $methodOverrides = [] ): HtmlTransformFactory { |
| 63 |
$factory = $this->createNoOpMock( HtmlTransformFactory::class, [ 'getHtmlToContentTransform' ] ); |
| 64 |
|
| 65 |
$factory->method( 'getHtmlToContentTransform' )->willReturnCallback( |
| 66 |
function ( $html ) use ( $methodOverrides ) { |
| 67 |
return $this->newHtmlToContentTransform( $html, $methodOverrides ); |
| 68 |
} |
| 69 |
); |
| 70 |
|
| 71 |
return $factory; |
| 72 |
} |
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
private function newHelper( |
| 87 |
array $transformMethodOverrides = [], |
| 88 |
?StatsFactory $stats = null, |
| 89 |
?PageIdentity $page = null, |
| 90 |
$body = '', |
| 91 |
array $parameters = [], |
| 92 |
?RevisionRecord $originalRevision = null, |
| 93 |
?Bcp47Code $pageLanguage = null |
| 94 |
): HtmlInputTransformHelper { |
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
$stats = $stats ?? StatsFactory::newNull(); |
| 99 |
return new HtmlInputTransformHelper( |
| 100 |
$stats, |
| 101 |
$this->newMockHtmlTransformFactory( $transformMethodOverrides ), |
| 102 |
$this->getServiceContainer()->getParsoidOutputStash(), |
| 103 |
$this->getServiceContainer()->getParserOutputAccess(), |
| 104 |
$this->getServiceContainer()->getPageStore(), |
| 105 |
$this->getServiceContainer()->getRevisionLookup(), |
| 106 |
[], |
| 107 |
$page, |
| 108 |
$body, |
| 109 |
$parameters, |
| 110 |
$originalRevision, |
| 111 |
$pageLanguage |
| 112 |
); |
| 113 |
} |
| 114 |
|
| 115 |
private function getTextFromFile( string $name ): string { |
| 116 |
return trim( file_get_contents( __DIR__ . "/../data/Transform/$name" ) ); |
| 117 |
} |
| 118 |
|
| 119 |
private function getJsonFromFile( string $name ): array { |
| 120 |
$text = $this->getTextFromFile( $name ); |
| 121 |
return json_decode( $text, JSON_OBJECT_AS_ARRAY ); |
| 122 |
} |
| 123 |
|
| 124 |
public function provideRequests() { |
| 125 |
$profileVersion = '2.4.0'; |
| 126 |
$wikitextProfileUri = 'https://www.mediawiki.org/wiki/Specs/wikitext/1.0.0'; |
| 127 |
$htmlProfileUri = 'https://www.mediawiki.org/wiki/Specs/HTML/' . $profileVersion; |
| 128 |
$dataParsoidProfileUri = 'https://www.mediawiki.org/wiki/Specs/data-parsoid/' . $profileVersion; |
| 129 |
|
| 130 |
$wikiTextContentType = "text/plain; charset=utf-8; profile=\"$wikitextProfileUri\""; |
| 131 |
$htmlContentType = "text/html;profile=\"$htmlProfileUri\""; |
| 132 |
$dataParsoidContentType = "application/json;profile=\"$dataParsoidProfileUri\""; |
| 133 |
|
| 134 |
$htmlHeaders = [ |
| 135 |
'content-type' => $htmlContentType, |
| 136 |
]; |
| 137 |
|
| 138 |
|
| 139 |
$htmlContentType999 = 'text/html;profile="https://www.mediawiki.org/wiki/Specs/HTML/999.0.0"'; |
| 140 |
$htmlHeaders999 = [ |
| 141 |
'content-type' => $htmlContentType999, |
| 142 |
]; |
| 143 |
|
| 144 |
|
| 145 |
$html = $this->getTextFromFile( 'MainPage-data-parsoid.html' ); |
| 146 |
$expectedText = [ |
| 147 |
'MediaWiki has been successfully installed', |
| 148 |
'== Getting started ==', |
| 149 |
]; |
| 150 |
|
| 151 |
$params = []; |
| 152 |
$body = [ 'html' => $html ]; |
| 153 |
yield 'should convert html to wikitext' => [ |
| 154 |
$body, |
| 155 |
$params, |
| 156 |
$expectedText, |
| 157 |
]; |
| 158 |
|
| 159 |
|
| 160 |
$params = [ |
| 161 |
'oldid' => 1, |
| 162 |
]; |
| 163 |
$body = [ 'html' => $html ]; |
| 164 |
yield 'should load original wikitext by revision id' => [ |
| 165 |
$body, |
| 166 |
$params, |
| 167 |
$expectedText, |
| 168 |
]; |
| 169 |
|
| 170 |
|
| 171 |
$originalWikitext = $this->getTextFromFile( 'OriginalMainPage.wikitext' ); |
| 172 |
$params = []; |
| 173 |
$body = [ |
| 174 |
'html' => $html, |
| 175 |
'original' => [ |
| 176 |
'wikitext' => [ |
| 177 |
'headers' => [ |
| 178 |
'content-type' => $wikiTextContentType, |
| 179 |
], |
| 180 |
'body' => $originalWikitext, |
| 181 |
] |
| 182 |
] |
| 183 |
]; |
| 184 |
yield 'should accept original wikitext in body' => [ |
| 185 |
$body, |
| 186 |
$params, |
| 187 |
$expectedText, |
| 188 |
]; |
| 189 |
|
| 190 |
|
| 191 |
$originalDataParsoid = $this->getJsonFromFile( 'MainPage-original.data-parsoid' ); |
| 192 |
$params = [ |
| 193 |
'from' => ParsoidFormatHelper::FORMAT_PAGEBUNDLE, |
| 194 |
]; |
| 195 |
$body = [ |
| 196 |
'html' => $html, |
| 197 |
'original' => [ |
| 198 |
'html' => [ |
| 199 |
'headers' => $htmlHeaders, |
| 200 |
'body' => $this->getTextFromFile( 'MainPage-original.html' ), |
| 201 |
], |
| 202 |
'data-parsoid' => [ |
| 203 |
'headers' => [ |
| 204 |
'content-type' => $dataParsoidContentType, |
| 205 |
], |
| 206 |
'body' => $originalDataParsoid |
| 207 |
] |
| 208 |
] |
| 209 |
]; |
| 210 |
yield 'should use original html for selser (default)' => [ |
| 211 |
$body, |
| 212 |
$params, |
| 213 |
$expectedText, |
| 214 |
]; |
| 215 |
|
| 216 |
|
| 217 |
$params = []; |
| 218 |
$body = [ |
| 219 |
'html' => $html, |
| 220 |
'original' => [ |
| 221 |
'html' => [ |
| 222 |
'headers' => [ |
| 223 |
|
| 224 |
|
| 225 |
'content-type' => 'text/html; profile="mediawiki.org/specs/html/1.1.1"', |
| 226 |
], |
| 227 |
'body' => $this->getTextFromFile( 'MainPage-data-parsoid-1.1.1.html' ), |
| 228 |
], |
| 229 |
'data-parsoid' => [ |
| 230 |
'headers' => [ |
| 231 |
'content-type' => $dataParsoidContentType, |
| 232 |
], |
| 233 |
'body' => $originalDataParsoid |
| 234 |
] |
| 235 |
] |
| 236 |
]; |
| 237 |
yield 'should use original html for selser (1.1.1, meta)' => [ |
| 238 |
$body, |
| 239 |
$params, |
| 240 |
$expectedText, |
| 241 |
]; |
| 242 |
|
| 243 |
|
| 244 |
$params = [ |
| 245 |
'from' => ParsoidFormatHelper::FORMAT_PAGEBUNDLE, |
| 246 |
]; |
| 247 |
$body = [ |
| 248 |
'html' => $html, |
| 249 |
'original' => [ |
| 250 |
'html' => [ |
| 251 |
'headers' => [ |
| 252 |
|
| 253 |
'content-type' => 'text/html; profile="mediawiki.org/specs/html/1.1.1"', |
| 254 |
], |
| 255 |
|
| 256 |
'body' => $this->getTextFromFile( 'MainPage-original.html' ), |
| 257 |
], |
| 258 |
'data-parsoid' => [ |
| 259 |
'headers' => [ |
| 260 |
'content-type' => $dataParsoidContentType, |
| 261 |
], |
| 262 |
'body' => $originalDataParsoid |
| 263 |
] |
| 264 |
] |
| 265 |
]; |
| 266 |
yield 'should use original html for selser (1.1.1, headers)' => [ |
| 267 |
$body, |
| 268 |
$params, |
| 269 |
$expectedText, |
| 270 |
]; |
| 271 |
|
| 272 |
|
| 273 |
|
| 274 |
|
| 275 |
$html = '<html><body id="mwAA"><div id="mwBB">Selser test</div></body></html>'; |
| 276 |
$dataParsoid = [ |
| 277 |
'ids' => [ |
| 278 |
'mwAA' => [], |
| 279 |
'mwBB' => [ 'autoInsertedEnd' => true, 'stx' => 'html' ] |
| 280 |
] |
| 281 |
]; |
| 282 |
|
| 283 |
$params = [ |
| 284 |
'oldid' => 1, |
| 285 |
]; |
| 286 |
$body = [ |
| 287 |
'html' => $html, |
| 288 |
'original' => [ |
| 289 |
'html' => [ |
| 290 |
'headers' => $htmlHeaders, |
| 291 |
|
| 292 |
'body' => $html |
| 293 |
], |
| 294 |
'data-parsoid' => [ |
| 295 |
'body' => $dataParsoid, |
| 296 |
] |
| 297 |
] |
| 298 |
]; |
| 299 |
yield 'should use selser, return original wikitext because the HTML didn\'t change' => [ |
| 300 |
$body, |
| 301 |
$params, |
| 302 |
null, |
| 303 |
]; |
| 304 |
|
| 305 |
|
| 306 |
|
| 307 |
$params = [ |
| 308 |
|
| 309 |
'from' => ParsoidFormatHelper::FORMAT_PAGEBUNDLE, |
| 310 |
]; |
| 311 |
$body = [ |
| 312 |
'html' => $html, |
| 313 |
'original' => [ |
| 314 |
'html' => [ |
| 315 |
'headers' => $htmlHeaders, |
| 316 |
|
| 317 |
'body' => $html |
| 318 |
], |
| 319 |
'data-parsoid' => [ |
| 320 |
'body' => $dataParsoid, |
| 321 |
] |
| 322 |
] |
| 323 |
]; |
| 324 |
yield 'Should fallback to non-selective serialization' => [ |
| 325 |
$body, |
| 326 |
$params, |
| 327 |
[ '<div>Selser test' ], |
| 328 |
]; |
| 329 |
|
| 330 |
|
| 331 |
$html = '<html><body id="mwAA"><div id="mwBB">data-parsoid test</div>' . |
| 332 |
'<div id="mwBB">data-parsoid test</div></body></html>'; |
| 333 |
$originalHtml = '<html><body id="mwAA"><div id="mwBB">data-parsoid test</div></body></html>'; |
| 334 |
|
| 335 |
$params = []; |
| 336 |
$body = [ |
| 337 |
'html' => $html, |
| 338 |
'original' => [ |
| 339 |
'html' => [ |
| 340 |
'headers' => $htmlHeaders, |
| 341 |
'body' => $originalHtml |
| 342 |
], |
| 343 |
'data-parsoid' => [ |
| 344 |
'body' => $dataParsoid, |
| 345 |
] |
| 346 |
] |
| 347 |
]; |
| 348 |
yield 'should apply data-parsoid to duplicated ids' => [ |
| 349 |
$body, |
| 350 |
$params, |
| 351 |
[ '<div>data-parsoid test<div>data-parsoid test' ], |
| 352 |
]; |
| 353 |
|
| 354 |
|
| 355 |
$html = '<html><body id="mwAA"><div id="mwBB">data-parsoid test</div>' . |
| 356 |
'<div id="mwBB">data-parsoid test</div></body></html>'; |
| 357 |
$originalHtml = '<html><body id="mwAA"><div id="mwBB">data-parsoid test</div></body></html>'; |
| 358 |
|
| 359 |
$params = [ |
| 360 |
'from' => ParsoidFormatHelper::FORMAT_HTML, |
| 361 |
]; |
| 362 |
$body = [ |
| 363 |
'html' => $html, |
| 364 |
'original' => [ |
| 365 |
'html' => [ |
| 366 |
'headers' => $htmlHeaders, |
| 367 |
'body' => $originalHtml |
| 368 |
], |
| 369 |
'data-parsoid' => [ |
| 370 |
|
| 371 |
|
| 372 |
'body' => $dataParsoid, |
| 373 |
] |
| 374 |
] |
| 375 |
]; |
| 376 |
yield 'should ignore data-parsoid if the input format is not pagebundle' => [ |
| 377 |
$body, |
| 378 |
$params, |
| 379 |
[ '<div>data-parsoid test</div><div>data-parsoid test</div>' ], |
| 380 |
]; |
| 381 |
|
| 382 |
|
| 383 |
$html = '<p about="#mwt1" typeof="mw:Transclusion" id="mwAQ">hi</p>'; |
| 384 |
$originalHtml = '<p about="#mwt1" typeof="mw:Transclusion" id="mwAQ">ho</p>'; |
| 385 |
$dataParsoid = [ 'ids' => [ 'mwAQ' => [ 'pi' => [ [ [ 'k' => '1' ] ] ] ] ] ]; |
| 386 |
$dataMediaWiki = [ |
| 387 |
'ids' => [ |
| 388 |
'mwAQ' => [ |
| 389 |
'parts' => [ [ |
| 390 |
'template' => [ |
| 391 |
'target' => [ 'wt' => '1x', 'href' => './Template:1x' ], |
| 392 |
'params' => [ '1' => [ 'wt' => 'hi' ] ], |
| 393 |
'i' => 0 |
| 394 |
] |
| 395 |
] ] |
| 396 |
] |
| 397 |
] |
| 398 |
]; |
| 399 |
$params = []; |
| 400 |
$body = [ |
| 401 |
'html' => $html, |
| 402 |
'original' => [ |
| 403 |
'html' => [ |
| 404 |
'headers' => $htmlHeaders, |
| 405 |
'body' => $originalHtml, |
| 406 |
], |
| 407 |
'data-parsoid' => [ |
| 408 |
'body' => $dataParsoid, |
| 409 |
], |
| 410 |
'data-mw' => [ |
| 411 |
'body' => $dataMediaWiki, |
| 412 |
], |
| 413 |
], |
| 414 |
]; |
| 415 |
yield 'should apply original data-mw' => [ |
| 416 |
$body, |
| 417 |
$params, |
| 418 |
[ '{{1x|hi}}' ], |
| 419 |
]; |
| 420 |
|
| 421 |
|
| 422 |
$html = '<p about="#mwt1" typeof="mw:Transclusion" data-mw=\'{"parts":[{"template":{"target":{"wt":"1x","href":"./Template:1x"},"params":{"1":{"wt":"hi"}},"i":0}}]}\' id="mwAQ">hi</p>'; |
| 423 |
$originalHtml = '<p about="#mwt1" typeof="mw:Transclusion" id="mwAQ">ho</p>'; |
| 424 |
$dataParsoid = [ 'ids' => [ 'mwAQ' => [ 'pi' => [ [ [ 'k' => '1' ] ] ] ] ] ]; |
| 425 |
$dataMediaWiki = [ 'ids' => [ 'mwAQ' => [] ] ]; |
| 426 |
$params = []; |
| 427 |
$body = [ |
| 428 |
'html' => $html, |
| 429 |
'original' => [ |
| 430 |
'html' => [ |
| 431 |
'headers' => $htmlHeaders, |
| 432 |
'body' => $originalHtml |
| 433 |
], |
| 434 |
'data-parsoid' => [ |
| 435 |
'body' => $dataParsoid, |
| 436 |
], |
| 437 |
'data-mw' => [ |
| 438 |
'body' => $dataMediaWiki, |
| 439 |
], |
| 440 |
] |
| 441 |
]; |
| 442 |
yield 'should give precedence to inline data-mw over original' => [ |
| 443 |
$body, |
| 444 |
$params, |
| 445 |
[ '{{1x|hi}}' ], |
| 446 |
]; |
| 447 |
|
| 448 |
|
| 449 |
$html = '<p about="#mwt1" typeof="mw:Transclusion" id="mwAQ">hi</p>'; |
| 450 |
$originalHtml = '<p about="#mwt1" typeof="mw:Transclusion" id="mwAQ">ho</p>'; |
| 451 |
$dataParsoid = [ 'ids' => [ 'mwAQ' => [ 'pi' => [ [ [ 'k' => '1' ] ] ] ] ] ]; |
| 452 |
$dataMediaWiki = [ 'ids' => [ 'mwAQ' => [] ] ]; |
| 453 |
$dataMediaWikiModified = [ |
| 454 |
'ids' => [ |
| 455 |
'mwAQ' => [ |
| 456 |
'parts' => [ [ |
| 457 |
'template' => [ |
| 458 |
'target' => [ 'wt' => '1x', 'href' => './Template:1x' ], |
| 459 |
'params' => [ '1' => [ 'wt' => 'hi' ] ], |
| 460 |
'i' => 0 |
| 461 |
] |
| 462 |
] ] |
| 463 |
] |
| 464 |
] |
| 465 |
]; |
| 466 |
$params = []; |
| 467 |
$body = [ |
| 468 |
'html' => $html, |
| 469 |
'data-mw' => [ |
| 470 |
'body' => $dataMediaWikiModified, |
| 471 |
], |
| 472 |
'original' => [ |
| 473 |
'html' => [ |
| 474 |
'headers' => $htmlHeaders999, |
| 475 |
'body' => $originalHtml |
| 476 |
], |
| 477 |
'data-parsoid' => [ |
| 478 |
'body' => $dataParsoid, |
| 479 |
], |
| 480 |
'data-mw' => [ |
| 481 |
'body' => $dataMediaWiki, |
| 482 |
], |
| 483 |
] |
| 484 |
]; |
| 485 |
yield 'should not apply original data-mw if modified is supplied' => [ |
| 486 |
$body, |
| 487 |
$params, |
| 488 |
[ '{{1x|hi}}' ], |
| 489 |
]; |
| 490 |
|
| 491 |
|
| 492 |
$html = $this->getTextFromFile( 'Image.html' ); |
| 493 |
$dataParsoid = [ 'ids' => [ |
| 494 |
'mwAg' => [ 'optList' => [ [ 'ck' => 'caption', 'ak' => 'Testing 123' ] ] ], |
| 495 |
'mwAw' => [ 'a' => [ 'href' => './File:Foobar.jpg' ], 'sa' => [] ], |
| 496 |
'mwBA' => [ |
| 497 |
'a' => [ 'resource' => './File:Foobar.jpg', 'height' => '28', 'width' => '240' ], |
| 498 |
'sa' => [ 'resource' => 'File:Foobar.jpg' ] |
| 499 |
] |
| 500 |
] ]; |
| 501 |
$dataMediaWiki = [ 'ids' => [ 'mwAg' => [ 'caption' => 'Testing 123' ] ] ]; |
| 502 |
|
| 503 |
$params = []; |
| 504 |
$body = [ |
| 505 |
'html' => $html, |
| 506 |
'original' => [ |
| 507 |
'data-parsoid' => [ |
| 508 |
'body' => $dataParsoid, |
| 509 |
], |
| 510 |
'data-mw' => [ |
| 511 |
'body' => $dataMediaWiki, |
| 512 |
], |
| 513 |
'html' => [ |
| 514 |
'headers' => $htmlHeaders999, |
| 515 |
'body' => $html |
| 516 |
], |
| 517 |
] |
| 518 |
]; |
| 519 |
yield 'should apply original data-mw when modified is absent (captions 1)' => [ |
| 520 |
$body, |
| 521 |
$params, |
| 522 |
[ '[[File:Foobar.jpg|Testing 123]]' ], |
| 523 |
]; |
| 524 |
|
| 525 |
|
| 526 |
$htmlModified = $this->getTextFromFile( 'Image-data-mw.html' ); |
| 527 |
$dataMediaWikiModified = [ |
| 528 |
'ids' => [ |
| 529 |
'mwAg' => [ 'caption' => 'Testing 123' ] |
| 530 |
] |
| 531 |
]; |
| 532 |
|
| 533 |
$params = []; |
| 534 |
$body = [ |
| 535 |
'html' => $htmlModified, |
| 536 |
'data-mw' => [ |
| 537 |
'body' => $dataMediaWikiModified, |
| 538 |
], |
| 539 |
'original' => [ |
| 540 |
'data-parsoid' => [ |
| 541 |
'body' => $dataParsoid, |
| 542 |
], |
| 543 |
'data-mw' => [ |
| 544 |
'body' => $dataMediaWiki, |
| 545 |
], |
| 546 |
'html' => [ |
| 547 |
'headers' => $htmlHeaders999, |
| 548 |
'body' => $html |
| 549 |
], |
| 550 |
] |
| 551 |
]; |
| 552 |
yield 'should give precedence to inline data-mw over modified (captions 2)' => [ |
| 553 |
$body, |
| 554 |
$params, |
| 555 |
[ '[[File:Foobar.jpg]]' ], |
| 556 |
]; |
| 557 |
|
| 558 |
|
| 559 |
$dataMediaWikiModified = [ |
| 560 |
'ids' => [ |
| 561 |
'mwAg' => [] |
| 562 |
] |
| 563 |
]; |
| 564 |
|
| 565 |
$params = []; |
| 566 |
$body = [ |
| 567 |
'html' => $html, |
| 568 |
'data-mw' => [ |
| 569 |
'body' => $dataMediaWikiModified, |
| 570 |
], |
| 571 |
'original' => [ |
| 572 |
'data-parsoid' => [ |
| 573 |
'body' => $dataParsoid, |
| 574 |
], |
| 575 |
'data-mw' => [ |
| 576 |
'body' => $dataMediaWiki, |
| 577 |
], |
| 578 |
'html' => [ |
| 579 |
'headers' => $htmlHeaders999, |
| 580 |
'body' => $html |
| 581 |
], |
| 582 |
] |
| 583 |
]; |
| 584 |
yield 'should give precedence to modified data-mw over original (captions 3)' => [ |
| 585 |
$body, |
| 586 |
$params, |
| 587 |
[ '[[File:Foobar.jpg]]' ], |
| 588 |
]; |
| 589 |
|
| 590 |
|
| 591 |
$htmlModified = 'Foo<h2></h2>Bar'; |
| 592 |
$params = [ |
| 593 |
'opts' => [ |
| 594 |
'original' => [] |
| 595 |
], |
| 596 |
]; |
| 597 |
$body = [ 'html' => $htmlModified ]; |
| 598 |
yield 'should apply extra normalizations' => [ |
| 599 |
$body, |
| 600 |
$params, |
| 601 |
[ 'FooBar' ], |
| 602 |
]; |
| 603 |
|
| 604 |
|
| 605 |
$htmlOfMinimal = $this->getTextFromFile( 'Minimal.html' ); |
| 606 |
$params = [ |
| 607 |
'from' => ParsoidFormatHelper::FORMAT_PAGEBUNDLE, |
| 608 |
]; |
| 609 |
$body = [ |
| 610 |
'html' => $htmlOfMinimal, |
| 611 |
'original' => [ |
| 612 |
'html' => [ |
| 613 |
'headers' => [ |
| 614 |
|
| 615 |
'content-type' => 'text/html;profile="https://www.mediawiki.org/wiki/Specs/HTML/999.0.0"' |
| 616 |
], |
| 617 |
|
| 618 |
'body' => $htmlOfMinimal, |
| 619 |
], |
| 620 |
'data-parsoid' => [ 'body' => [ 'ids' => [] ] ], |
| 621 |
'data-mw' => [ 'body' => [ 'ids' => [] ] ], |
| 622 |
] |
| 623 |
]; |
| 624 |
yield 'should apply version downgrade' => [ |
| 625 |
$body, |
| 626 |
$params, |
| 627 |
[ '123' ] |
| 628 |
]; |
| 629 |
|
| 630 |
|
| 631 |
$htmlOfMinimal = $this->getTextFromFile( 'Minimal.html' ); |
| 632 |
$params = []; |
| 633 |
$body = [ |
| 634 |
'html' => $htmlOfMinimal, |
| 635 |
'original' => [ |
| 636 |
'html' => [ |
| 637 |
'headers' => [ |
| 638 |
|
| 639 |
'content-type' => 'text/html;profile="https://www.mediawiki.org/wiki/Specs/HTML/2.4.0"' |
| 640 |
], |
| 641 |
|
| 642 |
'body' => $htmlOfMinimal, |
| 643 |
], |
| 644 |
'data-parsoid' => [ 'body' => [ 'ids' => [] ] ], |
| 645 |
] |
| 646 |
]; |
| 647 |
yield 'should not apply version downgrade if versions are the same' => [ |
| 648 |
$body, |
| 649 |
$params, |
| 650 |
[ '123' ] |
| 651 |
]; |
| 652 |
|
| 653 |
|
| 654 |
$html = $this->getTextFromFile( 'JsonConfig.html' ); |
| 655 |
$expectedText = [ |
| 656 |
'{"a":4,"b":3}', |
| 657 |
]; |
| 658 |
|
| 659 |
$params = [ |
| 660 |
'contentmodel' => CONTENT_MODEL_JSON, |
| 661 |
]; |
| 662 |
$body = [ 'html' => $html ]; |
| 663 |
yield 'should convert html to json' => [ |
| 664 |
$body, |
| 665 |
$params, |
| 666 |
$expectedText, |
| 667 |
[ 'content-type' => 'application/json' ], |
| 668 |
]; |
| 669 |
|
| 670 |
|
| 671 |
$htmlOfMinimal = $this->getTextFromFile( 'Minimal.html' ); |
| 672 |
$params = []; |
| 673 |
$body = [ |
| 674 |
'html' => $htmlOfMinimal, |
| 675 |
'original' => [], |
| 676 |
]; |
| 677 |
yield 'page bundle input should work with no original data present' => [ |
| 678 |
$body, |
| 679 |
$params, |
| 680 |
[ '123' ] |
| 681 |
]; |
| 682 |
} |
| 683 |
|
| 684 |
private function createResponse() { |
| 685 |
$responseFactory = new ResponseFactory( [ new TextFormatter( 'qqx' ) ] ); |
| 686 |
$response = $responseFactory->create(); |
| 687 |
return $response; |
| 688 |
} |
| 689 |
|
| 690 |
|
| 691 |
|
| 692 |
|
| 693 |
|
| 694 |
|
| 695 |
|
| 696 |
|
| 697 |
|
| 698 |
|
| 699 |
public function testResponse( $body, $params, $expectedText, array $expectedHeaders = [] ) { |
| 700 |
if ( !empty( $params['oldid'] ) ) { |
| 701 |
|
| 702 |
$originalContent = __METHOD__ . ' original content'; |
| 703 |
$page = $this->getNonexistingTestPage(); |
| 704 |
$this->editPage( $page, new WikitextContent( $originalContent ) ); |
| 705 |
$page = $page->getTitle(); |
| 706 |
$params['oldid'] = $page->getLatestRevID(); |
| 707 |
} else { |
| 708 |
$page = PageIdentityValue::localIdentity( 7, NS_MAIN, $body['pageName'] ?? 'HtmlInputTransformHelperTest' ); |
| 709 |
$originalContent = ''; |
| 710 |
} |
| 711 |
|
| 712 |
$statsCache = new StatsCache(); |
| 713 |
$statsdFactory = new BufferingStatsdDataFactory( '' ); |
| 714 |
$stats = new StatsFactory( $statsCache, new NullEmitter(), new NullLogger() ); |
| 715 |
$stats = $stats->withStatsdDataFactory( $statsdFactory ); |
| 716 |
|
| 717 |
|
| 718 |
$helper = $this->newHelper( [], $stats, $page, $body, $params ); |
| 719 |
|
| 720 |
$response = $this->createResponse(); |
| 721 |
$helper->putContent( $response ); |
| 722 |
|
| 723 |
foreach ( $expectedHeaders as $name => $value ) { |
| 724 |
$this->assertSame( $value, $response->getHeaderLine( $name ) ); |
| 725 |
} |
| 726 |
|
| 727 |
$body = $response->getBody(); |
| 728 |
$body->rewind(); |
| 729 |
$text = $body->getContents(); |
| 730 |
|
| 731 |
$expectedText ??= $originalContent; |
| 732 |
foreach ( (array)$expectedText as $exp ) { |
| 733 |
$this->assertStringContainsString( $exp, $text ); |
| 734 |
} |
| 735 |
|
| 736 |
|
| 737 |
|
| 738 |
|
| 739 |
$this->assertMetricsCount( 1, $statsdFactory, 'html_input_transform.original_html.' ); |
| 740 |
} |
| 741 |
|
| 742 |
private function assertMetricsCount( $expected, BufferingStatsdDataFactory $stats, $prefix = '' ) { |
| 743 |
$keys = []; |
| 744 |
foreach ( $stats->getData() as $datum ) { |
| 745 |
if ( str_starts_with( $datum->getKey(), $prefix ) ) { |
| 746 |
$keys[] = $datum->getKey(); |
| 747 |
} |
| 748 |
} |
| 749 |
|
| 750 |
$this->addToAssertionCount( 1 ); |
| 751 |
if ( count( $keys ) !== $expected ) { |
| 752 |
$this->fail( |
| 753 |
"Failed to assert that the number of metrics keys starting with '$prefix' is $expected. Keys: \n\t" |
| 754 |
. implode( "\n\t", $keys ) |
| 755 |
); |
| 756 |
} |
| 757 |
} |
| 758 |
|
| 759 |
public function provideOriginal() { |
| 760 |
$unchangedPB = new PageBundle( |
| 761 |
$this->getTextFromFile( 'MainPage-original.html' ), |
| 762 |
$this->getJsonFromFile( 'MainPage-original.data-parsoid' ), |
| 763 |
null, |
| 764 |
Parsoid::defaultHTMLVersion() |
| 765 |
); |
| 766 |
|
| 767 |
$originalContent = new WikitextContent( 'Goats are great!' ); |
| 768 |
$selserContext = new SelserContext( $unchangedPB, 0, $originalContent ); |
| 769 |
|
| 770 |
$unchangedPO = PageBundleParserOutputConverter::parserOutputFromPageBundle( $unchangedPB ); |
| 771 |
|
| 772 |
$renderID = new ParsoidRenderID( 0, 'testing' ); |
| 773 |
|
| 774 |
yield 'no original data' => [ |
| 775 |
$selserContext, |
| 776 |
null, |
| 777 |
null, |
| 778 |
[ |
| 779 |
'MediaWiki has been successfully installed', |
| 780 |
'== Getting started ==', |
| 781 |
] |
| 782 |
]; |
| 783 |
|
| 784 |
|
| 785 |
yield 'should load original wikitext by revision id' => [ |
| 786 |
$selserContext, |
| 787 |
1, |
| 788 |
$unchangedPB, |
| 789 |
null, |
| 790 |
]; |
| 791 |
|
| 792 |
|
| 793 |
$page = PageIdentityValue::localIdentity( 7, NS_MAIN, 'HtmlInputTransformHelperTest' ); |
| 794 |
$rev = new MutableRevisionRecord( $page ); |
| 795 |
$rev->setContent( SlotRecord::MAIN, new WikitextContent( 'Goats are great!' ) ); |
| 796 |
|
| 797 |
yield 'should use wikitext from fake revision' => [ |
| 798 |
$selserContext, |
| 799 |
$rev, |
| 800 |
$unchangedPO, |
| 801 |
'Goats are great!', |
| 802 |
]; |
| 803 |
|
| 804 |
|
| 805 |
yield 'should get original HTML from stash' => [ |
| 806 |
$selserContext, |
| 807 |
$rev, |
| 808 |
$renderID, |
| 809 |
'Goats are great!', |
| 810 |
]; |
| 811 |
} |
| 812 |
|
| 813 |
|
| 814 |
|
| 815 |
|
| 816 |
|
| 817 |
|
| 818 |
|
| 819 |
|
| 820 |
|
| 821 |
|
| 822 |
|
| 823 |
public function testSetOriginal( ?SelserContext $stashed, $rev, $originalRendering, $expectedText ) { |
| 824 |
if ( is_int( $rev ) && $rev > 0 ) { |
| 825 |
|
| 826 |
$originalContent = __METHOD__ . ' original content'; |
| 827 |
$page = $this->getNonexistingTestPage(); |
| 828 |
$this->editPage( $page, new WikitextContent( $originalContent ) ); |
| 829 |
$page = $page->getTitle(); |
| 830 |
$revId = $page->getLatestRevID() ?: 0; |
| 831 |
$rev = $revId; |
| 832 |
} elseif ( $rev instanceof RevisionRecord ) { |
| 833 |
$originalContentObj = $rev->getContent( SlotRecord::MAIN ); |
| 834 |
if ( !$originalContentObj instanceof TextContent ) { |
| 835 |
throw new LogicException( 'Not implemented' ); |
| 836 |
} |
| 837 |
$originalContent = $originalContentObj->getText(); |
| 838 |
$page = $rev->getPage(); |
| 839 |
$revId = $rev->getId() ?: 0; |
| 840 |
} else { |
| 841 |
$originalContent = ''; |
| 842 |
$page = PageIdentityValue::localIdentity( 7, NS_MAIN, 'HtmlInputTransformHelperTest' ); |
| 843 |
$revId = 0; |
| 844 |
} |
| 845 |
|
| 846 |
if ( $stashed ) { |
| 847 |
$renderID = new ParsoidRenderID( $revId, 'testing' ); |
| 848 |
$stash = $this->getServiceContainer()->getParsoidOutputStash(); |
| 849 |
$stash->set( $renderID, $stashed ); |
| 850 |
} |
| 851 |
|
| 852 |
$html = $this->getTextFromFile( 'MainPage-original.html' ); |
| 853 |
|
| 854 |
$params = []; |
| 855 |
$body = [ |
| 856 |
'html' => $html |
| 857 |
]; |
| 858 |
|
| 859 |
$statsCache = new StatsCache(); |
| 860 |
$statsdFactory = new BufferingStatsdDataFactory( '' ); |
| 861 |
$stats = new StatsFactory( $statsCache, new NullEmitter(), new NullLogger() ); |
| 862 |
$stats = $stats->withStatsdDataFactory( $statsdFactory ); |
| 863 |
|
| 864 |
$helper = $this->newHelper( [], $stats, $page, $body, $params ); |
| 865 |
$helper->setOriginal( $rev, $originalRendering ); |
| 866 |
|
| 867 |
$response = $this->createResponse(); |
| 868 |
$helper->putContent( $response ); |
| 869 |
|
| 870 |
$body = $response->getBody(); |
| 871 |
$body->rewind(); |
| 872 |
$text = $body->getContents(); |
| 873 |
|
| 874 |
$expectedText ??= $originalContent; |
| 875 |
foreach ( (array)$expectedText as $exp ) { |
| 876 |
$this->assertStringContainsString( $exp, $text ); |
| 877 |
} |
| 878 |
|
| 879 |
|
| 880 |
|
| 881 |
|
| 882 |
if ( $rev || $originalRendering ) { |
| 883 |
$this->assertMetricsCount( 1, $statsdFactory, 'html_input_transform.original_html.given' ); |
| 884 |
} else { |
| 885 |
$this->assertMetricsCount( 1, $statsdFactory, 'html_input_transform.original_html.not_given' ); |
| 886 |
} |
| 887 |
} |
| 888 |
|
| 889 |
|
| 890 |
|
| 891 |
|
| 892 |
public function testGetTransform() { |
| 893 |
$page = PageIdentityValue::localIdentity( 7, NS_MAIN, 'HtmlInputTransformHelperTest' ); |
| 894 |
$html = '<p>kittens are cute</p>'; |
| 895 |
|
| 896 |
$params = []; |
| 897 |
$body = [ |
| 898 |
'html' => $html |
| 899 |
]; |
| 900 |
|
| 901 |
$helper = $this->newHelper( [], StatsFactory::newNull(), $page, $body, $params ); |
| 902 |
|
| 903 |
$transform = $helper->getTransform(); |
| 904 |
|
| 905 |
$this->assertStringContainsString( 'kittens', ContentUtils::toXML( $transform->getModifiedDocument() ) ); |
| 906 |
} |
| 907 |
|
| 908 |
|
| 909 |
|
| 910 |
|
| 911 |
|
| 912 |
public function testResponseForFakeRevision() { |
| 913 |
$wikitext = 'Unsaved Revision Content'; |
| 914 |
|
| 915 |
$html = $this->getTextFromFile( 'Minimal.html' ); |
| 916 |
$page = PageIdentityValue::localIdentity( 7, NS_MAIN, $body['pageName'] ?? 'HtmlInputTransformHelperTest' ); |
| 917 |
|
| 918 |
|
| 919 |
|
| 920 |
$revision = new MutableRevisionRecord( $page ); |
| 921 |
$revision->setContent( SlotRecord::MAIN, new WikitextContent( $wikitext ) ); |
| 922 |
|
| 923 |
$params = []; |
| 924 |
$body = [ |
| 925 |
'html' => $html, |
| 926 |
'original' => [ |
| 927 |
'html' => [ |
| 928 |
'headers' => [ 'content-type' => 'text/html;profile="https://www.mediawiki.org/wiki/Specs/HTML/2.4.0"' ], |
| 929 |
|
| 930 |
'body' => $html |
| 931 |
], |
| 932 |
] |
| 933 |
]; |
| 934 |
|
| 935 |
$page = PageIdentityValue::localIdentity( 7, NS_MAIN, $body['pageName'] ?? 'HtmlInputTransformHelperTest' ); |
| 936 |
|
| 937 |
$helper = $this->newHelper( [], StatsFactory::newNull(), $page, $body, $params, $revision ); |
| 938 |
|
| 939 |
$response = $this->createResponse(); |
| 940 |
$helper->putContent( $response ); |
| 941 |
|
| 942 |
$body = $response->getBody(); |
| 943 |
$body->rewind(); |
| 944 |
|
| 945 |
|
| 946 |
$this->assertSame( $wikitext, $body->getContents() ); |
| 947 |
} |
| 948 |
|
| 949 |
public function testResponseWithRenderIdForExistingRevision() { |
| 950 |
$profileVersion = '2.4.0'; |
| 951 |
$htmlProfileUri = 'https://www.mediawiki.org/wiki/Specs/HTML/' . $profileVersion; |
| 952 |
$htmlContentType = "text/html;profile=\"$htmlProfileUri\""; |
| 953 |
|
| 954 |
$htmlHeaders = [ |
| 955 |
'content-type' => $htmlContentType, |
| 956 |
]; |
| 957 |
|
| 958 |
$page = $this->getExistingTestPage(); |
| 959 |
$oldWikitext = $page->getContent()->serialize(); |
| 960 |
|
| 961 |
$html = $this->getTextFromFile( 'MainPage-original.html' ); |
| 962 |
$dataParsoid = $this->getJsonFromFile( 'MainPage-original.data-parsoid' ); |
| 963 |
|
| 964 |
$pb = new PageBundle( |
| 965 |
$html, |
| 966 |
$dataParsoid, |
| 967 |
[], |
| 968 |
$profileVersion, |
| 969 |
$htmlHeaders, |
| 970 |
CONTENT_MODEL_WIKITEXT |
| 971 |
); |
| 972 |
|
| 973 |
$eTag = '"' . $page->getLatest() . '/just-a-test/edit"'; |
| 974 |
|
| 975 |
|
| 976 |
$body = [ 'html' => $html, 'original' => [ 'renderid' => $eTag ] ]; |
| 977 |
$params = []; |
| 978 |
|
| 979 |
$stash = $this->getServiceContainer()->getParsoidOutputStash(); |
| 980 |
$stash->set( |
| 981 |
ParsoidRenderID::newFromETag( $eTag ), |
| 982 |
new SelserContext( $pb, $page->getLatest() ), |
| 983 |
); |
| 984 |
|
| 985 |
$helper = $this->newHelper( [], StatsFactory::newNull(), $page, $body, $params ); |
| 986 |
|
| 987 |
$content = $helper->getContent(); |
| 988 |
|
| 989 |
|
| 990 |
|
| 991 |
|
| 992 |
$this->assertSame( $oldWikitext, $content->serialize() ); |
| 993 |
} |
| 994 |
|
| 995 |
public function testResponseWithRenderIdForUnsavedWikitext() { |
| 996 |
$profileVersion = '2.4.0'; |
| 997 |
$htmlProfileUri = 'https://www.mediawiki.org/wiki/Specs/HTML/' . $profileVersion; |
| 998 |
$htmlContentType = "text/html;profile=\"$htmlProfileUri\""; |
| 999 |
|
| 1000 |
$htmlHeaders = [ |
| 1001 |
'content-type' => $htmlContentType, |
| 1002 |
]; |
| 1003 |
|
| 1004 |
$page = $this->getNonexistingTestPage(); |
| 1005 |
|
| 1006 |
$html = $this->getTextFromFile( 'MainPage-original.html' ); |
| 1007 |
$dataParsoid = $this->getJsonFromFile( 'MainPage-original.data-parsoid' ); |
| 1008 |
$oldWikitext = 'Fake old wikitext'; |
| 1009 |
|
| 1010 |
$content = new WikitextContent( $oldWikitext ); |
| 1011 |
$pb = new PageBundle( |
| 1012 |
$html, |
| 1013 |
$dataParsoid, |
| 1014 |
[], |
| 1015 |
$profileVersion, |
| 1016 |
$htmlHeaders, |
| 1017 |
CONTENT_MODEL_WIKITEXT |
| 1018 |
); |
| 1019 |
|
| 1020 |
|
| 1021 |
|
| 1022 |
|
| 1023 |
|
| 1024 |
|
| 1025 |
$eTag = '"0/just-a-test/edit"'; |
| 1026 |
|
| 1027 |
|
| 1028 |
$body = [ 'html' => $html, 'original' => [ 'renderid' => $eTag ] ]; |
| 1029 |
$params = []; |
| 1030 |
|
| 1031 |
$stash = $this->getServiceContainer()->getParsoidOutputStash(); |
| 1032 |
$stash->set( |
| 1033 |
ParsoidRenderID::newFromETag( $eTag ), |
| 1034 |
new SelserContext( $pb, 0, $content ) |
| 1035 |
); |
| 1036 |
|
| 1037 |
$helper = $this->newHelper( [], StatsFactory::newNull(), $page, $body, $params ); |
| 1038 |
|
| 1039 |
$content = $helper->getContent(); |
| 1040 |
|
| 1041 |
|
| 1042 |
|
| 1043 |
|
| 1044 |
$this->assertSame( $oldWikitext, $content->serialize() ); |
| 1045 |
} |
| 1046 |
|
| 1047 |
public function testETagWithBadUUIDFails() { |
| 1048 |
$page = $this->getExistingTestPage(); |
| 1049 |
$html = 'whatever'; |
| 1050 |
|
| 1051 |
|
| 1052 |
|
| 1053 |
|
| 1054 |
$access = $this->getServiceContainer()->getParserOutputAccess(); |
| 1055 |
$pageLookup = $this->getServiceContainer()->getPageStore(); |
| 1056 |
$popt = ParserOptions::newFromAnon(); |
| 1057 |
$popt->setUseParsoid(); |
| 1058 |
$access->getParserOutput( $pageLookup->getPageByReference( $page ), $popt )->getValue(); |
| 1059 |
|
| 1060 |
$revid = $page->getLatest(); |
| 1061 |
$eTag = "\"$revid/nope-nope-nope\""; |
| 1062 |
|
| 1063 |
$body = [ 'html' => $html, 'original' => [ 'renderid' => $eTag ] ]; |
| 1064 |
$params = []; |
| 1065 |
|
| 1066 |
$this->expectException( HttpException::class ); |
| 1067 |
$this->expectExceptionCode( 412 ); |
| 1068 |
$helper = $this->newHelper( [], StatsFactory::newNull(), $page, $body, $params ); |
| 1069 |
$helper->getContent(); |
| 1070 |
} |
| 1071 |
|
| 1072 |
public function testETagWithBadRevIDFails() { |
| 1073 |
$page = $this->getExistingTestPage(); |
| 1074 |
$html = 'whatever'; |
| 1075 |
|
| 1076 |
|
| 1077 |
$eTag = "\"1111111/nope-nope-nope\""; |
| 1078 |
|
| 1079 |
$body = [ 'html' => $html, 'original' => [ 'renderid' => $eTag ] ]; |
| 1080 |
$params = []; |
| 1081 |
|
| 1082 |
$this->expectException( HttpException::class ); |
| 1083 |
$this->expectExceptionCode( 412 ); |
| 1084 |
$helper = $this->newHelper( [], StatsFactory::newNull(), $page, $body, $params ); |
| 1085 |
$helper->getContent(); |
| 1086 |
} |
| 1087 |
|
| 1088 |
public function testResponseWithRenderIDFallbackToParserCache() { |
| 1089 |
|
| 1090 |
$oldWikitext = '<p >testing</P>'; |
| 1091 |
$rev = $this->editPage( __METHOD__, $oldWikitext )->value['revision-record']; |
| 1092 |
$page = $rev->getPage(); |
| 1093 |
|
| 1094 |
$access = $this->getServiceContainer()->getParserOutputAccess(); |
| 1095 |
$pageLookup = $this->getServiceContainer()->getPageStore(); |
| 1096 |
|
| 1097 |
$popt = ParserOptions::newFromAnon(); |
| 1098 |
$popt->setUseParsoid(); |
| 1099 |
$pout = $access->getParserOutput( $pageLookup->getPageByReference( $page ), $popt )->getValue(); |
| 1100 |
|
| 1101 |
$key = ParsoidRenderID::newFromParserOutput( $pout )->getKey(); |
| 1102 |
$html = $pout->getRawText(); |
| 1103 |
|
| 1104 |
|
| 1105 |
$body = [ 'html' => $html, 'original' => [ 'renderid' => $key ] ]; |
| 1106 |
$params = []; |
| 1107 |
|
| 1108 |
|
| 1109 |
|
| 1110 |
|
| 1111 |
$helper = $this->newHelper( [], StatsFactory::newNull(), $page, $body, $params ); |
| 1112 |
$content = $helper->getContent(); |
| 1113 |
|
| 1114 |
|
| 1115 |
$this->assertSame( $oldWikitext, $content->serialize() ); |
| 1116 |
} |
| 1117 |
|
| 1118 |
public function testResponseWithRevisionIDFallbackToRendering() { |
| 1119 |
|
| 1120 |
$oldWikitext = '<p >testing</P>'; |
| 1121 |
$rev = $this->editPage( __METHOD__, $oldWikitext )->value['revision-record']; |
| 1122 |
$page = $rev->getPage(); |
| 1123 |
|
| 1124 |
$access = $this->getServiceContainer()->getParserOutputAccess(); |
| 1125 |
$pageLookup = $this->getServiceContainer()->getPageStore(); |
| 1126 |
|
| 1127 |
$popt = ParserOptions::newFromAnon(); |
| 1128 |
$popt->setUseParsoid(); |
| 1129 |
$pout = $access->getParserOutput( $pageLookup->getPageByReference( $page ), $popt )->getValue(); |
| 1130 |
$html = $pout->getRawText(); |
| 1131 |
|
| 1132 |
|
| 1133 |
$body = [ 'html' => $html, 'original' => [ 'revid' => $rev->getId() ] ]; |
| 1134 |
$params = []; |
| 1135 |
|
| 1136 |
|
| 1137 |
|
| 1138 |
|
| 1139 |
$helper = $this->newHelper( [], StatsFactory::newNull(), $page, $body, $params ); |
| 1140 |
$content = $helper->getContent(); |
| 1141 |
|
| 1142 |
|
| 1143 |
$this->assertSame( $oldWikitext, $content->serialize() ); |
| 1144 |
} |
| 1145 |
|
| 1146 |
public static function provideHandlesParsoidError() { |
| 1147 |
yield 'ClientError' => [ |
| 1148 |
new ClientError( 'TEST_TEST' ), |
| 1149 |
new LocalizedHttpException( |
| 1150 |
new MessageValue( 'rest-html-backend-error' ), |
| 1151 |
400, |
| 1152 |
[ |
| 1153 |
'reason' => 'TEST_TEST' |
| 1154 |
] |
| 1155 |
) |
| 1156 |
]; |
| 1157 |
yield 'ResourceLimitExceededException' => [ |
| 1158 |
new ResourceLimitExceededException( 'TEST_TEST' ), |
| 1159 |
new LocalizedHttpException( |
| 1160 |
new MessageValue( 'rest-resource-limit-exceeded' ), |
| 1161 |
413, |
| 1162 |
[ |
| 1163 |
'reason' => 'TEST_TEST' |
| 1164 |
] |
| 1165 |
) |
| 1166 |
]; |
| 1167 |
} |
| 1168 |
|
| 1169 |
|
| 1170 |
|
| 1171 |
|
| 1172 |
public function testHandlesParsoidError( |
| 1173 |
Exception $parsoidException, |
| 1174 |
Exception $expectedException |
| 1175 |
) { |
| 1176 |
$page = $this->getExistingTestPage( __METHOD__ ); |
| 1177 |
|
| 1178 |
$body = [ 'html' => 'hi', ]; |
| 1179 |
$params = []; |
| 1180 |
|
| 1181 |
$helper = $this->newHelper( [ |
| 1182 |
'htmlToContent' => static function () use ( $parsoidException ) { |
| 1183 |
throw $parsoidException; |
| 1184 |
} |
| 1185 |
], StatsFactory::newNull(), $page, $body, $params ); |
| 1186 |
|
| 1187 |
$this->expectExceptionObject( $expectedException ); |
| 1188 |
$helper->getContent(); |
| 1189 |
} |
| 1190 |
|
| 1191 |
public function testHandlesInvalidRenderID(): void { |
| 1192 |
$page = $this->getExistingTestPage( __METHOD__ ); |
| 1193 |
|
| 1194 |
$body = [ 'html' => 'hi', 'original' => [ 'renderid' => 'foo' ] ]; |
| 1195 |
$params = []; |
| 1196 |
|
| 1197 |
$this->expectExceptionObject( new LocalizedHttpException( |
| 1198 |
new MessageValue( 'rest-parsoid-bad-render-id', [ 'foo' ] ), |
| 1199 |
400 |
| 1200 |
) ); |
| 1201 |
|
| 1202 |
$this->newHelper( [], StatsFactory::newNull(), $page, $body, $params ); |
| 1203 |
} |
| 1204 |
|
| 1205 |
private function newHtmlToContentTransform( $html, $methodOverrides = [] ): HtmlToContentTransform { |
| 1206 |
$transform = $this->getMockBuilder( HtmlToContentTransform::class ) |
| 1207 |
->onlyMethods( array_keys( $methodOverrides ) ) |
| 1208 |
->setConstructorArgs( [ |
| 1209 |
$html, |
| 1210 |
$this->getExistingTestPage(), |
| 1211 |
new Parsoid( |
| 1212 |
$this->getServiceContainer()->getParsoidSiteConfig(), |
| 1213 |
$this->getServiceContainer()->getParsoidDataAccess() |
| 1214 |
), |
| 1215 |
MainConfigSchema::getDefaultValue( MainConfigNames::ParsoidSettings ), |
| 1216 |
$this->getServiceContainer()->getParsoidPageConfigFactory(), |
| 1217 |
$this->getServiceContainer()->getContentHandlerFactory() |
| 1218 |
] ) |
| 1219 |
->getMock(); |
| 1220 |
|
| 1221 |
foreach ( $methodOverrides as $method => $callback ) { |
| 1222 |
$transform->method( $method )->willReturnCallback( $callback ); |
| 1223 |
} |
| 1224 |
|
| 1225 |
return $transform; |
| 1226 |
} |
| 1227 |
|
| 1228 |
} |