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 dataGetPageLanguage() {
99 global $wgLanguageCode;
102 array( "Main", $wgLanguageCode ),
103 array( "Dummy:Foo", $wgLanguageCode ),
104 array( "MediaWiki:common.js", 'en' ),
105 array( "User:Foo/common.js", 'en' ),
106 array( "MediaWiki:common.css", 'en' ),
107 array( "User:Foo/common.css", 'en' ),
108 array( "User:Foo", $wgLanguageCode ),
110 array( CONTENT_MODEL_JAVASCRIPT
, 'javascript' ),
115 * @dataProvider dataGetPageLanguage
117 public function testGetPageLanguage( $title, $expected ) {
118 if ( is_string( $title ) ) {
119 $title = Title
::newFromText( $title );
122 $expected = wfGetLangObj( $expected );
124 $handler = ContentHandler
::getForTitle( $title );
125 $lang = $handler->getPageLanguage( $title );
127 $this->assertEquals( $expected->getCode(), $lang->getCode() );
130 public function testGetContentText_Null( ) {
131 global $wgContentHandlerTextFallback;
135 $wgContentHandlerTextFallback = 'fail';
136 $text = ContentHandler
::getContentText( $content );
137 $this->assertEquals( '', $text );
139 $wgContentHandlerTextFallback = 'serialize';
140 $text = ContentHandler
::getContentText( $content );
141 $this->assertEquals( '', $text );
143 $wgContentHandlerTextFallback = 'ignore';
144 $text = ContentHandler
::getContentText( $content );
145 $this->assertEquals( '', $text );
148 public function testGetContentText_TextContent( ) {
149 global $wgContentHandlerTextFallback;
151 $content = new WikitextContent( "hello world" );
153 $wgContentHandlerTextFallback = 'fail';
154 $text = ContentHandler
::getContentText( $content );
155 $this->assertEquals( $content->getNativeData(), $text );
157 $wgContentHandlerTextFallback = 'serialize';
158 $text = ContentHandler
::getContentText( $content );
159 $this->assertEquals( $content->serialize(), $text );
161 $wgContentHandlerTextFallback = 'ignore';
162 $text = ContentHandler
::getContentText( $content );
163 $this->assertEquals( $content->getNativeData(), $text );
166 public function testGetContentText_NonTextContent( ) {
167 global $wgContentHandlerTextFallback;
169 $content = new DummyContentForTesting( "hello world" );
171 $wgContentHandlerTextFallback = 'fail';
174 $text = ContentHandler
::getContentText( $content );
176 $this->fail( "ContentHandler::getContentText should have thrown an exception for non-text Content object" );
177 } catch (MWException
$ex) {
181 $wgContentHandlerTextFallback = 'serialize';
182 $text = ContentHandler
::getContentText( $content );
183 $this->assertEquals( $content->serialize(), $text );
185 $wgContentHandlerTextFallback = 'ignore';
186 $text = ContentHandler
::getContentText( $content );
187 $this->assertNull( $text );
190 #public static function makeContent( $text, Title $title, $modelId = null, $format = null )
192 public function dataMakeContent() {
194 array( 'hallo', 'Test', null, null, CONTENT_MODEL_WIKITEXT
, 'hallo', false ),
195 array( 'hallo', 'MediaWiki:Test.js', null, null, CONTENT_MODEL_JAVASCRIPT
, 'hallo', false ),
196 array( serialize('hallo'), 'Dummy:Test', null, null, "testing", 'hallo', false ),
198 array( 'hallo', 'Test', null, CONTENT_FORMAT_WIKITEXT
, CONTENT_MODEL_WIKITEXT
, 'hallo', false ),
199 array( 'hallo', 'MediaWiki:Test.js', null, CONTENT_FORMAT_JAVASCRIPT
, CONTENT_MODEL_JAVASCRIPT
, 'hallo', false ),
200 array( serialize('hallo'), 'Dummy:Test', null, "testing", "testing", 'hallo', false ),
202 array( 'hallo', 'Test', CONTENT_MODEL_CSS
, null, CONTENT_MODEL_CSS
, 'hallo', false ),
203 array( 'hallo', 'MediaWiki:Test.js', CONTENT_MODEL_CSS
, null, CONTENT_MODEL_CSS
, 'hallo', false ),
204 array( serialize('hallo'), 'Dummy:Test', CONTENT_MODEL_CSS
, null, CONTENT_MODEL_CSS
, serialize('hallo'), false ),
206 array( 'hallo', 'Test', CONTENT_MODEL_WIKITEXT
, "testing", null, null, true ),
207 array( 'hallo', 'MediaWiki:Test.js', CONTENT_MODEL_CSS
, "testing", null, null, true ),
208 array( 'hallo', 'Dummy:Test', CONTENT_MODEL_JAVASCRIPT
, "testing", null, null, true ),
213 * @dataProvider dataMakeContent
215 public function testMakeContent( $data, $title, $modelId, $format, $expectedModelId, $expectedNativeData, $shouldFail ) {
216 global $wgExtraNamespaces, $wgNamespaceContentModels, $wgContentHandlers;
218 $title = Title
::newFromText( $title );
221 $content = ContentHandler
::makeContent( $data, $title, $modelId, $format );
223 if ( $shouldFail ) $this->fail( "ContentHandler::makeContent should have failed!" );
225 $this->assertEquals( $expectedModelId, $content->getModel(), 'bad model id' );
226 $this->assertEquals( $expectedNativeData, $content->getNativeData(), 'bads native data' );
227 } catch ( MWException
$ex ) {
228 if ( !$shouldFail ) $this->fail( "ContentHandler::makeContent failed unexpectedly: " . $ex->getMessage() );
229 else $this->assertTrue( true ); // dummy, so we don't get the "test did not perform any assertions" message.
234 public function testSupportsSections() {
235 $this->markTestIncomplete( "not yet implemented" );
239 class DummyContentHandlerForTesting
extends ContentHandler
{
241 public function __construct( $dataModel ) {
242 parent
::__construct( $dataModel, array( "testing" ) );
246 * Serializes Content object of the type supported by this ContentHandler.
248 * @param Content $content the Content object to serialize
249 * @param null $format the desired serialization format
250 * @return String serialized form of the content
252 public function serializeContent( Content
$content, $format = null )
254 return $content->serialize();
258 * Unserializes a Content object of the type supported by this ContentHandler.
260 * @param $blob String serialized form of the content
261 * @param null $format the format used for serialization
262 * @return Content the Content object created by deserializing $blob
264 public function unserializeContent( $blob, $format = null )
266 $d = unserialize( $blob );
267 return new DummyContentForTesting( $d );
271 * Creates an empty Content object of the type supported by this ContentHandler.
274 public function makeEmptyContent()
276 return new DummyContentForTesting( '' );
280 class DummyContentForTesting
extends AbstractContent
{
282 public function __construct( $data ) {
283 parent
::__construct( "testing" );
288 public function serialize( $format = null ) {
289 return serialize( $this->data
);
293 * @return String a string representing the content in a way useful for building a full text search index.
294 * If no useful representation exists, this method returns an empty string.
296 public function getTextForSearchIndex()
302 * @return String the wikitext to include when another page includes this content, or false if the content is not
303 * includable in a wikitext page.
305 public function getWikitextForTransclusion()
311 * Returns a textual representation of the content suitable for use in edit summaries and log messages.
313 * @param int $maxlength maximum length of the summary text
314 * @return String the summary text
316 public function getTextForSummary( $maxlength = 250 )
322 * Returns native represenation of the data. Interpretation depends on the data model used,
323 * as given by getDataModel().
325 * @return mixed the native representation of the content. Could be a string, a nested array
326 * structure, an object, a binary blob... anything, really.
328 public function getNativeData()
334 * returns the content's nominal size in bogo-bytes.
338 public function getSize()
340 return strlen( $this->data
);
344 * Return a copy of this Content object. The following must be true for the object returned
345 * if $copy = $original->copy()
347 * * get_class($original) === get_class($copy)
348 * * $original->getModel() === $copy->getModel()
349 * * $original->equals( $copy )
351 * If and only if the Content object is imutable, the copy() method can and should
352 * return $this. That is, $copy === $original may be true, but only for imutable content
355 * @return Content. A copy of this object
357 public function copy()
363 * Returns true if this content is countable as a "real" wiki page, provided
364 * that it's also in a countable location (e.g. a current revision in the main namespace).
366 * @param $hasLinks Bool: if it is known whether this content contains links, provide this information here,
367 * to avoid redundant parsing to find out.
370 public function isCountable( $hasLinks = null )
376 * @param Title $title
378 * @param null|ParserOptions $options
379 * @param Boolean $generateHtml whether to generate Html (default: true). If false,
380 * the result of calling getText() on the ParserOutput object returned by
381 * this method is undefined.
383 * @return ParserOutput
385 public function getParserOutput( Title
$title, $revId = null, ParserOptions
$options = NULL, $generateHtml = true )
387 return new ParserOutput( $this->getNativeData() );