Merge "Avoid use of rollback() in WikiPage::doEditContent()"
[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',
262 'wgScript' => '/w/index.php',
263 'wgResourceBasePath' => '/w',
264 ) );
265 $target = Title::newFromText( "testUpdateRedirect_target" );
266
267 $content = new JavaScriptContent( $oldText );
268 $newContent = $content->updateRedirect( $target );
269
270 $this->assertEquals( $expectedText, $newContent->getNativeData() );
271 }
272
273 public static function provideUpdateRedirect() {
274 return array(
275 array(
276 '#REDIRECT [[Someplace]]',
277 '#REDIRECT [[Someplace]]',
278 ),
279
280 // @codingStandardsIgnoreStart Generic.Files.LineLength
281 array(
282 '/* #REDIRECT */mw.loader.load("//example.org/w/index.php?title=MediaWiki:MonoBook.js\u0026action=raw\u0026ctype=text/javascript");',
283 '/* #REDIRECT */mw.loader.load("//example.org/w/index.php?title=TestUpdateRedirect_target\u0026action=raw\u0026ctype=text/javascript");'
284 )
285 // @codingStandardsIgnoreEnd
286 );
287 }
288
289 /**
290 * @covers JavaScriptContent::getModel
291 */
292 public function testGetModel() {
293 $content = $this->newContent( "hello world." );
294
295 $this->assertEquals( CONTENT_MODEL_JAVASCRIPT, $content->getModel() );
296 }
297
298 /**
299 * @covers JavaScriptContent::getContentHandler
300 */
301 public function testGetContentHandler() {
302 $content = $this->newContent( "hello world." );
303
304 $this->assertEquals( CONTENT_MODEL_JAVASCRIPT, $content->getContentHandler()->getModelID() );
305 }
306
307 public static function dataEquals() {
308 return array(
309 array( new JavaScriptContent( "hallo" ), null, false ),
310 array( new JavaScriptContent( "hallo" ), new JavaScriptContent( "hallo" ), true ),
311 array( new JavaScriptContent( "hallo" ), new CssContent( "hallo" ), false ),
312 array( new JavaScriptContent( "hallo" ), new JavaScriptContent( "HALLO" ), false ),
313 );
314 }
315
316 /**
317 * @dataProvider provideGetRedirectTarget
318 */
319 public function testGetRedirectTarget( $title, $text ) {
320 $this->setMwGlobals( array(
321 'wgServer' => '//example.org',
322 'wgScriptPath' => '/w',
323 'wgScript' => '/w/index.php',
324 'wgResourceBasePath' => '/w',
325 ) );
326 $content = new JavaScriptContent( $text );
327 $target = $content->getRedirectTarget();
328 $this->assertEquals( $title, $target ? $target->getPrefixedText() : null );
329 }
330
331 /**
332 * Keep this in sync with JavaScriptContentHandlerTest::provideMakeRedirectContent()
333 */
334 public static function provideGetRedirectTarget() {
335 // @codingStandardsIgnoreStart Generic.Files.LineLength
336 return array(
337 array( 'MediaWiki:MonoBook.js', '/* #REDIRECT */mw.loader.load("//example.org/w/index.php?title=MediaWiki:MonoBook.js\u0026action=raw\u0026ctype=text/javascript");' ),
338 array( 'User:FooBar/common.js', '/* #REDIRECT */mw.loader.load("//example.org/w/index.php?title=User:FooBar/common.js\u0026action=raw\u0026ctype=text/javascript");' ),
339 array( 'Gadget:FooBaz.js', '/* #REDIRECT */mw.loader.load("//example.org/w/index.php?title=Gadget:FooBaz.js\u0026action=raw\u0026ctype=text/javascript");' ),
340 // No #REDIRECT comment
341 array( null, 'mw.loader.load("//example.org/w/index.php?title=MediaWiki:NoRedirect.js\u0026action=raw\u0026ctype=text/javascript");' ),
342 // Different domain
343 array( null, '/* #REDIRECT */mw.loader.load("//example.com/w/index.php?title=MediaWiki:OtherWiki.js\u0026action=raw\u0026ctype=text/javascript");' ),
344 );
345 // @codingStandardsIgnoreEnd
346 }
347 }