Merge "Drop cache interwiki"
[mediawiki.git] / tests / phpunit / includes / content / JavaScriptContentTest.php
blob8eeb7e675c086f9c2fda62b82481771c2c664b0e
1 <?php
3 use MediaWiki\Content\CssContent;
4 use MediaWiki\Content\JavaScriptContent;
5 use MediaWiki\MainConfigNames;
6 use MediaWiki\Title\Title;
8 /**
9 * Needs database to do link updates.
11 * @group ContentHandler
12 * @group Database
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() {
30 return [
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>',
37 [ " Foo \n ",
38 " Foo",
43 public static function dataGetRedirectTarget() {
44 return [
45 [ '#REDIRECT [[Test]]',
46 null,
48 [ '#REDIRECT Test',
49 null,
51 [ '* #REDIRECT [[Test]]',
52 null,
57 public static function dataIsCountable() {
58 return [
59 [ '',
60 null,
61 'any',
62 true
64 [ 'Foo',
65 null,
66 'any',
67 true
69 [ 'Foo',
70 null,
71 'link',
72 false
74 [ 'Foo [[bar]]',
75 null,
76 'link',
77 false
79 [ 'Foo',
80 true,
81 'link',
82 false
84 [ 'Foo [[bar]]',
85 false,
86 'link',
87 false
89 [ '#REDIRECT [[bar]]',
90 true,
91 'any',
92 true
94 [ '#REDIRECT [[bar]]',
95 true,
96 'link',
97 false
102 public static function dataGetTextForSummary() {
103 return [
104 [ "hello\nworld.",
106 'hello world.',
108 [ 'hello world.',
110 'hello...',
112 [ '[[hello world]].',
114 '[[hel...',
119 public function testMatchMagicWord() {
120 $mw = $this->getServiceContainer()->getMagicWordFactory()->get( "staticredirect" );
122 $content = $this->newContent( "#REDIRECT [[FOO]]\n__STATICREDIRECT__" );
123 $this->assertFalse(
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',
138 ] );
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() {
148 return [
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() {
174 return [
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',
191 ] );
192 $content = new JavaScriptContent( $text );
193 $target = $content->getRedirectTarget();
194 $this->assertEquals( $title, $target ? $target->getPrefixedText() : null );
197 public static function provideGetRedirectTarget() {
198 // Roundtrip testing
199 yield from JavaScriptContentHandlerTest::provideMakeRedirectContent();
201 // Additional cases that don't roundtrip (errors, and back-compat)
202 yield 'Missing #REDIRECT comment' => [
203 null,
204 'mw.loader.load("//example.org/w/index.php?title=MediaWiki:NoRedirect.js&action=raw&ctype=text/javascript");'
206 yield 'Different domain' => [
207 null,
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");'