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