4 * @group ContentHandler
7 * @note Declare that we are using the database, because otherwise we'll fail in the "databaseless" test run.
8 * This is because the LinkHolderArray used by the parser needs database access.
11 class ContentHandlerTest
extends MediaWikiTestCase
{
13 public function setUp() {
17 $this->setMwGlobals( array(
18 'wgExtraNamespaces' => array(
20 12313 => 'Dummy_talk',
22 // The below tests assume that namespaces not mentioned here (Help, User, MediaWiki, ..)
23 // default to CONTENT_MODEL_WIKITEXT.
24 'wgNamespaceContentModels' => array(
27 'wgContentHandlers' => array(
28 CONTENT_MODEL_WIKITEXT
=> 'WikitextContentHandler',
29 CONTENT_MODEL_JAVASCRIPT
=> 'JavaScriptContentHandler',
30 CONTENT_MODEL_CSS
=> 'CssContentHandler',
31 CONTENT_MODEL_TEXT
=> 'TextContentHandler',
32 'testing' => 'DummyContentHandlerForTesting',
36 // Reset namespace cache
37 MWNamespace
::getCanonicalNamespaces( true );
38 $wgContLang->resetNamespaces();
41 public function tearDown() {
44 // Reset namespace cache
45 MWNamespace
::getCanonicalNamespaces( true );
46 $wgContLang->resetNamespaces();
51 public static function dataGetDefaultModelFor() {
53 array( 'Help:Foo', CONTENT_MODEL_WIKITEXT
),
54 array( 'Help:Foo.js', CONTENT_MODEL_WIKITEXT
),
55 array( 'Help:Foo/bar.js', CONTENT_MODEL_WIKITEXT
),
56 array( 'User:Foo', CONTENT_MODEL_WIKITEXT
),
57 array( 'User:Foo.js', CONTENT_MODEL_WIKITEXT
),
58 array( 'User:Foo/bar.js', CONTENT_MODEL_JAVASCRIPT
),
59 array( 'User:Foo/bar.css', CONTENT_MODEL_CSS
),
60 array( 'User talk:Foo/bar.css', CONTENT_MODEL_WIKITEXT
),
61 array( 'User:Foo/bar.js.xxx', CONTENT_MODEL_WIKITEXT
),
62 array( 'User:Foo/bar.xxx', CONTENT_MODEL_WIKITEXT
),
63 array( 'MediaWiki:Foo.js', CONTENT_MODEL_JAVASCRIPT
),
64 array( 'MediaWiki:Foo.css', CONTENT_MODEL_CSS
),
65 array( 'MediaWiki:Foo.JS', CONTENT_MODEL_WIKITEXT
),
66 array( 'MediaWiki:Foo.CSS', CONTENT_MODEL_WIKITEXT
),
67 array( 'MediaWiki:Foo.css.xxx', CONTENT_MODEL_WIKITEXT
),
72 * @dataProvider dataGetDefaultModelFor
74 public function testGetDefaultModelFor( $title, $expectedModelId ) {
75 $title = Title
::newFromText( $title );
76 $this->assertEquals( $expectedModelId, ContentHandler
::getDefaultModelFor( $title ) );
80 * @dataProvider dataGetDefaultModelFor
82 public function testGetForTitle( $title, $expectedContentModel ) {
83 $title = Title
::newFromText( $title );
84 $handler = ContentHandler
::getForTitle( $title );
85 $this->assertEquals( $expectedContentModel, $handler->getModelID() );
88 public static function dataGetLocalizedName() {
91 array( "xyzzy", null ),
93 // XXX: depends on content language
94 array( CONTENT_MODEL_JAVASCRIPT
, '/javascript/i' ),
99 * @dataProvider dataGetLocalizedName
101 public function testGetLocalizedName( $id, $expected ) {
102 $name = ContentHandler
::getLocalizedName( $id );
105 $this->assertNotNull( $name, "no name found for content model $id" );
106 $this->assertTrue( preg_match( $expected, $name ) > 0,
107 "content model name for #$id did not match pattern $expected"
110 $this->assertEquals( $id, $name, "localization of unknown model $id should have "
111 . "fallen back to use the model id directly."
116 public static function dataGetPageLanguage() {
117 global $wgLanguageCode;
120 array( "Main", $wgLanguageCode ),
121 array( "Dummy:Foo", $wgLanguageCode ),
122 array( "MediaWiki:common.js", 'en' ),
123 array( "User:Foo/common.js", 'en' ),
124 array( "MediaWiki:common.css", 'en' ),
125 array( "User:Foo/common.css", 'en' ),
126 array( "User:Foo", $wgLanguageCode ),
128 array( CONTENT_MODEL_JAVASCRIPT
, 'javascript' ),
133 * @dataProvider dataGetPageLanguage
135 public function testGetPageLanguage( $title, $expected ) {
136 if ( is_string( $title ) ) {
137 $title = Title
::newFromText( $title );
140 $expected = wfGetLangObj( $expected );
142 $handler = ContentHandler
::getForTitle( $title );
143 $lang = $handler->getPageLanguage( $title );
145 $this->assertEquals( $expected->getCode(), $lang->getCode() );
148 public static function dataGetContentText_Null() {
151 array( 'serialize' ),
157 * @dataProvider dataGetContentText_Null
159 public function testGetContentText_Null( $contentHandlerTextFallback ) {
160 $this->setMwGlobals( 'wgContentHandlerTextFallback', $contentHandlerTextFallback );
164 $text = ContentHandler
::getContentText( $content );
165 $this->assertEquals( '', $text );
168 public static function dataGetContentText_TextContent() {
171 array( 'serialize' ),
177 * @dataProvider dataGetContentText_TextContent
179 public function testGetContentText_TextContent( $contentHandlerTextFallback ) {
180 $this->setMwGlobals( 'wgContentHandlerTextFallback', $contentHandlerTextFallback );
182 $content = new WikitextContent( "hello world" );
184 $text = ContentHandler
::getContentText( $content );
185 $this->assertEquals( $content->getNativeData(), $text );
189 * ContentHandler::getContentText should have thrown an exception for non-text Content object
190 * @expectedException MWException
192 public function testGetContentText_NonTextContent_fail() {
193 $this->setMwGlobals( 'wgContentHandlerTextFallback', 'fail' );
195 $content = new DummyContentForTesting( "hello world" );
197 ContentHandler
::getContentText( $content );
200 public function testGetContentText_NonTextContent_serialize() {
201 $this->setMwGlobals( 'wgContentHandlerTextFallback', 'serialize' );
203 $content = new DummyContentForTesting( "hello world" );
205 $text = ContentHandler
::getContentText( $content );
206 $this->assertEquals( $content->serialize(), $text );
209 public function testGetContentText_NonTextContent_ignore() {
210 $this->setMwGlobals( 'wgContentHandlerTextFallback', 'ignore' );
212 $content = new DummyContentForTesting( "hello world" );
214 $text = ContentHandler
::getContentText( $content );
215 $this->assertNull( $text );
219 public static function makeContent( $text, Title $title, $modelId = null, $format = null ) {}
222 public static function dataMakeContent() {
224 array( 'hallo', 'Help:Test', null, null, CONTENT_MODEL_WIKITEXT
, 'hallo', false ),
225 array( 'hallo', 'MediaWiki:Test.js', null, null, CONTENT_MODEL_JAVASCRIPT
, 'hallo', false ),
226 array( serialize( 'hallo' ), 'Dummy:Test', null, null, "testing", 'hallo', false ),
228 array( 'hallo', 'Help:Test', null, CONTENT_FORMAT_WIKITEXT
, CONTENT_MODEL_WIKITEXT
, 'hallo', false ),
229 array( 'hallo', 'MediaWiki:Test.js', null, CONTENT_FORMAT_JAVASCRIPT
, CONTENT_MODEL_JAVASCRIPT
, 'hallo', false ),
230 array( serialize( 'hallo' ), 'Dummy:Test', null, "testing", "testing", 'hallo', false ),
232 array( 'hallo', 'Help:Test', CONTENT_MODEL_CSS
, null, CONTENT_MODEL_CSS
, 'hallo', false ),
233 array( 'hallo', 'MediaWiki:Test.js', CONTENT_MODEL_CSS
, null, CONTENT_MODEL_CSS
, 'hallo', false ),
234 array( serialize( 'hallo' ), 'Dummy:Test', CONTENT_MODEL_CSS
, null, CONTENT_MODEL_CSS
, serialize( 'hallo' ), false ),
236 array( 'hallo', 'Help:Test', CONTENT_MODEL_WIKITEXT
, "testing", null, null, true ),
237 array( 'hallo', 'MediaWiki:Test.js', CONTENT_MODEL_CSS
, "testing", null, null, true ),
238 array( 'hallo', 'Dummy:Test', CONTENT_MODEL_JAVASCRIPT
, "testing", null, null, true ),
243 * @dataProvider dataMakeContent
245 public function testMakeContent( $data, $title, $modelId, $format, $expectedModelId, $expectedNativeData, $shouldFail ) {
246 $title = Title
::newFromText( $title );
249 $content = ContentHandler
::makeContent( $data, $title, $modelId, $format );
252 $this->fail( "ContentHandler::makeContent should have failed!" );
255 $this->assertEquals( $expectedModelId, $content->getModel(), 'bad model id' );
256 $this->assertEquals( $expectedNativeData, $content->getNativeData(), 'bads native data' );
257 } catch ( MWException
$ex ) {
258 if ( !$shouldFail ) {
259 $this->fail( "ContentHandler::makeContent failed unexpectedly: " . $ex->getMessage() );
261 // dummy, so we don't get the "test did not perform any assertions" message.
262 $this->assertTrue( true );
268 public function testSupportsSections() {
269 $this->markTestIncomplete( "not yet implemented" );
273 public function testRunLegacyHooks() {
274 Hooks
::register( 'testRunLegacyHooks', __CLASS__
. '::dummyHookHandler' );
276 $content = new WikitextContent( 'test text' );
277 $ok = ContentHandler
::runLegacyHooks( 'testRunLegacyHooks', array( 'foo', &$content, 'bar' ), false );
279 $this->assertTrue( $ok, "runLegacyHooks should have returned true" );
280 $this->assertEquals( "TEST TEXT", $content->getNativeData() );
283 public static function dummyHookHandler( $foo, &$text, $bar ) {
284 if ( $text === null ||
$text === false ) {
288 $text = strtoupper( $text );
294 class DummyContentHandlerForTesting
extends ContentHandler
{
296 public function __construct( $dataModel ) {
297 parent
::__construct( $dataModel, array( "testing" ) );
301 * Serializes Content object of the type supported by this ContentHandler.
303 * @param Content $content the Content object to serialize
304 * @param null $format the desired serialization format
305 * @return String serialized form of the content
307 public function serializeContent( Content
$content, $format = null ) {
308 return $content->serialize();
312 * Unserializes a Content object of the type supported by this ContentHandler.
314 * @param $blob String serialized form of the content
315 * @param null $format the format used for serialization
316 * @return Content the Content object created by deserializing $blob
318 public function unserializeContent( $blob, $format = null ) {
319 $d = unserialize( $blob );
321 return new DummyContentForTesting( $d );
325 * Creates an empty Content object of the type supported by this ContentHandler.
328 public function makeEmptyContent() {
329 return new DummyContentForTesting( '' );
333 class DummyContentForTesting
extends AbstractContent
{
335 public function __construct( $data ) {
336 parent
::__construct( "testing" );
341 public function serialize( $format = null ) {
342 return serialize( $this->data
);
346 * @return String a string representing the content in a way useful for building a full text search index.
347 * If no useful representation exists, this method returns an empty string.
349 public function getTextForSearchIndex() {
354 * @return String the wikitext to include when another page includes this content, or false if the content is not
355 * includable in a wikitext page.
357 public function getWikitextForTransclusion() {
362 * Returns a textual representation of the content suitable for use in edit summaries and log messages.
364 * @param int $maxlength Maximum length of the summary text.
365 * @return string The summary text.
367 public function getTextForSummary( $maxlength = 250 ) {
372 * Returns native represenation of the data. Interpretation depends on the data model used,
373 * as given by getDataModel().
375 * @return mixed the native representation of the content. Could be a string, a nested array
376 * structure, an object, a binary blob... anything, really.
378 public function getNativeData() {
383 * returns the content's nominal size in bogo-bytes.
387 public function getSize() {
388 return strlen( $this->data
);
392 * Return a copy of this Content object. The following must be true for the object returned
393 * if $copy = $original->copy()
395 * * get_class($original) === get_class($copy)
396 * * $original->getModel() === $copy->getModel()
397 * * $original->equals( $copy )
399 * If and only if the Content object is imutable, the copy() method can and should
400 * return $this. That is, $copy === $original may be true, but only for imutable content
403 * @return Content. A copy of this object.
405 public function copy() {
410 * Returns true if this content is countable as a "real" wiki page, provided
411 * that it's also in a countable location (e.g. a current revision in the main namespace).
413 * @param boolean $hasLinks if it is known whether this content contains links, provide this information here,
414 * to avoid redundant parsing to find out.
417 public function isCountable( $hasLinks = null ) {
422 * @param Title $title
424 * @param null|ParserOptions $options
425 * @param boolean $generateHtml whether to generate Html (default: true). If false,
426 * the result of calling getText() on the ParserOutput object returned by
427 * this method is undefined.
429 * @return ParserOutput
431 public function getParserOutput( Title
$title, $revId = null, ParserOptions
$options = null, $generateHtml = true ) {
432 return new ParserOutput( $this->getNativeData() );