Don't check namespace in SpecialWantedtemplates
[lhc/web/wiklou.git] / tests / phpunit / includes / content / JavaScriptContentTest.php
1 <?php
2
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 {
9
10 public function newContent( $text ) {
11 return new JavaScriptContent( $text );
12 }
13
14 public static function dataGetParserOutput() {
15 return array(
16 array(
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>"
21 ),
22 array(
23 'MediaWiki:Test.js',
24 null,
25 "hello(); // [[world]]\n",
26 "<pre class=\"mw-code mw-js\" dir=\"ltr\">\nhello(); // [[world]]\n\n</pre>",
27 array(
28 'Links' => array(
29 array( 'World' => 0 )
30 )
31 )
32 ),
33
34 // TODO: more...?
35 );
36 }
37
38 // XXX: Unused function
39 public static function dataGetSection() {
40 return array(
41 array( WikitextContentTest::$sections,
42 '0',
43 null
44 ),
45 array( WikitextContentTest::$sections,
46 '2',
47 null
48 ),
49 array( WikitextContentTest::$sections,
50 '8',
51 null
52 ),
53 );
54 }
55
56 // XXX: Unused function
57 public static function dataReplaceSection() {
58 return array(
59 array( WikitextContentTest::$sections,
60 '0',
61 'No more',
62 null,
63 null
64 ),
65 array( WikitextContentTest::$sections,
66 '',
67 'No more',
68 null,
69 null
70 ),
71 array( WikitextContentTest::$sections,
72 '2',
73 "== TEST ==\nmore fun",
74 null,
75 null
76 ),
77 array( WikitextContentTest::$sections,
78 '8',
79 'No more',
80 null,
81 null
82 ),
83 array( WikitextContentTest::$sections,
84 'new',
85 'No more',
86 'New',
87 null
88 ),
89 );
90 }
91
92 /**
93 * @covers JavaScriptContent::addSectionHeader
94 */
95 public function testAddSectionHeader() {
96 $content = $this->newContent( 'hello world' );
97 $c = $content->addSectionHeader( 'test' );
98
99 $this->assertTrue( $content->equals( $c ) );
100 }
101
102 // XXX: currently, preSaveTransform is applied to scripts. this may change or become optional.
103 public static function dataPreSaveTransform() {
104 return array(
105 array( 'hello this is ~~~',
106 "hello this is [[Special:Contributions/127.0.0.1|127.0.0.1]]",
107 ),
108 array( 'hello \'\'this\'\' is <nowiki>~~~</nowiki>',
109 'hello \'\'this\'\' is <nowiki>~~~</nowiki>',
110 ),
111 array( " Foo \n ",
112 " Foo",
113 ),
114 );
115 }
116
117 public static function dataPreloadTransform() {
118 return array(
119 array( 'hello this is ~~~',
120 'hello this is ~~~',
121 ),
122 array( 'hello \'\'this\'\' is <noinclude>foo</noinclude><includeonly>bar</includeonly>',
123 'hello \'\'this\'\' is <noinclude>foo</noinclude><includeonly>bar</includeonly>',
124 ),
125 );
126 }
127
128 public static function dataGetRedirectTarget() {
129 return array(
130 array( '#REDIRECT [[Test]]',
131 null,
132 ),
133 array( '#REDIRECT Test',
134 null,
135 ),
136 array( '* #REDIRECT [[Test]]',
137 null,
138 ),
139 );
140 }
141
142 /**
143 * @todo Test needs database!
144 */
145 /*
146 public function getRedirectChain() {
147 $text = $this->getNativeData();
148 return Title::newFromRedirectArray( $text );
149 }
150 */
151
152 /**
153 * @todo Test needs database!
154 */
155 /*
156 public function getUltimateRedirectTarget() {
157 $text = $this->getNativeData();
158 return Title::newFromRedirectRecurse( $text );
159 }
160 */
161
162 public static function dataIsCountable() {
163 return array(
164 array( '',
165 null,
166 'any',
167 true
168 ),
169 array( 'Foo',
170 null,
171 'any',
172 true
173 ),
174 array( 'Foo',
175 null,
176 'comma',
177 false
178 ),
179 array( 'Foo, bar',
180 null,
181 'comma',
182 false
183 ),
184 array( 'Foo',
185 null,
186 'link',
187 false
188 ),
189 array( 'Foo [[bar]]',
190 null,
191 'link',
192 false
193 ),
194 array( 'Foo',
195 true,
196 'link',
197 false
198 ),
199 array( 'Foo [[bar]]',
200 false,
201 'link',
202 false
203 ),
204 array( '#REDIRECT [[bar]]',
205 true,
206 'any',
207 true
208 ),
209 array( '#REDIRECT [[bar]]',
210 true,
211 'comma',
212 false
213 ),
214 array( '#REDIRECT [[bar]]',
215 true,
216 'link',
217 false
218 ),
219 );
220 }
221
222 public static function dataGetTextForSummary() {
223 return array(
224 array( "hello\nworld.",
225 16,
226 'hello world.',
227 ),
228 array( 'hello world.',
229 8,
230 'hello...',
231 ),
232 array( '[[hello world]].',
233 8,
234 '[[hel...',
235 ),
236 );
237 }
238
239 /**
240 * @covers JavaScriptContent::matchMagicWord
241 */
242 public function testMatchMagicWord() {
243 $mw = MagicWord::get( "staticredirect" );
244
245 $content = $this->newContent( "#REDIRECT [[FOO]]\n__STATICREDIRECT__" );
246 $this->assertFalse(
247 $content->matchMagicWord( $mw ),
248 "should not have matched magic word, since it's not wikitext"
249 );
250 }
251
252 /**
253 * @covers JavaScriptContent::updateRedirect
254 */
255 public function testUpdateRedirect() {
256 $target = Title::newFromText( "testUpdateRedirect_target" );
257
258 $content = $this->newContent( "#REDIRECT [[Someplace]]" );
259 $newContent = $content->updateRedirect( $target );
260
261 $this->assertTrue(
262 $content->equals( $newContent ),
263 "content should be unchanged since it's not wikitext"
264 );
265 }
266
267 /**
268 * @covers JavaScriptContent::getModel
269 */
270 public function testGetModel() {
271 $content = $this->newContent( "hello world." );
272
273 $this->assertEquals( CONTENT_MODEL_JAVASCRIPT, $content->getModel() );
274 }
275
276 /**
277 * @covers JavaScriptContent::getContentHandler
278 */
279 public function testGetContentHandler() {
280 $content = $this->newContent( "hello world." );
281
282 $this->assertEquals( CONTENT_MODEL_JAVASCRIPT, $content->getContentHandler()->getModelID() );
283 }
284
285 public static function dataEquals() {
286 return array(
287 array( new JavaScriptContent( "hallo" ), null, false ),
288 array( new JavaScriptContent( "hallo" ), new JavaScriptContent( "hallo" ), true ),
289 array( new JavaScriptContent( "hallo" ), new CssContent( "hallo" ), false ),
290 array( new JavaScriptContent( "hallo" ), new JavaScriptContent( "HALLO" ), false ),
291 );
292 }
293 }