| 1 |
<?php |
| 2 |
|
| 3 |
namespace MediaWiki\Tests\Maintenance; |
| 4 |
|
| 5 |
use MediaWiki\Permissions\Authority; |
| 6 |
use MediaWiki\Revision\SlotRecord; |
| 7 |
use MediaWiki\Title\Title; |
| 8 |
use RollbackEdits; |
| 9 |
use Wikimedia\Rdbms\IDBAccessObject; |
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
class RollbackEditsTest extends MaintenanceBaseTestCase { |
| 17 |
|
| 18 |
protected function getMaintenanceClass() { |
| 19 |
return RollbackEdits::class; |
| 20 |
} |
| 21 |
|
| 22 |
public function testExecuteForInvalidUserOption() { |
| 23 |
$this->maintenance->setOption( 'user', 'Template:Testing#test' ); |
| 24 |
$this->expectOutputRegex( '/Invalid username/' ); |
| 25 |
$this->expectCallToFatalError(); |
| 26 |
$this->maintenance->execute(); |
| 27 |
} |
| 28 |
|
| 29 |
public function testExecuteWhenUserHasNoPagesToRollback() { |
| 30 |
$this->maintenance->setOption( 'user', $this->getTestUser()->getUserIdentity()->getName() ); |
| 31 |
$this->maintenance->execute(); |
| 32 |
$this->expectOutputRegex( "/No suitable titles to be rolled back./" ); |
| 33 |
} |
| 34 |
|
| 35 |
public function testExecuteWhenAllTitlesInvalid() { |
| 36 |
$this->maintenance->setOption( 'titles', '::|~~~~' ); |
| 37 |
$this->maintenance->setOption( 'user', $this->getTestUser()->getUserIdentity()->getName() ); |
| 38 |
$this->maintenance->execute(); |
| 39 |
$this->expectOutputRegex( "/No suitable titles to be rolled back./" ); |
| 40 |
} |
| 41 |
|
| 42 |
public function testExecuteForAllNonExistingTitles() { |
| 43 |
$this->maintenance->setOption( 'titles', 'Non-existing-test-page' ); |
| 44 |
$this->maintenance->setOption( 'user', $this->getTestUser()->getUserIdentity()->getName() ); |
| 45 |
$this->maintenance->execute(); |
| 46 |
$this->expectOutputRegex( "/Processing Non-existing-test-page...Failed!/" ); |
| 47 |
} |
| 48 |
|
| 49 |
public function testExecuteForUnregisteredUsername() { |
| 50 |
$this->maintenance->setOption( 'titles', 'Non-existing-test-page' ); |
| 51 |
$this->maintenance->setOption( 'user', 'NonExistingTestUser' ); |
| 52 |
$this->expectOutputRegex( '/Unknown user/' ); |
| 53 |
$this->expectCallToFatalError(); |
| 54 |
$this->maintenance->execute(); |
| 55 |
} |
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
private function getExistingTestPages( int $count ): array { |
| 62 |
$returnArray = []; |
| 63 |
for ( $i = 0; $i < $count; $i++ ) { |
| 64 |
$returnArray[] = $this->getExistingTestPage()->getTitle(); |
| 65 |
} |
| 66 |
return $returnArray; |
| 67 |
} |
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
private function commonExecuteForSuccess( array $titlesToBeRolledBack, Authority $authority, array $options ) { |
| 76 |
$expectedOutputRegex = '/'; |
| 77 |
foreach ( $titlesToBeRolledBack as $title ) { |
| 78 |
$this->editPage( $title, 'testing-12345', '', NS_MAIN, $authority ); |
| 79 |
$expectedOutputRegex .= 'Processing ' . preg_quote( $title->getPrefixedText() ) . "\.\.\.Done!\n"; |
| 80 |
} |
| 81 |
$this->maintenance->setOption( 'user', $authority->getUser()->getName() ); |
| 82 |
foreach ( $options as $name => $value ) { |
| 83 |
$this->maintenance->setOption( $name, $value ); |
| 84 |
} |
| 85 |
$this->maintenance->execute(); |
| 86 |
$this->expectOutputRegex( $expectedOutputRegex . '/' ); |
| 87 |
foreach ( $titlesToBeRolledBack as $title ) { |
| 88 |
|
| 89 |
$latestRevision = $this->getServiceContainer()->getRevisionLookup() |
| 90 |
->getRevisionById( $title->getLatestRevID( IDBAccessObject::READ_LATEST ) ); |
| 91 |
$firstRevision = $this->getServiceContainer()->getRevisionLookup() |
| 92 |
->getFirstRevision( $title ); |
| 93 |
$this->assertSame( |
| 94 |
$firstRevision->getContent( SlotRecord::MAIN )->getWikitextForTransclusion(), |
| 95 |
$latestRevision->getContent( SlotRecord::MAIN )->getWikitextForTransclusion() |
| 96 |
); |
| 97 |
|
| 98 |
$this->assertContains( 'mw-rollback', $this->getServiceContainer()->getChangeTagsStore()->getTags( $this->getDb(), null, $latestRevision->getId() ) ); |
| 99 |
} |
| 100 |
} |
| 101 |
|
| 102 |
public function testExecuteForUser() { |
| 103 |
$testUser = $this->getTestUser()->getAuthority(); |
| 104 |
$titles = $this->getExistingTestPages( 2 ); |
| 105 |
$this->commonExecuteForSuccess( $titles, $testUser, [] ); |
| 106 |
} |
| 107 |
|
| 108 |
public function testExecuteForUserWithTitlesSet() { |
| 109 |
$testUser = $this->getTestUser()->getAuthority(); |
| 110 |
$titles = $this->getExistingTestPages( 3 ); |
| 111 |
$titleNotToBeRolledBack = array_pop( $titles ); |
| 112 |
$this->editPage( $titleNotToBeRolledBack, 'testing-12345', '', NS_MAIN, $testUser ); |
| 113 |
|
| 114 |
$this->commonExecuteForSuccess( |
| 115 |
$titles, $testUser, [ 'titles' => $titles[0]->getPrefixedText() . '|' . $titles[1]->getPrefixedText() ] |
| 116 |
); |
| 117 |
|
| 118 |
$latestRevision = $this->getServiceContainer()->getRevisionLookup() |
| 119 |
->getRevisionById( $titleNotToBeRolledBack->getLatestRevID() ); |
| 120 |
$firstRevision = $this->getServiceContainer()->getRevisionLookup() |
| 121 |
->getFirstRevision( $titleNotToBeRolledBack ); |
| 122 |
$this->assertNotSame( |
| 123 |
$firstRevision->getContent( SlotRecord::MAIN )->getWikitextForTransclusion(), |
| 124 |
$latestRevision->getContent( SlotRecord::MAIN )->getWikitextForTransclusion() |
| 125 |
); |
| 126 |
} |
| 127 |
|
| 128 |
public function testExecuteForUserWithUncanonicalisedName() { |
| 129 |
$contLang = $this->getServiceContainer()->getContentLanguage(); |
| 130 |
$testUser = $this->getTestUser()->getAuthority(); |
| 131 |
$titles = $this->getExistingTestPages( 2 ); |
| 132 |
$name = strtr( $contLang->lcfirst( $testUser->getUser()->getName() ), ' ', '_' ); |
| 133 |
$this->commonExecuteForSuccess( $titles, $testUser, [ 'user' => $name ] ); |
| 134 |
} |
| 135 |
} |