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