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]]',
144 public static function dataIsCountable() {
186 [ '#REDIRECT [[bar]]',
191 [ '#REDIRECT [[bar]]',
196 [ '#REDIRECT [[bar]]',
204 public static function dataGetTextForSummary() {
214 [ '[[hello world]].',
222 * @covers JavaScriptContent::matchMagicWord
224 public function testMatchMagicWord() {
225 $mw = MagicWord
::get( "staticredirect" );
227 $content = $this->newContent( "#REDIRECT [[FOO]]\n__STATICREDIRECT__" );
229 $content->matchMagicWord( $mw ),
230 "should not have matched magic word, since it's not wikitext"
235 * @covers JavaScriptContent::updateRedirect
236 * @dataProvider provideUpdateRedirect
238 public function testUpdateRedirect( $oldText, $expectedText ) {
239 $this->setMwGlobals( [
240 'wgServer' => '//example.org',
241 'wgScriptPath' => '/w',
242 'wgScript' => '/w/index.php',
243 'wgResourceBasePath' => '/w',
245 $target = Title
::newFromText( "testUpdateRedirect_target" );
247 $content = new JavaScriptContent( $oldText );
248 $newContent = $content->updateRedirect( $target );
250 $this->assertEquals( $expectedText, $newContent->getNativeData() );
253 public static function provideUpdateRedirect() {
256 '#REDIRECT [[Someplace]]',
257 '#REDIRECT [[Someplace]]',
260 // @codingStandardsIgnoreStart Generic.Files.LineLength
262 '/* #REDIRECT */mw.loader.load("//example.org/w/index.php?title=MediaWiki:MonoBook.js\u0026action=raw\u0026ctype=text/javascript");',
263 '/* #REDIRECT */mw.loader.load("//example.org/w/index.php?title=TestUpdateRedirect_target\u0026action=raw\u0026ctype=text/javascript");'
265 // @codingStandardsIgnoreEnd
270 * @covers JavaScriptContent::getModel
272 public function testGetModel() {
273 $content = $this->newContent( "hello world." );
275 $this->assertEquals( CONTENT_MODEL_JAVASCRIPT
, $content->getModel() );
279 * @covers JavaScriptContent::getContentHandler
281 public function testGetContentHandler() {
282 $content = $this->newContent( "hello world." );
284 $this->assertEquals( CONTENT_MODEL_JAVASCRIPT
, $content->getContentHandler()->getModelID() );
287 public static function dataEquals() {
289 [ new JavaScriptContent( "hallo" ), null, false ],
290 [ new JavaScriptContent( "hallo" ), new JavaScriptContent( "hallo" ), true ],
291 [ new JavaScriptContent( "hallo" ), new CssContent( "hallo" ), false ],
292 [ new JavaScriptContent( "hallo" ), new JavaScriptContent( "HALLO" ), false ],
297 * @dataProvider provideGetRedirectTarget
299 public function testGetRedirectTarget( $title, $text ) {
300 $this->setMwGlobals( [
301 'wgServer' => '//example.org',
302 'wgScriptPath' => '/w',
303 'wgScript' => '/w/index.php',
304 'wgResourceBasePath' => '/w',
306 $content = new JavaScriptContent( $text );
307 $target = $content->getRedirectTarget();
308 $this->assertEquals( $title, $target ?
$target->getPrefixedText() : null );
312 * Keep this in sync with JavaScriptContentHandlerTest::provideMakeRedirectContent()
314 public static function provideGetRedirectTarget() {
315 // @codingStandardsIgnoreStart Generic.Files.LineLength
318 'MediaWiki:MonoBook.js',
319 '/* #REDIRECT */mw.loader.load("//example.org/w/index.php?title=MediaWiki:MonoBook.js\u0026action=raw\u0026ctype=text/javascript");'
322 'User:FooBar/common.js',
323 '/* #REDIRECT */mw.loader.load("//example.org/w/index.php?title=User:FooBar/common.js\u0026action=raw\u0026ctype=text/javascript");'
327 '/* #REDIRECT */mw.loader.load("//example.org/w/index.php?title=Gadget:FooBaz.js\u0026action=raw\u0026ctype=text/javascript");'
329 // No #REDIRECT comment
332 'mw.loader.load("//example.org/w/index.php?title=MediaWiki:NoRedirect.js\u0026action=raw\u0026ctype=text/javascript");'
337 '/* #REDIRECT */mw.loader.load("//example.com/w/index.php?title=MediaWiki:OtherWiki.js\u0026action=raw\u0026ctype=text/javascript");'
340 // @codingStandardsIgnoreEnd