Merge "Introduce top level service locator."
[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 }
107
108 /**
109 * @dataProvider dataPreSaveTransform
110 * @covers TextContent::preSaveTransform
111 */
112 public function testPreSaveTransform( $text, $expected ) {
113 global $wgContLang;
114
115 $options = ParserOptions::newFromUserAndLang( $this->context->getUser(), $wgContLang );
116
117 $content = $this->newContent( $text );
118 $content = $content->preSaveTransform(
119 $this->context->getTitle(),
120 $this->context->getUser(),
121 $options
122 );
123
124 $this->assertEquals( $expected, $content->getNativeData() );
125 }
126
127 public static function dataPreloadTransform() {
128 return [
129 [
130 'hello this is ~~~',
131 'hello this is ~~~',
132 ],
133 ];
134 }
135
136 /**
137 * @dataProvider dataPreloadTransform
138 * @covers TextContent::preloadTransform
139 */
140 public function testPreloadTransform( $text, $expected ) {
141 global $wgContLang;
142 $options = ParserOptions::newFromUserAndLang( $this->context->getUser(), $wgContLang );
143
144 $content = $this->newContent( $text );
145 $content = $content->preloadTransform( $this->context->getTitle(), $options );
146
147 $this->assertEquals( $expected, $content->getNativeData() );
148 }
149
150 public static function dataGetRedirectTarget() {
151 return [
152 [ '#REDIRECT [[Test]]',
153 null,
154 ],
155 ];
156 }
157
158 /**
159 * @dataProvider dataGetRedirectTarget
160 * @covers TextContent::getRedirectTarget
161 */
162 public function testGetRedirectTarget( $text, $expected ) {
163 $content = $this->newContent( $text );
164 $t = $content->getRedirectTarget();
165
166 if ( is_null( $expected ) ) {
167 $this->assertNull( $t, "text should not have generated a redirect target: $text" );
168 } else {
169 $this->assertEquals( $expected, $t->getPrefixedText() );
170 }
171 }
172
173 /**
174 * @dataProvider dataGetRedirectTarget
175 * @covers TextContent::isRedirect
176 */
177 public function testIsRedirect( $text, $expected ) {
178 $content = $this->newContent( $text );
179
180 $this->assertEquals( !is_null( $expected ), $content->isRedirect() );
181 }
182
183 /**
184 * @todo Test needs database! Should be done by a test class in the Database group.
185 */
186 /*
187 public function getUltimateRedirectTarget() {
188 $text = $this->getNativeData();
189 return Title::newFromRedirectRecurse( $text );
190 }
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 [ 'Foo',
206 null,
207 'comma',
208 false
209 ],
210 [ 'Foo, bar',
211 null,
212 'comma',
213 false
214 ],
215 ];
216 }
217
218 /**
219 * @dataProvider dataIsCountable
220 * @group Database
221 * @covers TextContent::isCountable
222 */
223 public function testIsCountable( $text, $hasLinks, $mode, $expected ) {
224 $this->setMwGlobals( 'wgArticleCountMethod', $mode );
225
226 $content = $this->newContent( $text );
227
228 $v = $content->isCountable( $hasLinks, $this->context->getTitle() );
229
230 $this->assertEquals(
231 $expected,
232 $v,
233 'isCountable() returned unexpected value ' . var_export( $v, true )
234 . ' instead of ' . var_export( $expected, true )
235 . " in mode `$mode` for text \"$text\""
236 );
237 }
238
239 public static function dataGetTextForSummary() {
240 return [
241 [ "hello\nworld.",
242 16,
243 'hello world.',
244 ],
245 [ 'hello world.',
246 8,
247 'hello...',
248 ],
249 [ '[[hello world]].',
250 8,
251 '[[hel...',
252 ],
253 ];
254 }
255
256 /**
257 * @dataProvider dataGetTextForSummary
258 * @covers TextContent::getTextForSummary
259 */
260 public function testGetTextForSummary( $text, $maxlength, $expected ) {
261 $content = $this->newContent( $text );
262
263 $this->assertEquals( $expected, $content->getTextForSummary( $maxlength ) );
264 }
265
266 /**
267 * @covers TextContent::getTextForSearchIndex
268 */
269 public function testGetTextForSearchIndex() {
270 $content = $this->newContent( 'hello world.' );
271
272 $this->assertEquals( 'hello world.', $content->getTextForSearchIndex() );
273 }
274
275 /**
276 * @covers TextContent::copy
277 */
278 public function testCopy() {
279 $content = $this->newContent( 'hello world.' );
280 $copy = $content->copy();
281
282 $this->assertTrue( $content->equals( $copy ), 'copy must be equal to original' );
283 $this->assertEquals( 'hello world.', $copy->getNativeData() );
284 }
285
286 /**
287 * @covers TextContent::getSize
288 */
289 public function testGetSize() {
290 $content = $this->newContent( 'hello world.' );
291
292 $this->assertEquals( 12, $content->getSize() );
293 }
294
295 /**
296 * @covers TextContent::getNativeData
297 */
298 public function testGetNativeData() {
299 $content = $this->newContent( 'hello world.' );
300
301 $this->assertEquals( 'hello world.', $content->getNativeData() );
302 }
303
304 /**
305 * @covers TextContent::getWikitextForTransclusion
306 */
307 public function testGetWikitextForTransclusion() {
308 $content = $this->newContent( 'hello world.' );
309
310 $this->assertEquals( 'hello world.', $content->getWikitextForTransclusion() );
311 }
312
313 /**
314 * @covers TextContent::getModel
315 */
316 public function testGetModel() {
317 $content = $this->newContent( "hello world." );
318
319 $this->assertEquals( CONTENT_MODEL_TEXT, $content->getModel() );
320 }
321
322 /**
323 * @covers TextContent::getContentHandler
324 */
325 public function testGetContentHandler() {
326 $content = $this->newContent( "hello world." );
327
328 $this->assertEquals( CONTENT_MODEL_TEXT, $content->getContentHandler()->getModelID() );
329 }
330
331 public static function dataIsEmpty() {
332 return [
333 [ '', true ],
334 [ ' ', false ],
335 [ '0', false ],
336 [ 'hallo welt.', false ],
337 ];
338 }
339
340 /**
341 * @dataProvider dataIsEmpty
342 * @covers TextContent::isEmpty
343 */
344 public function testIsEmpty( $text, $empty ) {
345 $content = $this->newContent( $text );
346
347 $this->assertEquals( $empty, $content->isEmpty() );
348 }
349
350 public static function dataEquals() {
351 return [
352 [ new TextContent( "hallo" ), null, false ],
353 [ new TextContent( "hallo" ), new TextContent( "hallo" ), true ],
354 [ new TextContent( "hallo" ), new JavaScriptContent( "hallo" ), false ],
355 [ new TextContent( "hallo" ), new WikitextContent( "hallo" ), false ],
356 [ new TextContent( "hallo" ), new TextContent( "HALLO" ), false ],
357 ];
358 }
359
360 /**
361 * @dataProvider dataEquals
362 * @covers TextContent::equals
363 */
364 public function testEquals( Content $a, Content $b = null, $equal = false ) {
365 $this->assertEquals( $equal, $a->equals( $b ) );
366 }
367
368 public static function dataGetDeletionUpdates() {
369 return [
370 [ "TextContentTest_testGetSecondaryDataUpdates_1",
371 CONTENT_MODEL_TEXT, "hello ''world''\n",
372 []
373 ],
374 [ "TextContentTest_testGetSecondaryDataUpdates_2",
375 CONTENT_MODEL_TEXT, "hello [[world test 21344]]\n",
376 []
377 ],
378 // TODO: more...?
379 ];
380 }
381
382 /**
383 * @dataProvider dataGetDeletionUpdates
384 * @covers TextContent::getDeletionUpdates
385 */
386 public function testDeletionUpdates( $title, $model, $text, $expectedStuff ) {
387 $ns = $this->getDefaultWikitextNS();
388 $title = Title::newFromText( $title, $ns );
389
390 $content = ContentHandler::makeContent( $text, $title, $model );
391
392 $page = WikiPage::factory( $title );
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 if ( !$expectedStuff ) {
404 $this->assertTrue( true ); // make phpunit happy
405 return;
406 }
407
408 foreach ( $expectedStuff as $class => $fieldValues ) {
409 $this->assertArrayHasKey( $class, $updates, "missing an update of type $class" );
410
411 $update = $updates[$class];
412
413 foreach ( $fieldValues as $field => $value ) {
414 $v = $update->$field; # if the field doesn't exist, just crash and burn
415 $this->assertEquals( $value, $v, "unexpected value for field $field in instance of $class" );
416 }
417 }
418
419 $page->doDeleteArticle( '' );
420 }
421
422 public static function provideConvert() {
423 return [
424 [ // #0
425 'Hallo Welt',
426 CONTENT_MODEL_WIKITEXT,
427 'lossless',
428 'Hallo Welt'
429 ],
430 [ // #1
431 'Hallo Welt',
432 CONTENT_MODEL_WIKITEXT,
433 'lossless',
434 'Hallo Welt'
435 ],
436 [ // #1
437 'Hallo Welt',
438 CONTENT_MODEL_CSS,
439 'lossless',
440 'Hallo Welt'
441 ],
442 [ // #1
443 'Hallo Welt',
444 CONTENT_MODEL_JAVASCRIPT,
445 'lossless',
446 'Hallo Welt'
447 ],
448 ];
449 }
450
451 /**
452 * @dataProvider provideConvert
453 * @covers TextContent::convert
454 */
455 public function testConvert( $text, $model, $lossy, $expectedNative ) {
456 $content = $this->newContent( $text );
457
458 $converted = $content->convert( $model, $lossy );
459
460 if ( $expectedNative === false ) {
461 $this->assertFalse( $converted, "conversion to $model was expected to fail!" );
462 } else {
463 $this->assertInstanceOf( 'Content', $converted );
464 $this->assertEquals( $expectedNative, $converted->getNativeData() );
465 }
466 }
467 }