| 1 |
<?php |
| 2 |
|
| 3 |
namespace MediaWiki\Tests\Storage; |
| 4 |
|
| 5 |
use Article; |
| 6 |
use McrUndoAction; |
| 7 |
use MediaWiki\Content\WikitextContent; |
| 8 |
use MediaWiki\Context\DerivativeContext; |
| 9 |
use MediaWiki\Context\IContextSource; |
| 10 |
use MediaWiki\Context\RequestContext; |
| 11 |
use MediaWiki\EditPage\EditPage; |
| 12 |
use MediaWiki\Output\OutputPage; |
| 13 |
use MediaWiki\Request\FauxRequest; |
| 14 |
use MediaWiki\Revision\RevisionStoreRecord; |
| 15 |
use MediaWiki\Revision\SlotRecord; |
| 16 |
use MediaWiki\Storage\EditResult; |
| 17 |
use MediaWiki\Title\Title; |
| 18 |
use MediaWiki\User\User; |
| 19 |
use MediaWikiIntegrationTestCase; |
| 20 |
use WikiPage; |
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
class UndoIntegrationTest extends MediaWikiIntegrationTestCase { |
| 34 |
|
| 35 |
private const PAGE_NAME = 'McrUndoTestPage'; |
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
private function makeNewMcrUndoAction( |
| 47 |
IContextSource $context, |
| 48 |
Article $article, |
| 49 |
array $params |
| 50 |
): McrUndoAction { |
| 51 |
$request = new FauxRequest( $params ); |
| 52 |
$request->setVal( 'wpSave', '' ); |
| 53 |
$context->setRequest( $request ); |
| 54 |
|
| 55 |
$outputPage = $this->createMock( OutputPage::class ); |
| 56 |
$context->setOutput( $outputPage ); |
| 57 |
$context->setUser( $this->getTestSysop()->getUser() ); |
| 58 |
|
| 59 |
$services = $this->getServiceContainer(); |
| 60 |
$revisionRenderer = $services->getRevisionRenderer(); |
| 61 |
$revisionLookup = $services->getRevisionLookup(); |
| 62 |
$readOnlyMode = $services->getReadOnlyMode(); |
| 63 |
$commentFormatter = $services->getCommentFormatter(); |
| 64 |
$config = $services->getMainConfig(); |
| 65 |
return new class( |
| 66 |
$article, |
| 67 |
$context, |
| 68 |
$readOnlyMode, |
| 69 |
$revisionLookup, |
| 70 |
$revisionRenderer, |
| 71 |
$commentFormatter, |
| 72 |
$config |
| 73 |
) extends McrUndoAction { |
| 74 |
public function show() { |
| 75 |
|
| 76 |
$this->checkCanExecute( $this->getUser() ); |
| 77 |
} |
| 78 |
}; |
| 79 |
} |
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
private function setUpPageForTesting( array $revisions ): array { |
| 88 |
$this->getExistingTestPage( self::PAGE_NAME ); |
| 89 |
$revisionIds = []; |
| 90 |
foreach ( $revisions as $revisionContent ) { |
| 91 |
$revisionIds[] = $this->editPage( self::PAGE_NAME, $revisionContent ) |
| 92 |
->value['revision-record']->getId(); |
| 93 |
} |
| 94 |
$revisionIds['false'] = false; |
| 95 |
return $revisionIds; |
| 96 |
} |
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
private function setPageSaveCompleteHook( |
| 107 |
string $newContent, |
| 108 |
array $revisionIds, |
| 109 |
bool $isExactRevert, |
| 110 |
$oldestRevertedRevIndex, |
| 111 |
$newestRevertedRevIndex, |
| 112 |
$originalRevIndex |
| 113 |
) { |
| 114 |
|
| 115 |
$this->setTemporaryHook( |
| 116 |
'PageSaveComplete', |
| 117 |
function ( |
| 118 |
WikiPage $wikiPage, |
| 119 |
User $user, |
| 120 |
string $summary, |
| 121 |
int $flags, |
| 122 |
RevisionStoreRecord $revisionRecord, |
| 123 |
EditResult $editResult |
| 124 |
) use ( |
| 125 |
$newContent, |
| 126 |
$revisionIds, |
| 127 |
$isExactRevert, |
| 128 |
$oldestRevertedRevIndex, |
| 129 |
$newestRevertedRevIndex, |
| 130 |
$originalRevIndex |
| 131 |
) { |
| 132 |
$this->assertTrue( |
| 133 |
$editResult->isRevert(), |
| 134 |
'EditResult::isRevert()' |
| 135 |
); |
| 136 |
$this->assertSame( |
| 137 |
EditResult::REVERT_UNDO, |
| 138 |
$editResult->getRevertMethod(), |
| 139 |
'EditResult::getRevertMethod()' |
| 140 |
); |
| 141 |
$this->assertArrayEquals( [ 'mw-undo' ], |
| 142 |
$editResult->getRevertTags(), |
| 143 |
false, |
| 144 |
false, |
| 145 |
'EditResult::getRevertTags()' |
| 146 |
); |
| 147 |
$this->assertSame( |
| 148 |
$isExactRevert, |
| 149 |
$editResult->isExactRevert(), |
| 150 |
'EditResult::isExactRevert()' |
| 151 |
); |
| 152 |
$this->assertSame( |
| 153 |
$revisionIds[$oldestRevertedRevIndex], |
| 154 |
$editResult->getOldestRevertedRevisionId(), |
| 155 |
'EditResult::getOldestRevertedRevisionId()' |
| 156 |
); |
| 157 |
$this->assertSame( |
| 158 |
$revisionIds[$newestRevertedRevIndex], |
| 159 |
$editResult->getNewestRevertedRevisionId(), |
| 160 |
'EditResult::getNewestRevertedRevisionId()' |
| 161 |
); |
| 162 |
$this->assertSame( |
| 163 |
$revisionIds[$originalRevIndex], |
| 164 |
$editResult->getOriginalRevisionId(), |
| 165 |
'EditResult::getOriginalRevisionId()' |
| 166 |
); |
| 167 |
$mainContent = $revisionRecord->getContent( SlotRecord::MAIN ); |
| 168 |
|
| 169 |
$this->assertSame( |
| 170 |
$newContent, |
| 171 |
$mainContent->getText(), |
| 172 |
'RevisionRecord::getContent()' |
| 173 |
); |
| 174 |
} |
| 175 |
); |
| 176 |
} |
| 177 |
|
| 178 |
|
| 179 |
|
| 180 |
|
| 181 |
|
| 182 |
|
| 183 |
public static function provideUndos() { |
| 184 |
return [ |
| 185 |
'undoing a single revision' => [ |
| 186 |
[ '1', '2' ], |
| 187 |
'1', |
| 188 |
0, |
| 189 |
1, |
| 190 |
true, |
| 191 |
1, |
| 192 |
1, |
| 193 |
0 |
| 194 |
], |
| 195 |
'undoing multiple revisions' => [ |
| 196 |
[ '1', '2', '3', '4' ], |
| 197 |
'1', |
| 198 |
0, |
| 199 |
3, |
| 200 |
true, |
| 201 |
1, |
| 202 |
3, |
| 203 |
0 |
| 204 |
], |
| 205 |
'undoing an intermittent revision' => [ |
| 206 |
[ |
| 207 |
"line 1\n\nline 2\n\nline3", |
| 208 |
"line 1\n\nvandalism\n\nline3", |
| 209 |
"line 1\n\nvandalism\n\nline3 more content" |
| 210 |
], |
| 211 |
"line 1\n\nline 2\n\nline3 more content", |
| 212 |
0, |
| 213 |
1, |
| 214 |
false, |
| 215 |
1, |
| 216 |
1, |
| 217 |
'false' |
| 218 |
], |
| 219 |
'undoing multiple intermittent revisions' => [ |
| 220 |
[ |
| 221 |
"line 1\n\nline 2\n\nline3", |
| 222 |
"line 1\n\nvandalism\n\nline3", |
| 223 |
"line 1\n\nmore vandalism\n\nline3", |
| 224 |
"line 1\n\nmore vandalism\n\nline3 content" |
| 225 |
], |
| 226 |
"line 1\n\nline 2\n\nline3 content", |
| 227 |
0, |
| 228 |
2, |
| 229 |
false, |
| 230 |
1, |
| 231 |
2, |
| 232 |
'false' |
| 233 |
] |
| 234 |
]; |
| 235 |
} |
| 236 |
|
| 237 |
|
| 238 |
|
| 239 |
|
| 240 |
|
| 241 |
|
| 242 |
|
| 243 |
|
| 244 |
public static function provideIncompleteUndos() { |
| 245 |
return [ |
| 246 |
'undoing a revision without undoafter param' => [ |
| 247 |
[ '1', '2' ], |
| 248 |
'1', |
| 249 |
'false', |
| 250 |
1, |
| 251 |
true, |
| 252 |
1, |
| 253 |
1, |
| 254 |
0 |
| 255 |
], |
| 256 |
'undoing an intermittent revision without undoafter param' => [ |
| 257 |
[ |
| 258 |
"line 1\n\nline 2\n\nline3", |
| 259 |
"line 1\n\nvandalism\n\nline3", |
| 260 |
"line 1\n\nvandalism\n\nline3 more content" |
| 261 |
], |
| 262 |
"line 1\n\nline 2\n\nline3 more content", |
| 263 |
'false', |
| 264 |
1, |
| 265 |
false, |
| 266 |
1, |
| 267 |
1, |
| 268 |
'false' |
| 269 |
] |
| 270 |
]; |
| 271 |
} |
| 272 |
|
| 273 |
|
| 274 |
|
| 275 |
|
| 276 |
|
| 277 |
|
| 278 |
|
| 279 |
|
| 280 |
|
| 281 |
|
| 282 |
|
| 283 |
|
| 284 |
|
| 285 |
|
| 286 |
|
| 287 |
|
| 288 |
public function testMcrUndoAction( |
| 289 |
array $revisions, |
| 290 |
string $newContent, |
| 291 |
$undoafterIndex, |
| 292 |
$undoIndex, |
| 293 |
bool $isExactRevert, |
| 294 |
$oldestRevertedRevIndex, |
| 295 |
$newestRevertedRevIndex, |
| 296 |
$originalRevIndex |
| 297 |
) { |
| 298 |
$this->markTestSkippedIfNoDiff3(); |
| 299 |
|
| 300 |
$context = new DerivativeContext( RequestContext::getMain() ); |
| 301 |
$context->setUser( $this->getTestUser()->getUser() ); |
| 302 |
$revisionIds = $this->setUpPageForTesting( $revisions ); |
| 303 |
$article = Article::newFromTitle( Title::newFromText( self::PAGE_NAME ), $context ); |
| 304 |
|
| 305 |
$mcrUndoAction = $this->makeNewMcrUndoAction( |
| 306 |
$context, |
| 307 |
$article, |
| 308 |
[ |
| 309 |
'undoafter' => $revisionIds[$undoafterIndex], |
| 310 |
'undo' => $revisionIds[$undoIndex] |
| 311 |
] |
| 312 |
); |
| 313 |
|
| 314 |
$mcrUndoAction->show(); |
| 315 |
|
| 316 |
|
| 317 |
$this->setPageSaveCompleteHook( |
| 318 |
$newContent, |
| 319 |
$revisionIds, |
| 320 |
$isExactRevert, |
| 321 |
$oldestRevertedRevIndex, |
| 322 |
$newestRevertedRevIndex, |
| 323 |
$originalRevIndex |
| 324 |
); |
| 325 |
$mcrUndoAction->onSubmit( [] ); |
| 326 |
} |
| 327 |
|
| 328 |
|
| 329 |
|
| 330 |
|
| 331 |
|
| 332 |
|
| 333 |
|
| 334 |
|
| 335 |
|
| 336 |
|
| 337 |
|
| 338 |
|
| 339 |
|
| 340 |
|
| 341 |
|
| 342 |
|
| 343 |
|
| 344 |
public function testWikiPage( |
| 345 |
array $revisions, |
| 346 |
string $newContent, |
| 347 |
$undoafterIndex, |
| 348 |
$undoIndex, |
| 349 |
bool $isExactRevert, |
| 350 |
$oldestRevertedRevIndex, |
| 351 |
$newestRevertedRevIndex, |
| 352 |
$originalRevIndex |
| 353 |
) { |
| 354 |
$revisionIds = $this->setUpPageForTesting( $revisions ); |
| 355 |
|
| 356 |
|
| 357 |
$this->setPageSaveCompleteHook( |
| 358 |
$newContent, |
| 359 |
$revisionIds, |
| 360 |
$isExactRevert, |
| 361 |
$oldestRevertedRevIndex, |
| 362 |
$newestRevertedRevIndex, |
| 363 |
$originalRevIndex |
| 364 |
); |
| 365 |
|
| 366 |
$wikiPage = $this->getServiceContainer()->getWikiPageFactory()->newFromTitle( Title::newFromText( self::PAGE_NAME ) ); |
| 367 |
$wikiPage->doUserEditContent( |
| 368 |
new WikitextContent( $newContent ), |
| 369 |
$this->getTestSysop()->getUser(), |
| 370 |
'', |
| 371 |
0, |
| 372 |
$revisionIds[$undoafterIndex], |
| 373 |
[], |
| 374 |
$revisionIds[$undoIndex] |
| 375 |
); |
| 376 |
} |
| 377 |
|
| 378 |
|
| 379 |
|
| 380 |
|
| 381 |
|
| 382 |
|
| 383 |
|
| 384 |
|
| 385 |
|
| 386 |
|
| 387 |
|
| 388 |
|
| 389 |
|
| 390 |
|
| 391 |
|
| 392 |
|
| 393 |
|
| 394 |
public function testEditPage( |
| 395 |
array $revisions, |
| 396 |
string $newContent, |
| 397 |
$undoafterIndex, |
| 398 |
$undoIndex, |
| 399 |
bool $isExactRevert, |
| 400 |
$oldestRevertedRevIndex, |
| 401 |
$newestRevertedRevIndex, |
| 402 |
$originalRevIndex |
| 403 |
) { |
| 404 |
$this->markTestSkippedIfNoDiff3(); |
| 405 |
|
| 406 |
$revisionIds = $this->setUpPageForTesting( $revisions ); |
| 407 |
$context = new DerivativeContext( RequestContext::getMain() ); |
| 408 |
$context->setUser( $this->getTestUser()->getUser() ); |
| 409 |
$article = Article::newFromTitle( Title::newFromText( self::PAGE_NAME ), $context ); |
| 410 |
|
| 411 |
|
| 412 |
$this->setPageSaveCompleteHook( |
| 413 |
$newContent, |
| 414 |
$revisionIds, |
| 415 |
$isExactRevert, |
| 416 |
$oldestRevertedRevIndex, |
| 417 |
$newestRevertedRevIndex, |
| 418 |
$originalRevIndex |
| 419 |
); |
| 420 |
|
| 421 |
$request = new FauxRequest( |
| 422 |
[ |
| 423 |
|
| 424 |
|
| 425 |
|
| 426 |
'wpTextbox1' => $newContent, |
| 427 |
'wpEditToken' => $this->getTestSysop()->getUser()->getEditToken(), |
| 428 |
|
| 429 |
'wpUndidRevision' => $revisionIds[$undoIndex], |
| 430 |
'wpUndoAfter' => $revisionIds[$undoafterIndex], |
| 431 |
'wpStarttime' => wfTimestampNow(), |
| 432 |
'wpUnicodeCheck' => EditPage::UNICODE_CHECK, |
| 433 |
'model' => CONTENT_MODEL_WIKITEXT, |
| 434 |
'format' => CONTENT_FORMAT_WIKITEXT, |
| 435 |
], |
| 436 |
true |
| 437 |
); |
| 438 |
|
| 439 |
$editPage = new EditPage( $article ); |
| 440 |
$editPage->importFormData( $request ); |
| 441 |
$editPage->attemptSave( $result ); |
| 442 |
} |
| 443 |
|
| 444 |
|
| 445 |
|
| 446 |
|
| 447 |
|
| 448 |
public function testDirtyUndo() { |
| 449 |
$revisionIds = $this->setUpPageForTesting( [ |
| 450 |
"line 1\n\nline 2\n\nline3", |
| 451 |
"line 1\n\nvandalism\n\nline3", |
| 452 |
"line 1\n\nvandalism\n\nline3 more content" |
| 453 |
] ); |
| 454 |
$context = new DerivativeContext( RequestContext::getMain() ); |
| 455 |
$context->setUser( $this->getTestUser()->getUser() ); |
| 456 |
$article = Article::newFromTitle( Title::newFromText( self::PAGE_NAME ), $context ); |
| 457 |
|
| 458 |
|
| 459 |
$this->setTemporaryHook( |
| 460 |
'PageSaveComplete', |
| 461 |
function ( |
| 462 |
WikiPage $wikiPage, |
| 463 |
User $user, |
| 464 |
string $summary, |
| 465 |
int $flags, |
| 466 |
RevisionStoreRecord $revisionRecord, |
| 467 |
EditResult $editResult |
| 468 |
) { |
| 469 |
|
| 470 |
$this->assertFalse( |
| 471 |
$editResult->isRevert(), |
| 472 |
'EditResult::isRevert()' |
| 473 |
); |
| 474 |
} |
| 475 |
); |
| 476 |
|
| 477 |
$request = new FauxRequest( |
| 478 |
[ |
| 479 |
|
| 480 |
'wpTextbox1' => "line 1\n\nline 2\n\nline3 more content\n\neven more", |
| 481 |
'wpEditToken' => $this->getTestSysop()->getUser()->getEditToken(), |
| 482 |
'wpUndidRevision' => $revisionIds[1], |
| 483 |
'wpUndoAfter' => $revisionIds[0], |
| 484 |
'wpStarttime' => wfTimestampNow(), |
| 485 |
'wpUnicodeCheck' => EditPage::UNICODE_CHECK, |
| 486 |
'model' => CONTENT_MODEL_WIKITEXT, |
| 487 |
'format' => CONTENT_FORMAT_WIKITEXT, |
| 488 |
], |
| 489 |
true |
| 490 |
); |
| 491 |
|
| 492 |
$editPage = new EditPage( $article ); |
| 493 |
$editPage->importFormData( $request ); |
| 494 |
$editPage->attemptSave( $result ); |
| 495 |
} |
| 496 |
|
| 497 |
|
| 498 |
|
| 499 |
|
| 500 |
|
| 501 |
public function testImpossibleUndo() { |
| 502 |
$revisionIds = $this->setUpPageForTesting( [ |
| 503 |
"line 1\n\nline 2\n\nline3", |
| 504 |
"line 1\n\nvandalism\n\nline3", |
| 505 |
"line 1\n\nvandalism good content\n\nline3 more content" |
| 506 |
] ); |
| 507 |
|
| 508 |
$context = RequestContext::getMain(); |
| 509 |
$article = Article::newFromTitle( Title::newFromText( self::PAGE_NAME ), $context ); |
| 510 |
|
| 511 |
|
| 512 |
$this->setTemporaryHook( |
| 513 |
'PageSaveComplete', |
| 514 |
function ( |
| 515 |
WikiPage $wikiPage, |
| 516 |
User $user, |
| 517 |
string $summary, |
| 518 |
int $flags, |
| 519 |
RevisionStoreRecord $revisionRecord, |
| 520 |
EditResult $editResult |
| 521 |
) { |
| 522 |
$this->assertFalse( |
| 523 |
$editResult->isRevert(), |
| 524 |
'EditResult::isRevert()' |
| 525 |
); |
| 526 |
$this->assertTrue( |
| 527 |
$editResult->isNullEdit(), |
| 528 |
'EditResult::isNullEdit()' |
| 529 |
); |
| 530 |
} |
| 531 |
); |
| 532 |
|
| 533 |
$request = new FauxRequest( |
| 534 |
[ |
| 535 |
|
| 536 |
'wpTextbox1' => "line 1\n\nvandalism good content\n\nline3 more content", |
| 537 |
'wpEditToken' => $this->getTestSysop()->getUser()->getEditToken(), |
| 538 |
'wpUndidRevision' => $revisionIds[1], |
| 539 |
'wpUndoAfter' => $revisionIds[0], |
| 540 |
'wpStarttime' => wfTimestampNow(), |
| 541 |
'wpUnicodeCheck' => EditPage::UNICODE_CHECK, |
| 542 |
'model' => CONTENT_MODEL_WIKITEXT, |
| 543 |
'format' => CONTENT_FORMAT_WIKITEXT, |
| 544 |
], |
| 545 |
true |
| 546 |
); |
| 547 |
|
| 548 |
$editPage = new EditPage( $article ); |
| 549 |
$editPage->importFormData( $request ); |
| 550 |
$editPage->attemptSave( $result ); |
| 551 |
} |
| 552 |
} |