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