Import: Handle uploads with sha1 starting with 0 properly
[mediawiki.git] / tests / phpunit / includes / content / JavaScriptContentTest.php
bloba636e564b4b7fa57526bd8a86ef4775ac83e2bec
1 <?php
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 {
10 public function newContent( $text ) {
11 return new JavaScriptContent( $text );
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>"
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 )
34 // TODO: more...?
38 // XXX: Unused function
39 public static function dataGetSection() {
40 return array(
41 array( WikitextContentTest::$sections,
42 '0',
43 null
45 array( WikitextContentTest::$sections,
46 '2',
47 null
49 array( WikitextContentTest::$sections,
50 '8',
51 null
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
65 array( WikitextContentTest::$sections,
66 '',
67 'No more',
68 null,
69 null
71 array( WikitextContentTest::$sections,
72 '2',
73 "== TEST ==\nmore fun",
74 null,
75 null
77 array( WikitextContentTest::$sections,
78 '8',
79 'No more',
80 null,
81 null
83 array( WikitextContentTest::$sections,
84 'new',
85 'No more',
86 'New',
87 null
92 /**
93 * @covers JavaScriptContent::addSectionHeader
95 public function testAddSectionHeader() {
96 $content = $this->newContent( 'hello world' );
97 $c = $content->addSectionHeader( 'test' );
99 $this->assertTrue( $content->equals( $c ) );
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]]",
108 array( 'hello \'\'this\'\' is <nowiki>~~~</nowiki>',
109 'hello \'\'this\'\' is <nowiki>~~~</nowiki>',
111 array( " Foo \n ",
112 " Foo",
117 public static function dataPreloadTransform() {
118 return array(
119 array(
120 'hello this is ~~~',
121 'hello this is ~~~',
123 array(
124 'hello \'\'this\'\' is <noinclude>foo</noinclude><includeonly>bar</includeonly>',
125 'hello \'\'this\'\' is <noinclude>foo</noinclude><includeonly>bar</includeonly>',
130 public static function dataGetRedirectTarget() {
131 return array(
132 array( '#REDIRECT [[Test]]',
133 null,
135 array( '#REDIRECT Test',
136 null,
138 array( '* #REDIRECT [[Test]]',
139 null,
145 * @todo Test needs database!
148 public function getRedirectChain() {
149 $text = $this->getNativeData();
150 return Title::newFromRedirectArray( $text );
155 * @todo Test needs database!
158 public function getUltimateRedirectTarget() {
159 $text = $this->getNativeData();
160 return Title::newFromRedirectRecurse( $text );
164 public static function dataIsCountable() {
165 return array(
166 array( '',
167 null,
168 'any',
169 true
171 array( 'Foo',
172 null,
173 'any',
174 true
176 array( 'Foo',
177 null,
178 'comma',
179 false
181 array( 'Foo, bar',
182 null,
183 'comma',
184 false
186 array( 'Foo',
187 null,
188 'link',
189 false
191 array( 'Foo [[bar]]',
192 null,
193 'link',
194 false
196 array( 'Foo',
197 true,
198 'link',
199 false
201 array( 'Foo [[bar]]',
202 false,
203 'link',
204 false
206 array( '#REDIRECT [[bar]]',
207 true,
208 'any',
209 true
211 array( '#REDIRECT [[bar]]',
212 true,
213 'comma',
214 false
216 array( '#REDIRECT [[bar]]',
217 true,
218 'link',
219 false
224 public static function dataGetTextForSummary() {
225 return array(
226 array( "hello\nworld.",
228 'hello world.',
230 array( 'hello world.',
232 'hello...',
234 array( '[[hello world]].',
236 '[[hel...',
242 * @covers JavaScriptContent::matchMagicWord
244 public function testMatchMagicWord() {
245 $mw = MagicWord::get( "staticredirect" );
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"
255 * @covers JavaScriptContent::updateRedirect
256 * @dataProvider provideUpdateRedirect
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" );
267 $content = new JavaScriptContent( $oldText );
268 $newContent = $content->updateRedirect( $target );
270 $this->assertEquals( $expectedText, $newContent->getNativeData() );
273 public static function provideUpdateRedirect() {
274 return array(
275 array(
276 '#REDIRECT [[Someplace]]',
277 '#REDIRECT [[Someplace]]',
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");'
285 // @codingStandardsIgnoreEnd
290 * @covers JavaScriptContent::getModel
292 public function testGetModel() {
293 $content = $this->newContent( "hello world." );
295 $this->assertEquals( CONTENT_MODEL_JAVASCRIPT, $content->getModel() );
299 * @covers JavaScriptContent::getContentHandler
301 public function testGetContentHandler() {
302 $content = $this->newContent( "hello world." );
304 $this->assertEquals( CONTENT_MODEL_JAVASCRIPT, $content->getContentHandler()->getModelID() );
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 ),
317 * @dataProvider provideGetRedirectTarget
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 );
332 * Keep this in sync with JavaScriptContentHandlerTest::provideMakeRedirectContent()
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");' ),
345 // @codingStandardsIgnoreEnd