Update docs/hooks.txt for ShowSearchHitTitle
[mediawiki.git] / tests / phpunit / includes / content / JavaScriptContentTest.php
blob1c746bcdda25cf0e1ef770d1c111f58964600da5
1 <?php
3 /**
4 * @group ContentHandler
5 * @group Database
6 * ^--- needed, because we do need the database to test link updates
7 */
8 class JavaScriptContentTest extends TextContentTest {
10 public function newContent( $text ) {
11 return new JavaScriptContent( $text );
14 public static function dataGetParserOutput() {
15 return [
17 'MediaWiki:Test.js',
18 null,
19 "hello <world>\n",
20 "<pre class=\"mw-code mw-js\" dir=\"ltr\">\nhello &lt;world&gt;\n\n</pre>"
23 'MediaWiki:Test.js',
24 null,
25 "hello(); // [[world]]\n",
26 "<pre class=\"mw-code mw-js\" dir=\"ltr\">\nhello(); // [[world]]\n\n</pre>",
28 'Links' => [
29 [ 'World' => 0 ]
34 // TODO: more...?
38 // XXX: Unused function
39 public static function dataGetSection() {
40 return [
41 [ WikitextContentTest::$sections,
42 '0',
43 null
45 [ WikitextContentTest::$sections,
46 '2',
47 null
49 [ WikitextContentTest::$sections,
50 '8',
51 null
56 // XXX: Unused function
57 public static function dataReplaceSection() {
58 return [
59 [ WikitextContentTest::$sections,
60 '0',
61 'No more',
62 null,
63 null
65 [ WikitextContentTest::$sections,
66 '',
67 'No more',
68 null,
69 null
71 [ WikitextContentTest::$sections,
72 '2',
73 "== TEST ==\nmore fun",
74 null,
75 null
77 [ WikitextContentTest::$sections,
78 '8',
79 'No more',
80 null,
81 null
83 [ WikitextContentTest::$sections,
84 'new',
85 'No more',
86 'New',
87 null
92 /**
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() {
104 return [
105 [ 'hello this is ~~~',
106 "hello this is [[Special:Contributions/127.0.0.1|127.0.0.1]]",
108 [ 'hello \'\'this\'\' is <nowiki>~~~</nowiki>',
109 'hello \'\'this\'\' is <nowiki>~~~</nowiki>',
111 [ " Foo \n ",
112 " Foo",
117 public static function dataPreloadTransform() {
118 return [
120 'hello this is ~~~',
121 'hello this is ~~~',
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() {
131 return [
132 [ '#REDIRECT [[Test]]',
133 null,
135 [ '#REDIRECT Test',
136 null,
138 [ '* #REDIRECT [[Test]]',
139 null,
144 public static function dataIsCountable() {
145 return [
146 [ '',
147 null,
148 'any',
149 true
151 [ 'Foo',
152 null,
153 'any',
154 true
156 [ 'Foo',
157 null,
158 'comma',
159 false
161 [ 'Foo, bar',
162 null,
163 'comma',
164 false
166 [ 'Foo',
167 null,
168 'link',
169 false
171 [ 'Foo [[bar]]',
172 null,
173 'link',
174 false
176 [ 'Foo',
177 true,
178 'link',
179 false
181 [ 'Foo [[bar]]',
182 false,
183 'link',
184 false
186 [ '#REDIRECT [[bar]]',
187 true,
188 'any',
189 true
191 [ '#REDIRECT [[bar]]',
192 true,
193 'comma',
194 false
196 [ '#REDIRECT [[bar]]',
197 true,
198 'link',
199 false
204 public static function dataGetTextForSummary() {
205 return [
206 [ "hello\nworld.",
208 'hello world.',
210 [ 'hello world.',
212 'hello...',
214 [ '[[hello world]].',
216 '[[hel...',
222 * @covers JavaScriptContent::matchMagicWord
224 public function testMatchMagicWord() {
225 $mw = MagicWord::get( "staticredirect" );
227 $content = $this->newContent( "#REDIRECT [[FOO]]\n__STATICREDIRECT__" );
228 $this->assertFalse(
229 $content->matchMagicWord( $mw ),
230 "should not have matched magic word, since it's not wikitext"
235 * @covers JavaScriptContent::updateRedirect
236 * @dataProvider provideUpdateRedirect
238 public function testUpdateRedirect( $oldText, $expectedText ) {
239 $this->setMwGlobals( [
240 'wgServer' => '//example.org',
241 'wgScriptPath' => '/w',
242 'wgScript' => '/w/index.php',
243 'wgResourceBasePath' => '/w',
244 ] );
245 $target = Title::newFromText( "testUpdateRedirect_target" );
247 $content = new JavaScriptContent( $oldText );
248 $newContent = $content->updateRedirect( $target );
250 $this->assertEquals( $expectedText, $newContent->getNativeData() );
253 public static function provideUpdateRedirect() {
254 return [
256 '#REDIRECT [[Someplace]]',
257 '#REDIRECT [[Someplace]]',
260 // @codingStandardsIgnoreStart Generic.Files.LineLength
262 '/* #REDIRECT */mw.loader.load("//example.org/w/index.php?title=MediaWiki:MonoBook.js\u0026action=raw\u0026ctype=text/javascript");',
263 '/* #REDIRECT */mw.loader.load("//example.org/w/index.php?title=TestUpdateRedirect_target\u0026action=raw\u0026ctype=text/javascript");'
265 // @codingStandardsIgnoreEnd
270 * @covers JavaScriptContent::getModel
272 public function testGetModel() {
273 $content = $this->newContent( "hello world." );
275 $this->assertEquals( CONTENT_MODEL_JAVASCRIPT, $content->getModel() );
279 * @covers JavaScriptContent::getContentHandler
281 public function testGetContentHandler() {
282 $content = $this->newContent( "hello world." );
284 $this->assertEquals( CONTENT_MODEL_JAVASCRIPT, $content->getContentHandler()->getModelID() );
287 public static function dataEquals() {
288 return [
289 [ new JavaScriptContent( "hallo" ), null, false ],
290 [ new JavaScriptContent( "hallo" ), new JavaScriptContent( "hallo" ), true ],
291 [ new JavaScriptContent( "hallo" ), new CssContent( "hallo" ), false ],
292 [ new JavaScriptContent( "hallo" ), new JavaScriptContent( "HALLO" ), false ],
297 * @dataProvider provideGetRedirectTarget
299 public function testGetRedirectTarget( $title, $text ) {
300 $this->setMwGlobals( [
301 'wgServer' => '//example.org',
302 'wgScriptPath' => '/w',
303 'wgScript' => '/w/index.php',
304 'wgResourceBasePath' => '/w',
305 ] );
306 $content = new JavaScriptContent( $text );
307 $target = $content->getRedirectTarget();
308 $this->assertEquals( $title, $target ? $target->getPrefixedText() : null );
312 * Keep this in sync with JavaScriptContentHandlerTest::provideMakeRedirectContent()
314 public static function provideGetRedirectTarget() {
315 // @codingStandardsIgnoreStart Generic.Files.LineLength
316 return [
318 'MediaWiki:MonoBook.js',
319 '/* #REDIRECT */mw.loader.load("//example.org/w/index.php?title=MediaWiki:MonoBook.js\u0026action=raw\u0026ctype=text/javascript");'
322 'User:FooBar/common.js',
323 '/* #REDIRECT */mw.loader.load("//example.org/w/index.php?title=User:FooBar/common.js\u0026action=raw\u0026ctype=text/javascript");'
326 'Gadget:FooBaz.js',
327 '/* #REDIRECT */mw.loader.load("//example.org/w/index.php?title=Gadget:FooBaz.js\u0026action=raw\u0026ctype=text/javascript");'
329 // No #REDIRECT comment
331 null,
332 'mw.loader.load("//example.org/w/index.php?title=MediaWiki:NoRedirect.js\u0026action=raw\u0026ctype=text/javascript");'
334 // Different domain
336 null,
337 '/* #REDIRECT */mw.loader.load("//example.com/w/index.php?title=MediaWiki:OtherWiki.js\u0026action=raw\u0026ctype=text/javascript");'
340 // @codingStandardsIgnoreEnd