| 1 |
<?php |
| 2 |
|
| 3 |
namespace MediaWiki\Tests\Unit\Page; |
| 4 |
|
| 5 |
use BadMethodCallException; |
| 6 |
use Generator; |
| 7 |
use JobQueueGroup; |
| 8 |
use MediaWiki\Cache\BacklinkCacheFactory; |
| 9 |
use MediaWiki\CommentStore\CommentStore; |
| 10 |
use MediaWiki\Config\ServiceOptions; |
| 11 |
use MediaWiki\Linker\LinkTarget; |
| 12 |
use MediaWiki\MainConfigNames; |
| 13 |
use MediaWiki\Page\DeletePage; |
| 14 |
use MediaWiki\Page\PageIdentity; |
| 15 |
use MediaWiki\Page\PageIdentityValue; |
| 16 |
use MediaWiki\Page\ProperPageIdentity; |
| 17 |
use MediaWiki\Page\RedirectStore; |
| 18 |
use MediaWiki\Page\WikiPageFactory; |
| 19 |
use MediaWiki\Permissions\Authority; |
| 20 |
use MediaWiki\Permissions\PermissionStatus; |
| 21 |
use MediaWiki\Permissions\UltimateAuthority; |
| 22 |
use MediaWiki\Revision\RevisionRecord; |
| 23 |
use MediaWiki\Revision\RevisionStore; |
| 24 |
use MediaWiki\Tests\Unit\Permissions\MockAuthorityTrait; |
| 25 |
use MediaWiki\Title\NamespaceInfo; |
| 26 |
use MediaWiki\Title\Title; |
| 27 |
use MediaWiki\User\UserFactory; |
| 28 |
use MediaWiki\User\UserIdentityValue; |
| 29 |
use MediaWikiUnitTestCase; |
| 30 |
use PHPUnit\Framework\MockObject\MockObject; |
| 31 |
use Wikimedia\Message\ITextFormatter; |
| 32 |
use Wikimedia\ObjectCache\BagOStuff; |
| 33 |
use Wikimedia\Rdbms\IDatabase; |
| 34 |
use Wikimedia\Rdbms\IResultWrapper; |
| 35 |
use Wikimedia\Rdbms\LBFactory; |
| 36 |
use Wikimedia\Rdbms\SelectQueryBuilder; |
| 37 |
use WikiPage; |
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
class DeletePageTest extends MediaWikiUnitTestCase { |
| 43 |
use MockAuthorityTrait; |
| 44 |
|
| 45 |
private function getMockPage(): WikiPage { |
| 46 |
$ret = $this->createMock( WikiPage::class ); |
| 47 |
$ret->method( 'canExist' )->willReturn( true ); |
| 48 |
$ret->method( 'exists' )->willReturn( true ); |
| 49 |
$ret->method( 'getId' )->willReturn( 123 ); |
| 50 |
$ret->method( 'getRevisionRecord' )->willReturn( $this->createMock( RevisionRecord::class ) ); |
| 51 |
|
| 52 |
$title = $this->createMock( Title::class ); |
| 53 |
$title->method( 'getPrefixedText' )->willReturn( 'Foo' ); |
| 54 |
$title->method( 'getText' )->willReturn( 'Foo' ); |
| 55 |
$title->method( 'getDBkey' )->willReturn( 'Foo' ); |
| 56 |
$title->method( 'getNamespace' )->willReturn( 0 ); |
| 57 |
$ret->method( 'getTitle' )->willReturn( $title ); |
| 58 |
return $ret; |
| 59 |
} |
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
private function getMockRevisionStore(): RevisionStore { |
| 65 |
$ret = $this->createMock( RevisionStore::class ); |
| 66 |
$ret->method( 'getQueryInfo' )->willReturn( [ 'tables' => [], 'fields' => [], 'joins' => [] ] ); |
| 67 |
return $ret; |
| 68 |
} |
| 69 |
|
| 70 |
private function getServiceOptions( int $deleteLimit = 100 ): ServiceOptions { |
| 71 |
return new ServiceOptions( |
| 72 |
DeletePage::CONSTRUCTOR_OPTIONS, |
| 73 |
[ |
| 74 |
MainConfigNames::DeleteRevisionsBatchSize => 100, |
| 75 |
MainConfigNames::DeleteRevisionsLimit => $deleteLimit |
| 76 |
] |
| 77 |
); |
| 78 |
} |
| 79 |
|
| 80 |
private function getDeletePage( |
| 81 |
ProperPageIdentity $page, |
| 82 |
?Authority $deleter = null, |
| 83 |
?ServiceOptions $options = null, |
| 84 |
?RevisionStore $revStore = null, |
| 85 |
?WikiPageFactory $wpFactory = null, |
| 86 |
?NamespaceInfo $nsInfo = null |
| 87 |
): DeletePage { |
| 88 |
if ( !$wpFactory ) { |
| 89 |
$wpFactory = $this->createMock( WikiPageFactory::class ); |
| 90 |
$wpFactory->method( 'newFromTitle' )->willReturn( $this->getMockPage() ); |
| 91 |
} |
| 92 |
|
| 93 |
|
| 94 |
$db = $this->createMock( IDatabase::class ); |
| 95 |
$db->method( 'select' )->willReturn( $this->createMock( IResultWrapper::class ) ); |
| 96 |
$db->method( 'selectRowCount' )->willReturn( 42 ); |
| 97 |
$db->method( 'newSelectQueryBuilder' )->willReturn( new SelectQueryBuilder( $db ) ); |
| 98 |
|
| 99 |
$lbFactory = $this->createMock( LBFactory::class ); |
| 100 |
$lbFactory->method( 'getPrimaryDatabase' )->willReturn( $db ); |
| 101 |
|
| 102 |
$ret = new DeletePage( |
| 103 |
$this->createHookContainer(), |
| 104 |
$revStore ?? $this->getMockRevisionStore(), |
| 105 |
$lbFactory, |
| 106 |
$this->createMock( JobQueueGroup::class ), |
| 107 |
$this->createMock( CommentStore::class ), |
| 108 |
$options ?? $this->getServiceOptions(), |
| 109 |
$this->createMock( BagOStuff::class ), |
| 110 |
'wiki-id', |
| 111 |
'req-foo-bar', |
| 112 |
$wpFactory, |
| 113 |
$this->createMock( UserFactory::class ), |
| 114 |
$this->createMock( BacklinkCacheFactory::class ), |
| 115 |
$nsInfo ?? $this->createMock( NamespaceInfo::class ), |
| 116 |
$this->createMock( ITextFormatter::class ), |
| 117 |
$this->createMock( RedirectStore::class ), |
| 118 |
$page, |
| 119 |
$deleter ?? $this->createMock( Authority::class ) |
| 120 |
); |
| 121 |
$ret->setIsDeletePageUnitTest( true ); |
| 122 |
return $ret; |
| 123 |
} |
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
|
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
public function testPermissions( |
| 132 |
Authority $authority, |
| 133 |
bool $expectedGood, |
| 134 |
?string $expectedMessage = null, |
| 135 |
?ServiceOptions $options = null, |
| 136 |
?RevisionStore $revStore = null |
| 137 |
) { |
| 138 |
$dp = $this->getDeletePage( |
| 139 |
$this->getMockPage(), |
| 140 |
$authority, |
| 141 |
$options, |
| 142 |
$revStore |
| 143 |
); |
| 144 |
$status = $dp->deleteIfAllowed( 'foobar' ); |
| 145 |
$this->assertSame( $expectedGood, $status->isGood() ); |
| 146 |
if ( $expectedMessage !== null ) { |
| 147 |
$this->assertStatusError( $expectedMessage, $status ); |
| 148 |
} |
| 149 |
} |
| 150 |
|
| 151 |
public function providePermissions(): iterable { |
| 152 |
$cannotDeleteMsg = "You shall not delete!"; |
| 153 |
$cannotDeleteAuthority = $this->mockAnonAuthority( |
| 154 |
static function ( |
| 155 |
string $permission, |
| 156 |
?PageIdentity $page = null, |
| 157 |
?PermissionStatus $status = null |
| 158 |
) use ( $cannotDeleteMsg ): bool { |
| 159 |
if ( $permission === 'delete' ) { |
| 160 |
if ( $status ) { |
| 161 |
$status->fatal( $cannotDeleteMsg ); |
| 162 |
} |
| 163 |
return false; |
| 164 |
} |
| 165 |
return true; |
| 166 |
} |
| 167 |
); |
| 168 |
yield 'Cannot delete' => [ $cannotDeleteAuthority, false, $cannotDeleteMsg ]; |
| 169 |
|
| 170 |
$cannotBigDeleteMsg = 'delete-toomanyrevisions'; |
| 171 |
$cannotBigDeleteAuthority = $this->mockAnonAuthority( |
| 172 |
static function ( |
| 173 |
string $permission, |
| 174 |
?PageIdentity $page = null, |
| 175 |
?PermissionStatus $status = null |
| 176 |
) use ( $cannotBigDeleteMsg ): bool { |
| 177 |
if ( $permission === 'bigdelete' ) { |
| 178 |
if ( $status ) { |
| 179 |
$status->fatal( $cannotBigDeleteMsg ); |
| 180 |
} |
| 181 |
return false; |
| 182 |
} |
| 183 |
return true; |
| 184 |
} |
| 185 |
); |
| 186 |
$revDeleteLimit = -1; |
| 187 |
$cannotBigDeleteOptions = $this->getServiceOptions( $revDeleteLimit ); |
| 188 |
$revStore = $this->getMockRevisionStore(); |
| 189 |
$revStore->expects( $this->atLeastOnce() ) |
| 190 |
->method( 'countRevisionsByPageId' ) |
| 191 |
->willReturn( $revDeleteLimit + 42 ); |
| 192 |
yield 'Cannot bigdelete' => [ |
| 193 |
$cannotBigDeleteAuthority, |
| 194 |
false, |
| 195 |
$cannotBigDeleteMsg, |
| 196 |
$cannotBigDeleteOptions, |
| 197 |
$revStore |
| 198 |
]; |
| 199 |
|
| 200 |
$successAuthority = new UltimateAuthority( new UserIdentityValue( 42, 'Deleter' ) ); |
| 201 |
yield 'Successful' => [ $successAuthority, true ]; |
| 202 |
} |
| 203 |
|
| 204 |
|
| 205 |
|
| 206 |
|
| 207 |
public function testGetSuccessfulDeletionsIDs(): void { |
| 208 |
$delPage = $this->getDeletePage( |
| 209 |
$this->createMock( ProperPageIdentity::class ), |
| 210 |
$this->createMock( Authority::class ) |
| 211 |
); |
| 212 |
$delPage->deleteUnsafe( 'foo' ); |
| 213 |
$this->assertArrayHasKey( DeletePage::PAGE_BASE, $delPage->getSuccessfulDeletionsIDs() ); |
| 214 |
} |
| 215 |
|
| 216 |
|
| 217 |
|
| 218 |
|
| 219 |
public function testGetSuccessfulDeletionsIDs__notAttempted(): void { |
| 220 |
$delPage = $this->getDeletePage( |
| 221 |
$this->createMock( ProperPageIdentity::class ), |
| 222 |
$this->createMock( Authority::class ) |
| 223 |
); |
| 224 |
$this->expectException( BadMethodCallException::class ); |
| 225 |
$delPage->getSuccessfulDeletionsIDs(); |
| 226 |
} |
| 227 |
|
| 228 |
|
| 229 |
|
| 230 |
|
| 231 |
public function testDeletionsWereScheduled(): void { |
| 232 |
$delPage = $this->getDeletePage( |
| 233 |
$this->createMock( ProperPageIdentity::class ), |
| 234 |
$this->createMock( Authority::class ) |
| 235 |
); |
| 236 |
$delPage->deleteUnsafe( 'foo' ); |
| 237 |
$this->assertFalse( $delPage->deletionsWereScheduled()[DeletePage::PAGE_BASE] ); |
| 238 |
} |
| 239 |
|
| 240 |
|
| 241 |
|
| 242 |
|
| 243 |
public function testDeletionsWereScheduled__notAttempted(): void { |
| 244 |
$delPage = $this->getDeletePage( |
| 245 |
$this->createMock( ProperPageIdentity::class ), |
| 246 |
$this->createMock( Authority::class ) |
| 247 |
); |
| 248 |
$this->expectException( BadMethodCallException::class ); |
| 249 |
$delPage->deletionsWereScheduled(); |
| 250 |
} |
| 251 |
|
| 252 |
|
| 253 |
|
| 254 |
|
| 255 |
|
| 256 |
|
| 257 |
|
| 258 |
|
| 259 |
|
| 260 |
public function testCanProbablyDeleteAssociatedTalk( |
| 261 |
ProperPageIdentity $page, |
| 262 |
WikiPageFactory $wpFactory, |
| 263 |
?NamespaceInfo $nsInfo, |
| 264 |
?string $expectedMsg |
| 265 |
): void { |
| 266 |
$delPage = $this->getDeletePage( $page, null, null, null, $wpFactory, $nsInfo ); |
| 267 |
|
| 268 |
$res = $delPage->canProbablyDeleteAssociatedTalk(); |
| 269 |
if ( $expectedMsg === null ) { |
| 270 |
$this->assertStatusGood( $res ); |
| 271 |
} else { |
| 272 |
$this->assertStatusError( $expectedMsg, $res ); |
| 273 |
} |
| 274 |
} |
| 275 |
|
| 276 |
public function provideAssociatedTalk(): Generator { |
| 277 |
$getWpFactory = function ( bool $talkExists ): WikiPageFactory { |
| 278 |
$wpFactory = $this->createMock( WikiPageFactory::class ); |
| 279 |
$wpFactory->method( 'newFromTitle' )->willReturnCallback( function ( $t ) { |
| 280 |
$title = Title::castFromPageReference( $t ); |
| 281 |
$wikiPage = $this->createMock( WikiPage::class ); |
| 282 |
$wikiPage->method( 'getTitle' )->willReturn( $title ); |
| 283 |
$wikiPage->method( 'getNamespace' )->willReturn( $title->getNamespace() ); |
| 284 |
return $wikiPage; |
| 285 |
} ); |
| 286 |
$wpFactory->method( 'newFromLinkTarget' )->willReturnCallback( |
| 287 |
function ( LinkTarget $t ) use ( $talkExists ) { |
| 288 |
$existingTalk = $this->createMock( WikiPage::class ); |
| 289 |
$existingTalk->expects( $this->atLeastOnce() )->method( 'exists' )->willReturn( $talkExists ); |
| 290 |
return $existingTalk; |
| 291 |
} |
| 292 |
); |
| 293 |
return $wpFactory; |
| 294 |
}; |
| 295 |
$nsInfo = new NamespaceInfo( $this->createMock( ServiceOptions::class ), $this->createHookContainer(), [], [] ); |
| 296 |
|
| 297 |
$talkPage = new PageIdentityValue( 42, NS_TALK, 'Test talk page', PageIdentity::LOCAL ); |
| 298 |
yield 'Talk page' => [ $talkPage, $getWpFactory( false ), $nsInfo, 'delete-error-associated-alreadytalk' ]; |
| 299 |
|
| 300 |
$nonTalkPage = new PageIdentityValue( 44, NS_MAIN, 'Test article', PageIdentity::LOCAL ); |
| 301 |
yield 'Article without talk page' => |
| 302 |
[ $nonTalkPage, $getWpFactory( false ), $nsInfo, 'delete-error-associated-doesnotexist' ]; |
| 303 |
|
| 304 |
yield 'Article with talk page' => [ $nonTalkPage, $getWpFactory( true ), $nsInfo, null ]; |
| 305 |
} |
| 306 |
} |