4 * @group ContentHandler
6 class ContentHandlerTest
extends MediaWikiTestCase
{
8 protected function setUp() {
12 $this->setMwGlobals( array(
13 'wgExtraNamespaces' => array(
15 12313 => 'Dummy_talk',
17 // The below tests assume that namespaces not mentioned here (Help, User, MediaWiki, ..)
18 // default to CONTENT_MODEL_WIKITEXT.
19 'wgNamespaceContentModels' => array(
22 'wgContentHandlers' => array(
23 CONTENT_MODEL_WIKITEXT
=> 'WikitextContentHandler',
24 CONTENT_MODEL_JAVASCRIPT
=> 'JavaScriptContentHandler',
25 CONTENT_MODEL_CSS
=> 'CssContentHandler',
26 CONTENT_MODEL_TEXT
=> 'TextContentHandler',
27 'testing' => 'DummyContentHandlerForTesting',
31 // Reset namespace cache
32 MWNamespace
::getCanonicalNamespaces( true );
33 $wgContLang->resetNamespaces();
35 LinkCache
::destroySingleton();
38 protected function tearDown() {
41 // Reset namespace cache
42 MWNamespace
::getCanonicalNamespaces( true );
43 $wgContLang->resetNamespaces();
45 LinkCache
::destroySingleton();
50 public static function dataGetDefaultModelFor() {
52 array( 'Help:Foo', CONTENT_MODEL_WIKITEXT
),
53 array( 'Help:Foo.js', CONTENT_MODEL_WIKITEXT
),
54 array( 'Help:Foo/bar.js', CONTENT_MODEL_WIKITEXT
),
55 array( 'User:Foo', CONTENT_MODEL_WIKITEXT
),
56 array( 'User:Foo.js', CONTENT_MODEL_WIKITEXT
),
57 array( 'User:Foo/bar.js', CONTENT_MODEL_JAVASCRIPT
),
58 array( 'User:Foo/bar.css', CONTENT_MODEL_CSS
),
59 array( 'User talk:Foo/bar.css', CONTENT_MODEL_WIKITEXT
),
60 array( 'User:Foo/bar.js.xxx', CONTENT_MODEL_WIKITEXT
),
61 array( 'User:Foo/bar.xxx', CONTENT_MODEL_WIKITEXT
),
62 array( 'MediaWiki:Foo.js', CONTENT_MODEL_JAVASCRIPT
),
63 array( 'MediaWiki:Foo.css', CONTENT_MODEL_CSS
),
64 array( 'MediaWiki:Foo.JS', CONTENT_MODEL_WIKITEXT
),
65 array( 'MediaWiki:Foo.CSS', CONTENT_MODEL_WIKITEXT
),
66 array( 'MediaWiki:Foo.css.xxx', CONTENT_MODEL_WIKITEXT
),
71 * @dataProvider dataGetDefaultModelFor
72 * @covers ContentHandler::getDefaultModelFor
74 public function testGetDefaultModelFor( $title, $expectedModelId ) {
75 $title = Title
::newFromText( $title );
76 $this->assertEquals( $expectedModelId, ContentHandler
::getDefaultModelFor( $title ) );
80 * @dataProvider dataGetDefaultModelFor
81 * @covers ContentHandler::getForTitle
83 public function testGetForTitle( $title, $expectedContentModel ) {
84 $title = Title
::newFromText( $title );
85 LinkCache
::singleton()->addBadLinkObj( $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 );
142 LinkCache
::singleton()->addBadLinkObj( $title );
145 $expected = wfGetLangObj( $expected );
147 $handler = ContentHandler
::getForTitle( $title );
148 $lang = $handler->getPageLanguage( $title );
150 $this->assertEquals( $expected->getCode(), $lang->getCode() );
153 public static function dataGetContentText_Null() {
156 array( 'serialize' ),
162 * @dataProvider dataGetContentText_Null
163 * @covers ContentHandler::getContentText
165 public function testGetContentText_Null( $contentHandlerTextFallback ) {
166 $this->setMwGlobals( 'wgContentHandlerTextFallback', $contentHandlerTextFallback );
170 $text = ContentHandler
::getContentText( $content );
171 $this->assertEquals( '', $text );
174 public static function dataGetContentText_TextContent() {
177 array( 'serialize' ),
183 * @dataProvider dataGetContentText_TextContent
184 * @covers ContentHandler::getContentText
186 public function testGetContentText_TextContent( $contentHandlerTextFallback ) {
187 $this->setMwGlobals( 'wgContentHandlerTextFallback', $contentHandlerTextFallback );
189 $content = new WikitextContent( "hello world" );
191 $text = ContentHandler
::getContentText( $content );
192 $this->assertEquals( $content->getNativeData(), $text );
196 * ContentHandler::getContentText should have thrown an exception for non-text Content object
197 * @expectedException MWException
198 * @covers ContentHandler::getContentText
200 public function testGetContentText_NonTextContent_fail() {
201 $this->setMwGlobals( 'wgContentHandlerTextFallback', 'fail' );
203 $content = new DummyContentForTesting( "hello world" );
205 ContentHandler
::getContentText( $content );
209 * @covers ContentHandler::getContentText
211 public function testGetContentText_NonTextContent_serialize() {
212 $this->setMwGlobals( 'wgContentHandlerTextFallback', 'serialize' );
214 $content = new DummyContentForTesting( "hello world" );
216 $text = ContentHandler
::getContentText( $content );
217 $this->assertEquals( $content->serialize(), $text );
221 * @covers ContentHandler::getContentText
223 public function testGetContentText_NonTextContent_ignore() {
224 $this->setMwGlobals( 'wgContentHandlerTextFallback', 'ignore' );
226 $content = new DummyContentForTesting( "hello world" );
228 $text = ContentHandler
::getContentText( $content );
229 $this->assertNull( $text );
233 public static function makeContent( $text, Title $title, $modelId = null, $format = null ) {}
236 public static function dataMakeContent() {
238 array( 'hallo', 'Help:Test', null, null, CONTENT_MODEL_WIKITEXT
, 'hallo', false ),
239 array( 'hallo', 'MediaWiki:Test.js', null, null, CONTENT_MODEL_JAVASCRIPT
, 'hallo', false ),
240 array( serialize( 'hallo' ), 'Dummy:Test', null, null, "testing", 'hallo', false ),
246 CONTENT_FORMAT_WIKITEXT
,
247 CONTENT_MODEL_WIKITEXT
,
255 CONTENT_FORMAT_JAVASCRIPT
,
256 CONTENT_MODEL_JAVASCRIPT
,
260 array( serialize( 'hallo' ), 'Dummy:Test', null, "testing", "testing", 'hallo', false ),
262 array( 'hallo', 'Help:Test', CONTENT_MODEL_CSS
, null, CONTENT_MODEL_CSS
, 'hallo', false ),
273 serialize( 'hallo' ),
278 serialize( 'hallo' ),
282 array( 'hallo', 'Help:Test', CONTENT_MODEL_WIKITEXT
, "testing", null, null, true ),
283 array( 'hallo', 'MediaWiki:Test.js', CONTENT_MODEL_CSS
, "testing", null, null, true ),
284 array( 'hallo', 'Dummy:Test', CONTENT_MODEL_JAVASCRIPT
, "testing", null, null, true ),
289 * @dataProvider dataMakeContent
290 * @covers ContentHandler::makeContent
292 public function testMakeContent( $data, $title, $modelId, $format,
293 $expectedModelId, $expectedNativeData, $shouldFail
295 $title = Title
::newFromText( $title );
296 LinkCache
::singleton()->addBadLinkObj( $title );
298 $content = ContentHandler
::makeContent( $data, $title, $modelId, $format );
301 $this->fail( "ContentHandler::makeContent should have failed!" );
304 $this->assertEquals( $expectedModelId, $content->getModel(), 'bad model id' );
305 $this->assertEquals( $expectedNativeData, $content->getNativeData(), 'bads native data' );
306 } catch ( MWException
$ex ) {
307 if ( !$shouldFail ) {
308 $this->fail( "ContentHandler::makeContent failed unexpectedly: " . $ex->getMessage() );
310 // dummy, so we don't get the "test did not perform any assertions" message.
311 $this->assertTrue( true );
317 * Test if we become a "Created blank page" summary from getAutoSummary if no Content added to
320 public function testGetAutosummary() {
321 $this->setMwGlobals( 'wgContLang', Language
::factory( 'en' ) );
323 $content = new DummyContentHandlerForTesting( CONTENT_MODEL_WIKITEXT
);
324 $title = Title
::newFromText( 'Help:Test' );
325 // Create a new content object with no content
326 $newContent = ContentHandler
::makeContent( '', $title, null, null, CONTENT_MODEL_WIKITEXT
);
327 // first check, if we become a blank page created summary with the right bitmask
328 $autoSummary = $content->getAutosummary( null, $newContent, 97 );
329 $this->assertEquals( $autoSummary, 'Created blank page' );
330 // now check, what we become with another bitmask
331 $autoSummary = $content->getAutosummary( null, $newContent, 92 );
332 $this->assertEquals( $autoSummary, '' );
336 public function testSupportsSections() {
337 $this->markTestIncomplete( "not yet implemented" );
342 * @covers ContentHandler::runLegacyHooks
344 public function testRunLegacyHooks() {
345 Hooks
::register( 'testRunLegacyHooks', __CLASS__
. '::dummyHookHandler' );
347 $content = new WikitextContent( 'test text' );
348 $ok = ContentHandler
::runLegacyHooks(
349 'testRunLegacyHooks',
350 array( 'foo', &$content, 'bar' ),
354 $this->assertTrue( $ok, "runLegacyHooks should have returned true" );
355 $this->assertEquals( "TEST TEXT", $content->getNativeData() );
358 public static function dummyHookHandler( $foo, &$text, $bar ) {
359 if ( $text === null ||
$text === false ) {
363 $text = strtoupper( $text );
369 class DummyContentHandlerForTesting
extends ContentHandler
{
371 public function __construct( $dataModel ) {
372 parent
::__construct( $dataModel, array( "testing" ) );
376 * @see ContentHandler::serializeContent
378 * @param Content $content
379 * @param string $format
383 public function serializeContent( Content
$content, $format = null ) {
384 return $content->serialize();
388 * @see ContentHandler::unserializeContent
390 * @param string $blob
391 * @param string $format Unused.
395 public function unserializeContent( $blob, $format = null ) {
396 $d = unserialize( $blob );
398 return new DummyContentForTesting( $d );
402 * Creates an empty Content object of the type supported by this ContentHandler.
405 public function makeEmptyContent() {
406 return new DummyContentForTesting( '' );
410 class DummyContentForTesting
extends AbstractContent
{
412 public function __construct( $data ) {
413 parent
::__construct( "testing" );
418 public function serialize( $format = null ) {
419 return serialize( $this->data
);
423 * @return string A string representing the content in a way useful for
424 * building a full text search index. If no useful representation exists,
425 * this method returns an empty string.
427 public function getTextForSearchIndex() {
432 * @return string|bool The wikitext to include when another page includes this content,
433 * or false if the content is not includable in a wikitext page.
435 public function getWikitextForTransclusion() {
440 * Returns a textual representation of the content suitable for use in edit
441 * summaries and log messages.
443 * @param int $maxlength Maximum length of the summary text.
444 * @return string The summary text.
446 public function getTextForSummary( $maxlength = 250 ) {
451 * Returns native represenation of the data. Interpretation depends on the data model used,
452 * as given by getDataModel().
454 * @return mixed The native representation of the content. Could be a string, a nested array
455 * structure, an object, a binary blob... anything, really.
457 public function getNativeData() {
462 * returns the content's nominal size in bogo-bytes.
466 public function getSize() {
467 return strlen( $this->data
);
471 * Return a copy of this Content object. The following must be true for the object returned
472 * if $copy = $original->copy()
474 * * get_class($original) === get_class($copy)
475 * * $original->getModel() === $copy->getModel()
476 * * $original->equals( $copy )
478 * If and only if the Content object is imutable, the copy() method can and should
479 * return $this. That is, $copy === $original may be true, but only for imutable content
482 * @return Content A copy of this object
484 public function copy() {
489 * Returns true if this content is countable as a "real" wiki page, provided
490 * that it's also in a countable location (e.g. a current revision in the main namespace).
492 * @param bool $hasLinks If it is known whether this content contains links,
493 * provide this information here, to avoid redundant parsing to find out.
496 public function isCountable( $hasLinks = null ) {
501 * @param Title $title
502 * @param int $revId Unused.
503 * @param null|ParserOptions $options
504 * @param bool $generateHtml Whether to generate Html (default: true). If false, the result
505 * of calling getText() on the ParserOutput object returned by this method is undefined.
507 * @return ParserOutput
509 public function getParserOutput( Title
$title, $revId = null,
510 ParserOptions
$options = null, $generateHtml = true
512 return new ParserOutput( $this->getNativeData() );
516 * @see AbstractContent::fillParserOutput()
518 * @param Title $title Context title for parsing
519 * @param int|null $revId Revision ID (for {{REVISIONID}})
520 * @param ParserOptions $options Parser options
521 * @param bool $generateHtml Whether or not to generate HTML
522 * @param ParserOutput &$output The output object to fill (reference).
524 protected function fillParserOutput( Title
$title, $revId,
525 ParserOptions
$options, $generateHtml, ParserOutput
&$output ) {
526 $output = new ParserOutput( $this->getNativeData() );