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.
249 class DummyContentHandlerForTesting
extends ContentHandler
{
251 public function __construct( $dataModel ) {
252 parent
::__construct( $dataModel, array( 999999 ) );
256 * Serializes Content object of the type supported by this ContentHandler.
258 * @param Content $content the Content object to serialize
259 * @param null $format the desired serialization format
260 * @return String serialized form of the content
262 public function serializeContent( Content
$content, $format = null )
264 return $content->serialize();
268 * Unserializes a Content object of the type supported by this ContentHandler.
270 * @param $blob String serialized form of the content
271 * @param null $format the format used for serialization
272 * @return Content the Content object created by deserializing $blob
274 public function unserializeContent( $blob, $format = null )
276 $d = unserialize( $blob );
277 return new DummyContentForTesting( $d );
281 * Creates an empty Content object of the type supported by this ContentHandler.
284 public function makeEmptyContent()
286 return new DummyContentForTesting( '' );
290 class DummyContentForTesting
extends Content
{
292 public function __construct( $data ) {
293 parent
::__construct( 999999 );
298 public function serialize( $format = null ) {
299 return serialize( $this->data
);
303 * @return String a string representing the content in a way useful for building a full text search index.
304 * If no useful representation exists, this method returns an empty string.
306 public function getTextForSearchIndex()
312 * @return String the wikitext to include when another page includes this content, or false if the content is not
313 * includable in a wikitext page.
315 public function getWikitextForTransclusion()
321 * Returns a textual representation of the content suitable for use in edit summaries and log messages.
323 * @param int $maxlength maximum length of the summary text
324 * @return String the summary text
326 public function getTextForSummary( $maxlength = 250 )
332 * Returns native represenation of the data. Interpretation depends on the data model used,
333 * as given by getDataModel().
335 * @return mixed the native representation of the content. Could be a string, a nested array
336 * structure, an object, a binary blob... anything, really.
338 public function getNativeData()
344 * returns the content's nominal size in bogo-bytes.
348 public function getSize()
350 return strlen( $this->data
);
354 * Return a copy of this Content object. The following must be true for the object returned
355 * if $copy = $original->copy()
357 * * get_class($original) === get_class($copy)
358 * * $original->getModel() === $copy->getModel()
359 * * $original->equals( $copy )
361 * If and only if the Content object is imutable, the copy() method can and should
362 * return $this. That is, $copy === $original may be true, but only for imutable content
365 * @return Content. A copy of this object
367 public function copy()
373 * Returns true if this content is countable as a "real" wiki page, provided
374 * that it's also in a countable location (e.g. a current revision in the main namespace).
376 * @param $hasLinks Bool: if it is known whether this content contains links, provide this information here,
377 * to avoid redundant parsing to find out.
380 public function isCountable( $hasLinks = null )
386 * @param Title $title
388 * @param null|ParserOptions $options
389 * @param Boolean $generateHtml whether to generate Html (default: true). If false,
390 * the result of calling getText() on the ParserOutput object returned by
391 * this method is undefined.
393 * @return ParserOutput
395 public function getParserOutput( Title
$title, $revId = null, ParserOptions
$options = NULL, $generateHtml = true )
397 return new ParserOutput( $this->data
);