6c06049f3ed10952ef84d1b8d7c9da0232090e6b
[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 dataGetLocalizedNameName() {
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 dataGetLocalizedNameName
135 */
136 public function testGetLocalizedName( $id, $expected ) {
137 try{
138 $name = ContentHandler::getLocalizedName( $id );
139
140 if ( !$expected ) $this->fail("should not have a name for content id #$id");
141
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 } catch (MWException $e) {
145 if ( $expected ) $this->fail("failed to get name for content id #$id");
146 }
147 }
148
149 public function dataGetContentModelName() {
150 return array(
151 array( 0, null ),
152 array( null, null ),
153 array( 99887766, null ),
154
155 array( CONTENT_MODEL_JAVASCRIPT, 'javascript' ),
156 );
157 }
158
159 /**
160 * @dataProvider dataGetContentModelName
161 */
162 public function testGetContentModelName( $id, $expected ) {
163 try {
164 $name = ContentHandler::getContentModelName( $id );
165
166 if ( !$expected ) $this->fail("should not have a name for content id #$id");
167
168 $this->assertNotNull( $name, "no name found for content model #$id" );
169 $this->assertEquals( $expected, $name);
170 } catch (MWException $e) {
171 if ( $expected ) $this->fail("failed to get name for content id #$id");
172 }
173 }
174
175 /**
176 * @dataProvider dataGetContentModelName
177 */
178 public function testGetModelName( $id, $expected ) {
179 try {
180 $handler = ContentHandler::getForModelID( $id );
181 $name = $handler->getModelName();
182
183 if ( !$expected ) $this->fail("should not have a name for content id #$id");
184
185 $this->assertNotNull( $name, "no name found for content model #$id" );
186 $this->assertEquals( $expected, $name);
187 } catch (MWException $e) {
188 if ( $expected ) $this->fail("failed to get name for content id #$id");
189 }
190 }
191
192 public function testGetContentText_Null( ) {
193 global $wgContentHandlerTextFallback;
194
195 $content = null;
196
197 $wgContentHandlerTextFallback = 'fail';
198 $text = ContentHandler::getContentText( $content );
199 $this->assertEquals( '', $text );
200
201 $wgContentHandlerTextFallback = 'serialize';
202 $text = ContentHandler::getContentText( $content );
203 $this->assertEquals( '', $text );
204
205 $wgContentHandlerTextFallback = 'ignore';
206 $text = ContentHandler::getContentText( $content );
207 $this->assertEquals( '', $text );
208 }
209
210 public function testGetContentText_TextContent( ) {
211 global $wgContentHandlerTextFallback;
212
213 $content = new WikitextContent( "hello world" );
214
215 $wgContentHandlerTextFallback = 'fail';
216 $text = ContentHandler::getContentText( $content );
217 $this->assertEquals( $content->getNativeData(), $text );
218
219 $wgContentHandlerTextFallback = 'serialize';
220 $text = ContentHandler::getContentText( $content );
221 $this->assertEquals( $content->serialize(), $text );
222
223 $wgContentHandlerTextFallback = 'ignore';
224 $text = ContentHandler::getContentText( $content );
225 $this->assertEquals( $content->getNativeData(), $text );
226 }
227
228 public function testGetContentText_NonTextContent( ) {
229 global $wgContentHandlerTextFallback;
230
231 $content = new DummyContentForTesting( "hello world" );
232
233 $wgContentHandlerTextFallback = 'fail';
234
235 try {
236 $text = ContentHandler::getContentText( $content );
237
238 $this->fail( "ContentHandler::getContentText should have thrown an exception for non-text Content object" );
239 } catch (MWException $ex) {
240 // as expected
241 }
242
243 $wgContentHandlerTextFallback = 'serialize';
244 $text = ContentHandler::getContentText( $content );
245 $this->assertEquals( $content->serialize(), $text );
246
247 $wgContentHandlerTextFallback = 'ignore';
248 $text = ContentHandler::getContentText( $content );
249 $this->assertNull( $text );
250 }
251
252 #public static function makeContent( $text, Title $title, $modelId = null, $format = null )
253
254 public function dataMakeContent() {
255 return array(
256 array( 'hallo', 'Test', null, null, CONTENT_MODEL_WIKITEXT, 'hallo', false ),
257 array( 'hallo', 'MediaWiki:Test.js', null, null, CONTENT_MODEL_JAVASCRIPT, 'hallo', false ),
258 array( serialize('hallo'), 'Dummy:Test', null, null, 999999, 'hallo', false ),
259
260 array( 'hallo', 'Test', null, CONTENT_FORMAT_WIKITEXT, CONTENT_MODEL_WIKITEXT, 'hallo', false ),
261 array( 'hallo', 'MediaWiki:Test.js', null, CONTENT_FORMAT_JAVASCRIPT, CONTENT_MODEL_JAVASCRIPT, 'hallo', false ),
262 array( serialize('hallo'), 'Dummy:Test', null, 999999, 999999, 'hallo', false ),
263
264 array( 'hallo', 'Test', CONTENT_MODEL_CSS, null, CONTENT_MODEL_CSS, 'hallo', false ),
265 array( 'hallo', 'MediaWiki:Test.js', CONTENT_MODEL_CSS, null, CONTENT_MODEL_CSS, 'hallo', false ),
266 array( serialize('hallo'), 'Dummy:Test', CONTENT_MODEL_CSS, null, CONTENT_MODEL_CSS, serialize('hallo'), false ),
267
268 array( 'hallo', 'Test', CONTENT_MODEL_WIKITEXT, 999999, null, null, true ),
269 array( 'hallo', 'MediaWiki:Test.js', CONTENT_MODEL_CSS, 999999, null, null, true ),
270 array( 'hallo', 'Dummy:Test', CONTENT_MODEL_JAVASCRIPT, 999999, null, null, true ),
271 );
272 }
273
274 /**
275 * @dataProvider dataMakeContent
276 */
277 public function testMakeContent( $data, $title, $modelId, $format, $expectedModelId, $expectedNativeData, $shouldFail ) {
278 global $wgExtraNamespaces, $wgNamespaceContentModels, $wgContentHandlers;
279
280 $title = Title::newFromText( $title );
281
282 try {
283 $content = ContentHandler::makeContent( $data, $title, $modelId, $format );
284
285 if ( $shouldFail ) $this->fail( "ContentHandler::makeContent should have failed!" );
286
287 $this->assertEquals( $expectedModelId, $content->getModel(), 'bad model id' );
288 $this->assertEquals( $expectedNativeData, $content->getNativeData(), 'bads native data' );
289 } catch ( MWException $ex ) {
290 if ( !$shouldFail ) $this->fail( "ContentHandler::makeContent failed unexpectedly: " . $ex->getMessage() );
291 else $this->assertTrue( true ); // dummy, so we don't get the "test did not perform any assertions" message.
292 }
293
294 }
295
296 public function dataGetParserOutput() {
297 return array(
298 array("ContentHandlerTest_testGetParserOutput", "hello ''world''\n", "<p>hello <i>world</i>\n</p>"),
299 // @todo: more...?
300 );
301 }
302
303 /**
304 * @dataProvider dataGetParserOutput
305 */
306 public function testGetParserOutput( $title, $text, $expectedHtml ) {
307 $title = Title::newFromText( $title );
308 $handler = ContentHandler::getForModelID( $title->getContentModel() );
309 $content = ContentHandler::makeContent( $text, $title );
310
311 $po = $handler->getParserOutput( $content, $title );
312
313 $this->assertEquals( $expectedHtml, $po->getText() );
314 // @todo: assert more properties
315 }
316
317 public function dataGetSecondaryDataUpdates() {
318 return array(
319 array("ContentHandlerTest_testGetSecondaryDataUpdates_1", "hello ''world''\n",
320 array( 'LinksUpdate' => array( 'mRecursive' => true,
321 'mLinks' => array() ) )
322 ),
323 array("ContentHandlerTest_testGetSecondaryDataUpdates_2", "hello [[world test 21344]]\n",
324 array( 'LinksUpdate' => array( 'mRecursive' => true,
325 'mLinks' => array( array( 'World_test_21344' => 0 ) ) ) )
326 ),
327 // @todo: more...?
328 );
329 }
330
331 /**
332 * @dataProvider dataGetSecondaryDataUpdates
333 */
334 public function testGetSecondaryDataUpdates( $title, $text, $expectedStuff ) {
335 $title = Title::newFromText( $title );
336 $title->resetArticleID( 2342 ); //dummy id. fine as long as we don't try to execute the updates!
337
338 $handler = ContentHandler::getForModelID( $title->getContentModel() );
339 $content = ContentHandler::makeContent( $text, $title );
340
341 $updates = $handler->getSecondaryDataUpdates( $content, $title );
342
343 // make updates accessible by class name
344 foreach ( $updates as $update ) {
345 $class = get_class( $update );
346 $updates[ $class ] = $update;
347 }
348
349 foreach ( $expectedStuff as $class => $fieldValues ) {
350 $this->assertArrayHasKey( $class, $updates, "missing an update of type $class" );
351
352 $update = $updates[ $class ];
353
354 foreach ( $fieldValues as $field => $value ) {
355 $v = $update->$field; #if the field doesn't exist, just crash and burn
356 $this->assertEquals( $value, $v, "unexpected value for field $field in instance of $class" );
357 }
358 }
359 }
360
361 public function dataGetDeletionUpdates() {
362 return array(
363 array("ContentHandlerTest_testGetSecondaryDataUpdates_1", "hello ''world''\n",
364 array( 'LinksDeletionUpdate' => array( ) )
365 ),
366 array("ContentHandlerTest_testGetSecondaryDataUpdates_2", "hello [[world test 21344]]\n",
367 array( 'LinksDeletionUpdate' => array( ) )
368 ),
369 // @todo: more...?
370 );
371 }
372
373 /**
374 * @dataProvider dataGetDeletionUpdates
375 */
376 public function testDeletionUpdates( $title, $text, $expectedStuff ) {
377 $title = Title::newFromText( $title );
378 $title->resetArticleID( 2342 ); //dummy id. fine as long as we don't try to execute the updates!
379
380 $handler = ContentHandler::getForModelID( $title->getContentModel() );
381 $content = ContentHandler::makeContent( $text, $title );
382
383 $updates = $handler->getDeletionUpdates( $content, $title );
384
385 // make updates accessible by class name
386 foreach ( $updates as $update ) {
387 $class = get_class( $update );
388 $updates[ $class ] = $update;
389 }
390
391 foreach ( $expectedStuff as $class => $fieldValues ) {
392 $this->assertArrayHasKey( $class, $updates, "missing an update of type $class" );
393
394 $update = $updates[ $class ];
395
396 foreach ( $fieldValues as $field => $value ) {
397 $v = $update->$field; #if the field doesn't exist, just crash and burn
398 $this->assertEquals( $value, $v, "unexpected value for field $field in instance of $class" );
399 }
400 }
401 }
402
403 public function testSupportsSections() {
404 $this->markTestIncomplete( "not yet implemented" );
405 }
406 }
407
408 class DummyContentHandlerForTesting extends ContentHandler {
409
410 public function __construct( $dataModel ) {
411 parent::__construct( $dataModel, array( 999999 ) );
412 }
413
414 /**
415 * Serializes Content object of the type supported by this ContentHandler.
416 *
417 * @param Content $content the Content object to serialize
418 * @param null $format the desired serialization format
419 * @return String serialized form of the content
420 */
421 public function serializeContent( Content $content, $format = null )
422 {
423 return $content->serialize();
424 }
425
426 /**
427 * Unserializes a Content object of the type supported by this ContentHandler.
428 *
429 * @param $blob String serialized form of the content
430 * @param null $format the format used for serialization
431 * @return Content the Content object created by deserializing $blob
432 */
433 public function unserializeContent( $blob, $format = null )
434 {
435 $d = unserialize( $blob );
436 return new DummyContentForTesting( $d );
437 }
438
439 /**
440 * Creates an empty Content object of the type supported by this ContentHandler.
441 *
442 */
443 public function makeEmptyContent()
444 {
445 return new DummyContentForTesting( '' );
446 }
447
448 /**
449 * @param Content $content
450 * @param Title $title
451 * @param null $revId
452 * @param null|ParserOptions $options
453 * @param Boolean $generateHtml whether to generate Html (default: true). If false,
454 * the result of calling getText() on the ParserOutput object returned by
455 * this method is undefined.
456 *
457 * @return ParserOutput
458 */
459 public function getParserOutput( Content $content, Title $title, $revId = null, ParserOptions $options = NULL, $generateHtml = true )
460 {
461 return new ParserOutput( $content->getNativeData() );
462 }
463 }
464
465 class DummyContentForTesting extends AbstractContent {
466
467 public function __construct( $data ) {
468 parent::__construct( 999999 );
469
470 $this->data = $data;
471 }
472
473 public function serialize( $format = null ) {
474 return serialize( $this->data );
475 }
476
477 /**
478 * @return String a string representing the content in a way useful for building a full text search index.
479 * If no useful representation exists, this method returns an empty string.
480 */
481 public function getTextForSearchIndex()
482 {
483 return '';
484 }
485
486 /**
487 * @return String the wikitext to include when another page includes this content, or false if the content is not
488 * includable in a wikitext page.
489 */
490 public function getWikitextForTransclusion()
491 {
492 return false;
493 }
494
495 /**
496 * Returns a textual representation of the content suitable for use in edit summaries and log messages.
497 *
498 * @param int $maxlength maximum length of the summary text
499 * @return String the summary text
500 */
501 public function getTextForSummary( $maxlength = 250 )
502 {
503 return '';
504 }
505
506 /**
507 * Returns native represenation of the data. Interpretation depends on the data model used,
508 * as given by getDataModel().
509 *
510 * @return mixed the native representation of the content. Could be a string, a nested array
511 * structure, an object, a binary blob... anything, really.
512 */
513 public function getNativeData()
514 {
515 return $this->data;
516 }
517
518 /**
519 * returns the content's nominal size in bogo-bytes.
520 *
521 * @return int
522 */
523 public function getSize()
524 {
525 return strlen( $this->data );
526 }
527
528 /**
529 * Return a copy of this Content object. The following must be true for the object returned
530 * if $copy = $original->copy()
531 *
532 * * get_class($original) === get_class($copy)
533 * * $original->getModel() === $copy->getModel()
534 * * $original->equals( $copy )
535 *
536 * If and only if the Content object is imutable, the copy() method can and should
537 * return $this. That is, $copy === $original may be true, but only for imutable content
538 * objects.
539 *
540 * @return Content. A copy of this object
541 */
542 public function copy()
543 {
544 return $this;
545 }
546
547 /**
548 * Returns true if this content is countable as a "real" wiki page, provided
549 * that it's also in a countable location (e.g. a current revision in the main namespace).
550 *
551 * @param $hasLinks Bool: if it is known whether this content contains links, provide this information here,
552 * to avoid redundant parsing to find out.
553 * @return boolean
554 */
555 public function isCountable( $hasLinks = null )
556 {
557 return false;
558 }
559 }
560