MediaWiki REL1_32
TextboxBuilderTest.php
Go to the documentation of this file.
1<?php
22
23use Language;
26use Title;
27use User;
28
33
34 public function provideAddNewLineAtEnd() {
35 return [
36 [ '', '' ],
37 [ 'foo', "foo\n" ],
38 ];
39 }
40
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
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-existing-classname mw-new-classname' ],
116 ],
117 [
118 [ 'mw-new-classname', 'mw-existing-classname' ],
119 [ 'class' => 'mw-existing-classname' ],
120 [ 'class' => 'mw-existing-classname mw-new-classname' ],
121 ],
122 ];
123 }
124
128 public function testMergeClassesIntoAttributes( $inputClasses, $inputAttributes, $expected ) {
129 $builder = new TextboxBuilder();
130 $this->assertSame(
131 $expected,
132 $builder->mergeClassesIntoAttributes( $inputClasses, $inputAttributes )
133 );
134 }
135
137 return [
138 [
139 [ '' ],
140 [ 'isProtected' ],
141 [],
142 ],
143 [
144 true,
145 [],
146 [],
147 ],
148 [
149 true,
150 [ 'isProtected' ],
151 [ 'mw-textarea-protected' ]
152 ],
153 [
154 true,
155 [ 'isProtected', 'isSemiProtected' ],
156 [ 'mw-textarea-sprotected' ],
157 ],
158 [
159 true,
160 [ 'isProtected', 'isCascadeProtected' ],
161 [ 'mw-textarea-protected', 'mw-textarea-cprotected' ],
162 ],
163 [
164 true,
165 [ 'isProtected', 'isCascadeProtected', 'isSemiProtected' ],
166 [ 'mw-textarea-sprotected', 'mw-textarea-cprotected' ],
167 ],
168 ];
169 }
170
175 $restrictionLevels,
176 $protectionModes,
177 $expected
178 ) {
179 $this->setMwGlobals( [
180 // set to trick MWNamespace::getRestrictionLevels
181 'wgRestrictionLevels' => $restrictionLevels
182 ] );
183
184 $builder = new TextboxBuilder();
185 $this->assertSame( $expected, $builder->getTextboxProtectionCSSClasses(
186 $this->mockProtectedTitle( $protectionModes )
187 ) );
188 }
189
193 private function mockProtectedTitle( $methodsToReturnTrue ) {
194 $title = $this->getMockBuilder( Title::class )
195 ->disableOriginalConstructor()
196 ->getMock();
197
198 $title->expects( $this->any() )
199 ->method( 'getNamespace' )
200 ->will( $this->returnValue( 1 ) );
201
202 foreach ( $methodsToReturnTrue as $method ) {
203 $title->expects( $this->any() )
204 ->method( $method )
205 ->will( $this->returnValue( true ) );
206 }
207
208 return $title;
209 }
210}
they could even be mouse clicks or menu items whatever suits your program You should also get your if any
Definition COPYING.txt:326
Internationalisation code.
Definition Language.php:35
setMwGlobals( $pairs, $value=null)
Sets a global, maintaining a stashed version of the previous global to be restored in tearDown.
Helps EditPage build textboxes.
testMergeClassesIntoAttributes( $inputClasses, $inputAttributes, $expected)
provideMergeClassesIntoAttributes
testAddNewLineAtEnd( $input, $expected)
provideAddNewLineAtEnd
testGetTextboxProtectionCSSClasses( $restrictionLevels, $protectionModes, $expected)
provideGetTextboxProtectionCSSClasses
Represents a title within MediaWiki.
Definition Title.php:39
The User object encapsulates all of the user-specific settings (user_id, name, rights,...
Definition User.php:47
namespace and then decline to actually register it file or subcat img or subcat $title
Definition hooks.txt:994
null means default in associative array with keys and values unescaped Should be merged with default with a value of false meaning to suppress the attribute in associative array with keys and values unescaped noclasses just before the function returns a value If you return an< a > element with HTML attributes $attribs and contents $html will be returned If you return $ret will be returned and may include noclasses after processing & $attribs
Definition hooks.txt:2063
Copyright (C) 2017 Kunal Mehta legoktm@member.fsf.org
if(is_array($mode)) switch( $mode) $input