| 1 |
<?php |
| 2 |
|
| 3 |
namespace MediaWiki\Tests\Rest\Handler; |
| 4 |
|
| 5 |
use MediaWiki\Api\ApiUsageException; |
| 6 |
use MediaWiki\Config\HashConfig; |
| 7 |
use MediaWiki\Content\WikitextContent; |
| 8 |
use MediaWiki\Content\WikitextContentHandler; |
| 9 |
use MediaWiki\Json\FormatJson; |
| 10 |
use MediaWiki\Languages\LanguageNameUtils; |
| 11 |
use MediaWiki\Linker\LinkRenderer; |
| 12 |
use MediaWiki\MainConfigNames; |
| 13 |
use MediaWiki\Message\Message; |
| 14 |
use MediaWiki\Parser\MagicWordFactory; |
| 15 |
use MediaWiki\Parser\ParserFactory; |
| 16 |
use MediaWiki\Parser\Parsoid\ParsoidParserFactory; |
| 17 |
use MediaWiki\Rest\Handler\UpdateHandler; |
| 18 |
use MediaWiki\Rest\LocalizedHttpException; |
| 19 |
use MediaWiki\Rest\RequestData; |
| 20 |
use MediaWiki\Revision\MutableRevisionRecord; |
| 21 |
use MediaWiki\Revision\RevisionLookup; |
| 22 |
use MediaWiki\Revision\SlotRecord; |
| 23 |
use MediaWiki\Session\Token; |
| 24 |
use MediaWiki\Status\Status; |
| 25 |
use MediaWiki\Tests\Unit\DummyServicesTrait; |
| 26 |
use MediaWiki\Title\Title; |
| 27 |
use MediaWiki\Title\TitleFactory; |
| 28 |
use MediaWikiLangTestCase; |
| 29 |
use MockTitleTrait; |
| 30 |
use PHPUnit\Framework\MockObject\MockObject; |
| 31 |
use Wikimedia\Message\DataMessageValue; |
| 32 |
use Wikimedia\Message\MessageValue; |
| 33 |
use Wikimedia\Message\ParamType; |
| 34 |
use Wikimedia\Message\ScalarParam; |
| 35 |
use Wikimedia\UUID\GlobalIdGenerator; |
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
class UpdateHandlerTest extends MediaWikiLangTestCase { |
| 42 |
use ActionModuleBasedHandlerTestTrait; |
| 43 |
use DummyServicesTrait; |
| 44 |
use MockTitleTrait; |
| 45 |
|
| 46 |
private function newHandler( $resultData, $throwException = null, $csrfSafe = false ) { |
| 47 |
$config = new HashConfig( [ |
| 48 |
MainConfigNames::RightsUrl => 'https://creativecommons.org/licenses/by-sa/4.0/', |
| 49 |
MainConfigNames::RightsText => 'CC-BY-SA 4.0' |
| 50 |
] ); |
| 51 |
|
| 52 |
$wikitextContentHandler = new WikitextContentHandler( |
| 53 |
CONTENT_MODEL_WIKITEXT, |
| 54 |
$this->createMock( TitleFactory::class ), |
| 55 |
$this->createMock( ParserFactory::class ), |
| 56 |
$this->createMock( GlobalIdGenerator::class ), |
| 57 |
$this->createMock( LanguageNameUtils::class ), |
| 58 |
$this->createMock( LinkRenderer::class ), |
| 59 |
$this->createMock( MagicWordFactory::class ), |
| 60 |
$this->createMock( ParsoidParserFactory::class ) |
| 61 |
); |
| 62 |
|
| 63 |
|
| 64 |
$contentHandlerFactory = $this->getDummyContentHandlerFactory( |
| 65 |
[ CONTENT_MODEL_WIKITEXT => $wikitextContentHandler ] |
| 66 |
); |
| 67 |
|
| 68 |
$titleCodec = $this->getDummyMediaWikiTitleCodec(); |
| 69 |
|
| 70 |
|
| 71 |
$revisionLookup = $this->createNoOpMock( |
| 72 |
RevisionLookup::class, |
| 73 |
[ 'getRevisionById', 'getRevisionByTitle' ] |
| 74 |
); |
| 75 |
$revisionLookup->method( 'getRevisionById' ) |
| 76 |
->willReturnCallback( function ( $id ) { |
| 77 |
$title = $this->makeMockTitle( __CLASS__ ); |
| 78 |
$rev = new MutableRevisionRecord( $title ); |
| 79 |
$rev->setId( $id ); |
| 80 |
$rev->setContent( SlotRecord::MAIN, new WikitextContent( "Content of revision $id" ) ); |
| 81 |
$rev->setTimestamp( '2020-01-01T01:02:03Z' ); |
| 82 |
return $rev; |
| 83 |
} ); |
| 84 |
$revisionLookup->method( 'getRevisionByTitle' ) |
| 85 |
->willReturnCallback( static function ( $title ) { |
| 86 |
$rev = new MutableRevisionRecord( Title::castFromLinkTarget( $title ) ); |
| 87 |
$rev->setId( 1234 ); |
| 88 |
$rev->setContent( SlotRecord::MAIN, new WikitextContent( "Current content of $title" ) ); |
| 89 |
$rev->setTimestamp( '2020-01-01T01:02:03Z' ); |
| 90 |
return $rev; |
| 91 |
} ); |
| 92 |
|
| 93 |
$handler = new UpdateHandler( |
| 94 |
$config, |
| 95 |
$contentHandlerFactory, |
| 96 |
$titleCodec, |
| 97 |
$titleCodec, |
| 98 |
$revisionLookup |
| 99 |
); |
| 100 |
|
| 101 |
$apiMain = $this->getApiMain( $csrfSafe ); |
| 102 |
$dummyModule = $this->getDummyApiModule( $apiMain, 'edit', $resultData, $throwException ); |
| 103 |
|
| 104 |
$handler->setApiMain( $apiMain ); |
| 105 |
$handler->overrideActionModule( |
| 106 |
'edit', |
| 107 |
'action', |
| 108 |
$dummyModule |
| 109 |
); |
| 110 |
|
| 111 |
return $handler; |
| 112 |
} |
| 113 |
|
| 114 |
public static function provideExecute() { |
| 115 |
$token = strval( new Token( 'TOKEN', '' ) ); |
| 116 |
|
| 117 |
yield "create with token" => [ |
| 118 |
[ |
| 119 |
'method' => 'PUT', |
| 120 |
'pathParams' => [ 'title' => 'Foo' ], |
| 121 |
'headers' => [ |
| 122 |
'Content-Type' => 'application/json', |
| 123 |
], |
| 124 |
'bodyContents' => json_encode( [ |
| 125 |
'token' => $token, |
| 126 |
'source' => 'Lorem Ipsum', |
| 127 |
'comment' => 'Testing' |
| 128 |
] ), |
| 129 |
], |
| 130 |
[ |
| 131 |
'title' => 'Foo', |
| 132 |
'text' => 'Lorem Ipsum', |
| 133 |
'summary' => 'Testing', |
| 134 |
'createonly' => '1', |
| 135 |
'token' => $token, |
| 136 |
], |
| 137 |
[ |
| 138 |
"edit" => [ |
| 139 |
"new" => true, |
| 140 |
"result" => "Success", |
| 141 |
"pageid" => 94542, |
| 142 |
"title" => "Foo", |
| 143 |
"contentmodel" => "wikitext", |
| 144 |
"oldrevid" => 0, |
| 145 |
"newrevid" => 371707, |
| 146 |
"newtimestamp" => "2018-12-18T16:59:42Z", |
| 147 |
] |
| 148 |
], |
| 149 |
[ |
| 150 |
'id' => 94542, |
| 151 |
'title' => 'Foo', |
| 152 |
'key' => 'Foo', |
| 153 |
'content_model' => 'wikitext', |
| 154 |
'latest' => [ |
| 155 |
'id' => 371707, |
| 156 |
'timestamp' => "2018-12-18T16:59:42Z" |
| 157 |
], |
| 158 |
'license' => [ |
| 159 |
'url' => 'https://creativecommons.org/licenses/by-sa/4.0/', |
| 160 |
'title' => 'CC-BY-SA 4.0' |
| 161 |
], |
| 162 |
'source' => 'Content of revision 371707' |
| 163 |
], |
| 164 |
false, |
| 165 |
true, |
| 166 |
]; |
| 167 |
|
| 168 |
yield "create with model" => [ |
| 169 |
[ |
| 170 |
'method' => 'POST', |
| 171 |
'pathParams' => [ 'title' => 'Foo' ], |
| 172 |
'headers' => [ |
| 173 |
'Content-Type' => 'application/json', |
| 174 |
], |
| 175 |
'bodyContents' => json_encode( [ |
| 176 |
'token' => $token, |
| 177 |
'source' => 'Lorem Ipsum', |
| 178 |
'comment' => 'Testing', |
| 179 |
'content_model' => CONTENT_MODEL_WIKITEXT, |
| 180 |
] ), |
| 181 |
], |
| 182 |
[ |
| 183 |
'title' => 'Foo', |
| 184 |
'text' => 'Lorem Ipsum', |
| 185 |
'summary' => 'Testing', |
| 186 |
'contentmodel' => 'wikitext', |
| 187 |
'createonly' => '1', |
| 188 |
'token' => $token, |
| 189 |
], |
| 190 |
[ |
| 191 |
"edit" => [ |
| 192 |
"new" => true, |
| 193 |
"result" => "Success", |
| 194 |
"pageid" => 94542, |
| 195 |
"title" => "Foo", |
| 196 |
"contentmodel" => "wikitext", |
| 197 |
"oldrevid" => 0, |
| 198 |
"newrevid" => 371707, |
| 199 |
"newtimestamp" => "2018-12-18T16:59:42Z", |
| 200 |
] |
| 201 |
], |
| 202 |
[ |
| 203 |
'id' => 94542, |
| 204 |
'title' => 'Foo', |
| 205 |
'key' => 'Foo', |
| 206 |
'content_model' => 'wikitext', |
| 207 |
'latest' => [ |
| 208 |
'id' => 371707, |
| 209 |
'timestamp' => "2018-12-18T16:59:42Z" |
| 210 |
], |
| 211 |
'license' => [ |
| 212 |
'url' => 'https://creativecommons.org/licenses/by-sa/4.0/', |
| 213 |
'title' => 'CC-BY-SA 4.0' |
| 214 |
], |
| 215 |
'source' => 'Content of revision 371707' |
| 216 |
], |
| 217 |
false, |
| 218 |
true, |
| 219 |
]; |
| 220 |
|
| 221 |
yield "update with token" => [ |
| 222 |
[ |
| 223 |
'method' => 'PUT', |
| 224 |
'pathParams' => [ 'title' => 'foo bar' ], |
| 225 |
'headers' => [ |
| 226 |
'Content-Type' => 'application/json', |
| 227 |
], |
| 228 |
'bodyContents' => json_encode( [ |
| 229 |
'token' => $token, |
| 230 |
'source' => 'Lorem Ipsum', |
| 231 |
'comment' => 'Testing', |
| 232 |
'latest' => [ 'id' => 789123 ], |
| 233 |
] ), |
| 234 |
], |
| 235 |
[ |
| 236 |
'title' => 'foo bar', |
| 237 |
'text' => 'Lorem Ipsum', |
| 238 |
'summary' => 'Testing', |
| 239 |
'nocreate' => '1', |
| 240 |
'baserevid' => '789123', |
| 241 |
'token' => $token, |
| 242 |
], |
| 243 |
[ |
| 244 |
"edit" => [ |
| 245 |
"result" => "Success", |
| 246 |
"pageid" => 94542, |
| 247 |
"title" => "Foo_bar", |
| 248 |
"contentmodel" => "wikitext", |
| 249 |
"oldrevid" => 371705, |
| 250 |
"newrevid" => 371707, |
| 251 |
"newtimestamp" => "2018-12-18T16:59:42Z", |
| 252 |
] |
| 253 |
], |
| 254 |
[ |
| 255 |
'id' => 94542, |
| 256 |
'content_model' => 'wikitext', |
| 257 |
'title' => 'Foo bar', |
| 258 |
'key' => 'Foo_bar', |
| 259 |
'latest' => [ |
| 260 |
'id' => 371707, |
| 261 |
'timestamp' => "2018-12-18T16:59:42Z" |
| 262 |
], |
| 263 |
'license' => [ |
| 264 |
'url' => 'https://creativecommons.org/licenses/by-sa/4.0/', |
| 265 |
'title' => 'CC-BY-SA 4.0' |
| 266 |
], |
| 267 |
'source' => 'Content of revision 371707' |
| 268 |
], |
| 269 |
false, |
| 270 |
true, |
| 271 |
]; |
| 272 |
|
| 273 |
yield "update with model" => [ |
| 274 |
[ |
| 275 |
'method' => 'POST', |
| 276 |
'pathParams' => [ 'title' => 'foo bar' ], |
| 277 |
'headers' => [ |
| 278 |
'Content-Type' => 'application/json', |
| 279 |
], |
| 280 |
'bodyContents' => json_encode( [ |
| 281 |
'source' => 'Lorem Ipsum', |
| 282 |
'comment' => 'Testing', |
| 283 |
'content_model' => CONTENT_MODEL_WIKITEXT, |
| 284 |
'latest' => [ 'id' => 789123 ], |
| 285 |
] ), |
| 286 |
], |
| 287 |
[ |
| 288 |
'title' => 'foo bar', |
| 289 |
'text' => 'Lorem Ipsum', |
| 290 |
'summary' => 'Testing', |
| 291 |
'contentmodel' => 'wikitext', |
| 292 |
'nocreate' => '1', |
| 293 |
'baserevid' => '789123', |
| 294 |
'token' => '+\\', |
| 295 |
], |
| 296 |
[ |
| 297 |
"edit" => [ |
| 298 |
"result" => "Success", |
| 299 |
"pageid" => 94542, |
| 300 |
"title" => "Foo_bar", |
| 301 |
"contentmodel" => "wikitext", |
| 302 |
"oldrevid" => 371705, |
| 303 |
"newrevid" => 371707, |
| 304 |
"newtimestamp" => "2018-12-18T16:59:42Z", |
| 305 |
] |
| 306 |
], |
| 307 |
[ |
| 308 |
'id' => 94542, |
| 309 |
'content_model' => 'wikitext', |
| 310 |
'title' => 'Foo bar', |
| 311 |
'key' => 'Foo_bar', |
| 312 |
'latest' => [ |
| 313 |
'id' => 371707, |
| 314 |
'timestamp' => "2018-12-18T16:59:42Z" |
| 315 |
], |
| 316 |
'license' => [ |
| 317 |
'url' => 'https://creativecommons.org/licenses/by-sa/4.0/', |
| 318 |
'title' => 'CC-BY-SA 4.0' |
| 319 |
], |
| 320 |
'source' => 'Content of revision 371707' |
| 321 |
], |
| 322 |
true, |
| 323 |
false, |
| 324 |
]; |
| 325 |
|
| 326 |
yield "update without token" => [ |
| 327 |
[ |
| 328 |
'method' => 'PUT', |
| 329 |
'pathParams' => [ 'title' => 'Foo' ], |
| 330 |
'headers' => [ |
| 331 |
'Content-Type' => 'application/json', |
| 332 |
], |
| 333 |
'bodyContents' => json_encode( [ |
| 334 |
'source' => 'Lorem Ipsum', |
| 335 |
'comment' => 'Testing', |
| 336 |
'content_model' => CONTENT_MODEL_WIKITEXT, |
| 337 |
'latest' => [ 'id' => 789123 ], |
| 338 |
] ), |
| 339 |
], |
| 340 |
[ |
| 341 |
'title' => 'Foo', |
| 342 |
'text' => 'Lorem Ipsum', |
| 343 |
'summary' => 'Testing', |
| 344 |
'contentmodel' => 'wikitext', |
| 345 |
'nocreate' => '1', |
| 346 |
'baserevid' => '789123', |
| 347 |
'token' => '+\\', |
| 348 |
], |
| 349 |
[ |
| 350 |
"edit" => [ |
| 351 |
"result" => "Success", |
| 352 |
"pageid" => 94542, |
| 353 |
"title" => "Foo", |
| 354 |
"contentmodel" => "wikitext", |
| 355 |
"oldrevid" => 371705, |
| 356 |
"newrevid" => 371707, |
| 357 |
"newtimestamp" => "2018-12-18T16:59:42Z", |
| 358 |
] |
| 359 |
], |
| 360 |
[ |
| 361 |
'id' => 94542, |
| 362 |
'content_model' => 'wikitext', |
| 363 |
'latest' => [ |
| 364 |
'id' => 371707, |
| 365 |
'timestamp' => "2018-12-18T16:59:42Z" |
| 366 |
], |
| 367 |
'license' => [ |
| 368 |
'url' => 'https://creativecommons.org/licenses/by-sa/4.0/', |
| 369 |
'title' => 'CC-BY-SA 4.0' |
| 370 |
], |
| 371 |
], |
| 372 |
true, |
| 373 |
false, |
| 374 |
]; |
| 375 |
|
| 376 |
yield "null-edit (unchanged)" => [ |
| 377 |
[ |
| 378 |
'method' => 'PUT', |
| 379 |
'pathParams' => [ 'title' => 'Foo' ], |
| 380 |
'headers' => [ |
| 381 |
'Content-Type' => 'application/json', |
| 382 |
], |
| 383 |
'bodyContents' => json_encode( [ |
| 384 |
'source' => 'Lorem Ipsum', |
| 385 |
'comment' => 'Testing', |
| 386 |
'content_model' => CONTENT_MODEL_WIKITEXT, |
| 387 |
'latest' => [ 'id' => 789123 ], |
| 388 |
] ), |
| 389 |
], |
| 390 |
[ |
| 391 |
'title' => 'Foo', |
| 392 |
'text' => 'Lorem Ipsum', |
| 393 |
'summary' => 'Testing', |
| 394 |
'contentmodel' => 'wikitext', |
| 395 |
'nocreate' => '1', |
| 396 |
'baserevid' => '789123', |
| 397 |
'token' => '+\\', |
| 398 |
], |
| 399 |
[ |
| 400 |
"edit" => [ |
| 401 |
"result" => "Success", |
| 402 |
"pageid" => 94542, |
| 403 |
"title" => "Foo", |
| 404 |
"contentmodel" => "wikitext", |
| 405 |
"nochange" => "", |
| 406 |
] |
| 407 |
], |
| 408 |
[ |
| 409 |
'id' => 94542, |
| 410 |
'content_model' => 'wikitext', |
| 411 |
'latest' => [ |
| 412 |
'id' => 1234, |
| 413 |
'timestamp' => '2020-01-01T01:02:03Z' |
| 414 |
], |
| 415 |
'license' => [ |
| 416 |
'url' => 'https://creativecommons.org/licenses/by-sa/4.0/', |
| 417 |
'title' => 'CC-BY-SA 4.0' |
| 418 |
], |
| 419 |
], |
| 420 |
true, |
| 421 |
false, |
| 422 |
]; |
| 423 |
} |
| 424 |
|
| 425 |
|
| 426 |
|
| 427 |
|
| 428 |
public function testExecute( |
| 429 |
$requestData, |
| 430 |
$expectedActionParams, |
| 431 |
$actionResult, |
| 432 |
$expectedResponse, |
| 433 |
$csrfSafe, |
| 434 |
$hasToken |
| 435 |
) { |
| 436 |
$request = new RequestData( $requestData ); |
| 437 |
|
| 438 |
$handler = $this->newHandler( $actionResult, null, $csrfSafe ); |
| 439 |
|
| 440 |
$session = $this->getSession( $csrfSafe ); |
| 441 |
|
| 442 |
$session->method( 'hasToken' )->willReturn( $hasToken ); |
| 443 |
|
| 444 |
$session->method( 'getToken' )->willReturn( new Token( 'TOKEN', '' ) ); |
| 445 |
|
| 446 |
$responseData = $this->executeHandlerAndGetBodyData( |
| 447 |
$handler, $request, [], [], [], [], null, $session |
| 448 |
); |
| 449 |
|
| 450 |
|
| 451 |
foreach ( $expectedActionParams as $key => $value ) { |
| 452 |
$this->assertSame( |
| 453 |
$value, |
| 454 |
$handler->getApiMain()->getVal( $key ), |
| 455 |
"ApiEditPage param: $key" |
| 456 |
); |
| 457 |
} |
| 458 |
|
| 459 |
|
| 460 |
foreach ( $expectedResponse as $key => $value ) { |
| 461 |
$this->assertArrayHasKey( $key, $responseData ); |
| 462 |
$this->assertSame( |
| 463 |
$value, |
| 464 |
$responseData[ $key ], |
| 465 |
"UpdateHandler response field: $key" |
| 466 |
); |
| 467 |
} |
| 468 |
} |
| 469 |
|
| 470 |
public static function provideBodyValidation() { |
| 471 |
yield "missing source field" => [ |
| 472 |
[ |
| 473 |
'method' => 'PUT', |
| 474 |
'pathParams' => [ 'title' => 'Foo' ], |
| 475 |
'headers' => [ |
| 476 |
'Content-Type' => 'application/json', |
| 477 |
], |
| 478 |
'bodyContents' => json_encode( [ |
| 479 |
'token' => 'TOKEN', |
| 480 |
'comment' => 'Testing', |
| 481 |
'content_model' => CONTENT_MODEL_WIKITEXT, |
| 482 |
] ), |
| 483 |
], |
| 484 |
MessageValue::new( 'rest-body-validation-error', [ |
| 485 |
DataMessageValue::new( 'paramvalidator-missingparam', [], 'missingparam' ) |
| 486 |
->plaintextParams( 'source' ) |
| 487 |
] ), |
| 488 |
]; |
| 489 |
yield "missing comment field" => [ |
| 490 |
[ |
| 491 |
'method' => 'PUT', |
| 492 |
'pathParams' => [ 'title' => 'Foo' ], |
| 493 |
'headers' => [ |
| 494 |
'Content-Type' => 'application/json', |
| 495 |
], |
| 496 |
'bodyContents' => json_encode( [ |
| 497 |
'token' => 'TOKEN', |
| 498 |
'source' => 'Lorem Ipsum', |
| 499 |
'content_model' => CONTENT_MODEL_WIKITEXT, |
| 500 |
] ), |
| 501 |
], |
| 502 |
MessageValue::new( 'rest-body-validation-error', [ |
| 503 |
DataMessageValue::new( 'paramvalidator-missingparam', [], 'missingparam' ) |
| 504 |
->plaintextParams( 'comment' ) |
| 505 |
] ), |
| 506 |
]; |
| 507 |
} |
| 508 |
|
| 509 |
|
| 510 |
|
| 511 |
|
| 512 |
public function testBodyValidation( array $requestData, MessageValue $expectedMessage ) { |
| 513 |
$request = new RequestData( $requestData ); |
| 514 |
|
| 515 |
$handler = $this->newHandler( [] ); |
| 516 |
|
| 517 |
$exception = $this->executeHandlerAndGetHttpException( $handler, $request ); |
| 518 |
|
| 519 |
$this->assertSame( 400, $exception->getCode(), 'HTTP status' ); |
| 520 |
$this->assertInstanceOf( LocalizedHttpException::class, $exception ); |
| 521 |
|
| 522 |
|
| 523 |
$this->assertEquals( $expectedMessage, $exception->getMessageValue() ); |
| 524 |
} |
| 525 |
|
| 526 |
public static function provideHeaderValidation() { |
| 527 |
yield "bad content type" => [ |
| 528 |
[ |
| 529 |
'method' => 'PUT', |
| 530 |
'pathParams' => [ 'title' => 'Foo' ], |
| 531 |
'headers' => [ |
| 532 |
'Content-Type' => 'text/plain', |
| 533 |
], |
| 534 |
'bodyContents' => json_encode( [ |
| 535 |
'token' => 'TOKEN', |
| 536 |
'source' => 'Lorem Ipsum', |
| 537 |
'comment' => 'Testing', |
| 538 |
'content_model' => CONTENT_MODEL_WIKITEXT, |
| 539 |
] ), |
| 540 |
], |
| 541 |
415 |
| 542 |
]; |
| 543 |
} |
| 544 |
|
| 545 |
|
| 546 |
|
| 547 |
|
| 548 |
public function testHeaderValidation( array $requestData, $expectedStatus ) { |
| 549 |
$request = new RequestData( $requestData ); |
| 550 |
|
| 551 |
$handler = $this->newHandler( [] ); |
| 552 |
|
| 553 |
$exception = $this->executeHandlerAndGetHttpException( $handler, $request ); |
| 554 |
|
| 555 |
$this->assertSame( $expectedStatus, $exception->getCode(), 'HTTP status' ); |
| 556 |
} |
| 557 |
|
| 558 |
|
| 559 |
|
| 560 |
|
| 561 |
public static function provideErrorMapping() { |
| 562 |
yield "missingtitle" => [ |
| 563 |
new ApiUsageException( null, Status::newFatal( 'apierror-missingtitle' ) ), |
| 564 |
new LocalizedHttpException( new MessageValue( 'apierror-missingtitle' ), 404 ), |
| 565 |
]; |
| 566 |
yield "protectedpage" => [ |
| 567 |
new ApiUsageException( null, Status::newFatal( 'apierror-protectedpage' ) ), |
| 568 |
new LocalizedHttpException( new MessageValue( 'apierror-protectedpage' ), 403 ), |
| 569 |
]; |
| 570 |
yield "articleexists" => [ |
| 571 |
new ApiUsageException( null, Status::newFatal( 'apierror-articleexists' ) ), |
| 572 |
new LocalizedHttpException( |
| 573 |
new MessageValue( 'rest-update-cannot-create-page', [ 'Foo' ] ), |
| 574 |
409 |
| 575 |
), |
| 576 |
]; |
| 577 |
yield "editconflict" => [ |
| 578 |
new ApiUsageException( null, Status::newFatal( 'apierror-editconflict' ) ), |
| 579 |
new LocalizedHttpException( new MessageValue( 'apierror-editconflict' ), 409 ), |
| 580 |
]; |
| 581 |
yield "ratelimited" => [ |
| 582 |
new ApiUsageException( null, Status::newFatal( 'apierror-ratelimited' ) ), |
| 583 |
new LocalizedHttpException( new MessageValue( 'apierror-ratelimited' ), 429 ), |
| 584 |
]; |
| 585 |
yield "badtoken" => [ |
| 586 |
new ApiUsageException( |
| 587 |
null, |
| 588 |
Status::newFatal( 'apierror-badtoken', Message::plaintextParam( 'BAD' ) ) |
| 589 |
), |
| 590 |
new LocalizedHttpException( |
| 591 |
new MessageValue( |
| 592 |
'apierror-badtoken', |
| 593 |
[ new ScalarParam( ParamType::PLAINTEXT, 'BAD' ) ] |
| 594 |
), 403 |
| 595 |
), |
| 596 |
]; |
| 597 |
|
| 598 |
|
| 599 |
yield "no-direct-editing" => [ |
| 600 |
new ApiUsageException( null, Status::newFatal( 'apierror-no-direct-editing' ) ), |
| 601 |
new LocalizedHttpException( new MessageValue( 'apierror-no-direct-editing' ), 400 ), |
| 602 |
]; |
| 603 |
yield "badformat" => [ |
| 604 |
new ApiUsageException( null, Status::newFatal( 'apierror-badformat' ) ), |
| 605 |
new LocalizedHttpException( new MessageValue( 'apierror-badformat' ), 400 ), |
| 606 |
]; |
| 607 |
yield "emptypage" => [ |
| 608 |
new ApiUsageException( null, Status::newFatal( 'apierror-emptypage' ) ), |
| 609 |
new LocalizedHttpException( new MessageValue( 'apierror-emptypage' ), 400 ), |
| 610 |
]; |
| 611 |
} |
| 612 |
|
| 613 |
public function testErrorMapping() { |
| 614 |
foreach ( $this->provideErrorMapping() as $expected ) { |
| 615 |
$apiUsageException = $expected[0]; |
| 616 |
$expectedHttpException = $expected[1]; |
| 617 |
$requestData = [ |
| 618 |
'method' => 'POST', |
| 619 |
'pathParams' => [ 'title' => 'Foo' ], |
| 620 |
'headers' => [ |
| 621 |
'Content-Type' => 'application/json', |
| 622 |
], |
| 623 |
'bodyContents' => json_encode( [ |
| 624 |
'source' => 'Lorem Ipsum', |
| 625 |
'comment' => 'Testing', |
| 626 |
'content_model' => CONTENT_MODEL_WIKITEXT, |
| 627 |
] ), |
| 628 |
]; |
| 629 |
$request = new RequestData( $requestData ); |
| 630 |
|
| 631 |
$handler = $this->newHandler( [], $apiUsageException ); |
| 632 |
|
| 633 |
$exception = $this->executeHandlerAndGetHttpException( $handler, $request ); |
| 634 |
|
| 635 |
$this->assertSame( $expectedHttpException->getMessage(), $exception->getMessage() ); |
| 636 |
$this->assertSame( $expectedHttpException->getCode(), $exception->getCode(), 'HTTP status' ); |
| 637 |
|
| 638 |
$errorData = $exception->getErrorData(); |
| 639 |
if ( $expectedHttpException->getErrorData() ) { |
| 640 |
foreach ( $expectedHttpException->getErrorData() as $key => $value ) { |
| 641 |
$this->assertSame( $value, $errorData[$key], 'Error data key $key' ); |
| 642 |
} |
| 643 |
} |
| 644 |
|
| 645 |
if ( $expectedHttpException instanceof LocalizedHttpException ) { |
| 646 |
|
| 647 |
$this->assertEquals( |
| 648 |
$expectedHttpException->getMessageValue(), |
| 649 |
$exception->getMessageValue() |
| 650 |
); |
| 651 |
} |
| 652 |
} |
| 653 |
} |
| 654 |
|
| 655 |
public function testConflictOutput() { |
| 656 |
$requestData = [ |
| 657 |
'method' => 'POST', |
| 658 |
'pathParams' => [ 'title' => 'Foo' ], |
| 659 |
'headers' => [ |
| 660 |
'Content-Type' => 'application/json', |
| 661 |
], |
| 662 |
'bodyContents' => json_encode( [ |
| 663 |
'latest' => [ |
| 664 |
'id' => 17, |
| 665 |
], |
| 666 |
'source' => 'Lorem Ipsum', |
| 667 |
'comment' => 'Testing' |
| 668 |
] ), |
| 669 |
]; |
| 670 |
$request = new RequestData( $requestData ); |
| 671 |
|
| 672 |
$apiUsageException = new ApiUsageException( null, Status::newFatal( 'apierror-editconflict' ) ); |
| 673 |
$handler = $this->newHandler( [], $apiUsageException ); |
| 674 |
$handler->setJsonDiffFunction( [ $this, 'fakeJsonDiff' ] ); |
| 675 |
|
| 676 |
$exception = $this->executeHandlerAndGetHttpException( $handler, $request ); |
| 677 |
|
| 678 |
$this->assertSame( 409, $exception->getCode(), 'HTTP status' ); |
| 679 |
|
| 680 |
$expectedData = [ |
| 681 |
'local' => [ |
| 682 |
'from' => 'Content of revision 17', |
| 683 |
'to' => 'Lorem Ipsum', |
| 684 |
], |
| 685 |
'remote' => [ |
| 686 |
'from' => 'Content of revision 17', |
| 687 |
'to' => 'Current content of 0:Foo', |
| 688 |
], |
| 689 |
'base' => 17, |
| 690 |
'current' => 1234 |
| 691 |
]; |
| 692 |
|
| 693 |
$errorData = $exception->getErrorData(); |
| 694 |
foreach ( $expectedData as $key => $value ) { |
| 695 |
$this->assertSame( $value, $errorData[$key], "Error data key $key" ); |
| 696 |
} |
| 697 |
} |
| 698 |
|
| 699 |
public function fakeJsonDiff( $fromText, $toText ) { |
| 700 |
return FormatJson::encode( [ |
| 701 |
'from' => $fromText, |
| 702 |
'to' => $toText |
| 703 |
] ); |
| 704 |
} |
| 705 |
|
| 706 |
} |