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() {
119 array( 'hello this is ~~~',
122 array( 'hello \'\'this\'\' is <noinclude>foo</noinclude><includeonly>bar</includeonly>',
123 'hello \'\'this\'\' is <noinclude>foo</noinclude><includeonly>bar</includeonly>',
128 public static function dataGetRedirectTarget() {
130 array( '#REDIRECT [[Test]]',
133 array( '#REDIRECT Test',
136 array( '* #REDIRECT [[Test]]',
143 * @todo Test needs database!
146 public function getRedirectChain() {
147 $text = $this->getNativeData();
148 return Title::newFromRedirectArray( $text );
153 * @todo Test needs database!
156 public function getUltimateRedirectTarget() {
157 $text = $this->getNativeData();
158 return Title::newFromRedirectRecurse( $text );
162 public static function dataIsCountable() {
189 array( 'Foo [[bar]]',
199 array( 'Foo [[bar]]',
204 array( '#REDIRECT [[bar]]',
209 array( '#REDIRECT [[bar]]',
214 array( '#REDIRECT [[bar]]',
222 public static function dataGetTextForSummary() {
224 array( "hello\nworld.",
228 array( 'hello world.',
232 array( '[[hello world]].',
240 * @covers JavaScriptContent::matchMagicWord
242 public function testMatchMagicWord() {
243 $mw = MagicWord
::get( "staticredirect" );
245 $content = $this->newContent( "#REDIRECT [[FOO]]\n__STATICREDIRECT__" );
247 $content->matchMagicWord( $mw ),
248 "should not have matched magic word, since it's not wikitext"
253 * @covers JavaScriptContent::updateRedirect
254 * @dataProvider provideUpdateRedirect
256 public function testUpdateRedirect( $oldText, $expectedText ) {
257 $this->setMwGlobals( array(
258 'wgServer' => '//example.org',
259 'wgScriptPath' => '/w/index.php',
261 $target = Title
::newFromText( "testUpdateRedirect_target" );
263 $content = new JavaScriptContent( $oldText );
264 $newContent = $content->updateRedirect( $target );
266 $this->assertEquals( $expectedText, $newContent->getNativeData() );
269 public static function provideUpdateRedirect() {
272 '#REDIRECT [[Someplace]]',
273 '#REDIRECT [[Someplace]]',
276 // @codingStandardsIgnoreStart Generic.Files.LineLength
278 '/* #REDIRECT */mw.loader.load("//example.org/w/index.php?title=MediaWiki:MonoBook.js\u0026action=raw\u0026ctype=text/javascript");',
279 '/* #REDIRECT */mw.loader.load("//example.org/w/index.php?title=TestUpdateRedirect_target\u0026action=raw\u0026ctype=text/javascript");'
281 // @codingStandardsIgnoreEnd
286 * @covers JavaScriptContent::getModel
288 public function testGetModel() {
289 $content = $this->newContent( "hello world." );
291 $this->assertEquals( CONTENT_MODEL_JAVASCRIPT
, $content->getModel() );
295 * @covers JavaScriptContent::getContentHandler
297 public function testGetContentHandler() {
298 $content = $this->newContent( "hello world." );
300 $this->assertEquals( CONTENT_MODEL_JAVASCRIPT
, $content->getContentHandler()->getModelID() );
303 public static function dataEquals() {
305 array( new JavaScriptContent( "hallo" ), null, false ),
306 array( new JavaScriptContent( "hallo" ), new JavaScriptContent( "hallo" ), true ),
307 array( new JavaScriptContent( "hallo" ), new CssContent( "hallo" ), false ),
308 array( new JavaScriptContent( "hallo" ), new JavaScriptContent( "HALLO" ), false ),
313 * @dataProvider provideGetRedirectTarget
315 public function testGetRedirectTarget( $title, $text ) {
316 $this->setMwGlobals( array(
317 'wgServer' => '//example.org',
318 'wgScriptPath' => '/w/index.php',
320 $content = new JavaScriptContent( $text );
321 $target = $content->getRedirectTarget();
322 $this->assertEquals( $title, $target ?
$target->getPrefixedText() : null );
326 * Keep this in sync with JavaScriptContentHandlerTest::provideMakeRedirectContent()
328 public static function provideGetRedirectTarget() {
330 array( 'MediaWiki:MonoBook.js', '/* #REDIRECT */mw.loader.load("//example.org/w/index.php?title=MediaWiki:MonoBook.js\u0026action=raw\u0026ctype=text/javascript");' ),
331 array( 'User:FooBar/common.js', '/* #REDIRECT */mw.loader.load("//example.org/w/index.php?title=User:FooBar/common.js\u0026action=raw\u0026ctype=text/javascript");' ),
332 array( 'Gadget:FooBaz.js', '/* #REDIRECT */mw.loader.load("//example.org/w/index.php?title=Gadget:FooBaz.js\u0026action=raw\u0026ctype=text/javascript");' ),
333 // No #REDIRECT comment
334 array( null, 'mw.loader.load("//example.org/w/index.php?title=MediaWiki:NoRedirect.js\u0026action=raw\u0026ctype=text/javascript");' ),
336 array( null, '/* #REDIRECT */mw.loader.load("//example.com/w/index.php?title=MediaWiki:OtherWiki.js\u0026action=raw\u0026ctype=text/javascript");' ),