| 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 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 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 |
|
| 34 |
$this->editPage( $title, 'Some text', '', NS_MAIN, $sysop ); |
| 35 |
|
| 36 |
|
| 37 |
$this->editPage( $title, 'Vandalism', '', NS_MAIN, $user ); |
| 38 |
|
| 39 |
|
| 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 |
|
| 49 |
$latestRev = $revisionStore->getRevisionByTitle( $title ); |
| 50 |
$initialRev = $revisionStore->getFirstRevision( $title ); |
| 51 |
$this->assertTrue( $latestRev->hasSameContent( $initialRev ) ); |
| 52 |
|
| 53 |
$this->assertNotSame( $latestRev->getId(), $initialRev->getId() ); |
| 54 |
|
| 55 |
|
| 56 |
$this->assertArrayHasKey( 'rollback', $apiResult ); |
| 57 |
$this->assertSame( $title->getPrefixedText(), $apiResult['rollback']['title'] ); |
| 58 |
|
| 59 |
|
| 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 |
|
| 75 |
$this->editPage( $title, 'Some text', '', NS_MAIN, $sysop ); |
| 76 |
|
| 77 |
|
| 78 |
$this->editPage( $title, 'Vandalism', '', NS_MAIN, $user ); |
| 79 |
|
| 80 |
|
| 81 |
$apiResult = $this->doApiRequestWithToken( [ |
| 82 |
'action' => 'rollback', |
| 83 |
'title' => $title->getPrefixedText(), |
| 84 |
'user' => $user->getName(), |
| 85 |
'markbot' => true |
| 86 |
] )[0]; |
| 87 |
|
| 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 |
|
| 101 |
$this->editPage( $title, 'Some text', '', NS_MAIN, $sysop ); |
| 102 |
|
| 103 |
|
| 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 |
} |