Merge "Add semantic tags to license info text"
[lhc/web/wiklou.git] / tests / phpunit / includes / editpage / TextboxBuilderTest.php
1 <?php
2 /**
3 * Copyright (C) 2017 Kunal Mehta <legoktm@member.fsf.org>
4 *
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.
9 *
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.
14 *
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.
18 *
19 */
20
21 namespace MediaWiki\Tests\EditPage;
22
23 use Language;
24 use MediaWiki\EditPage\TextboxBuilder;
25 use MediaWikiTestCase;
26 use Title;
27 use User;
28
29 /**
30 * @covers \MediaWiki\EditPage\TextboxBuilder
31 */
32 class TextboxBuilderTest extends MediaWikiTestCase {
33
34 public function provideAddNewLineAtEnd() {
35 return [
36 [ '', '' ],
37 [ 'foo', "foo\n" ],
38 ];
39 }
40
41 /**
42 * @dataProvider provideAddNewLineAtEnd
43 */
44 public function testAddNewLineAtEnd( $input, $expected ) {
45 $builder = new TextboxBuilder();
46 $this->assertSame( $expected, $builder->addNewLineAtEnd( $input ) );
47 }
48
49 public function testBuildTextboxAttribs() {
50 $user = new User();
51 $user->setOption( 'editfont', 'monospace' );
52
53 $title = $this->getMockBuilder( Title::class )
54 ->disableOriginalConstructor()
55 ->getMock();
56 $title->expects( $this->any() )
57 ->method( 'getPageLanguage' )
58 ->will( $this->returnValue( Language::factory( 'en' ) ) );
59
60 $builder = new TextboxBuilder();
61 $attribs = $builder->buildTextboxAttribs(
62 'mw-textbox1',
63 [ 'class' => 'foo bar', 'data-foo' => '123', 'rows' => 30 ],
64 $user,
65 $title
66 );
67
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'] );
76
77 $attribs2 = $builder->buildTextboxAttribs(
78 'mw-textbox2', [ 'class' => [ 'foo', 'bar' ] ], $user, $title
79 );
80 // classes merged properly (array)
81 $this->assertSame( [ 'foo', 'bar', 'mw-editfont-monospace' ], $attribs2['class'] );
82
83 $attribs3 = $builder->buildTextboxAttribs(
84 'mw-textbox3', [], $user, $title
85 );
86 // classes ok when nothing to be merged
87 $this->assertSame( 'mw-editfont-monospace', $attribs3['class'] );
88 }
89
90 public function provideMergeClassesIntoAttributes() {
91 return [
92 [
93 [],
94 [],
95 [],
96 ],
97 [
98 [ 'mw-new-classname' ],
99 [],
100 [ 'class' => 'mw-new-classname' ],
101 ],
102 [
103 [],
104 [ 'title' => 'My Title' ],
105 [ 'title' => 'My Title' ],
106 ],
107 [
108 [ 'mw-new-classname' ],
109 [ 'title' => 'My Title' ],
110 [ 'title' => 'My Title', 'class' => 'mw-new-classname' ],
111 ],
112 [
113 [ 'mw-new-classname' ],
114 [ 'class' => 'mw-existing-classname' ],
115 [ 'class' => 'mw-new-classname mw-existing-classname' ],
116 ],
117 ];
118 }
119
120 /**
121 * @dataProvider provideMergeClassesIntoAttributes
122 */
123 public function testMergeClassesIntoAttributes( $inputClasses, $inputAttributes, $expected ) {
124 $builder = new TextboxBuilder();
125 $this->assertSame(
126 $expected,
127 $builder->mergeClassesIntoAttributes( $inputClasses, $inputAttributes )
128 );
129 }
130
131 public function provideGetTextboxProtectionCSSClasses() {
132 return [
133 [
134 [ '' ],
135 [ 'isProtected' ],
136 [],
137 ],
138 [
139 true,
140 [],
141 [],
142 ],
143 [
144 true,
145 [ 'isProtected' ],
146 [ 'mw-textarea-protected' ]
147 ],
148 [
149 true,
150 [ 'isProtected', 'isSemiProtected' ],
151 [ 'mw-textarea-sprotected' ],
152 ],
153 [
154 true,
155 [ 'isProtected', 'isCascadeProtected' ],
156 [ 'mw-textarea-protected', 'mw-textarea-cprotected' ],
157 ],
158 [
159 true,
160 [ 'isProtected', 'isCascadeProtected', 'isSemiProtected' ],
161 [ 'mw-textarea-sprotected', 'mw-textarea-cprotected' ],
162 ],
163 ];
164 }
165
166 /**
167 * @dataProvider provideGetTextboxProtectionCSSClasses
168 */
169 public function testGetTextboxProtectionCSSClasses(
170 $restrictionLevels,
171 $protectionModes,
172 $expected
173 ) {
174 $this->setMwGlobals( [
175 // set to trick MWNamespace::getRestrictionLevels
176 'wgRestrictionLevels' => $restrictionLevels
177 ] );
178
179 $builder = new TextboxBuilder();
180 $this->assertSame( $expected, $builder->getTextboxProtectionCSSClasses(
181 $this->mockProtectedTitle( $protectionModes )
182 ) );
183 }
184
185 /**
186 * @return Title
187 */
188 private function mockProtectedTitle( $methodsToReturnTrue ) {
189 $title = $this->getMockBuilder( Title::class )
190 ->disableOriginalConstructor()
191 ->getMock();
192
193 $title->expects( $this->any() )
194 ->method( 'getNamespace' )
195 ->will( $this->returnValue( 1 ) );
196
197 foreach ( $methodsToReturnTrue as $method ) {
198 $title->expects( $this->any() )
199 ->method( $method )
200 ->will( $this->returnValue( true ) );
201 }
202
203 return $title;
204 }
205 }