Added @group database to ContentHandlerTest.
[lhc/web/wiklou.git] / tests / phpunit / includes / ContentHandlerTest.php
1 <?php
2
3 /**
4 * @group ContentHandler
5 *
6 * @note: Declare that we are using the database, because otherwise we'll fail in the "databaseless" test run.
7 * This is because the LinkHolderArray used by the parser needs database access.
8 *
9 * @group Database
10 */
11 class ContentHandlerTest extends MediaWikiTestCase {
12
13 public function setUp() {
14 global $wgExtraNamespaces, $wgNamespaceContentModels, $wgContentHandlers, $wgContLang;
15
16 $wgExtraNamespaces[ 12312 ] = 'Dummy';
17 $wgExtraNamespaces[ 12313 ] = 'Dummy_talk';
18
19 $wgNamespaceContentModels[ 12312 ] = 999999;
20 $wgContentHandlers[ 999999 ] = 'DummyContentHandlerForTesting';
21
22 MWNamespace::getCanonicalNamespaces( true ); # reset namespace cache
23 $wgContLang->resetNamespaces(); # reset namespace cache
24 }
25
26 public function tearDown() {
27 global $wgExtraNamespaces, $wgNamespaceContentModels, $wgContentHandlers, $wgContLang;
28
29 unset( $wgExtraNamespaces[ 12312 ] );
30 unset( $wgExtraNamespaces[ 12313 ] );
31
32 unset( $wgNamespaceContentModels[ 12312 ] );
33 unset( $wgContentHandlers[ 999999 ] );
34
35 MWNamespace::getCanonicalNamespaces( true ); # reset namespace cache
36 $wgContLang->resetNamespaces(); # reset namespace cache
37 }
38
39 public function dataGetDefaultModelFor() {
40 return array(
41 array( 'Foo', CONTENT_MODEL_WIKITEXT ),
42 array( 'Foo.js', CONTENT_MODEL_WIKITEXT ),
43 array( 'Foo/bar.js', CONTENT_MODEL_WIKITEXT ),
44 array( 'User:Foo', CONTENT_MODEL_WIKITEXT ),
45 array( 'User:Foo.js', CONTENT_MODEL_WIKITEXT ),
46 array( 'User:Foo/bar.js', CONTENT_MODEL_JAVASCRIPT ),
47 array( 'User:Foo/bar.css', CONTENT_MODEL_CSS ),
48 array( 'User talk:Foo/bar.css', CONTENT_MODEL_WIKITEXT ),
49 array( 'User:Foo/bar.js.xxx', CONTENT_MODEL_WIKITEXT ),
50 array( 'User:Foo/bar.xxx', CONTENT_MODEL_WIKITEXT ),
51 array( 'MediaWiki:Foo.js', CONTENT_MODEL_JAVASCRIPT ),
52 array( 'MediaWiki:Foo.css', CONTENT_MODEL_CSS ),
53 array( 'MediaWiki:Foo.JS', CONTENT_MODEL_WIKITEXT ),
54 array( 'MediaWiki:Foo.CSS', CONTENT_MODEL_WIKITEXT ),
55 array( 'MediaWiki:Foo.css.xxx', CONTENT_MODEL_WIKITEXT ),
56 );
57 }
58
59 /**
60 * @dataProvider dataGetDefaultModelFor
61 */
62 public function testGetDefaultModelFor( $title, $expectedModelId ) {
63 $title = Title::newFromText( $title );
64 $this->assertEquals( $expectedModelId, ContentHandler::getDefaultModelFor( $title ) );
65 }
66 /**
67 * @dataProvider dataGetDefaultModelFor
68 */
69 public function testGetForTitle( $title, $expectedContentModel ) {
70 $title = Title::newFromText( $title );
71 $handler = ContentHandler::getForTitle( $title );
72 $this->assertEquals( $expectedContentModel, $handler->getModelID() );
73 }
74
75 public function dataGetContentFormatMimeType( ) {
76 return array(
77 array( 0, null ),
78 array( null, null ),
79 array( 99887766, null ),
80
81 array( CONTENT_FORMAT_WIKITEXT, 'text/x-wiki' ),
82 array( CONTENT_FORMAT_JAVASCRIPT, 'text/javascript' ),
83 array( CONTENT_FORMAT_CSS, 'text/css' ),
84 array( CONTENT_FORMAT_JSON, 'application/json' ),
85 array( CONTENT_FORMAT_XML, 'application/xml' ),
86 array( CONTENT_FORMAT_SERIALIZED, 'application/vnd.php.serialized' ),
87 );
88 }
89
90 /**
91 * @dataProvider dataGetContentFormatMimeType
92 */
93 public function testGetContentFormatMimeType( $id, $expectedMime ) {
94 $mime = ContentHandler::getContentFormatMimeType( $id );
95
96 $this->assertEquals( $expectedMime, $mime );
97 }
98
99 public function dataGetContentFormatID( ) {
100 return array(
101 array( '', null ),
102 array( 'foo', null ),
103 array( null, null ),
104
105 array( 'text/x-wiki', CONTENT_FORMAT_WIKITEXT ),
106 array( 'text/javascript', CONTENT_FORMAT_JAVASCRIPT ),
107 array( 'text/css', CONTENT_FORMAT_CSS ),
108 array( 'application/json', CONTENT_FORMAT_JSON ),
109 array( 'application/xml', CONTENT_FORMAT_XML ),
110 array( 'application/vnd.php.serialized', CONTENT_FORMAT_SERIALIZED ),
111 );
112 }
113
114 /**
115 * @dataProvider dataGetContentFormatID
116 */
117 public function testGetContentFormatID( $mime, $expectedId ) {
118 $id = ContentHandler::getContentFormatID( $mime );
119
120 $this->assertEquals( $expectedId, $id );
121 }
122
123 public function dataGetContentModelName() {
124 return array(
125 array( 0, null ),
126 array( null, null ),
127 array( 99887766, null ),
128
129 array( CONTENT_MODEL_JAVASCRIPT, '/javascript/i' ), //XXX: depends on content language
130 );
131 }
132
133 /**
134 * @dataProvider dataGetContentModelName
135 */
136 public function testGetContentModelName( $id, $expected ) {
137 $name = ContentHandler::getContentModelName( $id );
138
139 if ( $expected === null ) {
140 $this->assertNull( $name, "content model name for #$id was expected to be null" );
141 } else {
142 $this->assertNotNull( $name, "no name found for content model #$id" );
143 $this->assertTrue( preg_match( $expected, $name ) > 0 , "content model name for #$id did not match pattern $expected" );
144 }
145 }
146
147 public function testGetContentText_Null( ) {
148 global $wgContentHandlerTextFallback;
149
150 $content = null;
151
152 $wgContentHandlerTextFallback = 'fail';
153 $text = ContentHandler::getContentText( $content );
154 $this->assertEquals( '', $text );
155
156 $wgContentHandlerTextFallback = 'serialize';
157 $text = ContentHandler::getContentText( $content );
158 $this->assertEquals( '', $text );
159
160 $wgContentHandlerTextFallback = 'ignore';
161 $text = ContentHandler::getContentText( $content );
162 $this->assertEquals( '', $text );
163 }
164
165 public function testGetContentText_TextContent( ) {
166 global $wgContentHandlerTextFallback;
167
168 $content = new WikitextContent( "hello world" );
169
170 $wgContentHandlerTextFallback = 'fail';
171 $text = ContentHandler::getContentText( $content );
172 $this->assertEquals( $content->getNativeData(), $text );
173
174 $wgContentHandlerTextFallback = 'serialize';
175 $text = ContentHandler::getContentText( $content );
176 $this->assertEquals( $content->serialize(), $text );
177
178 $wgContentHandlerTextFallback = 'ignore';
179 $text = ContentHandler::getContentText( $content );
180 $this->assertEquals( $content->getNativeData(), $text );
181 }
182
183 public function testGetContentText_NonTextContent( ) {
184 global $wgContentHandlerTextFallback;
185
186 $content = new DummyContentForTesting( "hello world" );
187
188 $wgContentHandlerTextFallback = 'fail';
189
190 try {
191 $text = ContentHandler::getContentText( $content );
192
193 $this->fail( "ContentHandler::getContentText should have thrown an exception for non-text Content object" );
194 } catch (MWException $ex) {
195 // as expected
196 }
197
198 $wgContentHandlerTextFallback = 'serialize';
199 $text = ContentHandler::getContentText( $content );
200 $this->assertEquals( $content->serialize(), $text );
201
202 $wgContentHandlerTextFallback = 'ignore';
203 $text = ContentHandler::getContentText( $content );
204 $this->assertNull( $text );
205 }
206
207 #public static function makeContent( $text, Title $title, $modelId = null, $format = null )
208
209 public function dataMakeContent() {
210 return array(
211 array( 'hallo', 'Test', null, null, CONTENT_MODEL_WIKITEXT, 'hallo', false ),
212 array( 'hallo', 'MediaWiki:Test.js', null, null, CONTENT_MODEL_JAVASCRIPT, 'hallo', false ),
213 array( serialize('hallo'), 'Dummy:Test', null, null, 999999, 'hallo', false ),
214
215 array( 'hallo', 'Test', null, CONTENT_FORMAT_WIKITEXT, CONTENT_MODEL_WIKITEXT, 'hallo', false ),
216 array( 'hallo', 'MediaWiki:Test.js', null, CONTENT_FORMAT_JAVASCRIPT, CONTENT_MODEL_JAVASCRIPT, 'hallo', false ),
217 array( serialize('hallo'), 'Dummy:Test', null, 999999, 999999, 'hallo', false ),
218
219 array( 'hallo', 'Test', CONTENT_MODEL_CSS, null, CONTENT_MODEL_CSS, 'hallo', false ),
220 array( 'hallo', 'MediaWiki:Test.js', CONTENT_MODEL_CSS, null, CONTENT_MODEL_CSS, 'hallo', false ),
221 array( serialize('hallo'), 'Dummy:Test', CONTENT_MODEL_CSS, null, CONTENT_MODEL_CSS, serialize('hallo'), false ),
222
223 array( 'hallo', 'Test', CONTENT_MODEL_WIKITEXT, 999999, null, null, true ),
224 array( 'hallo', 'MediaWiki:Test.js', CONTENT_MODEL_CSS, 999999, null, null, true ),
225 array( 'hallo', 'Dummy:Test', CONTENT_MODEL_JAVASCRIPT, 999999, null, null, true ),
226 );
227 }
228
229 /**
230 * @dataProvider dataMakeContent
231 */
232 public function testMakeContent( $data, $title, $modelId, $format, $expectedModelId, $expectedNativeData, $shouldFail ) {
233 global $wgExtraNamespaces, $wgNamespaceContentModels, $wgContentHandlers;
234
235 $title = Title::newFromText( $title );
236
237 try {
238 $content = ContentHandler::makeContent( $data, $title, $modelId, $format );
239
240 if ( $shouldFail ) $this->fail( "ContentHandler::makeContent should have failed!" );
241
242 $this->assertEquals( $expectedModelId, $content->getModel(), 'bad model id' );
243 $this->assertEquals( $expectedNativeData, $content->getNativeData(), 'bads native data' );
244 } catch ( MWException $ex ) {
245 if ( !$shouldFail ) $this->fail( "ContentHandler::makeContent failed unexpectedly: " . $ex->getMessage() );
246 else $this->assertTrue( true ); // dummy, so we don't get the "test did not perform any assertions" message.
247 }
248
249 }
250
251 public function dataGetParserOutput() {
252 return array(
253 array("ContentHandlerTest_testGetParserOutput", "hello ''world''\n", "<p>hello <i>world</i>\n</p>"),
254 // @todo: more...?
255 );
256 }
257
258 /**
259 * @dataProvider dataGetParserOutput
260 */
261 public function testGetParserOutput( $title, $text, $expectedHtml ) {
262 $title = Title::newFromText( $title );
263 $handler = ContentHandler::getForModelID( $title->getContentModel() );
264 $content = ContentHandler::makeContent( $text, $title );
265
266 $po = $handler->getParserOutput( $content, $title );
267
268 $this->assertEquals( $expectedHtml, $po->getText() );
269 // @todo: assert more properties
270 }
271
272 public function dataGetSecondaryDataUpdates() {
273 return array(
274 array("ContentHandlerTest_testGetSecondaryDataUpdates_1", "hello ''world''\n",
275 array( 'LinksUpdate' => array( 'mRecursive' => true,
276 'mLinks' => array() ) )
277 ),
278 array("ContentHandlerTest_testGetSecondaryDataUpdates_2", "hello [[world test 21344]]\n",
279 array( 'LinksUpdate' => array( 'mRecursive' => true,
280 'mLinks' => array( array( 'World_test_21344' => 0 ) ) ) )
281 ),
282 // @todo: more...?
283 );
284 }
285
286 /**
287 * @dataProvider dataGetSecondaryDataUpdates
288 */
289 public function testGetSecondaryDataUpdates( $title, $text, $expectedStuff ) {
290 $title = Title::newFromText( $title );
291 $title->resetArticleID( 2342 ); //dummy id. fine as long as we don't try to execute the updates!
292
293 $handler = ContentHandler::getForModelID( $title->getContentModel() );
294 $content = ContentHandler::makeContent( $text, $title );
295
296 $updates = $handler->getSecondaryDataUpdates( $content, $title );
297
298 // make updates accessible by class name
299 foreach ( $updates as $update ) {
300 $class = get_class( $update );
301 $updates[ $class ] = $update;
302 }
303
304 foreach ( $expectedStuff as $class => $fieldValues ) {
305 $this->assertArrayHasKey( $class, $updates, "missing an update of type $class" );
306
307 $update = $updates[ $class ];
308
309 foreach ( $fieldValues as $field => $value ) {
310 $v = $update->$field; #if the field doesn't exist, just crash and burn
311 $this->assertEquals( $value, $v, "unexpected value for field $field in instance of $class" );
312 }
313 }
314 }
315
316 public function dataGetDeletionUpdates() {
317 return array(
318 array("ContentHandlerTest_testGetSecondaryDataUpdates_1", "hello ''world''\n",
319 array( 'LinksDeletionUpdate' => array( ) )
320 ),
321 array("ContentHandlerTest_testGetSecondaryDataUpdates_2", "hello [[world test 21344]]\n",
322 array( 'LinksDeletionUpdate' => array( ) )
323 ),
324 // @todo: more...?
325 );
326 }
327
328 /**
329 * @dataProvider dataGetDeletionUpdates
330 */
331 public function testDeletionUpdates( $title, $text, $expectedStuff ) {
332 $title = Title::newFromText( $title );
333 $title->resetArticleID( 2342 ); //dummy id. fine as long as we don't try to execute the updates!
334
335 $handler = ContentHandler::getForModelID( $title->getContentModel() );
336 $content = ContentHandler::makeContent( $text, $title );
337
338 $updates = $handler->getDeletionUpdates( $content, $title );
339
340 // make updates accessible by class name
341 foreach ( $updates as $update ) {
342 $class = get_class( $update );
343 $updates[ $class ] = $update;
344 }
345
346 foreach ( $expectedStuff as $class => $fieldValues ) {
347 $this->assertArrayHasKey( $class, $updates, "missing an update of type $class" );
348
349 $update = $updates[ $class ];
350
351 foreach ( $fieldValues as $field => $value ) {
352 $v = $update->$field; #if the field doesn't exist, just crash and burn
353 $this->assertEquals( $value, $v, "unexpected value for field $field in instance of $class" );
354 }
355 }
356 }
357 }
358
359 class DummyContentHandlerForTesting extends ContentHandler {
360
361 public function __construct( $dataModel ) {
362 parent::__construct( $dataModel, array( 999999 ) );
363 }
364
365 /**
366 * Serializes Content object of the type supported by this ContentHandler.
367 *
368 * @param Content $content the Content object to serialize
369 * @param null $format the desired serialization format
370 * @return String serialized form of the content
371 */
372 public function serializeContent( Content $content, $format = null )
373 {
374 return $content->serialize();
375 }
376
377 /**
378 * Unserializes a Content object of the type supported by this ContentHandler.
379 *
380 * @param $blob String serialized form of the content
381 * @param null $format the format used for serialization
382 * @return Content the Content object created by deserializing $blob
383 */
384 public function unserializeContent( $blob, $format = null )
385 {
386 $d = unserialize( $blob );
387 return new DummyContentForTesting( $d );
388 }
389
390 /**
391 * Creates an empty Content object of the type supported by this ContentHandler.
392 *
393 */
394 public function makeEmptyContent()
395 {
396 return new DummyContentForTesting( '' );
397 }
398
399 /**
400 * @param Content $content
401 * @param Title $title
402 * @param null $revId
403 * @param null|ParserOptions $options
404 * @param Boolean $generateHtml whether to generate Html (default: true). If false,
405 * the result of calling getText() on the ParserOutput object returned by
406 * this method is undefined.
407 *
408 * @return ParserOutput
409 */
410 public function getParserOutput( Content $content, Title $title, $revId = null, ParserOptions $options = NULL, $generateHtml = true )
411 {
412 return new ParserOutput( $content->getNativeData() );
413 }
414 }
415
416 class DummyContentForTesting extends AbstractContent {
417
418 public function __construct( $data ) {
419 parent::__construct( 999999 );
420
421 $this->data = $data;
422 }
423
424 public function serialize( $format = null ) {
425 return serialize( $this->data );
426 }
427
428 /**
429 * @return String a string representing the content in a way useful for building a full text search index.
430 * If no useful representation exists, this method returns an empty string.
431 */
432 public function getTextForSearchIndex()
433 {
434 return '';
435 }
436
437 /**
438 * @return String the wikitext to include when another page includes this content, or false if the content is not
439 * includable in a wikitext page.
440 */
441 public function getWikitextForTransclusion()
442 {
443 return false;
444 }
445
446 /**
447 * Returns a textual representation of the content suitable for use in edit summaries and log messages.
448 *
449 * @param int $maxlength maximum length of the summary text
450 * @return String the summary text
451 */
452 public function getTextForSummary( $maxlength = 250 )
453 {
454 return '';
455 }
456
457 /**
458 * Returns native represenation of the data. Interpretation depends on the data model used,
459 * as given by getDataModel().
460 *
461 * @return mixed the native representation of the content. Could be a string, a nested array
462 * structure, an object, a binary blob... anything, really.
463 */
464 public function getNativeData()
465 {
466 return $this->data;
467 }
468
469 /**
470 * returns the content's nominal size in bogo-bytes.
471 *
472 * @return int
473 */
474 public function getSize()
475 {
476 return strlen( $this->data );
477 }
478
479 /**
480 * Return a copy of this Content object. The following must be true for the object returned
481 * if $copy = $original->copy()
482 *
483 * * get_class($original) === get_class($copy)
484 * * $original->getModel() === $copy->getModel()
485 * * $original->equals( $copy )
486 *
487 * If and only if the Content object is imutable, the copy() method can and should
488 * return $this. That is, $copy === $original may be true, but only for imutable content
489 * objects.
490 *
491 * @return Content. A copy of this object
492 */
493 public function copy()
494 {
495 return $this;
496 }
497
498 /**
499 * Returns true if this content is countable as a "real" wiki page, provided
500 * that it's also in a countable location (e.g. a current revision in the main namespace).
501 *
502 * @param $hasLinks Bool: if it is known whether this content contains links, provide this information here,
503 * to avoid redundant parsing to find out.
504 * @return boolean
505 */
506 public function isCountable( $hasLinks = null )
507 {
508 return false;
509 }
510 }
511