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