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