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 ] = 999999;
20 $wgContentHandlers[ 999999 ] = '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[ 999999 ] );
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 dataGetContentFormatMimeType( ) {
79 array( 99887766, null ),
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' ),
91 * @dataProvider dataGetContentFormatMimeType
93 public function testGetContentFormatMimeType( $id, $expectedMime ) {
94 $mime = ContentHandler
::getContentFormatMimeType( $id );
96 $this->assertEquals( $expectedMime, $mime );
99 public function dataGetContentFormatID( ) {
102 array( 'foo', null ),
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
),
115 * @dataProvider dataGetContentFormatID
117 public function testGetContentFormatID( $mime, $expectedId ) {
118 $id = ContentHandler
::getContentFormatID( $mime );
120 $this->assertEquals( $expectedId, $id );
123 public function dataGetContentModelName() {
127 array( 99887766, null ),
129 array( CONTENT_MODEL_JAVASCRIPT
, '/javascript/i' ), //XXX: depends on content language
134 * @dataProvider dataGetContentModelName
136 public function testGetContentModelName( $id, $expected ) {
137 $name = ContentHandler
::getContentModelName( $id );
139 if ( $expected === null ) {
140 $this->assertNull( $name, "content model name for #$id was expected to be null" );
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" );
147 public function testGetContentText_Null( ) {
148 global $wgContentHandlerTextFallback;
152 $wgContentHandlerTextFallback = 'fail';
153 $text = ContentHandler
::getContentText( $content );
154 $this->assertEquals( '', $text );
156 $wgContentHandlerTextFallback = 'serialize';
157 $text = ContentHandler
::getContentText( $content );
158 $this->assertEquals( '', $text );
160 $wgContentHandlerTextFallback = 'ignore';
161 $text = ContentHandler
::getContentText( $content );
162 $this->assertEquals( '', $text );
165 public function testGetContentText_TextContent( ) {
166 global $wgContentHandlerTextFallback;
168 $content = new WikitextContent( "hello world" );
170 $wgContentHandlerTextFallback = 'fail';
171 $text = ContentHandler
::getContentText( $content );
172 $this->assertEquals( $content->getNativeData(), $text );
174 $wgContentHandlerTextFallback = 'serialize';
175 $text = ContentHandler
::getContentText( $content );
176 $this->assertEquals( $content->serialize(), $text );
178 $wgContentHandlerTextFallback = 'ignore';
179 $text = ContentHandler
::getContentText( $content );
180 $this->assertEquals( $content->getNativeData(), $text );
183 public function testGetContentText_NonTextContent( ) {
184 global $wgContentHandlerTextFallback;
186 $content = new DummyContentForTesting( "hello world" );
188 $wgContentHandlerTextFallback = 'fail';
191 $text = ContentHandler
::getContentText( $content );
193 $this->fail( "ContentHandler::getContentText should have thrown an exception for non-text Content object" );
194 } catch (MWException
$ex) {
198 $wgContentHandlerTextFallback = 'serialize';
199 $text = ContentHandler
::getContentText( $content );
200 $this->assertEquals( $content->serialize(), $text );
202 $wgContentHandlerTextFallback = 'ignore';
203 $text = ContentHandler
::getContentText( $content );
204 $this->assertNull( $text );
207 #public static function makeContent( $text, Title $title, $modelId = null, $format = null )
209 public function dataMakeContent() {
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 ),
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 ),
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 ),
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 ),
230 * @dataProvider dataMakeContent
232 public function testMakeContent( $data, $title, $modelId, $format, $expectedModelId, $expectedNativeData, $shouldFail ) {
233 global $wgExtraNamespaces, $wgNamespaceContentModels, $wgContentHandlers;
235 $title = Title
::newFromText( $title );
238 $content = ContentHandler
::makeContent( $data, $title, $modelId, $format );
240 if ( $shouldFail ) $this->fail( "ContentHandler::makeContent should have failed!" );
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.
251 public function dataGetParserOutput() {
253 array("ContentHandlerTest_testGetParserOutput", "hello ''world''\n", "<p>hello <i>world</i>\n</p>"),
259 * @dataProvider dataGetParserOutput
261 public function testGetParserOutput( $title, $text, $expectedHtml ) {
262 $title = Title
::newFromText( $title );
263 $handler = ContentHandler
::getForModelID( $title->getContentModel() );
264 $content = ContentHandler
::makeContent( $text, $title );
266 $po = $handler->getParserOutput( $content, $title );
268 $this->assertEquals( $expectedHtml, $po->getText() );
269 // @todo: assert more properties
272 public function dataGetSecondaryDataUpdates() {
274 array("ContentHandlerTest_testGetSecondaryDataUpdates_1", "hello ''world''\n",
275 array( 'LinksUpdate' => array( 'mRecursive' => true,
276 'mLinks' => array() ) )
278 array("ContentHandlerTest_testGetSecondaryDataUpdates_2", "hello [[world test 21344]]\n",
279 array( 'LinksUpdate' => array( 'mRecursive' => true,
280 'mLinks' => array( array( 'World_test_21344' => 0 ) ) ) )
287 * @dataProvider dataGetSecondaryDataUpdates
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!
293 $handler = ContentHandler
::getForModelID( $title->getContentModel() );
294 $content = ContentHandler
::makeContent( $text, $title );
296 $updates = $handler->getSecondaryDataUpdates( $content, $title );
298 // make updates accessible by class name
299 foreach ( $updates as $update ) {
300 $class = get_class( $update );
301 $updates[ $class ] = $update;
304 foreach ( $expectedStuff as $class => $fieldValues ) {
305 $this->assertArrayHasKey( $class, $updates, "missing an update of type $class" );
307 $update = $updates[ $class ];
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" );
316 public function dataGetDeletionUpdates() {
318 array("ContentHandlerTest_testGetSecondaryDataUpdates_1", "hello ''world''\n",
319 array( 'LinksDeletionUpdate' => array( ) )
321 array("ContentHandlerTest_testGetSecondaryDataUpdates_2", "hello [[world test 21344]]\n",
322 array( 'LinksDeletionUpdate' => array( ) )
329 * @dataProvider dataGetDeletionUpdates
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!
335 $handler = ContentHandler
::getForModelID( $title->getContentModel() );
336 $content = ContentHandler
::makeContent( $text, $title );
338 $updates = $handler->getDeletionUpdates( $content, $title );
340 // make updates accessible by class name
341 foreach ( $updates as $update ) {
342 $class = get_class( $update );
343 $updates[ $class ] = $update;
346 foreach ( $expectedStuff as $class => $fieldValues ) {
347 $this->assertArrayHasKey( $class, $updates, "missing an update of type $class" );
349 $update = $updates[ $class ];
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" );
359 class DummyContentHandlerForTesting
extends ContentHandler
{
361 public function __construct( $dataModel ) {
362 parent
::__construct( $dataModel, array( 999999 ) );
366 * Serializes Content object of the type supported by this ContentHandler.
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
372 public function serializeContent( Content
$content, $format = null )
374 return $content->serialize();
378 * Unserializes a Content object of the type supported by this ContentHandler.
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
384 public function unserializeContent( $blob, $format = null )
386 $d = unserialize( $blob );
387 return new DummyContentForTesting( $d );
391 * Creates an empty Content object of the type supported by this ContentHandler.
394 public function makeEmptyContent()
396 return new DummyContentForTesting( '' );
400 * @param Content $content
401 * @param Title $title
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.
408 * @return ParserOutput
410 public function getParserOutput( Content
$content, Title
$title, $revId = null, ParserOptions
$options = NULL, $generateHtml = true )
412 return new ParserOutput( $content->getNativeData() );
416 class DummyContentForTesting
extends AbstractContent
{
418 public function __construct( $data ) {
419 parent
::__construct( 999999 );
424 public function serialize( $format = null ) {
425 return serialize( $this->data
);
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.
432 public function getTextForSearchIndex()
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.
441 public function getWikitextForTransclusion()
447 * Returns a textual representation of the content suitable for use in edit summaries and log messages.
449 * @param int $maxlength maximum length of the summary text
450 * @return String the summary text
452 public function getTextForSummary( $maxlength = 250 )
458 * Returns native represenation of the data. Interpretation depends on the data model used,
459 * as given by getDataModel().
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.
464 public function getNativeData()
470 * returns the content's nominal size in bogo-bytes.
474 public function getSize()
476 return strlen( $this->data
);
480 * Return a copy of this Content object. The following must be true for the object returned
481 * if $copy = $original->copy()
483 * * get_class($original) === get_class($copy)
484 * * $original->getModel() === $copy->getModel()
485 * * $original->equals( $copy )
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
491 * @return Content. A copy of this object
493 public function copy()
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).
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.
506 public function isCountable( $hasLinks = null )