Merge "build: Enable jscs jsDoc rule 'checkTypes' and make pass"
[lhc/web/wiklou.git] / tests / phpunit / includes / content / JavaScriptContentTest.php
1 <?php
2
3 /**
4 * @group ContentHandler
5 * @group Database
6 * ^--- needed, because we do need the database to test link updates
7 */
8 class JavaScriptContentTest extends TextContentTest {
9
10 public function newContent( $text ) {
11 return new JavaScriptContent( $text );
12 }
13
14 public static function dataGetParserOutput() {
15 return array(
16 array(
17 'MediaWiki:Test.js',
18 null,
19 "hello <world>\n",
20 "<pre class=\"mw-code mw-js\" dir=\"ltr\">\nhello &lt;world&gt;\n\n</pre>"
21 ),
22 array(
23 'MediaWiki:Test.js',
24 null,
25 "hello(); // [[world]]\n",
26 "<pre class=\"mw-code mw-js\" dir=\"ltr\">\nhello(); // [[world]]\n\n</pre>",
27 array(
28 'Links' => array(
29 array( 'World' => 0 )
30 )
31 )
32 ),
33
34 // TODO: more...?
35 );
36 }
37
38 // XXX: Unused function
39 public static function dataGetSection() {
40 return array(
41 array( WikitextContentTest::$sections,
42 '0',
43 null
44 ),
45 array( WikitextContentTest::$sections,
46 '2',
47 null
48 ),
49 array( WikitextContentTest::$sections,
50 '8',
51 null
52 ),
53 );
54 }
55
56 // XXX: Unused function
57 public static function dataReplaceSection() {
58 return array(
59 array( WikitextContentTest::$sections,
60 '0',
61 'No more',
62 null,
63 null
64 ),
65 array( WikitextContentTest::$sections,
66 '',
67 'No more',
68 null,
69 null
70 ),
71 array( WikitextContentTest::$sections,
72 '2',
73 "== TEST ==\nmore fun",
74 null,
75 null
76 ),
77 array( WikitextContentTest::$sections,
78 '8',
79 'No more',
80 null,
81 null
82 ),
83 array( WikitextContentTest::$sections,
84 'new',
85 'No more',
86 'New',
87 null
88 ),
89 );
90 }
91
92 /**
93 * @covers JavaScriptContent::addSectionHeader
94 */
95 public function testAddSectionHeader() {
96 $content = $this->newContent( 'hello world' );
97 $c = $content->addSectionHeader( 'test' );
98
99 $this->assertTrue( $content->equals( $c ) );
100 }
101
102 // XXX: currently, preSaveTransform is applied to scripts. this may change or become optional.
103 public static function dataPreSaveTransform() {
104 return array(
105 array( 'hello this is ~~~',
106 "hello this is [[Special:Contributions/127.0.0.1|127.0.0.1]]",
107 ),
108 array( 'hello \'\'this\'\' is <nowiki>~~~</nowiki>',
109 'hello \'\'this\'\' is <nowiki>~~~</nowiki>',
110 ),
111 array( " Foo \n ",
112 " Foo",
113 ),
114 );
115 }
116
117 public static function dataPreloadTransform() {
118 return array(
119 array( 'hello this is ~~~',
120 'hello this is ~~~',
121 ),
122 array( 'hello \'\'this\'\' is <noinclude>foo</noinclude><includeonly>bar</includeonly>',
123 'hello \'\'this\'\' is <noinclude>foo</noinclude><includeonly>bar</includeonly>',
124 ),
125 );
126 }
127
128 public static function dataGetRedirectTarget() {
129 return array(
130 array( '#REDIRECT [[Test]]',
131 null,
132 ),
133 array( '#REDIRECT Test',
134 null,
135 ),
136 array( '* #REDIRECT [[Test]]',
137 null,
138 ),
139 );
140 }
141
142 /**
143 * @todo Test needs database!
144 */
145 /*
146 public function getRedirectChain() {
147 $text = $this->getNativeData();
148 return Title::newFromRedirectArray( $text );
149 }
150 */
151
152 /**
153 * @todo Test needs database!
154 */
155 /*
156 public function getUltimateRedirectTarget() {
157 $text = $this->getNativeData();
158 return Title::newFromRedirectRecurse( $text );
159 }
160 */
161
162 public static function dataIsCountable() {
163 return array(
164 array( '',
165 null,
166 'any',
167 true
168 ),
169 array( 'Foo',
170 null,
171 'any',
172 true
173 ),
174 array( 'Foo',
175 null,
176 'comma',
177 false
178 ),
179 array( 'Foo, bar',
180 null,
181 'comma',
182 false
183 ),
184 array( 'Foo',
185 null,
186 'link',
187 false
188 ),
189 array( 'Foo [[bar]]',
190 null,
191 'link',
192 false
193 ),
194 array( 'Foo',
195 true,
196 'link',
197 false
198 ),
199 array( 'Foo [[bar]]',
200 false,
201 'link',
202 false
203 ),
204 array( '#REDIRECT [[bar]]',
205 true,
206 'any',
207 true
208 ),
209 array( '#REDIRECT [[bar]]',
210 true,
211 'comma',
212 false
213 ),
214 array( '#REDIRECT [[bar]]',
215 true,
216 'link',
217 false
218 ),
219 );
220 }
221
222 public static function dataGetTextForSummary() {
223 return array(
224 array( "hello\nworld.",
225 16,
226 'hello world.',
227 ),
228 array( 'hello world.',
229 8,
230 'hello...',
231 ),
232 array( '[[hello world]].',
233 8,
234 '[[hel...',
235 ),
236 );
237 }
238
239 /**
240 * @covers JavaScriptContent::matchMagicWord
241 */
242 public function testMatchMagicWord() {
243 $mw = MagicWord::get( "staticredirect" );
244
245 $content = $this->newContent( "#REDIRECT [[FOO]]\n__STATICREDIRECT__" );
246 $this->assertFalse(
247 $content->matchMagicWord( $mw ),
248 "should not have matched magic word, since it's not wikitext"
249 );
250 }
251
252 /**
253 * @covers JavaScriptContent::updateRedirect
254 * @dataProvider provideUpdateRedirect
255 */
256 public function testUpdateRedirect( $oldText, $expectedText ) {
257 $this->setMwGlobals( array(
258 'wgServer' => '//example.org',
259 'wgScriptPath' => '/w/index.php',
260 ) );
261 $target = Title::newFromText( "testUpdateRedirect_target" );
262
263 $content = new JavaScriptContent( $oldText );
264 $newContent = $content->updateRedirect( $target );
265
266 $this->assertEquals( $expectedText, $newContent->getNativeData() );
267 }
268
269 public static function provideUpdateRedirect() {
270 return array(
271 array(
272 '#REDIRECT [[Someplace]]',
273 '#REDIRECT [[Someplace]]',
274 ),
275
276 // @codingStandardsIgnoreStart Generic.Files.LineLength
277 array(
278 '/* #REDIRECT */mw.loader.load("//example.org/w/index.php?title=MediaWiki:MonoBook.js\u0026action=raw\u0026ctype=text/javascript");',
279 '/* #REDIRECT */mw.loader.load("//example.org/w/index.php?title=TestUpdateRedirect_target\u0026action=raw\u0026ctype=text/javascript");'
280 )
281 // @codingStandardsIgnoreEnd
282 );
283 }
284
285 /**
286 * @covers JavaScriptContent::getModel
287 */
288 public function testGetModel() {
289 $content = $this->newContent( "hello world." );
290
291 $this->assertEquals( CONTENT_MODEL_JAVASCRIPT, $content->getModel() );
292 }
293
294 /**
295 * @covers JavaScriptContent::getContentHandler
296 */
297 public function testGetContentHandler() {
298 $content = $this->newContent( "hello world." );
299
300 $this->assertEquals( CONTENT_MODEL_JAVASCRIPT, $content->getContentHandler()->getModelID() );
301 }
302
303 public static function dataEquals() {
304 return array(
305 array( new JavaScriptContent( "hallo" ), null, false ),
306 array( new JavaScriptContent( "hallo" ), new JavaScriptContent( "hallo" ), true ),
307 array( new JavaScriptContent( "hallo" ), new CssContent( "hallo" ), false ),
308 array( new JavaScriptContent( "hallo" ), new JavaScriptContent( "HALLO" ), false ),
309 );
310 }
311
312 /**
313 * @dataProvider provideGetRedirectTarget
314 */
315 public function testGetRedirectTarget( $title, $text ) {
316 $this->setMwGlobals( array(
317 'wgServer' => '//example.org',
318 'wgScriptPath' => '/w/index.php',
319 ) );
320 $content = new JavaScriptContent( $text );
321 $target = $content->getRedirectTarget();
322 $this->assertEquals( $title, $target ? $target->getPrefixedText() : null );
323 }
324
325 /**
326 * Keep this in sync with JavaScriptContentHandlerTest::provideMakeRedirectContent()
327 */
328 public static function provideGetRedirectTarget() {
329 return array(
330 array( 'MediaWiki:MonoBook.js', '/* #REDIRECT */mw.loader.load("//example.org/w/index.php?title=MediaWiki:MonoBook.js\u0026action=raw\u0026ctype=text/javascript");' ),
331 array( 'User:FooBar/common.js', '/* #REDIRECT */mw.loader.load("//example.org/w/index.php?title=User:FooBar/common.js\u0026action=raw\u0026ctype=text/javascript");' ),
332 array( 'Gadget:FooBaz.js', '/* #REDIRECT */mw.loader.load("//example.org/w/index.php?title=Gadget:FooBaz.js\u0026action=raw\u0026ctype=text/javascript");' ),
333 // No #REDIRECT comment
334 array( null, 'mw.loader.load("//example.org/w/index.php?title=MediaWiki:NoRedirect.js\u0026action=raw\u0026ctype=text/javascript");' ),
335 // Different domain
336 array( null, '/* #REDIRECT */mw.loader.load("//example.com/w/index.php?title=MediaWiki:OtherWiki.js\u0026action=raw\u0026ctype=text/javascript");' ),
337 );
338 }
339 }