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 array( WikitextContentTest
::$sections,
45 array( WikitextContentTest
::$sections,
49 array( WikitextContentTest
::$sections,
56 // XXX: Unused function
57 public static function dataReplaceSection() {
59 array( WikitextContentTest
::$sections,
65 array( WikitextContentTest
::$sections,
71 array( WikitextContentTest
::$sections,
73 "== TEST ==\nmore fun",
77 array( WikitextContentTest
::$sections,
83 array( 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 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>',
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 array( '#REDIRECT [[Test]]',
135 array( '#REDIRECT Test',
138 array( '* #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() {
191 array( 'Foo [[bar]]',
201 array( 'Foo [[bar]]',
206 array( '#REDIRECT [[bar]]',
211 array( '#REDIRECT [[bar]]',
216 array( '#REDIRECT [[bar]]',
224 public static function dataGetTextForSummary() {
226 array( "hello\nworld.",
230 array( 'hello world.',
234 array( '[[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( array(
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 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',
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
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");' ),
343 array( null, '/* #REDIRECT */mw.loader.load("//example.com/w/index.php?title=MediaWiki:OtherWiki.js\u0026action=raw\u0026ctype=text/javascript");' ),
345 // @codingStandardsIgnoreEnd