1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137
|
<?php
namespace MediaWiki\Tests\Unit;
use MediaWiki\EditPage\SpamChecker;
use MediaWiki\Page\MovePage;
use MediaWiki\Page\PageIdentity;
use MediaWiki\Permissions\Authority;
use MediaWiki\Permissions\PermissionStatus;
use MediaWiki\Tests\Unit\Permissions\MockAuthorityTrait;
use MediaWikiUnitTestCase;
use MockTitleTrait;
/**
* @coversDefaultClass \MediaWiki\Page\MovePage
* @method MovePage newServiceInstance(string $serviceClass, array $parameterOverrides)
*/
class MovePageTest extends MediaWikiUnitTestCase {
use MockTitleTrait;
use MockAuthorityTrait;
use MockServiceDependenciesTrait;
public function provideCheckPermissions() {
yield 'all good and allowed' => [
'authority' => $this->mockRegisteredUltimateAuthority(),
'good' => true,
];
yield 'cannot move' => [
'authority' => $this->mockAnonAuthority( function (
string $permission,
PageIdentity $page,
PermissionStatus $status
) {
if ( $permission === 'move' ) {
$this->assertSame( 'Existent', $page->getDBkey() );
$status->fatal( 'test' );
return false;
}
return true;
} ),
'good' => false,
];
yield 'cannot edit old page' => [
'authority' => $this->mockAnonAuthority( static function (
string $permission,
PageIdentity $page,
PermissionStatus $status
) {
if ( $permission === 'edit' && $page->getDBkey() === 'Existent' ) {
$status->fatal( 'test' );
return false;
}
return true;
} ),
'good' => false,
];
yield 'cannot move-target' => [
'authority' => $this->mockAnonAuthority( function (
string $permission,
PageIdentity $page,
PermissionStatus $status
) {
if ( $permission === 'move-target' ) {
$this->assertSame( 'Existent2', $page->getDBkey() );
$status->fatal( 'test' );
return false;
}
return true;
} ),
'good' => false,
];
yield 'cannot edit new page' => [
'authority' => $this->mockAnonAuthority( static function (
string $permission,
PageIdentity $page,
PermissionStatus $status
) {
if ( $permission === 'edit' && $page->getDBkey() === 'Existent2' ) {
$status->fatal( 'test' );
return false;
}
return true;
} ),
'good' => false,
];
}
/**
* @dataProvider provideCheckPermissions
* @covers \MediaWiki\Page\MovePage::checkPermissions
* @covers \MediaWiki\Page\MovePage::authorizeMove
* @covers \MediaWiki\Page\MovePage::probablyCanMove
*/
public function testCheckPermissions( Authority $authority, bool $good ) {
$spamChecker = $this->createNoOpMock( SpamChecker::class, [ 'checkSummary' ] );
$spamChecker->method( 'checkSummary' )->willReturn( false );
$mp = $this->newServiceInstance(
MovePage::class,
[
'oldTitle' => $this->makeMockTitle( 'Existent' ),
'newTitle' => $this->makeMockTitle( 'Existent2' ),
'spamChecker' => $spamChecker,
]
);
foreach ( [ 'checkPermissions', 'authorizeMove', 'probablyCanMove' ] as $method ) {
$permissionStatus = $mp->$method( $authority, 'Testing' );
$this->assertSame( $good, $permissionStatus->isGood() );
}
}
/**
* @covers \MediaWiki\Page\MovePage::checkPermissions
* @covers \MediaWiki\Page\MovePage::authorizeMove
* @covers \MediaWiki\Page\MovePage::probablyCanMove
*/
public function testCheckPermissions_spam() {
$spamChecker = $this->createNoOpMock( SpamChecker::class, [ 'checkSummary' ] );
$spamChecker->method( 'checkSummary' )
->willReturnCallback( static function ( string $reason ) {
return $reason === 'SPAM';
} );
$mp = $this->newServiceInstance(
MovePage::class,
[
'oldTitle' => $this->makeMockTitle( 'Existent' ),
'newTitle' => $this->makeMockTitle( 'Existent2' ),
'spamChecker' => $spamChecker,
]
);
foreach ( [ 'checkPermissions', 'authorizeMove', 'probablyCanMove' ] as $method ) {
$notSpamStatus = $mp->$method( $this->mockRegisteredUltimateAuthority(), 'NOT_SPAM' );
$this->assertStatusGood( $notSpamStatus );
$spamStatus = $mp->$method( $this->mockRegisteredUltimateAuthority(), 'SPAM' );
$this->assertStatusError( 'spamprotectiontext', $spamStatus );
}
}
}
|