SECURITY: blacklist CSS var()
[lhc/web/wiklou.git] / tests / phpunit / includes / content / TextContentTest.php
1 <?php
2
3 use MediaWiki\MediaWikiServices;
4
5 /**
6 * @group ContentHandler
7 * @group Database
8 * ^--- needed, because we do need the database to test link updates
9 */
10 class TextContentTest extends MediaWikiLangTestCase {
11 protected $context;
12
13 protected function setUp() {
14 parent::setUp();
15
16 // trigger purging of all page related tables
17 $this->tablesUsed[] = 'page';
18 $this->tablesUsed[] = 'revision';
19
20 // Anon user
21 $user = new User();
22 $user->setName( '127.0.0.1' );
23
24 $this->context = new RequestContext( new FauxRequest() );
25 $this->context->setTitle( Title::newFromText( 'Test' ) );
26 $this->context->setUser( $user );
27
28 $this->setMwGlobals( [
29 'wgUser' => $user,
30 'wgTextModelsToParse' => [
31 CONTENT_MODEL_WIKITEXT,
32 CONTENT_MODEL_CSS,
33 CONTENT_MODEL_JAVASCRIPT,
34 ],
35 'wgTidyConfig' => [ 'driver' => 'RemexHtml' ],
36 'wgCapitalLinks' => true,
37 'wgHooks' => [], // bypass hook ContentGetParserOutput that force custom rendering
38 ] );
39
40 MWTidy::destroySingleton();
41 }
42
43 protected function tearDown() {
44 MWTidy::destroySingleton();
45 parent::tearDown();
46 }
47
48 /**
49 * @param string $text
50 * @return TextContent
51 */
52 public function newContent( $text ) {
53 return new TextContent( $text );
54 }
55
56 public static function dataGetParserOutput() {
57 return [
58 [
59 'TextContentTest_testGetParserOutput',
60 CONTENT_MODEL_TEXT,
61 "hello ''world'' & [[stuff]]\n", "hello ''world'' &amp; [[stuff]]",
62 [
63 'Links' => []
64 ]
65 ],
66 // TODO: more...?
67 ];
68 }
69
70 /**
71 * @dataProvider dataGetParserOutput
72 * @covers TextContent::getParserOutput
73 */
74 public function testGetParserOutput( $title, $model, $text, $expectedHtml,
75 $expectedFields = null
76 ) {
77 $title = Title::newFromText( $title );
78 $content = ContentHandler::makeContent( $text, $title, $model );
79
80 $po = $content->getParserOutput( $title );
81
82 $html = $po->getText();
83 $html = preg_replace( '#<!--.*?-->#sm', '', $html ); // strip comments
84
85 $this->assertEquals( $expectedHtml, trim( $html ) );
86
87 if ( $expectedFields ) {
88 foreach ( $expectedFields as $field => $exp ) {
89 $f = 'get' . ucfirst( $field );
90 $v = call_user_func( [ $po, $f ] );
91
92 if ( is_array( $exp ) ) {
93 $this->assertArrayEquals( $exp, $v );
94 } else {
95 $this->assertEquals( $exp, $v );
96 }
97 }
98 }
99
100 // TODO: assert more properties
101 }
102
103 public static function dataPreSaveTransform() {
104 return [
105 [
106 # 0: no signature resolution
107 'hello this is ~~~',
108 'hello this is ~~~',
109 ],
110 [
111 # 1: rtrim
112 " Foo \n ",
113 ' Foo',
114 ],
115 [
116 # 2: newline normalization
117 "LF\n\nCRLF\r\n\r\nCR\r\rEND",
118 "LF\n\nCRLF\n\nCR\n\nEND",
119 ],
120 ];
121 }
122
123 /**
124 * @dataProvider dataPreSaveTransform
125 * @covers TextContent::preSaveTransform
126 */
127 public function testPreSaveTransform( $text, $expected ) {
128 $options = ParserOptions::newFromUserAndLang( $this->context->getUser(),
129 MediaWikiServices::getInstance()->getContentLanguage() );
130
131 $content = $this->newContent( $text );
132 $content = $content->preSaveTransform(
133 $this->context->getTitle(),
134 $this->context->getUser(),
135 $options
136 );
137
138 $this->assertEquals( $expected, $content->getText() );
139 }
140
141 public static function dataPreloadTransform() {
142 return [
143 [
144 'hello this is ~~~',
145 'hello this is ~~~',
146 ],
147 ];
148 }
149
150 /**
151 * @dataProvider dataPreloadTransform
152 * @covers TextContent::preloadTransform
153 */
154 public function testPreloadTransform( $text, $expected ) {
155 $options = ParserOptions::newFromUserAndLang( $this->context->getUser(),
156 MediaWikiServices::getInstance()->getContentLanguage() );
157
158 $content = $this->newContent( $text );
159 $content = $content->preloadTransform( $this->context->getTitle(), $options );
160
161 $this->assertEquals( $expected, $content->getText() );
162 }
163
164 public static function dataGetRedirectTarget() {
165 return [
166 [ '#REDIRECT [[Test]]',
167 null,
168 ],
169 ];
170 }
171
172 /**
173 * @dataProvider dataGetRedirectTarget
174 * @covers TextContent::getRedirectTarget
175 */
176 public function testGetRedirectTarget( $text, $expected ) {
177 $content = $this->newContent( $text );
178 $t = $content->getRedirectTarget();
179
180 if ( is_null( $expected ) ) {
181 $this->assertNull( $t, "text should not have generated a redirect target: $text" );
182 } else {
183 $this->assertEquals( $expected, $t->getPrefixedText() );
184 }
185 }
186
187 /**
188 * @dataProvider dataGetRedirectTarget
189 * @covers TextContent::isRedirect
190 */
191 public function testIsRedirect( $text, $expected ) {
192 $content = $this->newContent( $text );
193
194 $this->assertEquals( !is_null( $expected ), $content->isRedirect() );
195 }
196
197 public static function dataIsCountable() {
198 return [
199 [ '',
200 null,
201 'any',
202 true
203 ],
204 [ 'Foo',
205 null,
206 'any',
207 true
208 ],
209 ];
210 }
211
212 /**
213 * @dataProvider dataIsCountable
214 * @covers TextContent::isCountable
215 */
216 public function testIsCountable( $text, $hasLinks, $mode, $expected ) {
217 $this->setMwGlobals( 'wgArticleCountMethod', $mode );
218
219 $content = $this->newContent( $text );
220
221 $v = $content->isCountable( $hasLinks, $this->context->getTitle() );
222
223 $this->assertEquals(
224 $expected,
225 $v,
226 'isCountable() returned unexpected value ' . var_export( $v, true )
227 . ' instead of ' . var_export( $expected, true )
228 . " in mode `$mode` for text \"$text\""
229 );
230 }
231
232 public static function dataGetTextForSummary() {
233 return [
234 [ "hello\nworld.",
235 16,
236 'hello world.',
237 ],
238 [ 'hello world.',
239 8,
240 'hello...',
241 ],
242 [ '[[hello world]].',
243 8,
244 '[[hel...',
245 ],
246 ];
247 }
248
249 /**
250 * @dataProvider dataGetTextForSummary
251 * @covers TextContent::getTextForSummary
252 */
253 public function testGetTextForSummary( $text, $maxlength, $expected ) {
254 $content = $this->newContent( $text );
255
256 $this->assertEquals( $expected, $content->getTextForSummary( $maxlength ) );
257 }
258
259 /**
260 * @covers TextContent::getTextForSearchIndex
261 */
262 public function testGetTextForSearchIndex() {
263 $content = $this->newContent( 'hello world.' );
264
265 $this->assertEquals( 'hello world.', $content->getTextForSearchIndex() );
266 }
267
268 /**
269 * @covers TextContent::copy
270 */
271 public function testCopy() {
272 $content = $this->newContent( 'hello world.' );
273 $copy = $content->copy();
274
275 $this->assertTrue( $content->equals( $copy ), 'copy must be equal to original' );
276 $this->assertEquals( 'hello world.', $copy->getText() );
277 }
278
279 /**
280 * @covers TextContent::getSize
281 */
282 public function testGetSize() {
283 $content = $this->newContent( 'hello world.' );
284
285 $this->assertEquals( 12, $content->getSize() );
286 }
287
288 /**
289 * @covers TextContent::getText
290 */
291 public function testGetText() {
292 $content = $this->newContent( 'hello world.' );
293
294 $this->assertEquals( 'hello world.', $content->getText() );
295 }
296
297 /**
298 * @covers TextContent::getNativeData
299 */
300 public function testGetNativeData() {
301 $content = $this->newContent( 'hello world.' );
302
303 $this->assertEquals( 'hello world.', $content->getText() );
304 }
305
306 /**
307 * @covers TextContent::getWikitextForTransclusion
308 */
309 public function testGetWikitextForTransclusion() {
310 $content = $this->newContent( 'hello world.' );
311
312 $this->assertEquals( 'hello world.', $content->getWikitextForTransclusion() );
313 }
314
315 /**
316 * @covers TextContent::getModel
317 */
318 public function testGetModel() {
319 $content = $this->newContent( "hello world." );
320
321 $this->assertEquals( CONTENT_MODEL_TEXT, $content->getModel() );
322 }
323
324 /**
325 * @covers TextContent::getContentHandler
326 */
327 public function testGetContentHandler() {
328 $content = $this->newContent( "hello world." );
329
330 $this->assertEquals( CONTENT_MODEL_TEXT, $content->getContentHandler()->getModelID() );
331 }
332
333 public static function dataIsEmpty() {
334 return [
335 [ '', true ],
336 [ ' ', false ],
337 [ '0', false ],
338 [ 'hallo welt.', false ],
339 ];
340 }
341
342 /**
343 * @dataProvider dataIsEmpty
344 * @covers TextContent::isEmpty
345 */
346 public function testIsEmpty( $text, $empty ) {
347 $content = $this->newContent( $text );
348
349 $this->assertEquals( $empty, $content->isEmpty() );
350 }
351
352 public static function dataEquals() {
353 return [
354 [ new TextContent( "hallo" ), null, false ],
355 [ new TextContent( "hallo" ), new TextContent( "hallo" ), true ],
356 [ new TextContent( "hallo" ), new JavaScriptContent( "hallo" ), false ],
357 [ new TextContent( "hallo" ), new WikitextContent( "hallo" ), false ],
358 [ new TextContent( "hallo" ), new TextContent( "HALLO" ), false ],
359 ];
360 }
361
362 /**
363 * @dataProvider dataEquals
364 * @covers TextContent::equals
365 */
366 public function testEquals( Content $a, Content $b = null, $equal = false ) {
367 $this->assertEquals( $equal, $a->equals( $b ) );
368 }
369
370 public static function dataGetDeletionUpdates() {
371 return [
372 [
373 CONTENT_MODEL_TEXT, "hello ''world''\n",
374 []
375 ],
376 [
377 CONTENT_MODEL_TEXT, "hello [[world test 21344]]\n",
378 []
379 ],
380 // TODO: more...?
381 ];
382 }
383
384 /**
385 * @dataProvider dataGetDeletionUpdates
386 * @covers TextContent::getDeletionUpdates
387 */
388 public function testDeletionUpdates( $model, $text, $expectedStuff ) {
389 $page = $this->getNonexistingTestPage( get_class( $this ) . '-' . $this->getName() );
390 $title = $page->getTitle();
391
392 $content = ContentHandler::makeContent( $text, $title, $model );
393 $page->doEditContent( $content, '' );
394
395 $updates = $content->getDeletionUpdates( $page );
396
397 // make updates accessible by class name
398 foreach ( $updates as $update ) {
399 $class = get_class( $update );
400 $updates[$class] = $update;
401 }
402
403 foreach ( $expectedStuff as $class => $fieldValues ) {
404 $this->assertArrayHasKey( $class, $updates, "missing an update of type $class" );
405
406 $update = $updates[$class];
407
408 foreach ( $fieldValues as $field => $value ) {
409 $v = $update->$field; # if the field doesn't exist, just crash and burn
410 $this->assertEquals( $value, $v, "unexpected value for field $field in instance of $class" );
411 }
412 }
413
414 // make phpunit happy even if $expectedStuff was empty
415 $this->assertTrue( true );
416 }
417
418 public static function provideConvert() {
419 return [
420 [ // #0
421 'Hallo Welt',
422 CONTENT_MODEL_WIKITEXT,
423 'lossless',
424 'Hallo Welt'
425 ],
426 [ // #1
427 'Hallo Welt',
428 CONTENT_MODEL_WIKITEXT,
429 'lossless',
430 'Hallo Welt'
431 ],
432 [ // #1
433 'Hallo Welt',
434 CONTENT_MODEL_CSS,
435 'lossless',
436 'Hallo Welt'
437 ],
438 [ // #1
439 'Hallo Welt',
440 CONTENT_MODEL_JAVASCRIPT,
441 'lossless',
442 'Hallo Welt'
443 ],
444 ];
445 }
446
447 /**
448 * @dataProvider provideConvert
449 * @covers TextContent::convert
450 */
451 public function testConvert( $text, $model, $lossy, $expectedNative ) {
452 $content = $this->newContent( $text );
453
454 /** @var TextContent $converted */
455 $converted = $content->convert( $model, $lossy );
456
457 if ( $expectedNative === false ) {
458 $this->assertFalse( $converted, "conversion to $model was expected to fail!" );
459 } else {
460 $this->assertInstanceOf( Content::class, $converted );
461 $this->assertEquals( $expectedNative, $converted->getText() );
462 }
463 }
464
465 /**
466 * @covers TextContent::normalizeLineEndings
467 * @dataProvider provideNormalizeLineEndings
468 */
469 public function testNormalizeLineEndings( $input, $expected ) {
470 $this->assertEquals( $expected, TextContent::normalizeLineEndings( $input ) );
471 }
472
473 public static function provideNormalizeLineEndings() {
474 return [
475 [
476 "Foo\r\nbar",
477 "Foo\nbar"
478 ],
479 [
480 "Foo\rbar",
481 "Foo\nbar"
482 ],
483 [
484 "Foobar\n ",
485 "Foobar"
486 ]
487 ];
488 }
489
490 /**
491 * @covers TextContent::__construct
492 * @covers TextContentHandler::serializeContent
493 */
494 public function testSerialize() {
495 $cnt = $this->newContent( 'testing text' );
496
497 $this->assertSame( 'testing text', $cnt->serialize() );
498 }
499
500 }