| 1 |
<?php |
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
namespace MediaWiki\Tests\EditPage; |
| 22 |
|
| 23 |
use MediaWiki\EditPage\TextboxBuilder; |
| 24 |
use MediaWiki\Language\Language; |
| 25 |
use MediaWiki\MainConfigNames; |
| 26 |
use MediaWiki\Page\PageIdentityValue; |
| 27 |
use MediaWiki\Permissions\RestrictionStore; |
| 28 |
use MediaWiki\Title\Title; |
| 29 |
use MediaWiki\User\Options\StaticUserOptionsLookup; |
| 30 |
use MediaWiki\User\UserIdentityValue; |
| 31 |
use MediaWikiIntegrationTestCase; |
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
class TextboxBuilderTest extends MediaWikiIntegrationTestCase { |
| 39 |
|
| 40 |
public static function provideGetTextboxProtectionCSSClasses() { |
| 41 |
return [ |
| 42 |
[ |
| 43 |
[ '' ], |
| 44 |
[ 'isProtected' ], |
| 45 |
[], |
| 46 |
], |
| 47 |
[ |
| 48 |
[ '', 'something' ], |
| 49 |
[], |
| 50 |
[], |
| 51 |
], |
| 52 |
[ |
| 53 |
[ '', 'something' ], |
| 54 |
[ 'isProtected' ], |
| 55 |
[ 'mw-textarea-protected' ] |
| 56 |
], |
| 57 |
[ |
| 58 |
[ '', 'something' ], |
| 59 |
[ 'isProtected', 'isSemiProtected' ], |
| 60 |
[ 'mw-textarea-sprotected' ], |
| 61 |
], |
| 62 |
[ |
| 63 |
[ '', 'something' ], |
| 64 |
[ 'isProtected', 'isCascadeProtected' ], |
| 65 |
[ 'mw-textarea-protected', 'mw-textarea-cprotected' ], |
| 66 |
], |
| 67 |
[ |
| 68 |
[ '', 'something' ], |
| 69 |
[ 'isProtected', 'isCascadeProtected', 'isSemiProtected' ], |
| 70 |
[ 'mw-textarea-sprotected', 'mw-textarea-cprotected' ], |
| 71 |
], |
| 72 |
]; |
| 73 |
} |
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
public function testGetTextboxProtectionCSSClasses( |
| 79 |
$restrictionLevels, |
| 80 |
$protectionModes, |
| 81 |
$expected |
| 82 |
) { |
| 83 |
$this->overrideConfigValue( |
| 84 |
|
| 85 |
MainConfigNames::RestrictionLevels, $restrictionLevels |
| 86 |
); |
| 87 |
|
| 88 |
$mockRestrictionStore = $this->createMock( RestrictionStore::class ); |
| 89 |
$pageIdValue = PageIdentityValue::localIdentity( 1, NS_MAIN, 'test' ); |
| 90 |
|
| 91 |
$mockRestrictionStore->method( |
| 92 |
$this->logicalOr( ...array_map( [ $this, 'identicalTo' ], $protectionModes ) ) |
| 93 |
)->willReturn( true ); |
| 94 |
|
| 95 |
$this->setService( 'RestrictionStore', $mockRestrictionStore ); |
| 96 |
|
| 97 |
$builder = new TextboxBuilder(); |
| 98 |
$this->assertSame( $expected, $builder->getTextboxProtectionCSSClasses( $pageIdValue ) ); |
| 99 |
} |
| 100 |
|
| 101 |
public function testBuildTextboxAttribs() { |
| 102 |
$user = UserIdentityValue::newRegistered( 42, 'Test' ); |
| 103 |
$mockUserOptionsLookup = new StaticUserOptionsLookup( [ |
| 104 |
'Test' => [ 'editfont' => 'monospace' ], |
| 105 |
] ); |
| 106 |
$this->setService( 'UserOptionsLookup', $mockUserOptionsLookup ); |
| 107 |
|
| 108 |
$enLanguage = $this->createMock( Language::class ); |
| 109 |
$enLanguage->method( 'getHtmlCode' )->willReturn( 'en' ); |
| 110 |
$enLanguage->method( 'getDir' )->willReturn( 'ltr' ); |
| 111 |
|
| 112 |
$title = $this->createMock( Title::class ); |
| 113 |
$title->method( 'getPageLanguage' )->willReturn( $enLanguage ); |
| 114 |
|
| 115 |
$builder = new TextboxBuilder(); |
| 116 |
$attribs = $builder->buildTextboxAttribs( |
| 117 |
'mw-textbox1', |
| 118 |
[ 'class' => 'foo bar', 'data-foo' => '123', 'rows' => 30 ], |
| 119 |
$user, |
| 120 |
$title |
| 121 |
); |
| 122 |
|
| 123 |
$this->assertIsArray( $attribs ); |
| 124 |
|
| 125 |
$this->assertArrayHasKey( 'data-foo', $attribs ); |
| 126 |
|
| 127 |
$this->assertSame( 'foo bar mw-editfont-monospace', $attribs['class'] ); |
| 128 |
|
| 129 |
$this->assertSame( 30, $attribs['rows'] ); |
| 130 |
$this->assertSame( 'en', $attribs['lang'] ); |
| 131 |
|
| 132 |
$attribs2 = $builder->buildTextboxAttribs( |
| 133 |
'mw-textbox2', [ 'class' => [ 'foo', 'bar' ] ], $user, $title |
| 134 |
); |
| 135 |
|
| 136 |
$this->assertSame( [ 'foo', 'bar', 'mw-editfont-monospace' ], $attribs2['class'] ); |
| 137 |
|
| 138 |
$attribs3 = $builder->buildTextboxAttribs( |
| 139 |
'mw-textbox3', [], $user, $title |
| 140 |
); |
| 141 |
|
| 142 |
$this->assertSame( 'mw-editfont-monospace', $attribs3['class'] ); |
| 143 |
} |
| 144 |
} |