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/index.php',
263 $target = Title
::newFromText( "testUpdateRedirect_target" );
265 $content = new JavaScriptContent( $oldText );
266 $newContent = $content->updateRedirect( $target );
268 $this->assertEquals( $expectedText, $newContent->getNativeData() );
271 public static function provideUpdateRedirect() {
274 '#REDIRECT [[Someplace]]',
275 '#REDIRECT [[Someplace]]',
278 // @codingStandardsIgnoreStart Generic.Files.LineLength
280 '/* #REDIRECT */mw.loader.load("//example.org/w/index.php?title=MediaWiki:MonoBook.js\u0026action=raw\u0026ctype=text/javascript");',
281 '/* #REDIRECT */mw.loader.load("//example.org/w/index.php?title=TestUpdateRedirect_target\u0026action=raw\u0026ctype=text/javascript");'
283 // @codingStandardsIgnoreEnd
288 * @covers JavaScriptContent::getModel
290 public function testGetModel() {
291 $content = $this->newContent( "hello world." );
293 $this->assertEquals( CONTENT_MODEL_JAVASCRIPT
, $content->getModel() );
297 * @covers JavaScriptContent::getContentHandler
299 public function testGetContentHandler() {
300 $content = $this->newContent( "hello world." );
302 $this->assertEquals( CONTENT_MODEL_JAVASCRIPT
, $content->getContentHandler()->getModelID() );
305 public static function dataEquals() {
307 array( new JavaScriptContent( "hallo" ), null, false ),
308 array( new JavaScriptContent( "hallo" ), new JavaScriptContent( "hallo" ), true ),
309 array( new JavaScriptContent( "hallo" ), new CssContent( "hallo" ), false ),
310 array( new JavaScriptContent( "hallo" ), new JavaScriptContent( "HALLO" ), false ),
315 * @dataProvider provideGetRedirectTarget
317 public function testGetRedirectTarget( $title, $text ) {
318 $this->setMwGlobals( array(
319 'wgServer' => '//example.org',
320 'wgScriptPath' => '/w/index.php',
322 $content = new JavaScriptContent( $text );
323 $target = $content->getRedirectTarget();
324 $this->assertEquals( $title, $target ?
$target->getPrefixedText() : null );
328 * Keep this in sync with JavaScriptContentHandlerTest::provideMakeRedirectContent()
330 public static function provideGetRedirectTarget() {
331 // @codingStandardsIgnoreStart Generic.Files.LineLength
333 array( 'MediaWiki:MonoBook.js', '/* #REDIRECT */mw.loader.load("//example.org/w/index.php?title=MediaWiki:MonoBook.js\u0026action=raw\u0026ctype=text/javascript");' ),
334 array( 'User:FooBar/common.js', '/* #REDIRECT */mw.loader.load("//example.org/w/index.php?title=User:FooBar/common.js\u0026action=raw\u0026ctype=text/javascript");' ),
335 array( 'Gadget:FooBaz.js', '/* #REDIRECT */mw.loader.load("//example.org/w/index.php?title=Gadget:FooBaz.js\u0026action=raw\u0026ctype=text/javascript");' ),
336 // No #REDIRECT comment
337 array( null, 'mw.loader.load("//example.org/w/index.php?title=MediaWiki:NoRedirect.js\u0026action=raw\u0026ctype=text/javascript");' ),
339 array( null, '/* #REDIRECT */mw.loader.load("//example.com/w/index.php?title=MediaWiki:OtherWiki.js\u0026action=raw\u0026ctype=text/javascript");' ),
341 // @codingStandardsIgnoreEnd