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