Debian Codegraph @

tests/phpunit/integration/includes/Storage/UndoIntegrationTest.php

raw symbols SCIP
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 * Integration tests for undos.
24 * TODO: This should also test edits with multiple slots.
25 *
26 * @covers \McrUndoAction
27 * @covers \WikiPage
28 * @covers \MediaWiki\EditPage\EditPage
29 *
30 * @group Database
31 * @group medium
32 */
33 class UndoIntegrationTest extends MediaWikiIntegrationTestCase {
34
35 private const PAGE_NAME = 'McrUndoTestPage';
36
37 /**
38 * Creates a new McrUndoAction object for testing.
39 *
40 * @param IContextSource $context
41 * @param Article $article
42 * @param array $params POST/GET parameters passed to the action on submit
43 *
44 * @return McrUndoAction
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 // Instead of trying to actually display anything, just initialize the class.
76 $this->checkCanExecute( $this->getUser() );
77 }
78 };
79 }
80
81 /**
82 * Convenience function for setting up a test page and filling it with edits.
83 * @param string[] $revisions
84 *
85 * @return array
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 * @param string $newContent
100 * @param array $revisionIds
101 * @param bool $isExactRevert
102 * @param int|string $oldestRevertedRevIndex
103 * @param int|string $newestRevertedRevIndex
104 * @param int|string $originalRevIndex
105 */
106 private function setPageSaveCompleteHook(
107 string $newContent,
108 array $revisionIds,
109 bool $isExactRevert,
110 $oldestRevertedRevIndex,
111 $newestRevertedRevIndex,
112 $originalRevIndex
113 ) {
114 // set up a temporary hook with asserts
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 /** @var WikitextContent $mainContent */
169 $this->assertSame(
170 $newContent,
171 $mainContent->getText(),
172 'RevisionRecord::getContent()'
173 );
174 }
175 );
176 }
177
178 /**
179 * Provides test cases for well-formed undos.
180 *
181 * @return array[]
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 * Provides test cases of undos with incomplete parameters.
239 * This should be handled well by EditPage and WikiPage.
240 * McrUndoAction just refuses to do anything.
241 *
242 * @return array[]
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 * Test how McrUndoAction cooperates with the PageUpdater by looking at values provided
275 * by the PageSaveComplete hook.
276 *
277 * @dataProvider provideUndos
278 *
279 * @param string[] $revisions
280 * @param string $newContent
281 * @param int|string $undoafterIndex
282 * @param int|string $undoIndex
283 * @param bool $isExactRevert
284 * @param int|string $oldestRevertedRevIndex
285 * @param int|string $newestRevertedRevIndex
286 * @param int|string $originalRevIndex
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 // This should initialize the action properly.
314 $mcrUndoAction->show();
315
316 // Set the hook and submit the request
317 $this->setPageSaveCompleteHook(
318 $newContent,
319 $revisionIds,
320 $isExactRevert,
321 $oldestRevertedRevIndex,
322 $newestRevertedRevIndex,
323 $originalRevIndex
324 );
325 $mcrUndoAction->onSubmit( [] );
326 }
327
328 /**
329 * Test how WikiPage cooperates with the PageUpdater by looking at values
330 * provided by the PageSaveComplete hook.
331 *
332 * @dataProvider provideUndos
333 * @dataProvider provideIncompleteUndos
334 *
335 * @param string[] $revisions
336 * @param string $newContent
337 * @param int|string $undoafterIndex
338 * @param int|string $undoIndex
339 * @param bool $isExactRevert
340 * @param int|string $oldestRevertedRevIndex
341 * @param int|string $newestRevertedRevIndex
342 * @param int|string $originalRevIndex
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 // Set the hook with asserts
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 * Test how EditPage and WikiPage work together and with the PageUpdater by looking
380 * at values provided by the PageSaveComplete hook.
381 *
382 * @dataProvider provideUndos
383 * @dataProvider provideIncompleteUndos
384 *
385 * @param string[] $revisions
386 * @param string $newContent
387 * @param int|string $undoafterIndex
388 * @param int|string $undoIndex
389 * @param bool $isExactRevert
390 * @param int|string $oldestRevertedRevIndex
391 * @param int|string $newestRevertedRevIndex
392 * @param int|string $originalRevIndex
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 // Set the hook with asserts
412 $this->setPageSaveCompleteHook(
413 $newContent,
414 $revisionIds,
415 $isExactRevert,
416 $oldestRevertedRevIndex,
417 $newestRevertedRevIndex,
418 $originalRevIndex
419 );
420
421 $request = new FauxRequest(
422 [
423 // We kind of let EditPage cheat here by providing the content of the page
424 // after the undo, but automatic conflict resolution is not the point of
425 // this test anyway.
426 'wpTextbox1' => $newContent,
427 'wpEditToken' => $this->getTestSysop()->getUser()->getEditToken(),
428 // These two parameters are the important ones here
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 * Test the case where the user undoes some edits, but applies additional changes before
446 * saving. EditPage should detect that and not mark such an edit as a revert.
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 // set up a temporary hook with asserts
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 // Just ensuring that the edit was not marked as a revert should be enough
470 $this->assertFalse(
471 $editResult->isRevert(),
472 'EditResult::isRevert()'
473 );
474 }
475 );
476
477 $request = new FauxRequest(
478 [
479 // We emulate the user applying additional changes on top of the undo.
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 * Test whether EditPage correctly handles situations where an undo is impossible.
499 * Ensures T262463 is fixed.
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 // set up a temporary hook with asserts
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 // We leave the "top" content in the textbox, as the undo should have failed
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 }