Merge "resourceloader: Remove wfExpandUrl() from ResourceLoaderImage"
[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 getUltimateRedirectTarget() {
149 $text = $this->getNativeData();
150 return Title::newFromRedirectRecurse( $text );
151 }
152 */
153
154 public static function dataIsCountable() {
155 return [
156 [ '',
157 null,
158 'any',
159 true
160 ],
161 [ 'Foo',
162 null,
163 'any',
164 true
165 ],
166 [ 'Foo',
167 null,
168 'comma',
169 false
170 ],
171 [ 'Foo, bar',
172 null,
173 'comma',
174 false
175 ],
176 [ 'Foo',
177 null,
178 'link',
179 false
180 ],
181 [ 'Foo [[bar]]',
182 null,
183 'link',
184 false
185 ],
186 [ 'Foo',
187 true,
188 'link',
189 false
190 ],
191 [ 'Foo [[bar]]',
192 false,
193 'link',
194 false
195 ],
196 [ '#REDIRECT [[bar]]',
197 true,
198 'any',
199 true
200 ],
201 [ '#REDIRECT [[bar]]',
202 true,
203 'comma',
204 false
205 ],
206 [ '#REDIRECT [[bar]]',
207 true,
208 'link',
209 false
210 ],
211 ];
212 }
213
214 public static function dataGetTextForSummary() {
215 return [
216 [ "hello\nworld.",
217 16,
218 'hello world.',
219 ],
220 [ 'hello world.',
221 8,
222 'hello...',
223 ],
224 [ '[[hello world]].',
225 8,
226 '[[hel...',
227 ],
228 ];
229 }
230
231 /**
232 * @covers JavaScriptContent::matchMagicWord
233 */
234 public function testMatchMagicWord() {
235 $mw = MagicWord::get( "staticredirect" );
236
237 $content = $this->newContent( "#REDIRECT [[FOO]]\n__STATICREDIRECT__" );
238 $this->assertFalse(
239 $content->matchMagicWord( $mw ),
240 "should not have matched magic word, since it's not wikitext"
241 );
242 }
243
244 /**
245 * @covers JavaScriptContent::updateRedirect
246 * @dataProvider provideUpdateRedirect
247 */
248 public function testUpdateRedirect( $oldText, $expectedText ) {
249 $this->setMwGlobals( [
250 'wgServer' => '//example.org',
251 'wgScriptPath' => '/w',
252 'wgScript' => '/w/index.php',
253 'wgResourceBasePath' => '/w',
254 ] );
255 $target = Title::newFromText( "testUpdateRedirect_target" );
256
257 $content = new JavaScriptContent( $oldText );
258 $newContent = $content->updateRedirect( $target );
259
260 $this->assertEquals( $expectedText, $newContent->getNativeData() );
261 }
262
263 public static function provideUpdateRedirect() {
264 return [
265 [
266 '#REDIRECT [[Someplace]]',
267 '#REDIRECT [[Someplace]]',
268 ],
269
270 // @codingStandardsIgnoreStart Generic.Files.LineLength
271 [
272 '/* #REDIRECT */mw.loader.load("//example.org/w/index.php?title=MediaWiki:MonoBook.js\u0026action=raw\u0026ctype=text/javascript");',
273 '/* #REDIRECT */mw.loader.load("//example.org/w/index.php?title=TestUpdateRedirect_target\u0026action=raw\u0026ctype=text/javascript");'
274 ]
275 // @codingStandardsIgnoreEnd
276 ];
277 }
278
279 /**
280 * @covers JavaScriptContent::getModel
281 */
282 public function testGetModel() {
283 $content = $this->newContent( "hello world." );
284
285 $this->assertEquals( CONTENT_MODEL_JAVASCRIPT, $content->getModel() );
286 }
287
288 /**
289 * @covers JavaScriptContent::getContentHandler
290 */
291 public function testGetContentHandler() {
292 $content = $this->newContent( "hello world." );
293
294 $this->assertEquals( CONTENT_MODEL_JAVASCRIPT, $content->getContentHandler()->getModelID() );
295 }
296
297 public static function dataEquals() {
298 return [
299 [ new JavaScriptContent( "hallo" ), null, false ],
300 [ new JavaScriptContent( "hallo" ), new JavaScriptContent( "hallo" ), true ],
301 [ new JavaScriptContent( "hallo" ), new CssContent( "hallo" ), false ],
302 [ new JavaScriptContent( "hallo" ), new JavaScriptContent( "HALLO" ), false ],
303 ];
304 }
305
306 /**
307 * @dataProvider provideGetRedirectTarget
308 */
309 public function testGetRedirectTarget( $title, $text ) {
310 $this->setMwGlobals( [
311 'wgServer' => '//example.org',
312 'wgScriptPath' => '/w',
313 'wgScript' => '/w/index.php',
314 'wgResourceBasePath' => '/w',
315 ] );
316 $content = new JavaScriptContent( $text );
317 $target = $content->getRedirectTarget();
318 $this->assertEquals( $title, $target ? $target->getPrefixedText() : null );
319 }
320
321 /**
322 * Keep this in sync with JavaScriptContentHandlerTest::provideMakeRedirectContent()
323 */
324 public static function provideGetRedirectTarget() {
325 // @codingStandardsIgnoreStart Generic.Files.LineLength
326 return [
327 [
328 'MediaWiki:MonoBook.js',
329 '/* #REDIRECT */mw.loader.load("//example.org/w/index.php?title=MediaWiki:MonoBook.js\u0026action=raw\u0026ctype=text/javascript");'
330 ],
331 [
332 'User:FooBar/common.js',
333 '/* #REDIRECT */mw.loader.load("//example.org/w/index.php?title=User:FooBar/common.js\u0026action=raw\u0026ctype=text/javascript");'
334 ],
335 [
336 'Gadget:FooBaz.js',
337 '/* #REDIRECT */mw.loader.load("//example.org/w/index.php?title=Gadget:FooBaz.js\u0026action=raw\u0026ctype=text/javascript");'
338 ],
339 // No #REDIRECT comment
340 [
341 null,
342 'mw.loader.load("//example.org/w/index.php?title=MediaWiki:NoRedirect.js\u0026action=raw\u0026ctype=text/javascript");'
343 ],
344 // Different domain
345 [
346 null,
347 '/* #REDIRECT */mw.loader.load("//example.com/w/index.php?title=MediaWiki:OtherWiki.js\u0026action=raw\u0026ctype=text/javascript");'
348 ],
349 ];
350 // @codingStandardsIgnoreEnd
351 }
352 }