3 use MediaWiki\Content\CssContent
;
4 use MediaWiki\Content\JavaScriptContent
;
5 use MediaWiki\MainConfigNames
;
6 use MediaWiki\Title\Title
;
9 * Needs database to do link updates.
11 * @group ContentHandler
13 * @covers \MediaWiki\Content\JavaScriptContent
15 class JavaScriptContentTest
extends TextContentTest
{
17 public function newContent( $text ) {
18 return new JavaScriptContent( $text );
21 public function testAddSectionHeader() {
22 $content = $this->newContent( 'hello world' );
23 $c = $content->addSectionHeader( 'test' );
25 $this->assertTrue( $content->equals( $c ) );
28 // XXX: currently, preSaveTransform is applied to scripts. this may change or become optional.
29 public static function dataPreSaveTransform() {
31 [ 'hello this is ~~~',
32 "hello this is [[Special:Contributions/127.0.0.1|127.0.0.1]]",
34 [ 'hello \'\'this\'\' is <nowiki>~~~</nowiki>',
35 'hello \'\'this\'\' is <nowiki>~~~</nowiki>',
43 public static function dataGetRedirectTarget() {
45 [ '#REDIRECT [[Test]]',
51 [ '* #REDIRECT [[Test]]',
57 public static function dataIsCountable() {
89 [ '#REDIRECT [[bar]]',
94 [ '#REDIRECT [[bar]]',
102 public static function dataGetTextForSummary() {
112 [ '[[hello world]].',
119 public function testMatchMagicWord() {
120 $mw = $this->getServiceContainer()->getMagicWordFactory()->get( "staticredirect" );
122 $content = $this->newContent( "#REDIRECT [[FOO]]\n__STATICREDIRECT__" );
124 $content->matchMagicWord( $mw ),
125 "should not have matched magic word, since it's not wikitext"
130 * @dataProvider provideUpdateRedirect
132 public function testUpdateRedirect( $oldText, $expectedText ) {
133 $this->overrideConfigValues( [
134 MainConfigNames
::Server
=> '//example.org',
135 MainConfigNames
::ScriptPath
=> '/w',
136 MainConfigNames
::Script
=> '/w/index.php',
137 MainConfigNames
::ResourceBasePath
=> '/w',
139 $target = Title
::makeTitle( NS_MAIN
, 'TestUpdateRedirect_target' );
141 $content = new JavaScriptContent( $oldText );
142 $newContent = $content->updateRedirect( $target );
144 $this->assertEquals( $expectedText, $newContent->getText() );
147 public static function provideUpdateRedirect() {
150 '#REDIRECT [[Someplace]]',
151 '#REDIRECT [[Someplace]]',
154 '/* #REDIRECT */mw.loader.load("//example.org/w/index.php?title=MediaWiki:MonoBook.js&action=raw&ctype=text/javascript");',
155 '/* #REDIRECT */mw.loader.load("//example.org/w/index.php?title=TestUpdateRedirect_target&action=raw&ctype=text/javascript");'
160 public function testGetModel() {
161 $content = $this->newContent( "hello world." );
163 $this->assertEquals( CONTENT_MODEL_JAVASCRIPT
, $content->getModel() );
166 public function testGetContentHandler() {
167 $content = $this->newContent( "hello world." );
169 $this->assertEquals( CONTENT_MODEL_JAVASCRIPT
, $content->getContentHandler()->getModelID() );
172 // NOTE: Overridden by subclass!
173 public static function dataEquals() {
175 [ new JavaScriptContent( "hallo" ), null, false ],
176 [ new JavaScriptContent( "hallo" ), new JavaScriptContent( "hallo" ), true ],
177 [ new JavaScriptContent( "hallo" ), new CssContent( "hallo" ), false ],
178 [ new JavaScriptContent( "hallo" ), new JavaScriptContent( "HALLO" ), false ],
183 * @dataProvider provideGetRedirectTarget
185 public function testGetRedirectTarget( $title, $text ) {
186 $this->overrideConfigValues( [
187 MainConfigNames
::Server
=> '//example.org',
188 MainConfigNames
::ScriptPath
=> '/w',
189 MainConfigNames
::Script
=> '/w/index.php',
190 MainConfigNames
::ResourceBasePath
=> '/w',
192 $content = new JavaScriptContent( $text );
193 $target = $content->getRedirectTarget();
194 $this->assertEquals( $title, $target ?
$target->getPrefixedText() : null );
197 public static function provideGetRedirectTarget() {
199 yield from JavaScriptContentHandlerTest
::provideMakeRedirectContent();
201 // Additional cases that don't roundtrip (errors, and back-compat)
202 yield
'Missing #REDIRECT comment' => [
204 'mw.loader.load("//example.org/w/index.php?title=MediaWiki:NoRedirect.js&action=raw&ctype=text/javascript");'
206 yield
'Different domain' => [
208 '/* #REDIRECT */mw.loader.load("//example.com/w/index.php?title=MediaWiki:OtherWiki.js&action=raw&ctype=text/javascript");'
210 yield
'Encoding before MW 1.42 (T107289)' => [
211 // \u0026 instead of literal &
212 'MediaWiki:MonoBook.js',
213 '/* #REDIRECT */mw.loader.load("//example.org/w/index.php?title=MediaWiki:MonoBook.js\u0026action=raw\u0026ctype=text/javascript");'