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