4 * @group ContentHandler
6 class ContentHandlerTest
extends MediaWikiTestCase
{
8 public function setUp() {
9 global $wgExtraNamespaces, $wgNamespaceContentModels, $wgContentHandlers, $wgContLang;
11 $wgExtraNamespaces[ 12312 ] = 'Dummy';
12 $wgExtraNamespaces[ 12313 ] = 'Dummy_talk';
14 $wgNamespaceContentModels[ 12312 ] = 999999;
15 $wgContentHandlers[ 999999 ] = 'DummyContentHandlerForTesting';
17 MWNamespace
::getCanonicalNamespaces( true ); # reset namespace cache
18 $wgContLang->resetNamespaces(); # reset namespace cache
21 public function tearDown() {
22 global $wgExtraNamespaces, $wgNamespaceContentModels, $wgContentHandlers, $wgContLang;
24 unset( $wgExtraNamespaces[ 12312 ] );
25 unset( $wgExtraNamespaces[ 12313 ] );
27 unset( $wgNamespaceContentModels[ 12312 ] );
28 unset( $wgContentHandlers[ 999999 ] );
30 MWNamespace
::getCanonicalNamespaces( true ); # reset namespace cache
31 $wgContLang->resetNamespaces(); # reset namespace cache
34 public function dataGetDefaultModelFor() {
36 array( 'Foo', CONTENT_MODEL_WIKITEXT
),
37 array( 'Foo.js', CONTENT_MODEL_WIKITEXT
),
38 array( 'Foo/bar.js', CONTENT_MODEL_WIKITEXT
),
39 array( 'User:Foo', CONTENT_MODEL_WIKITEXT
),
40 array( 'User:Foo.js', CONTENT_MODEL_WIKITEXT
),
41 array( 'User:Foo/bar.js', CONTENT_MODEL_JAVASCRIPT
),
42 array( 'User:Foo/bar.css', CONTENT_MODEL_CSS
),
43 array( 'User talk:Foo/bar.css', CONTENT_MODEL_WIKITEXT
),
44 array( 'User:Foo/bar.js.xxx', CONTENT_MODEL_WIKITEXT
),
45 array( 'User:Foo/bar.xxx', CONTENT_MODEL_WIKITEXT
),
46 array( 'MediaWiki:Foo.js', CONTENT_MODEL_JAVASCRIPT
),
47 array( 'MediaWiki:Foo.css', CONTENT_MODEL_CSS
),
48 array( 'MediaWiki:Foo.JS', CONTENT_MODEL_WIKITEXT
),
49 array( 'MediaWiki:Foo.CSS', CONTENT_MODEL_WIKITEXT
),
50 array( 'MediaWiki:Foo.css.xxx', CONTENT_MODEL_WIKITEXT
),
55 * @dataProvider dataGetDefaultModelFor
57 public function testGetDefaultModelFor( $title, $expectedModelId ) {
58 $title = Title
::newFromText( $title );
59 $this->assertEquals( $expectedModelId, ContentHandler
::getDefaultModelFor( $title ) );
62 * @dataProvider dataGetDefaultModelFor
64 public function testGetForTitle( $title, $expectedContentModel ) {
65 $title = Title
::newFromText( $title );
66 $handler = ContentHandler
::getForTitle( $title );
67 $this->assertEquals( $expectedContentModel, $handler->getModelID() );
70 public function dataGetContentFormatMimeType( ) {
74 array( 99887766, null ),
76 array( CONTENT_FORMAT_WIKITEXT
, 'text/x-wiki' ),
77 array( CONTENT_FORMAT_JAVASCRIPT
, 'text/javascript' ),
78 array( CONTENT_FORMAT_CSS
, 'text/css' ),
79 array( CONTENT_FORMAT_JSON
, 'application/json' ),
80 array( CONTENT_FORMAT_XML
, 'application/xml' ),
81 array( CONTENT_FORMAT_SERIALIZED
, 'application/vnd.php.serialized' ),
86 * @dataProvider dataGetContentFormatMimeType
88 public function testGetContentFormatMimeType( $id, $expectedMime ) {
89 $mime = ContentHandler
::getContentFormatMimeType( $id );
91 $this->assertEquals( $expectedMime, $mime );
94 public function dataGetContentFormatID( ) {
100 array( 'text/x-wiki', CONTENT_FORMAT_WIKITEXT
),
101 array( 'text/javascript', CONTENT_FORMAT_JAVASCRIPT
),
102 array( 'text/css', CONTENT_FORMAT_CSS
),
103 array( 'application/json', CONTENT_FORMAT_JSON
),
104 array( 'application/xml', CONTENT_FORMAT_XML
),
105 array( 'application/vnd.php.serialized', CONTENT_FORMAT_SERIALIZED
),
110 * @dataProvider dataGetContentFormatID
112 public function testGetContentFormatID( $mime, $expectedId ) {
113 $id = ContentHandler
::getContentFormatID( $mime );
115 $this->assertEquals( $expectedId, $id );
118 public function dataGetContentModelName() {
122 array( 99887766, null ),
124 array( CONTENT_MODEL_JAVASCRIPT
, '/javascript/i' ), //XXX: depends on content language
129 * @dataProvider dataGetContentModelName
131 public function testGetContentModelName( $id, $expected ) {
132 $name = ContentHandler
::getContentModelName( $id );
134 if ( $expected === null ) {
135 $this->assertNull( $name, "content model name for #$id was expected to be null" );
137 $this->assertNotNull( $name, "no name found for content model #$id" );
138 $this->assertTrue( preg_match( $expected, $name ) > 0 , "content model name for #$id did not match pattern $expected" );
142 public function testGetContentText_Null( ) {
143 global $wgContentHandlerTextFallback;
147 $wgContentHandlerTextFallback = 'fail';
148 $text = ContentHandler
::getContentText( $content );
149 $this->assertEquals( '', $text );
151 $wgContentHandlerTextFallback = 'serialize';
152 $text = ContentHandler
::getContentText( $content );
153 $this->assertEquals( '', $text );
155 $wgContentHandlerTextFallback = 'ignore';
156 $text = ContentHandler
::getContentText( $content );
157 $this->assertEquals( '', $text );
160 public function testGetContentText_TextContent( ) {
161 global $wgContentHandlerTextFallback;
163 $content = new WikitextContent( "hello world" );
165 $wgContentHandlerTextFallback = 'fail';
166 $text = ContentHandler
::getContentText( $content );
167 $this->assertEquals( $content->getNativeData(), $text );
169 $wgContentHandlerTextFallback = 'serialize';
170 $text = ContentHandler
::getContentText( $content );
171 $this->assertEquals( $content->serialize(), $text );
173 $wgContentHandlerTextFallback = 'ignore';
174 $text = ContentHandler
::getContentText( $content );
175 $this->assertEquals( $content->getNativeData(), $text );
178 public function testGetContentText_NonTextContent( ) {
179 global $wgContentHandlerTextFallback;
181 $content = new DummyContentForTesting( "hello world" );
183 $wgContentHandlerTextFallback = 'fail';
186 $text = ContentHandler
::getContentText( $content );
188 $this->fail( "ContentHandler::getContentText should have thrown an exception for non-text Content object" );
189 } catch (MWException
$ex) {
193 $wgContentHandlerTextFallback = 'serialize';
194 $text = ContentHandler
::getContentText( $content );
195 $this->assertEquals( $content->serialize(), $text );
197 $wgContentHandlerTextFallback = 'ignore';
198 $text = ContentHandler
::getContentText( $content );
199 $this->assertNull( $text );
202 #public static function makeContent( $text, Title $title, $modelId = null, $format = null )
204 public function dataMakeContent() {
206 array( 'hallo', 'Test', null, null, CONTENT_MODEL_WIKITEXT
, 'hallo', false ),
207 array( 'hallo', 'MediaWiki:Test.js', null, null, CONTENT_MODEL_JAVASCRIPT
, 'hallo', false ),
208 array( serialize('hallo'), 'Dummy:Test', null, null, 999999, 'hallo', false ),
210 array( 'hallo', 'Test', null, CONTENT_FORMAT_WIKITEXT
, CONTENT_MODEL_WIKITEXT
, 'hallo', false ),
211 array( 'hallo', 'MediaWiki:Test.js', null, CONTENT_FORMAT_JAVASCRIPT
, CONTENT_MODEL_JAVASCRIPT
, 'hallo', false ),
212 array( serialize('hallo'), 'Dummy:Test', null, 999999, 999999, 'hallo', false ),
214 array( 'hallo', 'Test', CONTENT_MODEL_CSS
, null, CONTENT_MODEL_CSS
, 'hallo', false ),
215 array( 'hallo', 'MediaWiki:Test.js', CONTENT_MODEL_CSS
, null, CONTENT_MODEL_CSS
, 'hallo', false ),
216 array( serialize('hallo'), 'Dummy:Test', CONTENT_MODEL_CSS
, null, CONTENT_MODEL_CSS
, serialize('hallo'), false ),
218 array( 'hallo', 'Test', CONTENT_MODEL_WIKITEXT
, 999999, null, null, true ),
219 array( 'hallo', 'MediaWiki:Test.js', CONTENT_MODEL_CSS
, 999999, null, null, true ),
220 array( 'hallo', 'Dummy:Test', CONTENT_MODEL_JAVASCRIPT
, 999999, null, null, true ),
225 * @dataProvider dataMakeContent
227 public function testMakeContent( $data, $title, $modelId, $format, $expectedModelId, $expectedNativeData, $shouldFail ) {
228 global $wgExtraNamespaces, $wgNamespaceContentModels, $wgContentHandlers;
230 $title = Title
::newFromText( $title );
233 $content = ContentHandler
::makeContent( $data, $title, $modelId, $format );
235 if ( $shouldFail ) $this->fail( "ContentHandler::makeContent should have failed!" );
237 $this->assertEquals( $expectedModelId, $content->getModel(), 'bad model id' );
238 $this->assertEquals( $expectedNativeData, $content->getNativeData(), 'bads native data' );
239 } catch ( MWException
$ex ) {
240 if ( !$shouldFail ) $this->fail( "ContentHandler::makeContent failed unexpectedly: " . $ex->getMessage() );
241 else $this->assertTrue( true ); // dummy, so we don't get the "test did not perform any assertions" message.
246 public function dataGetParserOutput() {
248 array("ContentHandlerTest_testGetParserOutput", "hello ''world''\n", "<p>hello <i>world</i>\n</p>"),
254 * @dataProvider dataGetParserOutput
256 public function testGetParserOutput( $title, $text, $expectedHtml ) {
257 $title = Title
::newFromText( $title );
258 $handler = ContentHandler
::getForModelID( $title->getContentModel() );
259 $content = ContentHandler
::makeContent( $text, $title );
261 $po = $handler->getParserOutput( $content, $title );
263 $this->assertEquals( $expectedHtml, $po->getText() );
264 // @todo: assert more properties
267 public function dataGetSecondaryDataUpdates() {
269 array("ContentHandlerTest_testGetSecondaryDataUpdates_1", "hello ''world''\n",
270 array( 'LinksUpdate' => array( 'mRecursive' => true,
271 'mLinks' => array() ) )
273 array("ContentHandlerTest_testGetSecondaryDataUpdates_2", "hello [[world test 21344]]\n",
274 array( 'LinksUpdate' => array( 'mRecursive' => true,
275 'mLinks' => array( array( 'World_test_21344' => 0 ) ) ) )
282 * @dataProvider dataGetSecondaryDataUpdates
284 public function testGetSecondaryDataUpdates( $title, $text, $expectedStuff ) {
285 $title = Title
::newFromText( $title );
286 $title->resetArticleID( 2342 ); //dummy id. fine as long as we don't try to execute the updates!
288 $handler = ContentHandler
::getForModelID( $title->getContentModel() );
289 $content = ContentHandler
::makeContent( $text, $title );
291 $updates = $handler->getSecondaryDataUpdates( $content, $title );
293 // make updates accessible by class name
294 foreach ( $updates as $update ) {
295 $class = get_class( $update );
296 $updates[ $class ] = $update;
299 foreach ( $expectedStuff as $class => $fieldValues ) {
300 $this->assertArrayHasKey( $class, $updates, "missing an update of type $class" );
302 $update = $updates[ $class ];
304 foreach ( $fieldValues as $field => $value ) {
305 $v = $update->$field; #if the field doesn't exist, just crash and burn
306 $this->assertEquals( $value, $v, "unexpected value for field $field in instance of $class" );
311 public function dataGetDeletionUpdates() {
313 array("ContentHandlerTest_testGetSecondaryDataUpdates_1", "hello ''world''\n",
314 array( 'LinksDeletionUpdate' => array( ) )
316 array("ContentHandlerTest_testGetSecondaryDataUpdates_2", "hello [[world test 21344]]\n",
317 array( 'LinksDeletionUpdate' => array( ) )
324 * @dataProvider dataGetDeletionUpdates
326 public function testDeletionUpdates( $title, $text, $expectedStuff ) {
327 $title = Title
::newFromText( $title );
328 $title->resetArticleID( 2342 ); //dummy id. fine as long as we don't try to execute the updates!
330 $handler = ContentHandler
::getForModelID( $title->getContentModel() );
331 $content = ContentHandler
::makeContent( $text, $title );
333 $updates = $handler->getDeletionUpdates( $content, $title );
335 // make updates accessible by class name
336 foreach ( $updates as $update ) {
337 $class = get_class( $update );
338 $updates[ $class ] = $update;
341 foreach ( $expectedStuff as $class => $fieldValues ) {
342 $this->assertArrayHasKey( $class, $updates, "missing an update of type $class" );
344 $update = $updates[ $class ];
346 foreach ( $fieldValues as $field => $value ) {
347 $v = $update->$field; #if the field doesn't exist, just crash and burn
348 $this->assertEquals( $value, $v, "unexpected value for field $field in instance of $class" );
354 class DummyContentHandlerForTesting
extends ContentHandler
{
356 public function __construct( $dataModel ) {
357 parent
::__construct( $dataModel, array( 999999 ) );
361 * Serializes Content object of the type supported by this ContentHandler.
363 * @param Content $content the Content object to serialize
364 * @param null $format the desired serialization format
365 * @return String serialized form of the content
367 public function serializeContent( Content
$content, $format = null )
369 return $content->serialize();
373 * Unserializes a Content object of the type supported by this ContentHandler.
375 * @param $blob String serialized form of the content
376 * @param null $format the format used for serialization
377 * @return Content the Content object created by deserializing $blob
379 public function unserializeContent( $blob, $format = null )
381 $d = unserialize( $blob );
382 return new DummyContentForTesting( $d );
386 * Creates an empty Content object of the type supported by this ContentHandler.
389 public function makeEmptyContent()
391 return new DummyContentForTesting( '' );
395 * @param Content $content
396 * @param Title $title
398 * @param null|ParserOptions $options
399 * @param Boolean $generateHtml whether to generate Html (default: true). If false,
400 * the result of calling getText() on the ParserOutput object returned by
401 * this method is undefined.
403 * @return ParserOutput
405 public function getParserOutput( Content
$content, Title
$title, $revId = null, ParserOptions
$options = NULL, $generateHtml = true )
407 return new ParserOutput( $content->getNativeData() );
411 class DummyContentForTesting
extends AbstractContent
{
413 public function __construct( $data ) {
414 parent
::__construct( 999999 );
419 public function serialize( $format = null ) {
420 return serialize( $this->data
);
424 * @return String a string representing the content in a way useful for building a full text search index.
425 * If no useful representation exists, this method returns an empty string.
427 public function getTextForSearchIndex()
433 * @return String the wikitext to include when another page includes this content, or false if the content is not
434 * includable in a wikitext page.
436 public function getWikitextForTransclusion()
442 * Returns a textual representation of the content suitable for use in edit summaries and log messages.
444 * @param int $maxlength maximum length of the summary text
445 * @return String the summary text
447 public function getTextForSummary( $maxlength = 250 )
453 * Returns native represenation of the data. Interpretation depends on the data model used,
454 * as given by getDataModel().
456 * @return mixed the native representation of the content. Could be a string, a nested array
457 * structure, an object, a binary blob... anything, really.
459 public function getNativeData()
465 * returns the content's nominal size in bogo-bytes.
469 public function getSize()
471 return strlen( $this->data
);
475 * Return a copy of this Content object. The following must be true for the object returned
476 * if $copy = $original->copy()
478 * * get_class($original) === get_class($copy)
479 * * $original->getModel() === $copy->getModel()
480 * * $original->equals( $copy )
482 * If and only if the Content object is imutable, the copy() method can and should
483 * return $this. That is, $copy === $original may be true, but only for imutable content
486 * @return Content. A copy of this object
488 public function copy()
494 * Returns true if this content is countable as a "real" wiki page, provided
495 * that it's also in a countable location (e.g. a current revision in the main namespace).
497 * @param $hasLinks Bool: if it is known whether this content contains links, provide this information here,
498 * to avoid redundant parsing to find out.
501 public function isCountable( $hasLinks = null )