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() {
44 //NOTE: assume that the Help namespace default to wikitext content
46 array( 'Help:Foo', CONTENT_MODEL_WIKITEXT
),
47 array( 'Help:Foo.js', CONTENT_MODEL_WIKITEXT
),
48 array( 'Help:Foo/bar.js', CONTENT_MODEL_WIKITEXT
),
49 array( 'User:Foo', CONTENT_MODEL_WIKITEXT
),
50 array( 'User:Foo.js', CONTENT_MODEL_WIKITEXT
),
51 array( 'User:Foo/bar.js', CONTENT_MODEL_JAVASCRIPT
),
52 array( 'User:Foo/bar.css', CONTENT_MODEL_CSS
),
53 array( 'User talk:Foo/bar.css', CONTENT_MODEL_WIKITEXT
),
54 array( 'User:Foo/bar.js.xxx', CONTENT_MODEL_WIKITEXT
),
55 array( 'User:Foo/bar.xxx', CONTENT_MODEL_WIKITEXT
),
56 array( 'MediaWiki:Foo.js', CONTENT_MODEL_JAVASCRIPT
),
57 array( 'MediaWiki:Foo.css', CONTENT_MODEL_CSS
),
58 array( 'MediaWiki:Foo.JS', CONTENT_MODEL_WIKITEXT
),
59 array( 'MediaWiki:Foo.CSS', CONTENT_MODEL_WIKITEXT
),
60 array( 'MediaWiki:Foo.css.xxx', CONTENT_MODEL_WIKITEXT
),
65 * @dataProvider dataGetDefaultModelFor
67 public function testGetDefaultModelFor( $title, $expectedModelId ) {
68 $title = Title
::newFromText( $title );
69 $this->assertEquals( $expectedModelId, ContentHandler
::getDefaultModelFor( $title ) );
72 * @dataProvider dataGetDefaultModelFor
74 public function testGetForTitle( $title, $expectedContentModel ) {
75 $title = Title
::newFromText( $title );
76 $handler = ContentHandler
::getForTitle( $title );
77 $this->assertEquals( $expectedContentModel, $handler->getModelID() );
80 public function dataGetLocalizedName() {
83 array( "xyzzy", null ),
85 array( CONTENT_MODEL_JAVASCRIPT
, '/javascript/i' ), //XXX: depends on content language
90 * @dataProvider dataGetLocalizedName
92 public function testGetLocalizedName( $id, $expected ) {
93 $name = ContentHandler
::getLocalizedName( $id );
96 $this->assertNotNull( $name, "no name found for content model $id" );
97 $this->assertTrue( preg_match( $expected, $name ) > 0 ,
98 "content model name for #$id did not match pattern $expected" );
100 $this->assertEquals( $id, $name, "localization of unknown model $id should have "
101 . "fallen back to use the model id directly." );
105 public function dataGetPageLanguage() {
106 global $wgLanguageCode;
109 array( "Main", $wgLanguageCode ),
110 array( "Dummy:Foo", $wgLanguageCode ),
111 array( "MediaWiki:common.js", 'en' ),
112 array( "User:Foo/common.js", 'en' ),
113 array( "MediaWiki:common.css", 'en' ),
114 array( "User:Foo/common.css", 'en' ),
115 array( "User:Foo", $wgLanguageCode ),
117 array( CONTENT_MODEL_JAVASCRIPT
, 'javascript' ),
122 * @dataProvider dataGetPageLanguage
124 public function testGetPageLanguage( $title, $expected ) {
125 if ( is_string( $title ) ) {
126 $title = Title
::newFromText( $title );
129 $expected = wfGetLangObj( $expected );
131 $handler = ContentHandler
::getForTitle( $title );
132 $lang = $handler->getPageLanguage( $title );
134 $this->assertEquals( $expected->getCode(), $lang->getCode() );
137 public function testGetContentText_Null( ) {
138 global $wgContentHandlerTextFallback;
142 $wgContentHandlerTextFallback = 'fail';
143 $text = ContentHandler
::getContentText( $content );
144 $this->assertEquals( '', $text );
146 $wgContentHandlerTextFallback = 'serialize';
147 $text = ContentHandler
::getContentText( $content );
148 $this->assertEquals( '', $text );
150 $wgContentHandlerTextFallback = 'ignore';
151 $text = ContentHandler
::getContentText( $content );
152 $this->assertEquals( '', $text );
155 public function testGetContentText_TextContent( ) {
156 global $wgContentHandlerTextFallback;
158 $content = new WikitextContent( "hello world" );
160 $wgContentHandlerTextFallback = 'fail';
161 $text = ContentHandler
::getContentText( $content );
162 $this->assertEquals( $content->getNativeData(), $text );
164 $wgContentHandlerTextFallback = 'serialize';
165 $text = ContentHandler
::getContentText( $content );
166 $this->assertEquals( $content->serialize(), $text );
168 $wgContentHandlerTextFallback = 'ignore';
169 $text = ContentHandler
::getContentText( $content );
170 $this->assertEquals( $content->getNativeData(), $text );
173 public function testGetContentText_NonTextContent( ) {
174 global $wgContentHandlerTextFallback;
176 $content = new DummyContentForTesting( "hello world" );
178 $wgContentHandlerTextFallback = 'fail';
181 $text = ContentHandler
::getContentText( $content );
183 $this->fail( "ContentHandler::getContentText should have thrown an exception for non-text Content object" );
184 } catch (MWException
$ex) {
188 $wgContentHandlerTextFallback = 'serialize';
189 $text = ContentHandler
::getContentText( $content );
190 $this->assertEquals( $content->serialize(), $text );
192 $wgContentHandlerTextFallback = 'ignore';
193 $text = ContentHandler
::getContentText( $content );
194 $this->assertNull( $text );
197 #public static function makeContent( $text, Title $title, $modelId = null, $format = null )
199 public function dataMakeContent() {
200 //NOTE: assume the Help namespace defaults to wikitext content
202 array( 'hallo', 'Help:Test', null, null, CONTENT_MODEL_WIKITEXT
, 'hallo', false ),
203 array( 'hallo', 'MediaWiki:Test.js', null, null, CONTENT_MODEL_JAVASCRIPT
, 'hallo', false ),
204 array( serialize('hallo'), 'Dummy:Test', null, null, "testing", 'hallo', false ),
206 array( 'hallo', 'Help:Test', null, CONTENT_FORMAT_WIKITEXT
, CONTENT_MODEL_WIKITEXT
, 'hallo', false ),
207 array( 'hallo', 'MediaWiki:Test.js', null, CONTENT_FORMAT_JAVASCRIPT
, CONTENT_MODEL_JAVASCRIPT
, 'hallo', false ),
208 array( serialize('hallo'), 'Dummy:Test', null, "testing", "testing", 'hallo', false ),
210 array( 'hallo', 'Help:Test', CONTENT_MODEL_CSS
, null, CONTENT_MODEL_CSS
, 'hallo', false ),
211 array( 'hallo', 'MediaWiki:Test.js', CONTENT_MODEL_CSS
, null, CONTENT_MODEL_CSS
, 'hallo', false ),
212 array( serialize('hallo'), 'Dummy:Test', CONTENT_MODEL_CSS
, null, CONTENT_MODEL_CSS
, serialize('hallo'), false ),
214 array( 'hallo', 'Help:Test', CONTENT_MODEL_WIKITEXT
, "testing", null, null, true ),
215 array( 'hallo', 'MediaWiki:Test.js', CONTENT_MODEL_CSS
, "testing", null, null, true ),
216 array( 'hallo', 'Dummy:Test', CONTENT_MODEL_JAVASCRIPT
, "testing", null, null, true ),
221 * @dataProvider dataMakeContent
223 public function testMakeContent( $data, $title, $modelId, $format, $expectedModelId, $expectedNativeData, $shouldFail ) {
224 global $wgExtraNamespaces, $wgNamespaceContentModels, $wgContentHandlers;
226 $title = Title
::newFromText( $title );
229 $content = ContentHandler
::makeContent( $data, $title, $modelId, $format );
231 if ( $shouldFail ) $this->fail( "ContentHandler::makeContent should have failed!" );
233 $this->assertEquals( $expectedModelId, $content->getModel(), 'bad model id' );
234 $this->assertEquals( $expectedNativeData, $content->getNativeData(), 'bads native data' );
235 } catch ( MWException
$ex ) {
236 if ( !$shouldFail ) $this->fail( "ContentHandler::makeContent failed unexpectedly: " . $ex->getMessage() );
237 else $this->assertTrue( true ); // dummy, so we don't get the "test did not perform any assertions" message.
242 public function testSupportsSections() {
243 $this->markTestIncomplete( "not yet implemented" );
246 public function testRunLegacyHooks() {
247 Hooks
::register( 'testRunLegacyHooks', __CLASS__
. '::dummyHookHandler' );
249 $content = new WikitextContent( 'test text' );
250 $ok = ContentHandler
::runLegacyHooks( 'testRunLegacyHooks', array( 'foo', &$content, 'bar' ), false );
252 $this->assertTrue( $ok, "runLegacyHooks should have returned true" );
253 $this->assertEquals( "TEST TEXT", $content->getNativeData() );
256 public static function dummyHookHandler( $foo, &$text, $bar ) {
257 if ( $text === null ||
$text === false ) {
261 $text = strtoupper( $text );
267 class DummyContentHandlerForTesting
extends ContentHandler
{
269 public function __construct( $dataModel ) {
270 parent
::__construct( $dataModel, array( "testing" ) );
274 * Serializes Content object of the type supported by this ContentHandler.
276 * @param Content $content the Content object to serialize
277 * @param null $format the desired serialization format
278 * @return String serialized form of the content
280 public function serializeContent( Content
$content, $format = null )
282 return $content->serialize();
286 * Unserializes a Content object of the type supported by this ContentHandler.
288 * @param $blob String serialized form of the content
289 * @param null $format the format used for serialization
290 * @return Content the Content object created by deserializing $blob
292 public function unserializeContent( $blob, $format = null )
294 $d = unserialize( $blob );
295 return new DummyContentForTesting( $d );
299 * Creates an empty Content object of the type supported by this ContentHandler.
302 public function makeEmptyContent()
304 return new DummyContentForTesting( '' );
308 class DummyContentForTesting
extends AbstractContent
{
310 public function __construct( $data ) {
311 parent
::__construct( "testing" );
316 public function serialize( $format = null ) {
317 return serialize( $this->data
);
321 * @return String a string representing the content in a way useful for building a full text search index.
322 * If no useful representation exists, this method returns an empty string.
324 public function getTextForSearchIndex()
330 * @return String the wikitext to include when another page includes this content, or false if the content is not
331 * includable in a wikitext page.
333 public function getWikitextForTransclusion()
339 * Returns a textual representation of the content suitable for use in edit summaries and log messages.
341 * @param int $maxlength maximum length of the summary text
342 * @return String the summary text
344 public function getTextForSummary( $maxlength = 250 )
350 * Returns native represenation of the data. Interpretation depends on the data model used,
351 * as given by getDataModel().
353 * @return mixed the native representation of the content. Could be a string, a nested array
354 * structure, an object, a binary blob... anything, really.
356 public function getNativeData()
362 * returns the content's nominal size in bogo-bytes.
366 public function getSize()
368 return strlen( $this->data
);
372 * Return a copy of this Content object. The following must be true for the object returned
373 * if $copy = $original->copy()
375 * * get_class($original) === get_class($copy)
376 * * $original->getModel() === $copy->getModel()
377 * * $original->equals( $copy )
379 * If and only if the Content object is imutable, the copy() method can and should
380 * return $this. That is, $copy === $original may be true, but only for imutable content
383 * @return Content. A copy of this object
385 public function copy()
391 * Returns true if this content is countable as a "real" wiki page, provided
392 * that it's also in a countable location (e.g. a current revision in the main namespace).
394 * @param $hasLinks Bool: if it is known whether this content contains links, provide this information here,
395 * to avoid redundant parsing to find out.
398 public function isCountable( $hasLinks = null )
404 * @param Title $title
406 * @param null|ParserOptions $options
407 * @param Boolean $generateHtml whether to generate Html (default: true). If false,
408 * the result of calling getText() on the ParserOutput object returned by
409 * this method is undefined.
411 * @return ParserOutput
413 public function getParserOutput( Title
$title, $revId = null, ParserOptions
$options = NULL, $generateHtml = true )
415 return new ParserOutput( $this->getNativeData() );