Debian Codegraph @

tests/phpunit/integration/includes/page/RollbackPageTest.php

raw symbols SCIP
1 <?php
2
3 namespace MediaWiki\Tests\Page;
4
5 use DatabaseLogEntry;
6 use MediaWiki\Content\JsonContent;
7 use MediaWiki\Content\WikitextContent;
8 use MediaWiki\MainConfigNames;
9 use MediaWiki\Page\PageIdentity;
10 use MediaWiki\Page\PageIdentityValue;
11 use MediaWiki\Page\RollbackPage;
12 use MediaWiki\Permissions\Authority;
13 use MediaWiki\Revision\SlotRecord;
14 use MediaWiki\Tests\Unit\MockServiceDependenciesTrait;
15 use MediaWiki\Tests\Unit\Permissions\MockAuthorityTrait;
16 use MediaWiki\Tests\User\TempUser\TempUserTestTrait;
17 use MediaWiki\Title\Title;
18 use MediaWiki\User\User;
19 use MediaWiki\User\UserFactory;
20 use MediaWiki\User\UserIdentityValue;
21 use MediaWikiIntegrationTestCase;
22 use RecentChange;
23 use Wikimedia\Rdbms\ReadOnlyMode;
24 use WikiPage;
25
26 /**
27 * @group Database
28 * @covers \MediaWiki\Page\RollbackPage
29 * @coversDefaultClass \MediaWiki\Page\RollbackPage
30 * @method RollbackPage newServiceInstance(string $serviceClass, array $parameterOverrides)
31 */
32 class RollbackPageTest extends MediaWikiIntegrationTestCase {
33 use MockAuthorityTrait;
34 use MockServiceDependenciesTrait;
35 use TempUserTestTrait;
36
37 protected function setUp(): void {
38 parent::setUp();
39
40 $this->overrideConfigValue( MainConfigNames::UseRCPatrol, true );
41 }
42
43 public function provideAuthorize() {
44 yield 'Allowed' => [
45 'authority' => $this->mockRegisteredUltimateAuthority(),
46 'expect' => true,
47 ];
48 yield 'No edit' => [
49 'authority' => $this->mockRegisteredAuthorityWithoutPermissions( [ 'edit' ] ),
50 'expect' => true,
51 ];
52 yield 'No rollback' => [
53 'authority' => $this->mockRegisteredAuthorityWithoutPermissions( [ 'rollback' ] ),
54 'expect' => true,
55 ];
56 }
57
58 /**
59 * @covers ::authorizeRollback
60 * @dataProvider provideAuthorize
61 */
62 public function testAuthorize( Authority $authority, bool $expect ) {
63 $this->assertSame(
64 $expect,
65 $this->getServiceContainer()
66 ->getRollbackPageFactory()
67 ->newRollbackPage(
68 new PageIdentityValue( 10, NS_MAIN, 'Test', PageIdentity::LOCAL ),
69 $authority,
70 new UserIdentityValue( 0, '127.0.0.1' )
71 )
72 ->authorizeRollback()
73 ->isGood()
74 );
75 }
76
77 public function testAuthorizeReadOnly() {
78 $mockReadOnly = $this->createMock( ReadOnlyMode::class );
79 $mockReadOnly->method( 'isReadOnly' )->willReturn( true );
80 $rollback = $this->newServiceInstance( RollbackPage::class, [
81 'readOnlyMode' => $mockReadOnly,
82 'performer' => $this->mockRegisteredUltimateAuthority()
83 ] );
84 $this->assertStatusNotOk( $rollback->authorizeRollback() );
85 }
86
87 /**
88 * @covers ::authorizeRollback
89 */
90 public function testAuthorizePingLimiter() {
91 $performer = $this->mockRegisteredUltimateAuthority();
92 $userMock = $this->createMock( User::class );
93 $userMock->method( 'pingLimiter' )
94 ->willReturnMap( [
95 [ 'rollback', 1, false ],
96 [ 'edit', 1, false ],
97 ] );
98 $userFactoryMock = $this->createMock( UserFactory::class );
99 $userFactoryMock->method( 'newFromAuthority' )
100 ->with( $performer )
101 ->willReturn( $userMock );
102 $rollbackPage = $this->newServiceInstance( RollbackPage::class, [
103 'performer' => $performer,
104 'userFactory' => $userFactoryMock
105 ] );
106 $this->assertStatusGood( $rollbackPage->authorizeRollback() );
107 }
108
109 public function testRollbackNotAllowed() {
110 $this->assertStatusNotOk( $this->newServiceInstance( RollbackPage::class, [
111 'performer' => $this->mockRegisteredNullAuthority()
112 ] )->rollbackIfAllowed() );
113 }
114
115 public function testRollback() {
116 $admin = $this->getTestSysop()->getUser();
117 $user1 = $this->getTestUser()->getUser();
118 // Use the confirmed group for user2 to make sure the user is different
119 $user2 = $this->getTestUser( [ 'confirmed' ] )->getUser();
120
121 $page = $this->getServiceContainer()->getWikiPageFactory()->newFromTitle( Title::newFromText( __METHOD__ ) );
122 // Make some edits
123 $text = "one";
124 $status1 = $this->editPage( $page, $text, "section one", NS_MAIN, $admin );
125 $this->assertStatusGood( $status1, 'edit 1 success' );
126
127 $text .= "\n\ntwo";
128 $status2 = $this->editPage( $page, $text, "adding section two", NS_MAIN, $user1 );
129 $this->assertStatusGood( $status2, 'edit 2 success' );
130
131 $text .= "\n\nthree";
132 $status3 = $this->editPage( $page, $text, "adding section three", NS_MAIN, $user2 );
133 $this->assertStatusGood( $status3, 'edit 3 success' );
134
135 $rev1 = $status1->getNewRevision();
136 $rev2 = $status2->getNewRevision();
137 $rev3 = $status3->getNewRevision();
138
139 $revisionStore = $this->getServiceContainer()->getRevisionStore();
140 /**
141 * We are having issues with doRollback spuriously failing. Apparently
142 * the last revision somehow goes missing or not committed under some
143 * circumstances. So, make sure the revisions have the correct usernames.
144 */
145 $this->assertEquals(
146 3,
147 $revisionStore->countRevisionsByPageId( $this->getDb(), $page->getId() )
148 );
149 $this->assertEquals( $admin->getName(), $rev1->getUser()->getName() );
150 $this->assertEquals( $user1->getName(), $rev2->getUser()->getName() );
151 $this->assertEquals( $user2->getName(), $rev3->getUser()->getName() );
152
153 $rc3 = $revisionStore->getRecentChange( $rev3 );
154 $this->assertEquals(
155 RecentChange::PRC_UNPATROLLED,
156 $rc3->getAttribute( 'rc_patrolled' )
157 );
158
159 // Now, try the actual rollback
160 $rollbackStatus = $this->getServiceContainer()
161 ->getRollbackPageFactory()
162 ->newRollbackPage( $page, $admin, $user2 )
163 ->rollbackIfAllowed();
164 $this->assertStatusGood( $rollbackStatus );
165
166 $this->assertEquals(
167 $rev2->getSha1(),
168 $page->getRevisionRecord()->getSha1(),
169 "rollback did not revert to the correct revision" );
170 $this->assertEquals( "one\n\ntwo", $page->getContent()->getText() );
171
172 $rc = $revisionStore->getRecentChange( $page->getRevisionRecord() );
173 $rc3 = $revisionStore->getRecentChange( $rev3 );
174
175 $this->assertNotNull( $rc, 'RecentChanges entry' );
176 $this->assertEquals(
177 RecentChange::PRC_AUTOPATROLLED,
178 $rc->getAttribute( 'rc_patrolled' ),
179 'rc_patrolled'
180 );
181 $this->assertEquals(
182 RecentChange::PRC_AUTOPATROLLED,
183 $rc3->getAttribute( 'rc_patrolled' )
184 );
185
186 $mainSlot = $page->getRevisionRecord()->getSlot( SlotRecord::MAIN );
187 $this->assertTrue( $mainSlot->isInherited(), 'isInherited' );
188 $this->assertSame( $rev2->getId(), $mainSlot->getOrigin(), 'getOrigin' );
189 }
190
191 public function testRollbackFailSameContent() {
192 $page = $this->getServiceContainer()->getWikiPageFactory()->newFromTitle( Title::newFromText( __METHOD__ ) );
193 $admin = $this->getTestSysop()->getUser();
194 $user1 = $this->getTestUser( [ 'sysop' ] )->getUser();
195
196 [ 'revision-one' => $rev1 ] = $this->prepareForRollback( $admin, $user1, $page );
197
198 $rollbackResult = $this->getServiceContainer()
199 ->getRollbackPageFactory()
200 ->newRollbackPage( $page, $admin, $user1 )
201 ->rollbackIfAllowed();
202 $this->assertStatusGood( $rollbackResult );
203
204 # now, try the rollback again
205 $rollbackResult = $this->getServiceContainer()
206 ->getRollbackPageFactory()
207 ->newRollbackPage( $page, $admin, $user1 )
208 ->rollback();
209 $this->assertStatusError( 'alreadyrolled', $rollbackResult );
210
211 $this->assertEquals( $rev1->getSha1(), $page->getRevisionRecord()->getSha1(),
212 "rollback did not revert to the correct revision" );
213 $this->assertEquals( "one", $page->getContent()->getText() );
214 }
215
216 public function testRollbackFailNotExisting() {
217 $rollbackStatus = $this->getServiceContainer()
218 ->getRollbackPageFactory()
219 ->newRollbackPage(
220 new PageIdentityValue( 0, NS_MAIN, __METHOD__, PageIdentityValue::LOCAL ),
221 $this->mockRegisteredUltimateAuthority(),
222 new UserIdentityValue( 0, '127.0.0.1' )
223 )
224 ->rollback();
225 $this->assertStatusError( 'notanarticle', $rollbackStatus );
226 }
227
228 /**
229 * @param Authority $user1
230 * @param Authority $user2
231 * @param WikiPage $page
232 * @return array with info about created page:
233 * 'revision-one' => RevisionRecord
234 * 'revision-two' => RevisionRecord
235 */
236 private function prepareForRollback( Authority $user1, Authority $user2, WikiPage $page ): array {
237 $result = [];
238 $text = "one";
239 $status = $this->editPage( $page, $text, "section one", NS_MAIN, $user1 );
240 $this->assertStatusGood( $status, 'edit 1 success' );
241 $result['revision-one'] = $status->getNewRevision();
242
243 $text .= "\n\ntwo";
244 $status = $this->editPage( $page, $text, "adding section two", NS_MAIN, $user2 );
245 $this->assertStatusGood( $status, 'edit 2 success' );
246 $result['revision-two'] = $status->getNewRevision();
247 return $result;
248 }
249
250 public function testRollbackTagging() {
251 if ( !in_array( 'mw-rollback', $this->getServiceContainer()->getChangeTagsStore()->getSoftwareTags() ) ) {
252 $this->markTestSkipped( 'Rollback tag deactivated, skipped the test.' );
253 }
254
255 $page = $this->getServiceContainer()->getWikiPageFactory()->newFromTitle( Title::newFromText( __METHOD__ ) );
256 $admin = $this->getTestSysop()->getUser();
257 $user1 = $this->getTestUser()->getUser();
258
259 $this->prepareForRollback( $admin, $user1, $page );
260
261 $rollbackResult = $this->getServiceContainer()
262 ->getRollbackPageFactory()
263 ->newRollbackPage( $page, $admin, $user1 )
264 ->setChangeTags( [ 'tag' ] )
265 ->rollbackIfAllowed();
266 $this->assertStatusGood( $rollbackResult );
267 $this->assertContains( 'mw-rollback', $rollbackResult->getValue()['tags'] );
268 $this->assertContains( 'tag', $rollbackResult->getValue()['tags'] );
269 }
270
271 public function testRollbackBot() {
272 $page = $this->getServiceContainer()->getWikiPageFactory()->newFromTitle( Title::newFromText( __METHOD__ ) );
273 $admin = $this->getTestSysop()->getUser();
274 $user1 = $this->getTestUser()->getUser();
275
276 $this->prepareForRollback( $admin, $user1, $page );
277
278 $rollbackResult = $this->getServiceContainer()
279 ->getRollbackPageFactory()
280 ->newRollbackPage( $page, $admin, $user1 )
281 ->markAsBot( true )
282 ->rollbackIfAllowed();
283 $this->assertStatusGood( $rollbackResult );
284 $rc = $this->getServiceContainer()->getRevisionStore()->getRecentChange( $page->getRevisionRecord() );
285 $this->assertNotNull( $rc );
286 $this->assertSame( '1', $rc->getAttribute( 'rc_bot' ) );
287 }
288
289 public function testRollbackBotNotAllowed() {
290 $page = $this->getServiceContainer()->getWikiPageFactory()->newFromTitle( Title::newFromText( __METHOD__ ) );
291 $admin = $this->mockUserAuthorityWithoutPermissions(
292 $this->getTestSysop()->getUser(), [ 'markbotedits', 'bot' ] );
293 $user1 = $this->getTestUser()->getUser();
294
295 $this->prepareForRollback( $admin, $user1, $page );
296
297 $rollbackResult = $this->getServiceContainer()
298 ->getRollbackPageFactory()
299 ->newRollbackPage( $page, $admin, $user1 )
300 ->markAsBot( true )
301 ->rollbackIfAllowed();
302 $this->assertStatusGood( $rollbackResult );
303 $rc = $this->getServiceContainer()->getRevisionStore()->getRecentChange( $page->getRevisionRecord() );
304 $this->assertNotNull( $rc );
305 $this->assertSame( '0', $rc->getAttribute( 'rc_bot' ) );
306 }
307
308 public static function provideRollbackPatrolAndBot() {
309 yield 'mark as bot' => [ true ];
310 yield 'do not mark as bot' => [ false ];
311 }
312
313 /**
314 * @dataProvider provideRollbackPatrolAndBot
315 */
316 public function testRollbackPatrolAndBot( bool $markAsBot ) {
317 $page = $this->getServiceContainer()->getWikiPageFactory()->newFromTitle( Title::newFromText( __METHOD__ ) );
318 $admin = $this->getTestSysop()->getUser();
319 $user1 = $this->getTestUser()->getUser();
320
321 [
322 'revision-one' => $rev1,
323 'revision-two' => $rev2,
324 ] = $this->prepareForRollback( $admin, $user1, $page );
325
326 $text = "one\n\ntwo\n\nthree";
327 $status = $this->editPage( $page, $text, "adding section three", NS_MAIN, $user1 );
328 $this->assertStatusGood( $status, 'edit 3 success' );
329 $rev3 = $status->getNewRevision();
330
331 $revisionStore = $this->getServiceContainer()->getRevisionStore();
332
333 $rc1 = $revisionStore->getRecentChange( $rev1 );
334 $this->assertEquals(
335 RecentChange::PRC_AUTOPATROLLED,
336 $rc1->getAttribute( 'rc_patrolled' )
337 );
338
339 // manually patrol the first reverted revision
340 $rc2 = $revisionStore->getRecentChange( $rev2 );
341 $rc2->reallyMarkPatrolled();
342
343 $rc3 = $revisionStore->getRecentChange( $rev3 );
344 $this->assertEquals(
345 RecentChange::PRC_UNPATROLLED,
346 $rc3->getAttribute( 'rc_patrolled' )
347 );
348
349 $rollbackResult = $this->getServiceContainer()
350 ->getRollbackPageFactory()
351 ->newRollbackPage( $page, $admin, $user1 )
352 ->markAsBot( $markAsBot )
353 ->rollback();
354 $this->assertStatusGood( $rollbackResult );
355
356 $rc1 = $revisionStore->getRecentChange( $rev1 );
357 $this->assertEquals(
358 RecentChange::PRC_AUTOPATROLLED,
359 $rc1->getAttribute( 'rc_patrolled' )
360 );
361 $this->assertFalse( (bool)$rc1->getAttribute( 'rc_bot' ) );
362
363 $rc2 = $revisionStore->getRecentChange( $rev2 );
364 $this->assertEquals(
365 RecentChange::PRC_PATROLLED,
366 $rc2->getAttribute( 'rc_patrolled' )
367 );
368 $this->assertSame( $markAsBot, (bool)$rc2->getAttribute( 'rc_bot' ) );
369
370 $rc3 = $revisionStore->getRecentChange( $rev3 );
371 $this->assertEquals(
372 RecentChange::PRC_AUTOPATROLLED,
373 $rc3->getAttribute( 'rc_patrolled' )
374 );
375 $this->assertSame( $markAsBot, (bool)$rc3->getAttribute( 'rc_bot' ) );
376 }
377
378 public function testRollbackCustomSummary() {
379 $page = $this->getServiceContainer()->getWikiPageFactory()->newFromTitle( Title::newFromText( __METHOD__ ) );
380 $admin = $this->getTestSysop()->getUser();
381 $user1 = $this->getTestUser()->getUser();
382
383 $revisions = $this->prepareForRollback( $admin, $user1, $page );
384
385 $rollbackResult = $this->getServiceContainer()
386 ->getRollbackPageFactory()
387 ->newRollbackPage( $page, $admin, $user1 )
388 ->setSummary( 'TEST! $1 $2 $3 $4 $5 $6' )
389 ->rollbackIfAllowed();
390 $this->assertStatusGood( $rollbackResult );
391 $targetTimestamp = $this->getServiceContainer()
392 ->getContentLanguage()
393 ->timeanddate( $revisions['revision-one']->getTimestamp() );
394 $currentTimestamp = $this->getServiceContainer()
395 ->getContentLanguage()
396 ->timeanddate( $revisions['revision-two']->getTimestamp() );
397 $expectedSummary = implode( ' ', [
398 'TEST!',
399 $admin->getName(),
400 $user1->getName(),
401 $revisions['revision-one']->getId(),
402 $targetTimestamp,
403 $revisions['revision-two']->getId(),
404 $currentTimestamp
405 ] );
406 $this->assertSame( $expectedSummary, $page->getRevisionRecord()->getComment()->text );
407 $rc = $this->getServiceContainer()->getRevisionStore()->getRecentChange( $page->getRevisionRecord() );
408 $this->assertNotNull( $rc );
409 $this->assertSame( $expectedSummary, $rc->getAttribute( 'rc_comment' ) );
410 }
411
412 public function testRollbackChangesContentModel() {
413 $page = $this->getServiceContainer()->getWikiPageFactory()->newFromTitle( Title::newFromText( __METHOD__ ) );
414 $admin = $this->getTestSysop()->getUser();
415 $user1 = $this->getTestUser()->getUser();
416
417 $status1 = $this->editPage( $page, new JsonContent( '{}' ),
418 "it's json", NS_MAIN, $admin );
419 $this->assertStatusGood( $status1, 'edit 1 success' );
420
421 $status1 = $this->editPage( $page, new WikitextContent( 'bla' ),
422 "no, it's wikitext", NS_MAIN, $user1 );
423 $this->assertStatusGood( $status1, 'edit 2 success' );
424
425 $rollbackResult = $this->getServiceContainer()
426 ->getRollbackPageFactory()
427 ->newRollbackPage( $page, $admin, $user1 )
428 ->setSummary( 'TESTING' )
429 ->rollbackIfAllowed();
430 $this->assertStatusGood( $rollbackResult );
431 $logRow = DatabaseLogEntry::newSelectQueryBuilder( $this->getDb() )
432 ->where( [ 'log_namespace' => NS_MAIN, 'log_title' => __METHOD__, 'log_type' => 'contentmodel' ] )
433 ->caller( __METHOD__ )->fetchRow();
434 $this->assertNotNull( $logRow );
435 $this->assertSame( $admin->getUser()->getName(), $logRow->user_name );
436 $this->assertSame( 'TESTING', $logRow->log_comment_text );
437 }
438
439 public function testRollbackOfIPRevisionWhenTemporaryAccountsAreEnabledT371094() {
440 // Set up the test page to have one revision by a user and then the second revision performed by an IP address.
441 $this->disableAutoCreateTempUser();
442 $page = $this->getServiceContainer()->getWikiPageFactory()->newFromTitle( Title::newFromText( __METHOD__ ) );
443 $admin = $this->getTestSysop()->getUser();
444 $anonUser = $this->mockAnonUltimateAuthority();
445
446 $this->prepareForRollback( $admin, $anonUser, $page );
447
448 // Enable temporary accounts and then perform the rollback
449 $this->enableAutoCreateTempUser();
450 $rollbackResult = $this->getServiceContainer()
451 ->getRollbackPageFactory()
452 ->newRollbackPage( $page, $admin, $anonUser->getUser() )
453 ->rollbackIfAllowed();
454 // Ensure that the rollback worked as expected, as previously this failed with an exception if
455 // rolling back a IP revision.
456 $this->assertStatusGood( $rollbackResult );
457 }
458 }