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