3 * Copyright (C) 2017 Kunal Mehta <legoktm@member.fsf.org>
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
21 namespace MediaWiki\Tests\EditPage
;
24 use MediaWiki\EditPage\TextboxBuilder
;
25 use MediaWikiTestCase
;
30 * @covers \MediaWiki\EditPage\TextboxBuilder
32 class TextboxBuilderTest
extends MediaWikiTestCase
{
34 public function provideAddNewLineAtEnd() {
42 * @dataProvider provideAddNewLineAtEnd
44 public function testAddNewLineAtEnd( $input, $expected ) {
45 $builder = new TextboxBuilder();
46 $this->assertSame( $expected, $builder->addNewLineAtEnd( $input ) );
49 public function testBuildTextboxAttribs() {
51 $user->setOption( 'editfont', 'monospace' );
53 $title = $this->getMockBuilder( Title
::class )
54 ->disableOriginalConstructor()
56 $title->expects( $this->any() )
57 ->method( 'getPageLanguage' )
58 ->will( $this->returnValue( Language
::factory( 'en' ) ) );
60 $builder = new TextboxBuilder();
61 $attribs = $builder->buildTextboxAttribs(
63 [ 'class' => 'foo bar', 'data-foo' => '123', 'rows' => 30 ],
68 $this->assertInternalType( 'array', $attribs );
69 // custom attrib showed up
70 $this->assertArrayHasKey( 'data-foo', $attribs );
71 // classes merged properly (string)
72 $this->assertSame( 'foo bar mw-editfont-monospace', $attribs['class'] );
73 // overrides in custom attrib worked
74 $this->assertSame( 30, $attribs['rows'] );
75 $this->assertSame( 'en', $attribs['lang'] );
77 $attribs2 = $builder->buildTextboxAttribs(
78 'mw-textbox2', [ 'class' => [ 'foo', 'bar' ] ], $user, $title
80 // classes merged properly (array)
81 $this->assertSame( [ 'foo', 'bar', 'mw-editfont-monospace' ], $attribs2['class'] );
83 $attribs3 = $builder->buildTextboxAttribs(
84 'mw-textbox3', [], $user, $title
86 // classes ok when nothing to be merged
87 $this->assertSame( 'mw-editfont-monospace', $attribs3['class'] );
90 public function provideMergeClassesIntoAttributes() {
98 [ 'mw-new-classname' ],
100 [ 'class' => 'mw-new-classname' ],
104 [ 'title' => 'My Title' ],
105 [ 'title' => 'My Title' ],
108 [ 'mw-new-classname' ],
109 [ 'title' => 'My Title' ],
110 [ 'title' => 'My Title', 'class' => 'mw-new-classname' ],
113 [ 'mw-new-classname' ],
114 [ 'class' => 'mw-existing-classname' ],
115 [ 'class' => 'mw-existing-classname mw-new-classname' ],
118 [ 'mw-new-classname', 'mw-existing-classname' ],
119 [ 'class' => 'mw-existing-classname' ],
120 [ 'class' => 'mw-existing-classname mw-new-classname' ],
126 * @dataProvider provideMergeClassesIntoAttributes
128 public function testMergeClassesIntoAttributes( $inputClasses, $inputAttributes, $expected ) {
129 $builder = new TextboxBuilder();
132 $builder->mergeClassesIntoAttributes( $inputClasses, $inputAttributes )
136 public function provideGetTextboxProtectionCSSClasses() {
151 [ 'mw-textarea-protected' ]
155 [ 'isProtected', 'isSemiProtected' ],
156 [ 'mw-textarea-sprotected' ],
160 [ 'isProtected', 'isCascadeProtected' ],
161 [ 'mw-textarea-protected', 'mw-textarea-cprotected' ],
165 [ 'isProtected', 'isCascadeProtected', 'isSemiProtected' ],
166 [ 'mw-textarea-sprotected', 'mw-textarea-cprotected' ],
172 * @dataProvider provideGetTextboxProtectionCSSClasses
174 public function testGetTextboxProtectionCSSClasses(
179 $this->setMwGlobals( [
180 // set to trick MWNamespace::getRestrictionLevels
181 'wgRestrictionLevels' => $restrictionLevels
184 $builder = new TextboxBuilder();
185 $this->assertSame( $expected, $builder->getTextboxProtectionCSSClasses(
186 $this->mockProtectedTitle( $protectionModes )
193 private function mockProtectedTitle( $methodsToReturnTrue ) {
194 $title = $this->getMockBuilder( Title
::class )
195 ->disableOriginalConstructor()
198 $title->expects( $this->any() )
199 ->method( 'getNamespace' )
200 ->will( $this->returnValue( 1 ) );
202 foreach ( $methodsToReturnTrue as $method ) {
203 $title->expects( $this->any() )
205 ->will( $this->returnValue( true ) );