4 * @group ContentHandler
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.
11 class ContentHandlerTest
extends MediaWikiTestCase
{
13 public function setUp() {
14 global $wgExtraNamespaces, $wgNamespaceContentModels, $wgContentHandlers, $wgContLang;
16 $wgExtraNamespaces[ 12312 ] = 'Dummy';
17 $wgExtraNamespaces[ 12313 ] = 'Dummy_talk';
19 $wgNamespaceContentModels[ 12312 ] = "testing";
20 $wgContentHandlers[ "testing" ] = 'DummyContentHandlerForTesting';
22 MWNamespace
::getCanonicalNamespaces( true ); # reset namespace cache
23 $wgContLang->resetNamespaces(); # reset namespace cache
26 public function tearDown() {
27 global $wgExtraNamespaces, $wgNamespaceContentModels, $wgContentHandlers, $wgContLang;
29 unset( $wgExtraNamespaces[ 12312 ] );
30 unset( $wgExtraNamespaces[ 12313 ] );
32 unset( $wgNamespaceContentModels[ 12312 ] );
33 unset( $wgContentHandlers[ "testing" ] );
35 MWNamespace
::getCanonicalNamespaces( true ); # reset namespace cache
36 $wgContLang->resetNamespaces(); # reset namespace cache
39 public function dataGetDefaultModelFor() {
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
),
60 * @dataProvider dataGetDefaultModelFor
62 public function testGetDefaultModelFor( $title, $expectedModelId ) {
63 $title = Title
::newFromText( $title );
64 $this->assertEquals( $expectedModelId, ContentHandler
::getDefaultModelFor( $title ) );
67 * @dataProvider dataGetDefaultModelFor
69 public function testGetForTitle( $title, $expectedContentModel ) {
70 $title = Title
::newFromText( $title );
71 $handler = ContentHandler
::getForTitle( $title );
72 $this->assertEquals( $expectedContentModel, $handler->getModelID() );
75 public function dataGetLocalizedName() {
78 array( "xyzzy", null ),
80 array( CONTENT_MODEL_JAVASCRIPT
, '/javascript/i' ), //XXX: depends on content language
85 * @dataProvider dataGetLocalizedName
87 public function testGetLocalizedName( $id, $expected ) {
88 $name = ContentHandler
::getLocalizedName( $id );
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" );
94 $this->assertEquals( $id, $name, "localization of unknown model $id should have fallen back to use the model id directly." );
98 public function dataGetModelName() {
101 array( "xyzzy", null ),
103 array( CONTENT_MODEL_JAVASCRIPT
, 'javascript' ),
108 * @dataProvider dataGetModelName
110 public function testGetModelName( $id, $expected ) {
112 $handler = ContentHandler
::getForModelID( $id );
113 $name = $handler->getModelID();
115 if ( !$expected ) $this->fail("should not have a name for content id #$id");
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");
124 public function testGetContentText_Null( ) {
125 global $wgContentHandlerTextFallback;
129 $wgContentHandlerTextFallback = 'fail';
130 $text = ContentHandler
::getContentText( $content );
131 $this->assertEquals( '', $text );
133 $wgContentHandlerTextFallback = 'serialize';
134 $text = ContentHandler
::getContentText( $content );
135 $this->assertEquals( '', $text );
137 $wgContentHandlerTextFallback = 'ignore';
138 $text = ContentHandler
::getContentText( $content );
139 $this->assertEquals( '', $text );
142 public function testGetContentText_TextContent( ) {
143 global $wgContentHandlerTextFallback;
145 $content = new WikitextContent( "hello world" );
147 $wgContentHandlerTextFallback = 'fail';
148 $text = ContentHandler
::getContentText( $content );
149 $this->assertEquals( $content->getNativeData(), $text );
151 $wgContentHandlerTextFallback = 'serialize';
152 $text = ContentHandler
::getContentText( $content );
153 $this->assertEquals( $content->serialize(), $text );
155 $wgContentHandlerTextFallback = 'ignore';
156 $text = ContentHandler
::getContentText( $content );
157 $this->assertEquals( $content->getNativeData(), $text );
160 public function testGetContentText_NonTextContent( ) {
161 global $wgContentHandlerTextFallback;
163 $content = new DummyContentForTesting( "hello world" );
165 $wgContentHandlerTextFallback = 'fail';
168 $text = ContentHandler
::getContentText( $content );
170 $this->fail( "ContentHandler::getContentText should have thrown an exception for non-text Content object" );
171 } catch (MWException
$ex) {
175 $wgContentHandlerTextFallback = 'serialize';
176 $text = ContentHandler
::getContentText( $content );
177 $this->assertEquals( $content->serialize(), $text );
179 $wgContentHandlerTextFallback = 'ignore';
180 $text = ContentHandler
::getContentText( $content );
181 $this->assertNull( $text );
184 #public static function makeContent( $text, Title $title, $modelId = null, $format = null )
186 public function dataMakeContent() {
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 ),
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 ),
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 ),
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 ),
207 * @dataProvider dataMakeContent
209 public function testMakeContent( $data, $title, $modelId, $format, $expectedModelId, $expectedNativeData, $shouldFail ) {
210 global $wgExtraNamespaces, $wgNamespaceContentModels, $wgContentHandlers;
212 $title = Title
::newFromText( $title );
215 $content = ContentHandler
::makeContent( $data, $title, $modelId, $format );
217 if ( $shouldFail ) $this->fail( "ContentHandler::makeContent should have failed!" );
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.
228 public function dataGetParserOutput() {
230 array("ContentHandlerTest_testGetParserOutput", "hello ''world''\n", "<p>hello <i>world</i>\n</p>"),
236 * @dataProvider dataGetParserOutput
238 public function testGetParserOutput( $title, $text, $expectedHtml ) {
239 $title = Title
::newFromText( $title );
240 $handler = ContentHandler
::getForModelID( $title->getContentModel() );
241 $content = ContentHandler
::makeContent( $text, $title );
243 $po = $handler->getParserOutput( $content, $title );
245 $this->assertEquals( $expectedHtml, $po->getText() );
246 // @todo: assert more properties
249 public function dataGetSecondaryDataUpdates() {
251 array("ContentHandlerTest_testGetSecondaryDataUpdates_1", "hello ''world''\n",
252 array( 'LinksUpdate' => array( 'mRecursive' => true,
253 'mLinks' => array() ) )
255 array("ContentHandlerTest_testGetSecondaryDataUpdates_2", "hello [[world test 21344]]\n",
256 array( 'LinksUpdate' => array( 'mRecursive' => true,
257 'mLinks' => array( array( 'World_test_21344' => 0 ) ) ) )
264 * @dataProvider dataGetSecondaryDataUpdates
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!
270 $handler = ContentHandler
::getForModelID( $title->getContentModel() );
271 $content = ContentHandler
::makeContent( $text, $title );
273 $updates = $handler->getSecondaryDataUpdates( $content, $title );
275 // make updates accessible by class name
276 foreach ( $updates as $update ) {
277 $class = get_class( $update );
278 $updates[ $class ] = $update;
281 foreach ( $expectedStuff as $class => $fieldValues ) {
282 $this->assertArrayHasKey( $class, $updates, "missing an update of type $class" );
284 $update = $updates[ $class ];
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" );
293 public function dataGetDeletionUpdates() {
295 array("ContentHandlerTest_testGetSecondaryDataUpdates_1", "hello ''world''\n",
296 array( 'LinksDeletionUpdate' => array( ) )
298 array("ContentHandlerTest_testGetSecondaryDataUpdates_2", "hello [[world test 21344]]\n",
299 array( 'LinksDeletionUpdate' => array( ) )
306 * @dataProvider dataGetDeletionUpdates
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!
312 $handler = ContentHandler
::getForModelID( $title->getContentModel() );
313 $content = ContentHandler
::makeContent( $text, $title );
315 $updates = $handler->getDeletionUpdates( $content, $title );
317 // make updates accessible by class name
318 foreach ( $updates as $update ) {
319 $class = get_class( $update );
320 $updates[ $class ] = $update;
323 foreach ( $expectedStuff as $class => $fieldValues ) {
324 $this->assertArrayHasKey( $class, $updates, "missing an update of type $class" );
326 $update = $updates[ $class ];
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" );
335 public function testSupportsSections() {
336 $this->markTestIncomplete( "not yet implemented" );
340 class DummyContentHandlerForTesting
extends ContentHandler
{
342 public function __construct( $dataModel ) {
343 parent
::__construct( $dataModel, array( "testing" ) );
347 * Serializes Content object of the type supported by this ContentHandler.
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
353 public function serializeContent( Content
$content, $format = null )
355 return $content->serialize();
359 * Unserializes a Content object of the type supported by this ContentHandler.
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
365 public function unserializeContent( $blob, $format = null )
367 $d = unserialize( $blob );
368 return new DummyContentForTesting( $d );
372 * Creates an empty Content object of the type supported by this ContentHandler.
375 public function makeEmptyContent()
377 return new DummyContentForTesting( '' );
381 * @param Content $content
382 * @param Title $title
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.
389 * @return ParserOutput
391 public function getParserOutput( Content
$content, Title
$title, $revId = null, ParserOptions
$options = NULL, $generateHtml = true )
393 return new ParserOutput( $content->getNativeData() );
397 class DummyContentForTesting
extends AbstractContent
{
399 public function __construct( $data ) {
400 parent
::__construct( "testing" );
405 public function serialize( $format = null ) {
406 return serialize( $this->data
);
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.
413 public function getTextForSearchIndex()
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.
422 public function getWikitextForTransclusion()
428 * Returns a textual representation of the content suitable for use in edit summaries and log messages.
430 * @param int $maxlength maximum length of the summary text
431 * @return String the summary text
433 public function getTextForSummary( $maxlength = 250 )
439 * Returns native represenation of the data. Interpretation depends on the data model used,
440 * as given by getDataModel().
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.
445 public function getNativeData()
451 * returns the content's nominal size in bogo-bytes.
455 public function getSize()
457 return strlen( $this->data
);
461 * Return a copy of this Content object. The following must be true for the object returned
462 * if $copy = $original->copy()
464 * * get_class($original) === get_class($copy)
465 * * $original->getModel() === $copy->getModel()
466 * * $original->equals( $copy )
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
472 * @return Content. A copy of this object
474 public function copy()
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).
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.
487 public function isCountable( $hasLinks = null )