Merge "Move action filter logic to LogPager"
[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 public static function dataIsCountable() {
184 return [
185 [ '',
186 null,
187 'any',
188 true
189 ],
190 [ 'Foo',
191 null,
192 'any',
193 true
194 ],
195 [ 'Foo',
196 null,
197 'comma',
198 false
199 ],
200 [ 'Foo, bar',
201 null,
202 'comma',
203 false
204 ],
205 ];
206 }
207
208 /**
209 * @dataProvider dataIsCountable
210 * @group Database
211 * @covers TextContent::isCountable
212 */
213 public function testIsCountable( $text, $hasLinks, $mode, $expected ) {
214 $this->setMwGlobals( 'wgArticleCountMethod', $mode );
215
216 $content = $this->newContent( $text );
217
218 $v = $content->isCountable( $hasLinks, $this->context->getTitle() );
219
220 $this->assertEquals(
221 $expected,
222 $v,
223 'isCountable() returned unexpected value ' . var_export( $v, true )
224 . ' instead of ' . var_export( $expected, true )
225 . " in mode `$mode` for text \"$text\""
226 );
227 }
228
229 public static function dataGetTextForSummary() {
230 return [
231 [ "hello\nworld.",
232 16,
233 'hello world.',
234 ],
235 [ 'hello world.',
236 8,
237 'hello...',
238 ],
239 [ '[[hello world]].',
240 8,
241 '[[hel...',
242 ],
243 ];
244 }
245
246 /**
247 * @dataProvider dataGetTextForSummary
248 * @covers TextContent::getTextForSummary
249 */
250 public function testGetTextForSummary( $text, $maxlength, $expected ) {
251 $content = $this->newContent( $text );
252
253 $this->assertEquals( $expected, $content->getTextForSummary( $maxlength ) );
254 }
255
256 /**
257 * @covers TextContent::getTextForSearchIndex
258 */
259 public function testGetTextForSearchIndex() {
260 $content = $this->newContent( 'hello world.' );
261
262 $this->assertEquals( 'hello world.', $content->getTextForSearchIndex() );
263 }
264
265 /**
266 * @covers TextContent::copy
267 */
268 public function testCopy() {
269 $content = $this->newContent( 'hello world.' );
270 $copy = $content->copy();
271
272 $this->assertTrue( $content->equals( $copy ), 'copy must be equal to original' );
273 $this->assertEquals( 'hello world.', $copy->getNativeData() );
274 }
275
276 /**
277 * @covers TextContent::getSize
278 */
279 public function testGetSize() {
280 $content = $this->newContent( 'hello world.' );
281
282 $this->assertEquals( 12, $content->getSize() );
283 }
284
285 /**
286 * @covers TextContent::getNativeData
287 */
288 public function testGetNativeData() {
289 $content = $this->newContent( 'hello world.' );
290
291 $this->assertEquals( 'hello world.', $content->getNativeData() );
292 }
293
294 /**
295 * @covers TextContent::getWikitextForTransclusion
296 */
297 public function testGetWikitextForTransclusion() {
298 $content = $this->newContent( 'hello world.' );
299
300 $this->assertEquals( 'hello world.', $content->getWikitextForTransclusion() );
301 }
302
303 /**
304 * @covers TextContent::getModel
305 */
306 public function testGetModel() {
307 $content = $this->newContent( "hello world." );
308
309 $this->assertEquals( CONTENT_MODEL_TEXT, $content->getModel() );
310 }
311
312 /**
313 * @covers TextContent::getContentHandler
314 */
315 public function testGetContentHandler() {
316 $content = $this->newContent( "hello world." );
317
318 $this->assertEquals( CONTENT_MODEL_TEXT, $content->getContentHandler()->getModelID() );
319 }
320
321 public static function dataIsEmpty() {
322 return [
323 [ '', true ],
324 [ ' ', false ],
325 [ '0', false ],
326 [ 'hallo welt.', false ],
327 ];
328 }
329
330 /**
331 * @dataProvider dataIsEmpty
332 * @covers TextContent::isEmpty
333 */
334 public function testIsEmpty( $text, $empty ) {
335 $content = $this->newContent( $text );
336
337 $this->assertEquals( $empty, $content->isEmpty() );
338 }
339
340 public static function dataEquals() {
341 return [
342 [ new TextContent( "hallo" ), null, false ],
343 [ new TextContent( "hallo" ), new TextContent( "hallo" ), true ],
344 [ new TextContent( "hallo" ), new JavaScriptContent( "hallo" ), false ],
345 [ new TextContent( "hallo" ), new WikitextContent( "hallo" ), false ],
346 [ new TextContent( "hallo" ), new TextContent( "HALLO" ), false ],
347 ];
348 }
349
350 /**
351 * @dataProvider dataEquals
352 * @covers TextContent::equals
353 */
354 public function testEquals( Content $a, Content $b = null, $equal = false ) {
355 $this->assertEquals( $equal, $a->equals( $b ) );
356 }
357
358 public static function dataGetDeletionUpdates() {
359 return [
360 [ "TextContentTest_testGetSecondaryDataUpdates_1",
361 CONTENT_MODEL_TEXT, "hello ''world''\n",
362 []
363 ],
364 [ "TextContentTest_testGetSecondaryDataUpdates_2",
365 CONTENT_MODEL_TEXT, "hello [[world test 21344]]\n",
366 []
367 ],
368 // TODO: more...?
369 ];
370 }
371
372 /**
373 * @dataProvider dataGetDeletionUpdates
374 * @covers TextContent::getDeletionUpdates
375 */
376 public function testDeletionUpdates( $title, $model, $text, $expectedStuff ) {
377 $ns = $this->getDefaultWikitextNS();
378 $title = Title::newFromText( $title, $ns );
379
380 $content = ContentHandler::makeContent( $text, $title, $model );
381
382 $page = WikiPage::factory( $title );
383 $page->doEditContent( $content, '' );
384
385 $updates = $content->getDeletionUpdates( $page );
386
387 // make updates accessible by class name
388 foreach ( $updates as $update ) {
389 $class = get_class( $update );
390 $updates[$class] = $update;
391 }
392
393 if ( !$expectedStuff ) {
394 $this->assertTrue( true ); // make phpunit happy
395 return;
396 }
397
398 foreach ( $expectedStuff as $class => $fieldValues ) {
399 $this->assertArrayHasKey( $class, $updates, "missing an update of type $class" );
400
401 $update = $updates[$class];
402
403 foreach ( $fieldValues as $field => $value ) {
404 $v = $update->$field; # if the field doesn't exist, just crash and burn
405 $this->assertEquals( $value, $v, "unexpected value for field $field in instance of $class" );
406 }
407 }
408
409 $page->doDeleteArticle( '' );
410 }
411
412 public static function provideConvert() {
413 return [
414 [ // #0
415 'Hallo Welt',
416 CONTENT_MODEL_WIKITEXT,
417 'lossless',
418 'Hallo Welt'
419 ],
420 [ // #1
421 'Hallo Welt',
422 CONTENT_MODEL_WIKITEXT,
423 'lossless',
424 'Hallo Welt'
425 ],
426 [ // #1
427 'Hallo Welt',
428 CONTENT_MODEL_CSS,
429 'lossless',
430 'Hallo Welt'
431 ],
432 [ // #1
433 'Hallo Welt',
434 CONTENT_MODEL_JAVASCRIPT,
435 'lossless',
436 'Hallo Welt'
437 ],
438 ];
439 }
440
441 /**
442 * @dataProvider provideConvert
443 * @covers TextContent::convert
444 */
445 public function testConvert( $text, $model, $lossy, $expectedNative ) {
446 $content = $this->newContent( $text );
447
448 $converted = $content->convert( $model, $lossy );
449
450 if ( $expectedNative === false ) {
451 $this->assertFalse( $converted, "conversion to $model was expected to fail!" );
452 } else {
453 $this->assertInstanceOf( 'Content', $converted );
454 $this->assertEquals( $expectedNative, $converted->getNativeData() );
455 }
456 }
457 }