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