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() {
16 global $wgExtraNamespaces, $wgNamespaceContentModels, $wgContentHandlers, $wgContLang;
18 $wgExtraNamespaces[ 12312 ] = 'Dummy';
19 $wgExtraNamespaces[ 12313 ] = 'Dummy_talk';
21 $wgNamespaceContentModels[ 12312 ] = "testing";
22 $wgContentHandlers[ "testing" ] = 'DummyContentHandlerForTesting';
24 MWNamespace
::getCanonicalNamespaces( true ); # reset namespace cache
25 $wgContLang->resetNamespaces(); # reset namespace cache
28 public function teardown() {
29 global $wgExtraNamespaces, $wgNamespaceContentModels, $wgContentHandlers, $wgContLang;
31 unset( $wgExtraNamespaces[ 12312 ] );
32 unset( $wgExtraNamespaces[ 12313 ] );
34 unset( $wgNamespaceContentModels[ 12312 ] );
35 unset( $wgContentHandlers[ "testing" ] );
37 MWNamespace
::getCanonicalNamespaces( true ); # reset namespace cache
38 $wgContLang->resetNamespaces(); # reset namespace cache
43 public function dataGetDefaultModelFor() {
45 array( 'Foo', CONTENT_MODEL_WIKITEXT
),
46 array( 'Foo.js', CONTENT_MODEL_WIKITEXT
),
47 array( 'Foo/bar.js', CONTENT_MODEL_WIKITEXT
),
48 array( 'User:Foo', CONTENT_MODEL_WIKITEXT
),
49 array( 'User:Foo.js', CONTENT_MODEL_WIKITEXT
),
50 array( 'User:Foo/bar.js', CONTENT_MODEL_JAVASCRIPT
),
51 array( 'User:Foo/bar.css', CONTENT_MODEL_CSS
),
52 array( 'User talk:Foo/bar.css', CONTENT_MODEL_WIKITEXT
),
53 array( 'User:Foo/bar.js.xxx', CONTENT_MODEL_WIKITEXT
),
54 array( 'User:Foo/bar.xxx', CONTENT_MODEL_WIKITEXT
),
55 array( 'MediaWiki:Foo.js', CONTENT_MODEL_JAVASCRIPT
),
56 array( 'MediaWiki:Foo.css', CONTENT_MODEL_CSS
),
57 array( 'MediaWiki:Foo.JS', CONTENT_MODEL_WIKITEXT
),
58 array( 'MediaWiki:Foo.CSS', CONTENT_MODEL_WIKITEXT
),
59 array( 'MediaWiki:Foo.css.xxx', CONTENT_MODEL_WIKITEXT
),
64 * @dataProvider dataGetDefaultModelFor
66 public function testGetDefaultModelFor( $title, $expectedModelId ) {
67 $title = Title
::newFromText( $title );
68 $this->assertEquals( $expectedModelId, ContentHandler
::getDefaultModelFor( $title ) );
71 * @dataProvider dataGetDefaultModelFor
73 public function testGetForTitle( $title, $expectedContentModel ) {
74 $title = Title
::newFromText( $title );
75 $handler = ContentHandler
::getForTitle( $title );
76 $this->assertEquals( $expectedContentModel, $handler->getModelID() );
79 public function dataGetLocalizedName() {
82 array( "xyzzy", null ),
84 array( CONTENT_MODEL_JAVASCRIPT
, '/javascript/i' ), //XXX: depends on content language
89 * @dataProvider dataGetLocalizedName
91 public function testGetLocalizedName( $id, $expected ) {
92 $name = ContentHandler
::getLocalizedName( $id );
95 $this->assertNotNull( $name, "no name found for content model $id" );
96 $this->assertTrue( preg_match( $expected, $name ) > 0 ,
97 "content model name for #$id did not match pattern $expected" );
99 $this->assertEquals( $id, $name, "localization of unknown model $id should have "
100 . "fallen back to use the model id directly." );
104 public function dataGetPageLanguage() {
105 global $wgLanguageCode;
108 array( "Main", $wgLanguageCode ),
109 array( "Dummy:Foo", $wgLanguageCode ),
110 array( "MediaWiki:common.js", 'en' ),
111 array( "User:Foo/common.js", 'en' ),
112 array( "MediaWiki:common.css", 'en' ),
113 array( "User:Foo/common.css", 'en' ),
114 array( "User:Foo", $wgLanguageCode ),
116 array( CONTENT_MODEL_JAVASCRIPT
, 'javascript' ),
121 * @dataProvider dataGetPageLanguage
123 public function testGetPageLanguage( $title, $expected ) {
124 if ( is_string( $title ) ) {
125 $title = Title
::newFromText( $title );
128 $expected = wfGetLangObj( $expected );
130 $handler = ContentHandler
::getForTitle( $title );
131 $lang = $handler->getPageLanguage( $title );
133 $this->assertEquals( $expected->getCode(), $lang->getCode() );
136 public function testGetContentText_Null( ) {
137 global $wgContentHandlerTextFallback;
141 $wgContentHandlerTextFallback = 'fail';
142 $text = ContentHandler
::getContentText( $content );
143 $this->assertEquals( '', $text );
145 $wgContentHandlerTextFallback = 'serialize';
146 $text = ContentHandler
::getContentText( $content );
147 $this->assertEquals( '', $text );
149 $wgContentHandlerTextFallback = 'ignore';
150 $text = ContentHandler
::getContentText( $content );
151 $this->assertEquals( '', $text );
154 public function testGetContentText_TextContent( ) {
155 global $wgContentHandlerTextFallback;
157 $content = new WikitextContent( "hello world" );
159 $wgContentHandlerTextFallback = 'fail';
160 $text = ContentHandler
::getContentText( $content );
161 $this->assertEquals( $content->getNativeData(), $text );
163 $wgContentHandlerTextFallback = 'serialize';
164 $text = ContentHandler
::getContentText( $content );
165 $this->assertEquals( $content->serialize(), $text );
167 $wgContentHandlerTextFallback = 'ignore';
168 $text = ContentHandler
::getContentText( $content );
169 $this->assertEquals( $content->getNativeData(), $text );
172 public function testGetContentText_NonTextContent( ) {
173 global $wgContentHandlerTextFallback;
175 $content = new DummyContentForTesting( "hello world" );
177 $wgContentHandlerTextFallback = 'fail';
180 $text = ContentHandler
::getContentText( $content );
182 $this->fail( "ContentHandler::getContentText should have thrown an exception for non-text Content object" );
183 } catch (MWException
$ex) {
187 $wgContentHandlerTextFallback = 'serialize';
188 $text = ContentHandler
::getContentText( $content );
189 $this->assertEquals( $content->serialize(), $text );
191 $wgContentHandlerTextFallback = 'ignore';
192 $text = ContentHandler
::getContentText( $content );
193 $this->assertNull( $text );
196 #public static function makeContent( $text, Title $title, $modelId = null, $format = null )
198 public function dataMakeContent() {
200 array( 'hallo', 'Test', null, null, CONTENT_MODEL_WIKITEXT
, 'hallo', false ),
201 array( 'hallo', 'MediaWiki:Test.js', null, null, CONTENT_MODEL_JAVASCRIPT
, 'hallo', false ),
202 array( serialize('hallo'), 'Dummy:Test', null, null, "testing", 'hallo', false ),
204 array( 'hallo', 'Test', null, CONTENT_FORMAT_WIKITEXT
, CONTENT_MODEL_WIKITEXT
, 'hallo', false ),
205 array( 'hallo', 'MediaWiki:Test.js', null, CONTENT_FORMAT_JAVASCRIPT
, CONTENT_MODEL_JAVASCRIPT
, 'hallo', false ),
206 array( serialize('hallo'), 'Dummy:Test', null, "testing", "testing", 'hallo', false ),
208 array( 'hallo', 'Test', CONTENT_MODEL_CSS
, null, CONTENT_MODEL_CSS
, 'hallo', false ),
209 array( 'hallo', 'MediaWiki:Test.js', CONTENT_MODEL_CSS
, null, CONTENT_MODEL_CSS
, 'hallo', false ),
210 array( serialize('hallo'), 'Dummy:Test', CONTENT_MODEL_CSS
, null, CONTENT_MODEL_CSS
, serialize('hallo'), false ),
212 array( 'hallo', 'Test', CONTENT_MODEL_WIKITEXT
, "testing", null, null, true ),
213 array( 'hallo', 'MediaWiki:Test.js', CONTENT_MODEL_CSS
, "testing", null, null, true ),
214 array( 'hallo', 'Dummy:Test', CONTENT_MODEL_JAVASCRIPT
, "testing", null, null, true ),
219 * @dataProvider dataMakeContent
221 public function testMakeContent( $data, $title, $modelId, $format, $expectedModelId, $expectedNativeData, $shouldFail ) {
222 global $wgExtraNamespaces, $wgNamespaceContentModels, $wgContentHandlers;
224 $title = Title
::newFromText( $title );
227 $content = ContentHandler
::makeContent( $data, $title, $modelId, $format );
229 if ( $shouldFail ) $this->fail( "ContentHandler::makeContent should have failed!" );
231 $this->assertEquals( $expectedModelId, $content->getModel(), 'bad model id' );
232 $this->assertEquals( $expectedNativeData, $content->getNativeData(), 'bads native data' );
233 } catch ( MWException
$ex ) {
234 if ( !$shouldFail ) $this->fail( "ContentHandler::makeContent failed unexpectedly: " . $ex->getMessage() );
235 else $this->assertTrue( true ); // dummy, so we don't get the "test did not perform any assertions" message.
240 public function testSupportsSections() {
241 $this->markTestIncomplete( "not yet implemented" );
244 public function testRunLegacyHooks() {
245 Hooks
::register( 'testRunLegacyHooks', __CLASS__
. '::dummyHookHandler' );
247 $content = new WikitextContent( 'test text' );
248 $ok = ContentHandler
::runLegacyHooks( 'testRunLegacyHooks', array( 'foo', &$content, 'bar' ), false );
250 $this->assertTrue( $ok, "runLegacyHooks should have returned true" );
251 $this->assertEquals( "TEST TEXT", $content->getNativeData() );
254 public static function dummyHookHandler( $foo, &$text, $bar ) {
255 if ( $text === null ||
$text === false ) {
259 $text = strtoupper( $text );
265 class DummyContentHandlerForTesting
extends ContentHandler
{
267 public function __construct( $dataModel ) {
268 parent
::__construct( $dataModel, array( "testing" ) );
272 * Serializes Content object of the type supported by this ContentHandler.
274 * @param Content $content the Content object to serialize
275 * @param null $format the desired serialization format
276 * @return String serialized form of the content
278 public function serializeContent( Content
$content, $format = null )
280 return $content->serialize();
284 * Unserializes a Content object of the type supported by this ContentHandler.
286 * @param $blob String serialized form of the content
287 * @param null $format the format used for serialization
288 * @return Content the Content object created by deserializing $blob
290 public function unserializeContent( $blob, $format = null )
292 $d = unserialize( $blob );
293 return new DummyContentForTesting( $d );
297 * Creates an empty Content object of the type supported by this ContentHandler.
300 public function makeEmptyContent()
302 return new DummyContentForTesting( '' );
306 class DummyContentForTesting
extends AbstractContent
{
308 public function __construct( $data ) {
309 parent
::__construct( "testing" );
314 public function serialize( $format = null ) {
315 return serialize( $this->data
);
319 * @return String a string representing the content in a way useful for building a full text search index.
320 * If no useful representation exists, this method returns an empty string.
322 public function getTextForSearchIndex()
328 * @return String the wikitext to include when another page includes this content, or false if the content is not
329 * includable in a wikitext page.
331 public function getWikitextForTransclusion()
337 * Returns a textual representation of the content suitable for use in edit summaries and log messages.
339 * @param int $maxlength maximum length of the summary text
340 * @return String the summary text
342 public function getTextForSummary( $maxlength = 250 )
348 * Returns native represenation of the data. Interpretation depends on the data model used,
349 * as given by getDataModel().
351 * @return mixed the native representation of the content. Could be a string, a nested array
352 * structure, an object, a binary blob... anything, really.
354 public function getNativeData()
360 * returns the content's nominal size in bogo-bytes.
364 public function getSize()
366 return strlen( $this->data
);
370 * Return a copy of this Content object. The following must be true for the object returned
371 * if $copy = $original->copy()
373 * * get_class($original) === get_class($copy)
374 * * $original->getModel() === $copy->getModel()
375 * * $original->equals( $copy )
377 * If and only if the Content object is imutable, the copy() method can and should
378 * return $this. That is, $copy === $original may be true, but only for imutable content
381 * @return Content. A copy of this object
383 public function copy()
389 * Returns true if this content is countable as a "real" wiki page, provided
390 * that it's also in a countable location (e.g. a current revision in the main namespace).
392 * @param $hasLinks Bool: if it is known whether this content contains links, provide this information here,
393 * to avoid redundant parsing to find out.
396 public function isCountable( $hasLinks = null )
402 * @param Title $title
404 * @param null|ParserOptions $options
405 * @param Boolean $generateHtml whether to generate Html (default: true). If false,
406 * the result of calling getText() on the ParserOutput object returned by
407 * this method is undefined.
409 * @return ParserOutput
411 public function getParserOutput( Title
$title, $revId = null, ParserOptions
$options = NULL, $generateHtml = true )
413 return new ParserOutput( $this->getNativeData() );