ImageRedirectConstraint: accept Content objects

More common interface for constraints

Bug: T157658
Change-Id: Ia50c3fb647ed803a69f5e4511047f1ab88e43bf7
This commit is contained in:
DannyS712 2020-12-09 21:25:28 +00:00
parent 3ae6356876
commit c4b1396e0d
4 changed files with 37 additions and 33 deletions

View file

@ -2023,8 +2023,8 @@ class EditPage implements IEditObject {
);
$constraintRunner->addConstraint(
$constraintFactory->newImageRedirectConstraint(
$textbox_content,
$this->mTitle,
$textbox_content->isRedirect(),
$user
)
);

View file

@ -179,20 +179,20 @@ class EditConstraintFactory {
}
/**
* @param Content $newContent
* @param LinkTarget $title
* @param bool $isRedirect
* @param User $user
* @return ImageRedirectConstraint
*/
public function newImageRedirectConstraint(
Content $newContent,
LinkTarget $title,
bool $isRedirect,
User $user
) : ImageRedirectConstraint {
return new ImageRedirectConstraint(
$this->permissionManager,
$newContent,
$title,
$isRedirect,
$user
);
}

View file

@ -20,6 +20,7 @@
namespace MediaWiki\EditPage\Constraint;
use Content;
use MediaWiki\Linker\LinkTarget;
use MediaWiki\Permissions\PermissionManager;
use StatusValue;
@ -38,12 +39,12 @@ class ImageRedirectConstraint implements IEditConstraint {
/** @var PermissionManager */
private $permissionManager;
/** @var Content */
private $newContent;
/** @var LinkTarget */
private $title;
/** @var bool */
private $isRedirect;
/** @var User */
private $user;
@ -52,26 +53,26 @@ class ImageRedirectConstraint implements IEditConstraint {
/**
* @param PermissionManager $permissionManager
* @param Content $newContent
* @param LinkTarget $title
* @param bool $isRedirect
* @param User $user
*/
public function __construct(
PermissionManager $permissionManager,
Content $newContent,
LinkTarget $title,
bool $isRedirect,
User $user
) {
$this->permissionManager = $permissionManager;
$this->newContent = $newContent;
$this->title = $title;
$this->isRedirect = $isRedirect;
$this->user = $user;
}
public function checkConstraint() : string {
// Check isn't simple enough to just repeat when getting the status
if ( $this->title->getNamespace() === NS_FILE &&
$this->isRedirect &&
$this->newContent->isRedirect() &&
!$this->permissionManager->userHasRight( $this->user, 'upload' )
) {
$this->result = self::CONSTRAINT_FAILED;

View file

@ -32,10 +32,21 @@ use MediaWiki\Permissions\PermissionManager;
class ImageRedirectConstraintTest extends MediaWikiUnitTestCase {
use EditConstraintTestTrait;
public function testPass() {
/**
* @param bool $userIsAnon
* @param bool $userHasRight
* @return ImageRedirectConstraint
*/
private function getConstraint( bool $userIsAnon, bool $userHasRight ) {
$content = $this->createMock( Content::class );
$content->method( 'isRedirect' )->willReturn( true );
$title = $this->createMock( Title::class );
$title->method( 'getNamespace' )->willReturn( NS_FILE );
$user = $this->createMock( User::class );
$user->method( 'isAnon' )->willReturn( $userIsAnon );
$permissionManager = $this->createMock( PermissionManager::class );
$permissionManager->expects( $this->once() )
->method( 'userHasRight' )
@ -43,14 +54,21 @@ class ImageRedirectConstraintTest extends MediaWikiUnitTestCase {
$this->equalTo( $user ),
$this->equalTo( 'upload' )
)
->willReturn( true );
->willReturn( $userHasRight );
$constraint = new ImageRedirectConstraint(
return new ImageRedirectConstraint(
$permissionManager,
$content,
$title,
true, // Is redirect
$user
);
}
public function testPass() {
$constraint = $this->getConstraint(
true, // is anon, does not matter
true // has `upload` right
);
$this->assertConstraintPassed( $constraint );
}
@ -60,24 +78,9 @@ class ImageRedirectConstraintTest extends MediaWikiUnitTestCase {
* @param int $expectedValue
*/
public function testFailure( $isAnon, $expectedValue ) {
$title = $this->createMock( Title::class );
$title->method( 'getNamespace' )->willReturn( NS_FILE );
$user = $this->createMock( User::class );
$user->method( 'isAnon' )->willReturn( $isAnon );
$permissionManager = $this->createMock( PermissionManager::class );
$permissionManager->expects( $this->once() )
->method( 'userHasRight' )
->with(
$this->equalTo( $user ),
$this->equalTo( 'upload' )
)
->willReturn( false );
$constraint = new ImageRedirectConstraint(
$permissionManager,
$title,
true, // Is redirect
$user
$constraint = $this->getConstraint(
$isAnon,
false // lacks `upload` right
);
$this->assertConstraintFailed( $constraint, $expectedValue );
}