3 use MediaWiki\MediaWikiServices
;
6 * @group ContentHandler
8 * ^--- needed, because we do need the database to test link updates
10 class JavaScriptContentTest
extends TextContentTest
{
12 public function newContent( $text ) {
13 return new JavaScriptContent( $text );
16 public static function dataGetParserOutput() {
22 "<pre class=\"mw-code mw-js\" dir=\"ltr\">\nhello <world>\n\n</pre>"
27 "hello(); // [[world]]\n",
28 "<pre class=\"mw-code mw-js\" dir=\"ltr\">\nhello(); // [[world]]\n\n</pre>",
40 // XXX: Unused function
41 public static function dataGetSection() {
43 [ WikitextContentTest
::$sections,
47 [ WikitextContentTest
::$sections,
51 [ WikitextContentTest
::$sections,
58 // XXX: Unused function
59 public static function dataReplaceSection() {
61 [ WikitextContentTest
::$sections,
67 [ WikitextContentTest
::$sections,
73 [ WikitextContentTest
::$sections,
75 "== TEST ==\nmore fun",
79 [ WikitextContentTest
::$sections,
85 [ WikitextContentTest
::$sections,
95 * @covers JavaScriptContent::addSectionHeader
97 public function testAddSectionHeader() {
98 $content = $this->newContent( 'hello world' );
99 $c = $content->addSectionHeader( 'test' );
101 $this->assertTrue( $content->equals( $c ) );
104 // XXX: currently, preSaveTransform is applied to scripts. this may change or become optional.
105 public static function dataPreSaveTransform() {
107 [ 'hello this is ~~~',
108 "hello this is [[Special:Contributions/127.0.0.1|127.0.0.1]]",
110 [ 'hello \'\'this\'\' is <nowiki>~~~</nowiki>',
111 'hello \'\'this\'\' is <nowiki>~~~</nowiki>',
119 public static function dataPreloadTransform() {
126 'hello \'\'this\'\' is <noinclude>foo</noinclude><includeonly>bar</includeonly>',
127 'hello \'\'this\'\' is <noinclude>foo</noinclude><includeonly>bar</includeonly>',
132 public static function dataGetRedirectTarget() {
134 [ '#REDIRECT [[Test]]',
140 [ '* #REDIRECT [[Test]]',
146 public static function dataIsCountable() {
178 [ '#REDIRECT [[bar]]',
183 [ '#REDIRECT [[bar]]',
191 public static function dataGetTextForSummary() {
201 [ '[[hello world]].',
209 * @covers JavaScriptContent::matchMagicWord
211 public function testMatchMagicWord() {
212 $mw = MediaWikiServices
::getInstance()->getMagicWordFactory()->get( "staticredirect" );
214 $content = $this->newContent( "#REDIRECT [[FOO]]\n__STATICREDIRECT__" );
216 $content->matchMagicWord( $mw ),
217 "should not have matched magic word, since it's not wikitext"
222 * @covers JavaScriptContent::updateRedirect
223 * @dataProvider provideUpdateRedirect
225 public function testUpdateRedirect( $oldText, $expectedText ) {
226 $this->setMwGlobals( [
227 'wgServer' => '//example.org',
228 'wgScriptPath' => '/w',
229 'wgScript' => '/w/index.php',
230 'wgResourceBasePath' => '/w',
232 $target = Title
::newFromText( "testUpdateRedirect_target" );
234 $content = new JavaScriptContent( $oldText );
235 $newContent = $content->updateRedirect( $target );
237 $this->assertEquals( $expectedText, $newContent->getNativeData() );
240 public static function provideUpdateRedirect() {
241 // phpcs:disable Generic.Files.LineLength
244 '#REDIRECT [[Someplace]]',
245 '#REDIRECT [[Someplace]]',
248 '/* #REDIRECT */mw.loader.load("//example.org/w/index.php?title=MediaWiki:MonoBook.js\u0026action=raw\u0026ctype=text/javascript");',
249 '/* #REDIRECT */mw.loader.load("//example.org/w/index.php?title=TestUpdateRedirect_target\u0026action=raw\u0026ctype=text/javascript");'
256 * @covers JavaScriptContent::getModel
258 public function testGetModel() {
259 $content = $this->newContent( "hello world." );
261 $this->assertEquals( CONTENT_MODEL_JAVASCRIPT
, $content->getModel() );
265 * @covers JavaScriptContent::getContentHandler
267 public function testGetContentHandler() {
268 $content = $this->newContent( "hello world." );
270 $this->assertEquals( CONTENT_MODEL_JAVASCRIPT
, $content->getContentHandler()->getModelID() );
273 public static function dataEquals() {
275 [ new JavaScriptContent( "hallo" ), null, false ],
276 [ new JavaScriptContent( "hallo" ), new JavaScriptContent( "hallo" ), true ],
277 [ new JavaScriptContent( "hallo" ), new CssContent( "hallo" ), false ],
278 [ new JavaScriptContent( "hallo" ), new JavaScriptContent( "HALLO" ), false ],
283 * @covers JavaScriptContent::getRedirectTarget
284 * @dataProvider provideGetRedirectTarget
286 public function testGetRedirectTarget( $title, $text ) {
287 $this->setMwGlobals( [
288 'wgServer' => '//example.org',
289 'wgScriptPath' => '/w',
290 'wgScript' => '/w/index.php',
291 'wgResourceBasePath' => '/w',
293 $content = new JavaScriptContent( $text );
294 $target = $content->getRedirectTarget();
295 $this->assertEquals( $title, $target ?
$target->getPrefixedText() : null );
299 * Keep this in sync with JavaScriptContentHandlerTest::provideMakeRedirectContent()
301 public static function provideGetRedirectTarget() {
302 // phpcs:disable Generic.Files.LineLength
305 'MediaWiki:MonoBook.js',
306 '/* #REDIRECT */mw.loader.load("//example.org/w/index.php?title=MediaWiki:MonoBook.js\u0026action=raw\u0026ctype=text/javascript");'
309 'User:FooBar/common.js',
310 '/* #REDIRECT */mw.loader.load("//example.org/w/index.php?title=User:FooBar/common.js\u0026action=raw\u0026ctype=text/javascript");'
314 '/* #REDIRECT */mw.loader.load("//example.org/w/index.php?title=Gadget:FooBaz.js\u0026action=raw\u0026ctype=text/javascript");'
318 'User:😂/unicode.js',
319 '/* #REDIRECT */mw.loader.load("//example.org/w/index.php?title=User:%F0%9F%98%82/unicode.js\u0026action=raw\u0026ctype=text/javascript");'
321 // No #REDIRECT comment
324 'mw.loader.load("//example.org/w/index.php?title=MediaWiki:NoRedirect.js\u0026action=raw\u0026ctype=text/javascript");'
329 '/* #REDIRECT */mw.loader.load("//example.com/w/index.php?title=MediaWiki:OtherWiki.js\u0026action=raw\u0026ctype=text/javascript");'