Merge "Unify handling of *-message(s) settings in HTMLForm"
[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 [
16 [
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 [
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 [
28 'Links' => [
29 [ 'World' => 0 ]
30 ]
31 ]
32 ],
33
34 // TODO: more...?
35 ];
36 }
37
38 // XXX: Unused function
39 public static function dataGetSection() {
40 return [
41 [ WikitextContentTest::$sections,
42 '0',
43 null
44 ],
45 [ WikitextContentTest::$sections,
46 '2',
47 null
48 ],
49 [ WikitextContentTest::$sections,
50 '8',
51 null
52 ],
53 ];
54 }
55
56 // XXX: Unused function
57 public static function dataReplaceSection() {
58 return [
59 [ WikitextContentTest::$sections,
60 '0',
61 'No more',
62 null,
63 null
64 ],
65 [ WikitextContentTest::$sections,
66 '',
67 'No more',
68 null,
69 null
70 ],
71 [ WikitextContentTest::$sections,
72 '2',
73 "== TEST ==\nmore fun",
74 null,
75 null
76 ],
77 [ WikitextContentTest::$sections,
78 '8',
79 'No more',
80 null,
81 null
82 ],
83 [ 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 [
105 [ 'hello this is ~~~',
106 "hello this is [[Special:Contributions/127.0.0.1|127.0.0.1]]",
107 ],
108 [ 'hello \'\'this\'\' is <nowiki>~~~</nowiki>',
109 'hello \'\'this\'\' is <nowiki>~~~</nowiki>',
110 ],
111 [ " Foo \n ",
112 " Foo",
113 ],
114 ];
115 }
116
117 public static function dataPreloadTransform() {
118 return [
119 [
120 'hello this is ~~~',
121 'hello this is ~~~',
122 ],
123 [
124 'hello \'\'this\'\' is <noinclude>foo</noinclude><includeonly>bar</includeonly>',
125 'hello \'\'this\'\' is <noinclude>foo</noinclude><includeonly>bar</includeonly>',
126 ],
127 ];
128 }
129
130 public static function dataGetRedirectTarget() {
131 return [
132 [ '#REDIRECT [[Test]]',
133 null,
134 ],
135 [ '#REDIRECT Test',
136 null,
137 ],
138 [ '* #REDIRECT [[Test]]',
139 null,
140 ],
141 ];
142 }
143
144 /**
145 * @todo Test needs database!
146 */
147 /*
148 public function getRedirectChain() {
149 $text = $this->getNativeData();
150 return Title::newFromRedirectArray( $text );
151 }
152 */
153
154 /**
155 * @todo Test needs database!
156 */
157 /*
158 public function getUltimateRedirectTarget() {
159 $text = $this->getNativeData();
160 return Title::newFromRedirectRecurse( $text );
161 }
162 */
163
164 public static function dataIsCountable() {
165 return [
166 [ '',
167 null,
168 'any',
169 true
170 ],
171 [ 'Foo',
172 null,
173 'any',
174 true
175 ],
176 [ 'Foo',
177 null,
178 'comma',
179 false
180 ],
181 [ 'Foo, bar',
182 null,
183 'comma',
184 false
185 ],
186 [ 'Foo',
187 null,
188 'link',
189 false
190 ],
191 [ 'Foo [[bar]]',
192 null,
193 'link',
194 false
195 ],
196 [ 'Foo',
197 true,
198 'link',
199 false
200 ],
201 [ 'Foo [[bar]]',
202 false,
203 'link',
204 false
205 ],
206 [ '#REDIRECT [[bar]]',
207 true,
208 'any',
209 true
210 ],
211 [ '#REDIRECT [[bar]]',
212 true,
213 'comma',
214 false
215 ],
216 [ '#REDIRECT [[bar]]',
217 true,
218 'link',
219 false
220 ],
221 ];
222 }
223
224 public static function dataGetTextForSummary() {
225 return [
226 [ "hello\nworld.",
227 16,
228 'hello world.',
229 ],
230 [ 'hello world.',
231 8,
232 'hello...',
233 ],
234 [ '[[hello world]].',
235 8,
236 '[[hel...',
237 ],
238 ];
239 }
240
241 /**
242 * @covers JavaScriptContent::matchMagicWord
243 */
244 public function testMatchMagicWord() {
245 $mw = MagicWord::get( "staticredirect" );
246
247 $content = $this->newContent( "#REDIRECT [[FOO]]\n__STATICREDIRECT__" );
248 $this->assertFalse(
249 $content->matchMagicWord( $mw ),
250 "should not have matched magic word, since it's not wikitext"
251 );
252 }
253
254 /**
255 * @covers JavaScriptContent::updateRedirect
256 * @dataProvider provideUpdateRedirect
257 */
258 public function testUpdateRedirect( $oldText, $expectedText ) {
259 $this->setMwGlobals( [
260 'wgServer' => '//example.org',
261 'wgScriptPath' => '/w',
262 'wgScript' => '/w/index.php',
263 'wgResourceBasePath' => '/w',
264 ] );
265 $target = Title::newFromText( "testUpdateRedirect_target" );
266
267 $content = new JavaScriptContent( $oldText );
268 $newContent = $content->updateRedirect( $target );
269
270 $this->assertEquals( $expectedText, $newContent->getNativeData() );
271 }
272
273 public static function provideUpdateRedirect() {
274 return [
275 [
276 '#REDIRECT [[Someplace]]',
277 '#REDIRECT [[Someplace]]',
278 ],
279
280 // @codingStandardsIgnoreStart Generic.Files.LineLength
281 [
282 '/* #REDIRECT */mw.loader.load("//example.org/w/index.php?title=MediaWiki:MonoBook.js\u0026action=raw\u0026ctype=text/javascript");',
283 '/* #REDIRECT */mw.loader.load("//example.org/w/index.php?title=TestUpdateRedirect_target\u0026action=raw\u0026ctype=text/javascript");'
284 ]
285 // @codingStandardsIgnoreEnd
286 ];
287 }
288
289 /**
290 * @covers JavaScriptContent::getModel
291 */
292 public function testGetModel() {
293 $content = $this->newContent( "hello world." );
294
295 $this->assertEquals( CONTENT_MODEL_JAVASCRIPT, $content->getModel() );
296 }
297
298 /**
299 * @covers JavaScriptContent::getContentHandler
300 */
301 public function testGetContentHandler() {
302 $content = $this->newContent( "hello world." );
303
304 $this->assertEquals( CONTENT_MODEL_JAVASCRIPT, $content->getContentHandler()->getModelID() );
305 }
306
307 public static function dataEquals() {
308 return [
309 [ new JavaScriptContent( "hallo" ), null, false ],
310 [ new JavaScriptContent( "hallo" ), new JavaScriptContent( "hallo" ), true ],
311 [ new JavaScriptContent( "hallo" ), new CssContent( "hallo" ), false ],
312 [ new JavaScriptContent( "hallo" ), new JavaScriptContent( "HALLO" ), false ],
313 ];
314 }
315
316 /**
317 * @dataProvider provideGetRedirectTarget
318 */
319 public function testGetRedirectTarget( $title, $text ) {
320 $this->setMwGlobals( [
321 'wgServer' => '//example.org',
322 'wgScriptPath' => '/w',
323 'wgScript' => '/w/index.php',
324 'wgResourceBasePath' => '/w',
325 ] );
326 $content = new JavaScriptContent( $text );
327 $target = $content->getRedirectTarget();
328 $this->assertEquals( $title, $target ? $target->getPrefixedText() : null );
329 }
330
331 /**
332 * Keep this in sync with JavaScriptContentHandlerTest::provideMakeRedirectContent()
333 */
334 public static function provideGetRedirectTarget() {
335 // @codingStandardsIgnoreStart Generic.Files.LineLength
336 return [
337 [
338 'MediaWiki:MonoBook.js',
339 '/* #REDIRECT */mw.loader.load("//example.org/w/index.php?title=MediaWiki:MonoBook.js\u0026action=raw\u0026ctype=text/javascript");'
340 ],
341 [
342 'User:FooBar/common.js',
343 '/* #REDIRECT */mw.loader.load("//example.org/w/index.php?title=User:FooBar/common.js\u0026action=raw\u0026ctype=text/javascript");'
344 ],
345 [
346 'Gadget:FooBaz.js',
347 '/* #REDIRECT */mw.loader.load("//example.org/w/index.php?title=Gadget:FooBaz.js\u0026action=raw\u0026ctype=text/javascript");'
348 ],
349 // No #REDIRECT comment
350 [
351 null,
352 'mw.loader.load("//example.org/w/index.php?title=MediaWiki:NoRedirect.js\u0026action=raw\u0026ctype=text/javascript");'
353 ],
354 // Different domain
355 [
356 null,
357 '/* #REDIRECT */mw.loader.load("//example.com/w/index.php?title=MediaWiki:OtherWiki.js\u0026action=raw\u0026ctype=text/javascript");'
358 ],
359 ];
360 // @codingStandardsIgnoreEnd
361 }
362 }