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