Merge "Update comment about enabled extensions"
[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(
120 'hello this is ~~~',
121 'hello this is ~~~',
122 ),
123 array(
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 array(
132 array( '#REDIRECT [[Test]]',
133 null,
134 ),
135 array( '#REDIRECT Test',
136 null,
137 ),
138 array( '* #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 array(
166 array( '',
167 null,
168 'any',
169 true
170 ),
171 array( 'Foo',
172 null,
173 'any',
174 true
175 ),
176 array( 'Foo',
177 null,
178 'comma',
179 false
180 ),
181 array( 'Foo, bar',
182 null,
183 'comma',
184 false
185 ),
186 array( 'Foo',
187 null,
188 'link',
189 false
190 ),
191 array( 'Foo [[bar]]',
192 null,
193 'link',
194 false
195 ),
196 array( 'Foo',
197 true,
198 'link',
199 false
200 ),
201 array( 'Foo [[bar]]',
202 false,
203 'link',
204 false
205 ),
206 array( '#REDIRECT [[bar]]',
207 true,
208 'any',
209 true
210 ),
211 array( '#REDIRECT [[bar]]',
212 true,
213 'comma',
214 false
215 ),
216 array( '#REDIRECT [[bar]]',
217 true,
218 'link',
219 false
220 ),
221 );
222 }
223
224 public static function dataGetTextForSummary() {
225 return array(
226 array( "hello\nworld.",
227 16,
228 'hello world.',
229 ),
230 array( 'hello world.',
231 8,
232 'hello...',
233 ),
234 array( '[[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( array(
260 'wgServer' => '//example.org',
261 'wgScriptPath' => '/w/index.php',
262 ) );
263 $target = Title::newFromText( "testUpdateRedirect_target" );
264
265 $content = new JavaScriptContent( $oldText );
266 $newContent = $content->updateRedirect( $target );
267
268 $this->assertEquals( $expectedText, $newContent->getNativeData() );
269 }
270
271 public static function provideUpdateRedirect() {
272 return array(
273 array(
274 '#REDIRECT [[Someplace]]',
275 '#REDIRECT [[Someplace]]',
276 ),
277
278 // @codingStandardsIgnoreStart Generic.Files.LineLength
279 array(
280 '/* #REDIRECT */mw.loader.load("//example.org/w/index.php?title=MediaWiki:MonoBook.js\u0026action=raw\u0026ctype=text/javascript");',
281 '/* #REDIRECT */mw.loader.load("//example.org/w/index.php?title=TestUpdateRedirect_target\u0026action=raw\u0026ctype=text/javascript");'
282 )
283 // @codingStandardsIgnoreEnd
284 );
285 }
286
287 /**
288 * @covers JavaScriptContent::getModel
289 */
290 public function testGetModel() {
291 $content = $this->newContent( "hello world." );
292
293 $this->assertEquals( CONTENT_MODEL_JAVASCRIPT, $content->getModel() );
294 }
295
296 /**
297 * @covers JavaScriptContent::getContentHandler
298 */
299 public function testGetContentHandler() {
300 $content = $this->newContent( "hello world." );
301
302 $this->assertEquals( CONTENT_MODEL_JAVASCRIPT, $content->getContentHandler()->getModelID() );
303 }
304
305 public static function dataEquals() {
306 return array(
307 array( new JavaScriptContent( "hallo" ), null, false ),
308 array( new JavaScriptContent( "hallo" ), new JavaScriptContent( "hallo" ), true ),
309 array( new JavaScriptContent( "hallo" ), new CssContent( "hallo" ), false ),
310 array( new JavaScriptContent( "hallo" ), new JavaScriptContent( "HALLO" ), false ),
311 );
312 }
313
314 /**
315 * @dataProvider provideGetRedirectTarget
316 */
317 public function testGetRedirectTarget( $title, $text ) {
318 $this->setMwGlobals( array(
319 'wgServer' => '//example.org',
320 'wgScriptPath' => '/w/index.php',
321 ) );
322 $content = new JavaScriptContent( $text );
323 $target = $content->getRedirectTarget();
324 $this->assertEquals( $title, $target ? $target->getPrefixedText() : null );
325 }
326
327 /**
328 * Keep this in sync with JavaScriptContentHandlerTest::provideMakeRedirectContent()
329 */
330 public static function provideGetRedirectTarget() {
331 // @codingStandardsIgnoreStart Generic.Files.LineLength
332 return array(
333 array( 'MediaWiki:MonoBook.js', '/* #REDIRECT */mw.loader.load("//example.org/w/index.php?title=MediaWiki:MonoBook.js\u0026action=raw\u0026ctype=text/javascript");' ),
334 array( 'User:FooBar/common.js', '/* #REDIRECT */mw.loader.load("//example.org/w/index.php?title=User:FooBar/common.js\u0026action=raw\u0026ctype=text/javascript");' ),
335 array( 'Gadget:FooBaz.js', '/* #REDIRECT */mw.loader.load("//example.org/w/index.php?title=Gadget:FooBaz.js\u0026action=raw\u0026ctype=text/javascript");' ),
336 // No #REDIRECT comment
337 array( null, 'mw.loader.load("//example.org/w/index.php?title=MediaWiki:NoRedirect.js\u0026action=raw\u0026ctype=text/javascript");' ),
338 // Different domain
339 array( null, '/* #REDIRECT */mw.loader.load("//example.com/w/index.php?title=MediaWiki:OtherWiki.js\u0026action=raw\u0026ctype=text/javascript");' ),
340 );
341 // @codingStandardsIgnoreEnd
342 }
343 }