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