| 1 |
<?php |
| 2 |
|
| 3 |
namespace MediaWiki\Extension\AbuseFilter\Tests\Unit; |
| 4 |
|
| 5 |
use InvalidArgumentException; |
| 6 |
use MediaWiki\Extension\AbuseFilter\CentralDBManager; |
| 7 |
use MediaWiki\Extension\AbuseFilter\EditRevUpdater; |
| 8 |
use MediaWiki\Linker\LinkTarget; |
| 9 |
use MediaWiki\Revision\MutableRevisionRecord; |
| 10 |
use MediaWiki\Revision\RevisionLookup; |
| 11 |
use MediaWiki\Title\Title; |
| 12 |
use MediaWiki\Title\TitleValue; |
| 13 |
use MediaWikiUnitTestCase; |
| 14 |
use Wikimedia\Rdbms\IDatabase; |
| 15 |
use Wikimedia\Rdbms\LBFactory; |
| 16 |
use Wikimedia\Rdbms\UpdateQueryBuilder; |
| 17 |
use WikiPage; |
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
class EditRevUpdaterTest extends MediaWikiUnitTestCase { |
| 25 |
|
| 26 |
public function testConstruct() { |
| 27 |
$this->assertInstanceOf( |
| 28 |
EditRevUpdater::class, |
| 29 |
new EditRevUpdater( |
| 30 |
$this->createMock( CentralDBManager::class ), |
| 31 |
$this->createMock( RevisionLookup::class ), |
| 32 |
$this->createMock( LBFactory::class ), |
| 33 |
'' |
| 34 |
) |
| 35 |
); |
| 36 |
} |
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
private function getUpdater( |
| 45 |
?IDatabase $localDB = null, |
| 46 |
?IDatabase $centralDB = null, |
| 47 |
?RevisionLookup $revLookup = null |
| 48 |
): EditRevUpdater { |
| 49 |
$lbFactory = $this->createMock( LBFactory::class ); |
| 50 |
$lbFactory->method( 'getPrimaryDatabase' ) |
| 51 |
->willReturn( $localDB ?? $this->createMock( IDatabase::class ) ); |
| 52 |
|
| 53 |
$dbManager = $this->createMock( CentralDBManager::class ); |
| 54 |
$dbManager->method( 'getConnection' ) |
| 55 |
->willReturn( $centralDB ?? $this->createMock( IDatabase::class ) ); |
| 56 |
|
| 57 |
return new EditRevUpdater( |
| 58 |
$dbManager, |
| 59 |
$revLookup ?? $this->createMock( RevisionLookup::class ), |
| 60 |
$lbFactory, |
| 61 |
'fake-wiki-id' |
| 62 |
); |
| 63 |
} |
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
private function getPageAndRev( LinkTarget $target ): array { |
| 70 |
$title = Title::newFromLinkTarget( $target ); |
| 71 |
|
| 72 |
$title->mArticleID = 123456; |
| 73 |
|
| 74 |
$wikiPage = $this->createMock( WikiPage::class ); |
| 75 |
$wikiPage->method( 'getTitle' )->willReturn( $title ); |
| 76 |
|
| 77 |
return [ $wikiPage, new MutableRevisionRecord( $title ) ]; |
| 78 |
} |
| 79 |
|
| 80 |
public function testUpdateRev_noIDs() { |
| 81 |
$titleValue = new TitleValue( NS_PROJECT, 'EditRevUpdater' ); |
| 82 |
$this->assertFalse( $this->getUpdater()->updateRev( ...$this->getPageAndRev( $titleValue ) ) ); |
| 83 |
} |
| 84 |
|
| 85 |
public function testUpdateRev_differentPages() { |
| 86 |
$titleValue = new TitleValue( NS_PROJECT, 'EditRevUpdater' ); |
| 87 |
$updater = $this->getUpdater(); |
| 88 |
$diffTitleValue = new TitleValue( NS_HELP, 'Foobar' ); |
| 89 |
[ $diffPage, ] = $this->getPageAndRev( $diffTitleValue ); |
| 90 |
$updater->setLastEditPage( $diffPage ); |
| 91 |
$updater->setLogIdsForTarget( $titleValue, [ 'local' => [ 1, 2 ], 'global' => [] ] ); |
| 92 |
$this->assertFalse( $updater->updateRev( ...$this->getPageAndRev( $titleValue ) ) ); |
| 93 |
} |
| 94 |
|
| 95 |
public function testUpdateRev_nullEdit() { |
| 96 |
$titleValue = new TitleValue( NS_PROJECT, 'EditRevUpdater' ); |
| 97 |
[ $page, $rev ] = $this->getPageAndRev( $titleValue ); |
| 98 |
$rev->setParentId( 42 ); |
| 99 |
$revLookup = $this->createMock( RevisionLookup::class ); |
| 100 |
$revLookup->expects( $this->once() )->method( 'getRevisionById' )->with( 42 )->willReturn( $rev ); |
| 101 |
$updater = $this->getUpdater( null, null, $revLookup ); |
| 102 |
$updater->setLastEditPage( $page ); |
| 103 |
$updater->setLogIdsForTarget( $titleValue, [ 'local' => [ 1 ], 'global' => [ 1 ] ] ); |
| 104 |
|
| 105 |
$this->assertFalse( $updater->updateRev( $page, $rev ) ); |
| 106 |
} |
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
public function testUpdateRev_success( array $ids ) { |
| 113 |
$titleValue = new TitleValue( NS_PROJECT, 'EditRevUpdater' ); |
| 114 |
[ $page, $rev ] = $this->getPageAndRev( $titleValue ); |
| 115 |
$localDB = $this->createMock( IDatabase::class ); |
| 116 |
$localDB->expects( $ids['local'] ? $this->once() : $this->never() )->method( 'update' ); |
| 117 |
$localDB->expects( $ids['local'] ? $this->once() : $this->never() )->method( 'newUpdateQueryBuilder' ) |
| 118 |
->willReturnCallback( static function () use ( $localDB ) { |
| 119 |
return new UpdateQueryBuilder( $localDB ); |
| 120 |
} ); |
| 121 |
$centralDB = $this->createMock( IDatabase::class ); |
| 122 |
$centralDB->expects( $ids['global'] ? $this->once() : $this->never() )->method( 'update' ); |
| 123 |
$centralDB->expects( $ids['global'] ? $this->once() : $this->never() )->method( 'newUpdateQueryBuilder' ) |
| 124 |
->willReturnCallback( static function () use ( $centralDB ) { |
| 125 |
return new UpdateQueryBuilder( $centralDB ); |
| 126 |
} ); |
| 127 |
$updater = $this->getUpdater( $localDB, $centralDB ); |
| 128 |
$updater->setLastEditPage( $page ); |
| 129 |
$updater->setLogIdsForTarget( $titleValue, $ids ); |
| 130 |
|
| 131 |
$this->assertTrue( $updater->updateRev( $page, $rev ) ); |
| 132 |
} |
| 133 |
|
| 134 |
public static function provideIDsSuccess(): array { |
| 135 |
return [ |
| 136 |
'local only' => [ [ 'local' => [ 1, 2 ], 'global' => [] ] ], |
| 137 |
'global only' => [ [ 'local' => [], 'global' => [ 1, 2 ] ] ], |
| 138 |
'local and global' => [ [ 'local' => [ 1, 2 ], 'global' => [ 1, 2 ] ] ], |
| 139 |
]; |
| 140 |
} |
| 141 |
|
| 142 |
public function testUpdateRev_multipleTitles() { |
| 143 |
$goodTitleValue = new TitleValue( NS_PROJECT, 'EditRevUpdater' ); |
| 144 |
$badTitleValue = new TitleValue( NS_PROJECT, 'These should not be used' ); |
| 145 |
$goodIDs = [ 'local' => [ 1, 2 ], 'global' => [] ]; |
| 146 |
$badIDs = [ 'local' => [], 'global' => [ 1, 2 ] ]; |
| 147 |
[ $page, $rev ] = $this->getPageAndRev( $goodTitleValue ); |
| 148 |
$localDB = $this->createMock( IDatabase::class ); |
| 149 |
$localDB->expects( $this->once() )->method( 'newUpdateQueryBuilder' ) |
| 150 |
->willReturnCallback( static function () use ( $localDB ) { |
| 151 |
return new UpdateQueryBuilder( $localDB ); |
| 152 |
} ); |
| 153 |
$centralDB = $this->createMock( IDatabase::class ); |
| 154 |
$centralDB->expects( $this->never() )->method( 'newUpdateQueryBuilder' ); |
| 155 |
$updater = $this->getUpdater( $localDB, $centralDB ); |
| 156 |
$updater->setLastEditPage( $page ); |
| 157 |
$updater->setLogIdsForTarget( $goodTitleValue, $goodIDs ); |
| 158 |
$updater->setLogIdsForTarget( $badTitleValue, $badIDs ); |
| 159 |
|
| 160 |
$this->assertTrue( $updater->updateRev( $page, $rev ) ); |
| 161 |
} |
| 162 |
|
| 163 |
public function testClearLastEditPage() { |
| 164 |
$titleValue = new TitleValue( NS_PROJECT, 'EditRevUpdater-clear' ); |
| 165 |
[ $page, $revisionRecord ] = $this->getPageAndRev( $titleValue ); |
| 166 |
$updater = $this->getUpdater(); |
| 167 |
$updater->setLastEditPage( $page ); |
| 168 |
$updater->setLogIdsForTarget( $titleValue, [ 'local' => [ 1, 2 ], 'global' => [] ] ); |
| 169 |
$updater->clearLastEditPage(); |
| 170 |
$this->assertFalse( $updater->updateRev( $page, $revisionRecord ) ); |
| 171 |
} |
| 172 |
|
| 173 |
|
| 174 |
|
| 175 |
|
| 176 |
|
| 177 |
public function testSetLogIdsForTarget_invalid( array $ids ) { |
| 178 |
$updater = $this->getUpdater(); |
| 179 |
$this->expectException( InvalidArgumentException::class ); |
| 180 |
$updater->setLogIdsForTarget( new TitleValue( NS_MAIN, 'x' ), $ids ); |
| 181 |
} |
| 182 |
|
| 183 |
public static function provideInvalidIDs(): array { |
| 184 |
return [ |
| 185 |
'empty' => [ [] ], |
| 186 |
'missing key' => [ [ 'local' => [ 1 ] ] ], |
| 187 |
'extra key' => [ [ 'local' => [ 1 ], 'global' => [ 1 ], 'foo' => [ 1 ] ] ], |
| 188 |
]; |
| 189 |
} |
| 190 |
} |