Debian Codegraph @

tests/phpunit/includes/api/ApiRollbackTest.php

raw symbols SCIP
1 <?php
2
3 namespace MediaWiki\Tests\Api;
4
5 use MediaWiki\Api\ApiUsageException;
6 use MediaWiki\MainConfigNames;
7 use MediaWiki\Title\Title;
8
9 /**
10 * Tests for Rollback API.
11 *
12 * @group API
13 * @group Database
14 * @group medium
15 *
16 * @covers MediaWiki\Api\ApiRollback
17 */
18 class ApiRollbackTest extends ApiTestCase {
19
20 protected function setUp(): void {
21 parent::setUp();
22
23 $this->overrideConfigValue( MainConfigNames::WatchlistExpiry, true );
24 }
25
26 public function testProtectWithWatch(): void {
27 $title = Title::makeTitle( NS_MAIN, 'TestProtectWithWatch' );
28 $revisionStore = $this->getServiceContainer()->getRevisionStore();
29
30 $user = $this->getTestUser()->getUser();
31 $sysop = $this->getTestSysop()->getUser();
32
33 // Create page as sysop.
34 $this->editPage( $title, 'Some text', '', NS_MAIN, $sysop );
35
36 // Edit as non-sysop.
37 $this->editPage( $title, 'Vandalism', '', NS_MAIN, $user );
38
39 // Rollback as sysop.
40 $apiResult = $this->doApiRequestWithToken( [
41 'action' => 'rollback',
42 'title' => $title->getPrefixedText(),
43 'user' => $user->getName(),
44 'watchlist' => 'watch',
45 'watchlistexpiry' => '99990123000000',
46 ] )[0];
47
48 // Content of latest revision should match the initial.
49 $latestRev = $revisionStore->getRevisionByTitle( $title );
50 $initialRev = $revisionStore->getFirstRevision( $title );
51 $this->assertTrue( $latestRev->hasSameContent( $initialRev ) );
52 // ...but have different rev IDs.
53 $this->assertNotSame( $latestRev->getId(), $initialRev->getId() );
54
55 // Make sure the API response looks good.
56 $this->assertArrayHasKey( 'rollback', $apiResult );
57 $this->assertSame( $title->getPrefixedText(), $apiResult['rollback']['title'] );
58
59 // And that the page was temporarily watched.
60 $this->assertTrue( $this->getServiceContainer()->getWatchlistManager()->isTempWatched( $sysop, $title ) );
61
62 $recentChange = $revisionStore->getRecentChange( $latestRev );
63 $this->assertSame( '0', $recentChange->getAttribute( 'rc_bot' ) );
64 $this->assertSame( $sysop->getName(), $recentChange->getAttribute( 'rc_user_text' ) );
65 }
66
67 public function testRollbackMarkAsBot() {
68 $revisionStore = $this->getServiceContainer()->getRevisionStore();
69 $title = Title::makeTitle( NS_MAIN, 'ApiRollbackTest::testRollbackMarkAsBot' );
70
71 $user = $this->getTestUser()->getUser();
72 $sysop = $this->getTestSysop()->getUser();
73
74 // Create page as sysop.
75 $this->editPage( $title, 'Some text', '', NS_MAIN, $sysop );
76
77 // Edit as non-sysop.
78 $this->editPage( $title, 'Vandalism', '', NS_MAIN, $user );
79
80 // Rollback as sysop.
81 $apiResult = $this->doApiRequestWithToken( [
82 'action' => 'rollback',
83 'title' => $title->getPrefixedText(),
84 'user' => $user->getName(),
85 'markbot' => true
86 ] )[0];
87 // Make sure the API response looks good.
88 $this->assertArrayHasKey( 'rollback', $apiResult );
89 $this->assertSame( $title->getPrefixedText(), $apiResult['rollback']['title'] );
90
91 $recentChange = $revisionStore->getRecentChange( $revisionStore->getRevisionByTitle( $title ) );
92 $this->assertSame( '1', $recentChange->getAttribute( 'rc_bot' ) );
93 }
94
95 public function testRollbackNoToken() {
96 $user = $this->getTestUser()->getUser();
97 $sysop = $this->getTestSysop()->getUser();
98
99 $title = Title::makeTitle( NS_MAIN, 'ApiRollbackTest::testRollbackNoToken' );
100 // Create page as sysop.
101 $this->editPage( $title, 'Some text', '', NS_MAIN, $sysop );
102
103 // Edit as non-sysop.
104 $this->editPage( $title, 'Vandalism', '', NS_MAIN, $user );
105
106 $this->expectException( ApiUsageException::class );
107 $this->doApiRequest( [
108 'action' => 'rollback',
109 'title' => $title->getPrefixedText(),
110 'user' => $user->getName(),
111 ] )[0];
112 }
113 }