Debian Codegraph @

tests/phpunit/includes/content/JavaScriptContentTest.php

raw symbols SCIP
1 <?php
2 declare( strict_types = 1 );
3
4 namespace MediaWiki\Tests\Content;
5
6 use JavaScriptContentHandlerTest;
7 use MediaWiki\Content\CssContent;
8 use MediaWiki\Content\JavaScriptContent;
9 use MediaWiki\MainConfigNames;
10 use MediaWiki\Title\Title;
11
12 /**
13 * Needs database to do link updates.
14 *
15 * @group ContentHandler
16 * @group Database
17 * @covers \MediaWiki\Content\JavaScriptContent
18 */
19 class JavaScriptContentTest extends TextContentTest {
20 use ContentSerializationTestTrait;
21
22 public function newContent( $text ) {
23 return new JavaScriptContent( $text );
24 }
25
26 public function testAddSectionHeader() {
27 $content = $this->newContent( 'hello world' );
28 $c = $content->addSectionHeader( 'test' );
29
30 $this->assertTrue( $content->equals( $c ) );
31 }
32
33 // XXX: currently, preSaveTransform is applied to scripts. this may change or become optional.
34 public static function dataPreSaveTransform() {
35 return [
36 [ 'hello this is ~~~',
37 "hello this is [[Special:Contributions/127.0.0.1|127.0.0.1]]",
38 ],
39 [ 'hello \'\'this\'\' is <nowiki>~~~</nowiki>',
40 'hello \'\'this\'\' is <nowiki>~~~</nowiki>',
41 ],
42 [ " Foo \n ",
43 " Foo",
44 ],
45 ];
46 }
47
48 public static function dataGetRedirectTarget() {
49 return [
50 [ '#REDIRECT [[Test]]',
51 null,
52 ],
53 [ '#REDIRECT Test',
54 null,
55 ],
56 [ '* #REDIRECT [[Test]]',
57 null,
58 ],
59 ];
60 }
61
62 public static function dataIsCountable() {
63 return [
64 [ '',
65 null,
66 'any',
67 true
68 ],
69 [ 'Foo',
70 null,
71 'any',
72 true
73 ],
74 [ 'Foo',
75 null,
76 'link',
77 false
78 ],
79 [ 'Foo [[bar]]',
80 null,
81 'link',
82 false
83 ],
84 [ 'Foo',
85 true,
86 'link',
87 false
88 ],
89 [ 'Foo [[bar]]',
90 false,
91 'link',
92 false
93 ],
94 [ '#REDIRECT [[bar]]',
95 true,
96 'any',
97 true
98 ],
99 [ '#REDIRECT [[bar]]',
100 true,
101 'link',
102 false
103 ],
104 ];
105 }
106
107 public static function dataGetTextForSummary() {
108 return [
109 [ "hello\nworld.",
110 16,
111 'hello world.',
112 ],
113 [ 'hello world.',
114 8,
115 'hello...',
116 ],
117 [ '[[hello world]].',
118 8,
119 '[[hel...',
120 ],
121 ];
122 }
123
124 public function testMatchMagicWord() {
125 $mw = $this->getServiceContainer()->getMagicWordFactory()->get( "staticredirect" );
126
127 $content = $this->newContent( "#REDIRECT [[FOO]]\n__STATICREDIRECT__" );
128 $this->assertFalse(
129 $content->matchMagicWord( $mw ),
130 "should not have matched magic word, since it's not wikitext"
131 );
132 }
133
134 /**
135 * @dataProvider provideUpdateRedirect
136 */
137 public function testUpdateRedirect( $oldText, $expectedText ) {
138 $this->overrideConfigValues( [
139 MainConfigNames::Server => '//example.org',
140 MainConfigNames::ScriptPath => '/w',
141 MainConfigNames::Script => '/w/index.php',
142 MainConfigNames::ResourceBasePath => '/w',
143 ] );
144 $target = Title::makeTitle( NS_MAIN, 'TestUpdateRedirect_target' );
145
146 $content = new JavaScriptContent( $oldText );
147 $newContent = $content->updateRedirect( $target );
148
149 $this->assertEquals( $expectedText, $newContent->getText() );
150 }
151
152 public static function provideUpdateRedirect() {
153 return [
154 [
155 '#REDIRECT [[Someplace]]',
156 '#REDIRECT [[Someplace]]',
157 ],
158 [
159 '/* #REDIRECT */mw.loader.load("//example.org/w/index.php?title=MediaWiki:MonoBook.js&action=raw&ctype=text/javascript");',
160 '/* #REDIRECT */mw.loader.load("//example.org/w/index.php?title=TestUpdateRedirect_target&action=raw&ctype=text/javascript");'
161 ]
162 ];
163 }
164
165 public function testGetModel() {
166 $content = $this->newContent( "hello world." );
167
168 $this->assertEquals( CONTENT_MODEL_JAVASCRIPT, $content->getModel() );
169 }
170
171 public function testGetContentHandler() {
172 $content = $this->newContent( "hello world." );
173
174 $this->assertEquals( CONTENT_MODEL_JAVASCRIPT, $content->getContentHandler()->getModelID() );
175 }
176
177 // NOTE: Overridden by subclass!
178 public static function dataEquals() {
179 return [
180 [ new JavaScriptContent( "hallo" ), null, false ],
181 [ new JavaScriptContent( "hallo" ), new JavaScriptContent( "hallo" ), true ],
182 [ new JavaScriptContent( "hallo" ), new CssContent( "hallo" ), false ],
183 [ new JavaScriptContent( "hallo" ), new JavaScriptContent( "HALLO" ), false ],
184 ];
185 }
186
187 /**
188 * @dataProvider provideGetRedirectTarget
189 */
190 public function testGetRedirectTarget( $title, $text ) {
191 $this->overrideConfigValues( [
192 MainConfigNames::Server => '//example.org',
193 MainConfigNames::ScriptPath => '/w',
194 MainConfigNames::Script => '/w/index.php',
195 MainConfigNames::ResourceBasePath => '/w',
196 ] );
197 $content = new JavaScriptContent( $text );
198 $target = $content->getRedirectTarget();
199 $this->assertEquals( $title, $target ? $target->getPrefixedText() : null );
200 }
201
202 public static function provideGetRedirectTarget() {
203 // Roundtrip testing
204 yield from JavaScriptContentHandlerTest::provideMakeRedirectContent();
205
206 // Additional cases that don't roundtrip (errors, and back-compat)
207 yield 'Missing #REDIRECT comment' => [
208 null,
209 'mw.loader.load("//example.org/w/index.php?title=MediaWiki:NoRedirect.js&action=raw&ctype=text/javascript");'
210 ];
211 yield 'Different domain' => [
212 null,
213 '/* #REDIRECT */mw.loader.load("//example.com/w/index.php?title=MediaWiki:OtherWiki.js&action=raw&ctype=text/javascript");'
214 ];
215 yield 'Encoding before MW 1.42 (T107289)' => [
216 // \u0026 instead of literal &
217 'MediaWiki:MonoBook.js',
218 '/* #REDIRECT */mw.loader.load("//example.org/w/index.php?title=MediaWiki:MonoBook.js\u0026action=raw\u0026ctype=text/javascript");'
219 ];
220 }
221
222 public static function getClassToTest(): string {
223 return JavaScriptContent::class;
224 }
225
226 public static function getTestInstancesAndAssertions(): array {
227 $redirects = JavaScriptContentHandlerTest::provideMakeRedirectContent();
228 [ $redirectTitle, $redirectBlob ] = $redirects['MediaWiki namespace page'];
229 return [
230 'basic' => [
231 'instance' => new JavaScriptContent( '/* hello */' ),
232 'assertions' => static function ( $testCase, $obj ) {
233 $testCase->assertInstanceof( JavaScriptContent::class, $obj );
234 $testCase->assertSame( '/* hello */', $obj->getText() );
235 $testCase->assertNull( $obj->getRedirectTarget() );
236 },
237 ],
238 'redirect' => [
239 'instance' => new JavaScriptContent( $redirectBlob ),
240 'assertions' => static function ( $testCase, $obj ) use ( $redirectTitle, $redirectBlob ) {
241 $testCase->overrideConfigValues( [
242 MainConfigNames::Server => '//example.org',
243 MainConfigNames::ScriptPath => '/w',
244 MainConfigNames::Script => '/w/index.php',
245 MainConfigNames::ResourceBasePath => '/w',
246 ] );
247 $testCase->assertInstanceof( JavaScriptContent::class, $obj );
248 $testCase->assertSame( $redirectBlob, $obj->getText() );
249 $testCase->assertSame( $redirectTitle, $obj->getRedirectTarget()->getPrefixedText() );
250 },
251 ],
252 ];
253 }
254 }