Merge "mediawiki.content.json: Remove file and author annotations"
[mediawiki.git] / tests / phpunit / includes / actions / RollbackActionTest.php
blob8cf9557775ceffb3d47e489aa69132266c5d138f
1 <?php
3 namespace MediaWiki\Tests\Action;
5 use Article;
6 use ErrorPageError;
7 use MediaWiki\Context\DerivativeContext;
8 use MediaWiki\Context\RequestContext;
9 use MediaWiki\Request\FauxRequest;
10 use MediaWiki\Request\WebRequest;
11 use MediaWiki\Title\Title;
12 use MediaWiki\User\User;
13 use MediaWikiIntegrationTestCase;
14 use RollbackAction;
16 /**
17 * @covers \RollbackAction
18 * @group Database
20 class RollbackActionTest extends MediaWikiIntegrationTestCase {
22 /** @var User */
23 private $vandal;
25 /** @var User */
26 private $sysop;
28 /** @var Title */
29 private $testPage;
31 protected function setUp(): void {
32 parent::setUp();
33 $this->testPage = Title::makeTitle( NS_MAIN, 'RollbackActionTest' );
35 $this->vandal = $this->getTestUser()->getUser();
36 $this->sysop = $this->getTestSysop()->getUser();
37 $this->editPage( $this->testPage, 'Some text', '', NS_MAIN, $this->sysop );
38 $this->editPage( $this->testPage, 'Vandalism', '', NS_MAIN, $this->vandal );
41 private function getRollbackAction( WebRequest $request ) {
42 $context = new DerivativeContext( RequestContext::getMain() );
43 $context->setTitle( $this->testPage );
44 $context->setRequest( $request );
45 $context->setUser( $this->sysop );
46 $mwServices = $this->getServiceContainer();
47 return new RollbackAction(
48 Article::newFromTitle( $this->testPage, $context ),
49 $context,
50 $mwServices->getContentHandlerFactory(),
51 $mwServices->getRollbackPageFactory(),
52 $mwServices->getUserOptionsLookup(),
53 $mwServices->getWatchlistManager(),
54 $mwServices->getCommentFormatter()
58 public static function provideRollbackParamFail() {
59 yield 'No from parameter' => [
60 'requestParams' => [],
62 yield 'Non existent user' => [
63 'requestParams' => [
64 'from' => 'abirvalg',
67 yield 'User mismatch' => [
68 'requestParams' => [
69 'from' => 'UTSysop',
74 /**
75 * @dataProvider provideRollbackParamFail
77 public function testRollbackParamFail( array $requestParams ) {
78 $request = new FauxRequest( $requestParams );
79 $rollbackAction = $this->getRollbackAction( $request );
80 $this->expectException( ErrorPageError::class );
81 $rollbackAction->handleRollbackRequest();
84 public function testRollbackTokenMismatch() {
85 $request = new FauxRequest( [
86 'from' => $this->vandal->getName(),
87 'token' => 'abrvalg',
88 ] );
89 $rollbackAction = $this->getRollbackAction( $request );
90 $this->expectException( ErrorPageError::class );
91 $rollbackAction->handleRollbackRequest();
94 public function testRollback() {
95 $editTracker = $this->getServiceContainer()->getUserEditTracker();
96 $editCount = $editTracker->getUserEditCount( $this->sysop );
98 $request = new FauxRequest( [
99 'from' => $this->vandal->getName(),
100 'token' => $this->sysop->getEditToken( 'rollback' ),
101 ] );
102 $rollbackAction = $this->getRollbackAction( $request );
103 $rollbackAction->handleRollbackRequest();
105 $revisionStore = $this->getServiceContainer()->getRevisionStore();
106 // Content of latest revision should match the initial.
107 $latestRev = $revisionStore->getRevisionByTitle( $this->testPage );
108 $initialRev = $revisionStore->getFirstRevision( $this->testPage );
109 $this->assertTrue( $latestRev->hasSameContent( $initialRev ) );
110 // ...but have different rev IDs.
111 $this->assertNotSame( $latestRev->getId(), $initialRev->getId() );
113 $recentChange = $revisionStore->getRecentChange( $latestRev );
114 $this->assertSame( '0', $recentChange->getAttribute( 'rc_bot' ) );
115 $this->assertSame( $this->sysop->getName(), $recentChange->getAttribute( 'rc_user_text' ) );
117 // T382592
118 $editTracker->clearUserEditCache( $this->sysop );
119 $this->runDeferredUpdates();
120 $this->assertSame( $editCount + 1, $editTracker->getUserEditCount( $this->sysop ) );
123 public function testRollbackMarkBot() {
124 $request = new FauxRequest( [
125 'from' => $this->vandal->getName(),
126 'token' => $this->sysop->getEditToken( 'rollback' ),
127 'bot' => true,
128 ] );
129 $rollbackAction = $this->getRollbackAction( $request );
130 $rollbackAction->handleRollbackRequest();
132 $revisionStore = $this->getServiceContainer()->getRevisionStore();
133 $latestRev = $revisionStore->getRevisionByTitle( $this->testPage );
134 $recentChange = $revisionStore->getRecentChange( $latestRev );
135 $this->assertSame( '1', $recentChange->getAttribute( 'rc_bot' ) );