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 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() {
206 [ '#REDIRECT [[bar]]',
211 [ '#REDIRECT [[bar]]',
216 [ '#REDIRECT [[bar]]',
224 public static function dataGetTextForSummary() {
234 [ '[[hello world]].',
242 * @covers JavaScriptContent::matchMagicWord
244 public function testMatchMagicWord() {
245 $mw = MagicWord
::get( "staticredirect" );
247 $content = $this->newContent( "#REDIRECT [[FOO]]\n__STATICREDIRECT__" );
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( [
260 'wgServer' => '//example.org',
261 'wgScriptPath' => '/w',
262 'wgScript' => '/w/index.php',
263 'wgResourceBasePath' => '/w',
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() {
276 '#REDIRECT [[Someplace]]',
277 '#REDIRECT [[Someplace]]',
280 // @codingStandardsIgnoreStart Generic.Files.LineLength
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() {
309 [ new JavaScriptContent( "hallo" ), null, false ],
310 [ new JavaScriptContent( "hallo" ), new JavaScriptContent( "hallo" ), true ],
311 [ new JavaScriptContent( "hallo" ), new CssContent( "hallo" ), false ],
312 [ new JavaScriptContent( "hallo" ), new JavaScriptContent( "HALLO" ), false ],
317 * @dataProvider provideGetRedirectTarget
319 public function testGetRedirectTarget( $title, $text ) {
320 $this->setMwGlobals( [
321 'wgServer' => '//example.org',
322 'wgScriptPath' => '/w',
323 'wgScript' => '/w/index.php',
324 'wgResourceBasePath' => '/w',
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
338 'MediaWiki:MonoBook.js',
339 '/* #REDIRECT */mw.loader.load("//example.org/w/index.php?title=MediaWiki:MonoBook.js\u0026action=raw\u0026ctype=text/javascript");'
342 'User:FooBar/common.js',
343 '/* #REDIRECT */mw.loader.load("//example.org/w/index.php?title=User:FooBar/common.js\u0026action=raw\u0026ctype=text/javascript");'
347 '/* #REDIRECT */mw.loader.load("//example.org/w/index.php?title=Gadget:FooBaz.js\u0026action=raw\u0026ctype=text/javascript");'
349 // No #REDIRECT comment
352 'mw.loader.load("//example.org/w/index.php?title=MediaWiki:NoRedirect.js\u0026action=raw\u0026ctype=text/javascript");'
357 '/* #REDIRECT */mw.loader.load("//example.com/w/index.php?title=MediaWiki:OtherWiki.js\u0026action=raw\u0026ctype=text/javascript");'
360 // @codingStandardsIgnoreEnd