4 * @group ContentHandler
7 * @note Declare that we are using the database, because otherwise we'll fail in
8 * the "databaseless" test run. This is because the LinkHolderArray used by the
9 * parser needs database access.
11 class ContentHandlerTest
extends MediaWikiTestCase
{
13 protected 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 protected 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
73 * @covers ContentHandler::getDefaultModelFor
75 public function testGetDefaultModelFor( $title, $expectedModelId ) {
76 $title = Title
::newFromText( $title );
77 $this->assertEquals( $expectedModelId, ContentHandler
::getDefaultModelFor( $title ) );
81 * @dataProvider dataGetDefaultModelFor
82 * @covers ContentHandler::getForTitle
84 public function testGetForTitle( $title, $expectedContentModel ) {
85 $title = Title
::newFromText( $title );
86 $handler = ContentHandler
::getForTitle( $title );
87 $this->assertEquals( $expectedContentModel, $handler->getModelID() );
90 public static function dataGetLocalizedName() {
93 array( "xyzzy", null ),
95 // XXX: depends on content language
96 array( CONTENT_MODEL_JAVASCRIPT
, '/javascript/i' ),
101 * @dataProvider dataGetLocalizedName
102 * @covers ContentHandler::getLocalizedName
104 public function testGetLocalizedName( $id, $expected ) {
105 $name = ContentHandler
::getLocalizedName( $id );
108 $this->assertNotNull( $name, "no name found for content model $id" );
109 $this->assertTrue( preg_match( $expected, $name ) > 0,
110 "content model name for #$id did not match pattern $expected"
113 $this->assertEquals( $id, $name, "localization of unknown model $id should have "
114 . "fallen back to use the model id directly."
119 public static function dataGetPageLanguage() {
120 global $wgLanguageCode;
123 array( "Main", $wgLanguageCode ),
124 array( "Dummy:Foo", $wgLanguageCode ),
125 array( "MediaWiki:common.js", 'en' ),
126 array( "User:Foo/common.js", 'en' ),
127 array( "MediaWiki:common.css", 'en' ),
128 array( "User:Foo/common.css", 'en' ),
129 array( "User:Foo", $wgLanguageCode ),
131 array( CONTENT_MODEL_JAVASCRIPT
, 'javascript' ),
136 * @dataProvider dataGetPageLanguage
137 * @covers ContentHandler::getPageLanguage
139 public function testGetPageLanguage( $title, $expected ) {
140 if ( is_string( $title ) ) {
141 $title = Title
::newFromText( $title );
144 $expected = wfGetLangObj( $expected );
146 $handler = ContentHandler
::getForTitle( $title );
147 $lang = $handler->getPageLanguage( $title );
149 $this->assertEquals( $expected->getCode(), $lang->getCode() );
152 public static function dataGetContentText_Null() {
155 array( 'serialize' ),
161 * @dataProvider dataGetContentText_Null
162 * @covers ContentHandler::getContentText
164 public function testGetContentText_Null( $contentHandlerTextFallback ) {
165 $this->setMwGlobals( 'wgContentHandlerTextFallback', $contentHandlerTextFallback );
169 $text = ContentHandler
::getContentText( $content );
170 $this->assertEquals( '', $text );
173 public static function dataGetContentText_TextContent() {
176 array( 'serialize' ),
182 * @dataProvider dataGetContentText_TextContent
183 * @covers ContentHandler::getContentText
185 public function testGetContentText_TextContent( $contentHandlerTextFallback ) {
186 $this->setMwGlobals( 'wgContentHandlerTextFallback', $contentHandlerTextFallback );
188 $content = new WikitextContent( "hello world" );
190 $text = ContentHandler
::getContentText( $content );
191 $this->assertEquals( $content->getNativeData(), $text );
195 * ContentHandler::getContentText should have thrown an exception for non-text Content object
196 * @expectedException MWException
197 * @covers ContentHandler::getContentText
199 public function testGetContentText_NonTextContent_fail() {
200 $this->setMwGlobals( 'wgContentHandlerTextFallback', 'fail' );
202 $content = new DummyContentForTesting( "hello world" );
204 ContentHandler
::getContentText( $content );
208 * @covers ContentHandler::getContentText
210 public function testGetContentText_NonTextContent_serialize() {
211 $this->setMwGlobals( 'wgContentHandlerTextFallback', 'serialize' );
213 $content = new DummyContentForTesting( "hello world" );
215 $text = ContentHandler
::getContentText( $content );
216 $this->assertEquals( $content->serialize(), $text );
220 * @covers ContentHandler::getContentText
222 public function testGetContentText_NonTextContent_ignore() {
223 $this->setMwGlobals( 'wgContentHandlerTextFallback', 'ignore' );
225 $content = new DummyContentForTesting( "hello world" );
227 $text = ContentHandler
::getContentText( $content );
228 $this->assertNull( $text );
232 public static function makeContent( $text, Title $title, $modelId = null, $format = null ) {}
235 public static function dataMakeContent() {
237 array( 'hallo', 'Help:Test', null, null, CONTENT_MODEL_WIKITEXT
, 'hallo', false ),
238 array( 'hallo', 'MediaWiki:Test.js', null, null, CONTENT_MODEL_JAVASCRIPT
, 'hallo', false ),
239 array( serialize( 'hallo' ), 'Dummy:Test', null, null, "testing", 'hallo', false ),
245 CONTENT_FORMAT_WIKITEXT
,
246 CONTENT_MODEL_WIKITEXT
,
254 CONTENT_FORMAT_JAVASCRIPT
,
255 CONTENT_MODEL_JAVASCRIPT
,
259 array( serialize( 'hallo' ), 'Dummy:Test', null, "testing", "testing", 'hallo', false ),
261 array( 'hallo', 'Help:Test', CONTENT_MODEL_CSS
, null, CONTENT_MODEL_CSS
, 'hallo', false ),
272 serialize( 'hallo' ),
277 serialize( 'hallo' ),
281 array( 'hallo', 'Help:Test', CONTENT_MODEL_WIKITEXT
, "testing", null, null, true ),
282 array( 'hallo', 'MediaWiki:Test.js', CONTENT_MODEL_CSS
, "testing", null, null, true ),
283 array( 'hallo', 'Dummy:Test', CONTENT_MODEL_JAVASCRIPT
, "testing", null, null, true ),
288 * @dataProvider dataMakeContent
289 * @covers ContentHandler::makeContent
291 public function testMakeContent( $data, $title, $modelId, $format,
292 $expectedModelId, $expectedNativeData, $shouldFail
294 $title = Title
::newFromText( $title );
297 $content = ContentHandler
::makeContent( $data, $title, $modelId, $format );
300 $this->fail( "ContentHandler::makeContent should have failed!" );
303 $this->assertEquals( $expectedModelId, $content->getModel(), 'bad model id' );
304 $this->assertEquals( $expectedNativeData, $content->getNativeData(), 'bads native data' );
305 } catch ( MWException
$ex ) {
306 if ( !$shouldFail ) {
307 $this->fail( "ContentHandler::makeContent failed unexpectedly: " . $ex->getMessage() );
309 // dummy, so we don't get the "test did not perform any assertions" message.
310 $this->assertTrue( true );
316 * Test if we become a "Created blank page" summary from getAutoSummary if no Content added to
319 public function testGetAutosummary() {
320 $this->setMwGlobals( 'wgContLang', Language
::factory( 'en' ) );
322 $content = new DummyContentHandlerForTesting( CONTENT_MODEL_WIKITEXT
);
323 $title = Title
::newFromText( 'Help:Test' );
324 // Create a new content object with no content
325 $newContent = ContentHandler
::makeContent( '', $title, null, null, CONTENT_MODEL_WIKITEXT
);
326 // first check, if we become a blank page created summary with the right bitmask
327 $autoSummary = $content->getAutosummary( null, $newContent, 97 );
328 $this->assertEquals( $autoSummary, 'Created blank page' );
329 // now check, what we become with another bitmask
330 $autoSummary = $content->getAutosummary( null, $newContent, 92 );
331 $this->assertEquals( $autoSummary, '' );
335 public function testSupportsSections() {
336 $this->markTestIncomplete( "not yet implemented" );
341 * @covers ContentHandler::runLegacyHooks
343 public function testRunLegacyHooks() {
344 Hooks
::register( 'testRunLegacyHooks', __CLASS__
. '::dummyHookHandler' );
346 $content = new WikitextContent( 'test text' );
347 $ok = ContentHandler
::runLegacyHooks(
348 'testRunLegacyHooks',
349 array( 'foo', &$content, 'bar' ),
353 $this->assertTrue( $ok, "runLegacyHooks should have returned true" );
354 $this->assertEquals( "TEST TEXT", $content->getNativeData() );
357 public static function dummyHookHandler( $foo, &$text, $bar ) {
358 if ( $text === null ||
$text === false ) {
362 $text = strtoupper( $text );
368 class DummyContentHandlerForTesting
extends ContentHandler
{
370 public function __construct( $dataModel ) {
371 parent
::__construct( $dataModel, array( "testing" ) );
375 * @see ContentHandler::serializeContent
377 * @param Content $content
378 * @param string $format
382 public function serializeContent( Content
$content, $format = null ) {
383 return $content->serialize();
387 * @see ContentHandler::unserializeContent
389 * @param string $blob
390 * @param string $format Unused.
394 public function unserializeContent( $blob, $format = null ) {
395 $d = unserialize( $blob );
397 return new DummyContentForTesting( $d );
401 * Creates an empty Content object of the type supported by this ContentHandler.
404 public function makeEmptyContent() {
405 return new DummyContentForTesting( '' );
409 class DummyContentForTesting
extends AbstractContent
{
411 public function __construct( $data ) {
412 parent
::__construct( "testing" );
417 public function serialize( $format = null ) {
418 return serialize( $this->data
);
422 * @return string A string representing the content in a way useful for
423 * building a full text search index. If no useful representation exists,
424 * this method returns an empty string.
426 public function getTextForSearchIndex() {
431 * @return string|bool The wikitext to include when another page includes this content,
432 * or false if the content is not includable in a wikitext page.
434 public function getWikitextForTransclusion() {
439 * Returns a textual representation of the content suitable for use in edit
440 * summaries and log messages.
442 * @param int $maxlength Maximum length of the summary text.
443 * @return string The summary text.
445 public function getTextForSummary( $maxlength = 250 ) {
450 * Returns native represenation of the data. Interpretation depends on the data model used,
451 * as given by getDataModel().
453 * @return mixed The native representation of the content. Could be a string, a nested array
454 * structure, an object, a binary blob... anything, really.
456 public function getNativeData() {
461 * returns the content's nominal size in bogo-bytes.
465 public function getSize() {
466 return strlen( $this->data
);
470 * Return a copy of this Content object. The following must be true for the object returned
471 * if $copy = $original->copy()
473 * * get_class($original) === get_class($copy)
474 * * $original->getModel() === $copy->getModel()
475 * * $original->equals( $copy )
477 * If and only if the Content object is imutable, the copy() method can and should
478 * return $this. That is, $copy === $original may be true, but only for imutable content
481 * @return Content A copy of this object
483 public function copy() {
488 * Returns true if this content is countable as a "real" wiki page, provided
489 * that it's also in a countable location (e.g. a current revision in the main namespace).
491 * @param bool $hasLinks If it is known whether this content contains links,
492 * provide this information here, to avoid redundant parsing to find out.
495 public function isCountable( $hasLinks = null ) {
500 * @param Title $title
501 * @param int $revId Unused.
502 * @param null|ParserOptions $options
503 * @param bool $generateHtml Whether to generate Html (default: true). If false, the result
504 * of calling getText() on the ParserOutput object returned by this method is undefined.
506 * @return ParserOutput
508 public function getParserOutput( Title
$title, $revId = null,
509 ParserOptions
$options = null, $generateHtml = true
511 return new ParserOutput( $this->getNativeData() );
515 * @see AbstractContent::fillParserOutput()
517 * @param Title $title Context title for parsing
518 * @param int|null $revId Revision ID (for {{REVISIONID}})
519 * @param ParserOptions $options Parser options
520 * @param bool $generateHtml Whether or not to generate HTML
521 * @param ParserOutput &$output The output object to fill (reference).
523 protected function fillParserOutput( Title
$title, $revId,
524 ParserOptions
$options, $generateHtml, ParserOutput
&$output ) {
525 $output = new ParserOutput( $this->getNativeData() );