| 1 |
<?php |
| 2 |
|
| 3 |
namespace MediaWiki\Tests\Action; |
| 4 |
|
| 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; |
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
class RollbackActionTest extends MediaWikiIntegrationTestCase { |
| 21 |
|
| 22 |
|
| 23 |
private $vandal; |
| 24 |
|
| 25 |
|
| 26 |
private $sysop; |
| 27 |
|
| 28 |
|
| 29 |
private $testPage; |
| 30 |
|
| 31 |
protected function setUp(): void { |
| 32 |
parent::setUp(); |
| 33 |
$this->testPage = Title::makeTitle( NS_MAIN, 'RollbackActionTest' ); |
| 34 |
|
| 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 ); |
| 39 |
} |
| 40 |
|
| 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() |
| 55 |
); |
| 56 |
} |
| 57 |
|
| 58 |
public static function provideRollbackParamFail() { |
| 59 |
yield 'No from parameter' => [ |
| 60 |
'requestParams' => [], |
| 61 |
]; |
| 62 |
yield 'Non existent user' => [ |
| 63 |
'requestParams' => [ |
| 64 |
'from' => 'abirvalg', |
| 65 |
], |
| 66 |
]; |
| 67 |
yield 'User mismatch' => [ |
| 68 |
'requestParams' => [ |
| 69 |
'from' => 'UTSysop', |
| 70 |
], |
| 71 |
]; |
| 72 |
} |
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
public function testRollbackParamFail( array $requestParams ) { |
| 78 |
$request = new FauxRequest( $requestParams ); |
| 79 |
$rollbackAction = $this->getRollbackAction( $request ); |
| 80 |
$this->expectException( ErrorPageError::class ); |
| 81 |
$rollbackAction->handleRollbackRequest(); |
| 82 |
} |
| 83 |
|
| 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(); |
| 92 |
} |
| 93 |
|
| 94 |
public function testRollback() { |
| 95 |
$request = new FauxRequest( [ |
| 96 |
'from' => $this->vandal->getName(), |
| 97 |
'token' => $this->sysop->getEditToken( 'rollback' ), |
| 98 |
] ); |
| 99 |
$rollbackAction = $this->getRollbackAction( $request ); |
| 100 |
$rollbackAction->handleRollbackRequest(); |
| 101 |
|
| 102 |
$revisionStore = $this->getServiceContainer()->getRevisionStore(); |
| 103 |
|
| 104 |
$latestRev = $revisionStore->getRevisionByTitle( $this->testPage ); |
| 105 |
$initialRev = $revisionStore->getFirstRevision( $this->testPage ); |
| 106 |
$this->assertTrue( $latestRev->hasSameContent( $initialRev ) ); |
| 107 |
|
| 108 |
$this->assertNotSame( $latestRev->getId(), $initialRev->getId() ); |
| 109 |
|
| 110 |
$recentChange = $revisionStore->getRecentChange( $latestRev ); |
| 111 |
$this->assertSame( '0', $recentChange->getAttribute( 'rc_bot' ) ); |
| 112 |
$this->assertSame( $this->sysop->getName(), $recentChange->getAttribute( 'rc_user_text' ) ); |
| 113 |
} |
| 114 |
|
| 115 |
public function testRollbackMarkBot() { |
| 116 |
$request = new FauxRequest( [ |
| 117 |
'from' => $this->vandal->getName(), |
| 118 |
'token' => $this->sysop->getEditToken( 'rollback' ), |
| 119 |
'bot' => true, |
| 120 |
] ); |
| 121 |
$rollbackAction = $this->getRollbackAction( $request ); |
| 122 |
$rollbackAction->handleRollbackRequest(); |
| 123 |
|
| 124 |
$revisionStore = $this->getServiceContainer()->getRevisionStore(); |
| 125 |
$latestRev = $revisionStore->getRevisionByTitle( $this->testPage ); |
| 126 |
$recentChange = $revisionStore->getRecentChange( $latestRev ); |
| 127 |
$this->assertSame( '1', $recentChange->getAttribute( 'rc_bot' ) ); |
| 128 |
} |
| 129 |
} |