4 * @group ContentHandler
6 * ^--- needed, because we do need the database to test link updates
8 class JavaScriptContentTest
extends TextContentTest
{
10 public function newContent( $text ) {
11 return new JavaScriptContent( $text );
14 public static function dataGetParserOutput() {
20 "<pre class=\"mw-code mw-js\" dir=\"ltr\">\nhello <world>\n\n</pre>"
25 "hello(); // [[world]]\n",
26 "<pre class=\"mw-code mw-js\" dir=\"ltr\">\nhello(); // [[world]]\n\n</pre>",
38 // XXX: Unused function
39 public static function dataGetSection() {
41 [ WikitextContentTest
::$sections,
45 [ WikitextContentTest
::$sections,
49 [ WikitextContentTest
::$sections,
56 // XXX: Unused function
57 public static function dataReplaceSection() {
59 [ WikitextContentTest
::$sections,
65 [ WikitextContentTest
::$sections,
71 [ WikitextContentTest
::$sections,
73 "== TEST ==\nmore fun",
77 [ WikitextContentTest
::$sections,
83 [ WikitextContentTest
::$sections,
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() {
105 [ 'hello this is ~~~',
106 "hello this is [[Special:Contributions/127.0.0.1|127.0.0.1]]",
108 [ 'hello \'\'this\'\' is <nowiki>~~~</nowiki>',
109 'hello \'\'this\'\' is <nowiki>~~~</nowiki>',
117 public static function dataPreloadTransform() {
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() {
132 [ '#REDIRECT [[Test]]',
138 [ '* #REDIRECT [[Test]]',
145 * @todo Test needs database!
148 public function getUltimateRedirectTarget() {
149 $text = $this->getNativeData();
150 return Title::newFromRedirectRecurse( $text );
154 public static function dataIsCountable() {
196 [ '#REDIRECT [[bar]]',
201 [ '#REDIRECT [[bar]]',
206 [ '#REDIRECT [[bar]]',
214 public static function dataGetTextForSummary() {
224 [ '[[hello world]].',
232 * @covers JavaScriptContent::matchMagicWord
234 public function testMatchMagicWord() {
235 $mw = MagicWord
::get( "staticredirect" );
237 $content = $this->newContent( "#REDIRECT [[FOO]]\n__STATICREDIRECT__" );
239 $content->matchMagicWord( $mw ),
240 "should not have matched magic word, since it's not wikitext"
245 * @covers JavaScriptContent::updateRedirect
246 * @dataProvider provideUpdateRedirect
248 public function testUpdateRedirect( $oldText, $expectedText ) {
249 $this->setMwGlobals( [
250 'wgServer' => '//example.org',
251 'wgScriptPath' => '/w',
252 'wgScript' => '/w/index.php',
253 'wgResourceBasePath' => '/w',
255 $target = Title
::newFromText( "testUpdateRedirect_target" );
257 $content = new JavaScriptContent( $oldText );
258 $newContent = $content->updateRedirect( $target );
260 $this->assertEquals( $expectedText, $newContent->getNativeData() );
263 public static function provideUpdateRedirect() {
266 '#REDIRECT [[Someplace]]',
267 '#REDIRECT [[Someplace]]',
270 // @codingStandardsIgnoreStart Generic.Files.LineLength
272 '/* #REDIRECT */mw.loader.load("//example.org/w/index.php?title=MediaWiki:MonoBook.js\u0026action=raw\u0026ctype=text/javascript");',
273 '/* #REDIRECT */mw.loader.load("//example.org/w/index.php?title=TestUpdateRedirect_target\u0026action=raw\u0026ctype=text/javascript");'
275 // @codingStandardsIgnoreEnd
280 * @covers JavaScriptContent::getModel
282 public function testGetModel() {
283 $content = $this->newContent( "hello world." );
285 $this->assertEquals( CONTENT_MODEL_JAVASCRIPT
, $content->getModel() );
289 * @covers JavaScriptContent::getContentHandler
291 public function testGetContentHandler() {
292 $content = $this->newContent( "hello world." );
294 $this->assertEquals( CONTENT_MODEL_JAVASCRIPT
, $content->getContentHandler()->getModelID() );
297 public static function dataEquals() {
299 [ new JavaScriptContent( "hallo" ), null, false ],
300 [ new JavaScriptContent( "hallo" ), new JavaScriptContent( "hallo" ), true ],
301 [ new JavaScriptContent( "hallo" ), new CssContent( "hallo" ), false ],
302 [ new JavaScriptContent( "hallo" ), new JavaScriptContent( "HALLO" ), false ],
307 * @dataProvider provideGetRedirectTarget
309 public function testGetRedirectTarget( $title, $text ) {
310 $this->setMwGlobals( [
311 'wgServer' => '//example.org',
312 'wgScriptPath' => '/w',
313 'wgScript' => '/w/index.php',
314 'wgResourceBasePath' => '/w',
316 $content = new JavaScriptContent( $text );
317 $target = $content->getRedirectTarget();
318 $this->assertEquals( $title, $target ?
$target->getPrefixedText() : null );
322 * Keep this in sync with JavaScriptContentHandlerTest::provideMakeRedirectContent()
324 public static function provideGetRedirectTarget() {
325 // @codingStandardsIgnoreStart Generic.Files.LineLength
328 'MediaWiki:MonoBook.js',
329 '/* #REDIRECT */mw.loader.load("//example.org/w/index.php?title=MediaWiki:MonoBook.js\u0026action=raw\u0026ctype=text/javascript");'
332 'User:FooBar/common.js',
333 '/* #REDIRECT */mw.loader.load("//example.org/w/index.php?title=User:FooBar/common.js\u0026action=raw\u0026ctype=text/javascript");'
337 '/* #REDIRECT */mw.loader.load("//example.org/w/index.php?title=Gadget:FooBaz.js\u0026action=raw\u0026ctype=text/javascript");'
339 // No #REDIRECT comment
342 'mw.loader.load("//example.org/w/index.php?title=MediaWiki:NoRedirect.js\u0026action=raw\u0026ctype=text/javascript");'
347 '/* #REDIRECT */mw.loader.load("//example.com/w/index.php?title=MediaWiki:OtherWiki.js\u0026action=raw\u0026ctype=text/javascript");'
350 // @codingStandardsIgnoreEnd