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