editpage: Replace usage of Title in TextboxBuilder::class

In order for us to keep backward compatibility, cast the page identity
back to a Title in: `buildTextboxAttribs()` and use within the code.

Change-Id: Ia55251ee7f730636d6e85bf069734ff462119f0d
This commit is contained in:
Derick Alangi 2021-09-08 15:37:45 +01:00
parent be6e658c67
commit b648efacc5
2 changed files with 24 additions and 15 deletions

View file

@ -25,6 +25,7 @@
namespace MediaWiki\EditPage;
use MediaWiki\MediaWikiServices;
use MediaWiki\Page\PageIdentity;
use MediaWiki\User\UserIdentity;
use Sanitizer;
use Title;
@ -69,24 +70,25 @@ class TextboxBuilder {
}
/**
* @param Title $title
* @param PageIdentity $page
* @return string[]
*/
public function getTextboxProtectionCSSClasses( Title $title ) {
public function getTextboxProtectionCSSClasses( PageIdentity $page ) {
$classes = []; // Textarea CSS
if ( $title->isProtected( 'edit' ) &&
MediaWikiServices::getInstance()->getPermissionManager()
->getNamespaceRestrictionLevels( $title->getNamespace() ) !== [ '' ]
$services = MediaWikiServices::getInstance();
if ( $services->getRestrictionStore()->isProtected( $page, 'edit' ) &&
$services->getPermissionManager()
->getNamespaceRestrictionLevels( $page->getNamespace() ) !== [ '' ]
) {
# Is the title semi-protected?
if ( $title->isSemiProtected() ) {
if ( $services->getRestrictionStore()->isSemiProtected( $page ) ) {
$classes[] = 'mw-textarea-sprotected';
} else {
# Then it must be protected based on static groups (regular)
$classes[] = 'mw-textarea-protected';
}
# Is the title cascade-protected?
if ( $title->isCascadeProtected() ) {
if ( $services->getRestrictionStore()->isCascadeProtected( $page ) ) {
$classes[] = 'mw-textarea-cprotected';
}
}
@ -98,10 +100,12 @@ class TextboxBuilder {
* @param string $name
* @param mixed[] $customAttribs
* @param UserIdentity $user
* @param Title $title
* @param PageIdentity $page
* @return mixed[]
*/
public function buildTextboxAttribs( $name, array $customAttribs, UserIdentity $user, Title $title ) {
public function buildTextboxAttribs(
$name, array $customAttribs, UserIdentity $user, PageIdentity $page
) {
$attribs = $customAttribs + [
'accesskey' => ',',
'id' => $name,
@ -129,6 +133,7 @@ class TextboxBuilder {
$attribs['class'] = $class;
}
$title = Title::castFromPageIdentity( $page );
$pageLang = $title->getPageLanguage();
$attribs['lang'] = $pageLang->getHtmlCode();
$attribs['dir'] = $pageLang->getDir();

View file

@ -22,6 +22,8 @@ namespace MediaWiki\Tests\EditPage;
use Language;
use MediaWiki\EditPage\TextboxBuilder;
use MediaWiki\Page\PageIdentityValue;
use MediaWiki\Permissions\RestrictionStore;
use MediaWiki\User\StaticUserOptionsLookup;
use MediaWiki\User\UserIdentityValue;
use MediaWikiIntegrationTestCase;
@ -82,15 +84,17 @@ class TextboxBuilderTest extends MediaWikiIntegrationTestCase {
'wgRestrictionLevels' => $restrictionLevels
] );
$title = $this->createMock( Title::class );
$title->method( 'getNamespace' )->willReturn( 1 );
$mockRestrictionStore = $this->createMock( RestrictionStore::class );
$pageIdValue = PageIdentityValue::localIdentity( 1, NS_MAIN, 'test' );
foreach ( $protectionModes as $method ) {
$title->method( $method )->willReturn( true );
}
$mockRestrictionStore->method(
$this->logicalOr( ...array_map( [ $this, 'identicalTo' ], $protectionModes ) )
)->willReturn( true );
$this->setService( 'RestrictionStore', $mockRestrictionStore );
$builder = new TextboxBuilder();
$this->assertSame( $expected, $builder->getTextboxProtectionCSSClasses( $title ) );
$this->assertSame( $expected, $builder->getTextboxProtectionCSSClasses( $pageIdValue ) );
}
public function testBuildTextboxAttribs() {