3 class ContentHandlerTest
extends MediaWikiTestCase
{
5 public function dataGetDefaultModelFor() {
7 array( 'Foo', CONTENT_MODEL_WIKITEXT
),
8 array( 'Foo.js', CONTENT_MODEL_WIKITEXT
),
9 array( 'Foo/bar.js', CONTENT_MODEL_WIKITEXT
),
10 array( 'User:Foo', CONTENT_MODEL_WIKITEXT
),
11 array( 'User:Foo.js', CONTENT_MODEL_WIKITEXT
),
12 array( 'User:Foo/bar.js', CONTENT_MODEL_JAVASCRIPT
),
13 array( 'User:Foo/bar.css', CONTENT_MODEL_CSS
),
14 array( 'User talk:Foo/bar.css', CONTENT_MODEL_WIKITEXT
),
15 array( 'User:Foo/bar.js.xxx', CONTENT_MODEL_WIKITEXT
),
16 array( 'User:Foo/bar.xxx', CONTENT_MODEL_WIKITEXT
),
17 array( 'MediaWiki:Foo.js', CONTENT_MODEL_JAVASCRIPT
),
18 array( 'MediaWiki:Foo.css', CONTENT_MODEL_CSS
),
19 array( 'MediaWiki:Foo.JS', CONTENT_MODEL_WIKITEXT
),
20 array( 'MediaWiki:Foo.CSS', CONTENT_MODEL_WIKITEXT
),
21 array( 'MediaWiki:Foo.css.xxx', CONTENT_MODEL_WIKITEXT
),
26 * @dataProvider dataGetDefaultModelFor
28 public function testGetDefaultModelFor( $title, $expectedModelName ) {
29 $title = Title
::newFromText( $title );
30 $this->assertEquals( $expectedModelName, ContentHandler
::getDefaultModelFor( $title ) );
33 * @dataProvider dataGetDefaultModelFor
35 public function testGetForTitle( $title, $expectedContentModel ) {
36 $title = Title
::newFromText( $title );
37 $handler = ContentHandler
::getForTitle( $title );
38 $this->assertEquals( $expectedContentModel, $handler->getModelName() );
41 public function testGetContentText_Null( ) {
42 global $wgContentHandlerTextFallback;
46 $wgContentHandlerTextFallback = 'fail';
47 $text = ContentHandler
::getContentText( $content );
48 $this->assertEquals( '', $text );
50 $wgContentHandlerTextFallback = 'serialize';
51 $text = ContentHandler
::getContentText( $content );
52 $this->assertEquals( '', $text );
54 $wgContentHandlerTextFallback = 'ignore';
55 $text = ContentHandler
::getContentText( $content );
56 $this->assertEquals( '', $text );
59 public function testGetContentText_TextContent( ) {
60 global $wgContentHandlerTextFallback;
62 $content = new WikitextContent( "hello world" );
64 $wgContentHandlerTextFallback = 'fail';
65 $text = ContentHandler
::getContentText( $content );
66 $this->assertEquals( $content->getNativeData(), $text );
68 $wgContentHandlerTextFallback = 'serialize';
69 $text = ContentHandler
::getContentText( $content );
70 $this->assertEquals( $content->serialize(), $text );
72 $wgContentHandlerTextFallback = 'ignore';
73 $text = ContentHandler
::getContentText( $content );
74 $this->assertEquals( $content->getNativeData(), $text );
77 public function testGetContentText_NonTextContent( ) {
78 global $wgContentHandlerTextFallback;
80 $content = new DummyContentForTesting( "hello world" );
82 $wgContentHandlerTextFallback = 'fail';
85 $text = ContentHandler
::getContentText( $content );
87 $this->fail( "ContentHandler::getContentText should have thrown an exception for non-text Content object" );
88 } catch (MWException
$ex) {
92 $wgContentHandlerTextFallback = 'serialize';
93 $text = ContentHandler
::getContentText( $content );
94 $this->assertEquals( $content->serialize(), $text );
96 $wgContentHandlerTextFallback = 'ignore';
97 $text = ContentHandler
::getContentText( $content );
98 $this->assertNull( $text );
101 #public static function makeContent( $text, Title $title, $modelName = null, $format = null )
103 public function dataMakeContent() {
105 array( 'hallo', 'Test', null, null, CONTENT_MODEL_WIKITEXT
, 'hallo', false ),
106 array( 'hallo', 'MediaWiki:Test.js', null, null, CONTENT_MODEL_JAVASCRIPT
, 'hallo', false ),
107 array( serialize('hallo'), 'Dummy:Test', null, null, 'DUMMY', 'hallo', false ),
109 array( 'hallo', 'Test', null, 'application/x-wikitext', CONTENT_MODEL_WIKITEXT
, 'hallo', false ),
110 array( 'hallo', 'MediaWiki:Test.js', null, 'text/javascript', CONTENT_MODEL_JAVASCRIPT
, 'hallo', false ),
111 array( serialize('hallo'), 'Dummy:Test', null, 'dummy', 'DUMMY', 'hallo', false ),
113 array( 'hallo', 'Test', CONTENT_MODEL_CSS
, null, CONTENT_MODEL_CSS
, 'hallo', false ),
114 array( 'hallo', 'MediaWiki:Test.js', CONTENT_MODEL_CSS
, null, CONTENT_MODEL_CSS
, 'hallo', false ),
115 array( serialize('hallo'), 'Dummy:Test', CONTENT_MODEL_CSS
, null, CONTENT_MODEL_CSS
, serialize('hallo'), false ),
117 array( 'hallo', 'Test', CONTENT_MODEL_WIKITEXT
, 'dummy', null, null, true ),
118 array( 'hallo', 'MediaWiki:Test.js', CONTENT_MODEL_CSS
, 'dummy', null, null, true ),
119 array( 'hallo', 'Dummy:Test', CONTENT_MODEL_JAVASCRIPT
, 'dummy', null, null, true ),
124 * @dataProvider dataMakeContent
126 public function testMakeContent( $data, $title, $modelName, $format, $expectedModelName, $expectedNativeData, $shouldFail ) {
127 global $wgExtraNamespaces, $wgNamespaceContentModels, $wgContentHandlers;
129 $title = Title
::newFromText( $title );
132 $content = ContentHandler
::makeContent( $data, $title, $modelName, $format );
134 if ( $shouldFail ) $this->fail( "ContentHandler::makeContent should have failed!" );
136 $this->assertEquals( $expectedModelName, $content->getModelName(), 'bad model name' );
137 $this->assertEquals( $expectedNativeData, $content->getNativeData(), 'bads native data' );
138 } catch ( MWException
$ex ) {
139 if ( !$shouldFail ) $this->fail( "ContentHandler::makeContent failed unexpectedly!" );
140 else $this->assertTrue( true ); // dummy, so we don't get the "test did not perform any assertions" message.
146 public function setup() {
147 global $wgExtraNamespaces, $wgNamespaceContentModels, $wgContentHandlers;
149 $wgExtraNamespaces[ 12312 ] = 'Dummy';
150 $wgExtraNamespaces[ 12313 ] = 'Dummy_talk';
152 $wgNamespaceContentModels[ 12312 ] = 'DUMMY';
153 $wgContentHandlers[ 'DUMMY' ] = 'DummyContentHandlerForTesting';
156 public function teardown() {
157 global $wgExtraNamespaces, $wgNamespaceContentModels, $wgContentHandlers;
159 unset( $wgExtraNamespaces[ 12312 ] );
160 unset( $wgExtraNamespaces[ 12313 ] );
162 unset( $wgNamespaceContentModels[ 12312 ] );
163 unset( $wgContentHandlers[ 'DUMMY' ] );
168 class DummyContentHandlerForTesting
extends ContentHandler
{
170 public function __construct( $dataModel ) {
171 parent
::__construct( $dataModel, array('dummy') );
175 * Serializes Content object of the type supported by this ContentHandler.
177 * @param Content $content the Content object to serialize
178 * @param null $format the desired serialization format
179 * @return String serialized form of the content
181 public function serializeContent( Content
$content, $format = null )
183 return $content->serialize();
187 * Unserializes a Content object of the type supported by this ContentHandler.
189 * @param $blob String serialized form of the content
190 * @param null $format the format used for serialization
191 * @return Content the Content object created by deserializing $blob
193 public function unserializeContent( $blob, $format = null )
195 $d = unserialize( $blob );
196 return new DummyContentForTesting( $d );
200 * Creates an empty Content object of the type supported by this ContentHandler.
203 public function makeEmptyContent()
205 return new DummyContentForTesting( '' );
209 class DummyContentForTesting
extends Content
{
211 public function __construct( $data ) {
212 parent
::__construct( "DUMMY" );
217 public function serialize() {
218 return serialize( $this->data
);
222 * @return String a string representing the content in a way useful for building a full text search index.
223 * If no useful representation exists, this method returns an empty string.
225 public function getTextForSearchIndex()
231 * @return String the wikitext to include when another page includes this content, or false if the content is not
232 * includable in a wikitext page.
234 public function getWikitextForTransclusion()
240 * Returns a textual representation of the content suitable for use in edit summaries and log messages.
242 * @param int $maxlength maximum length of the summary text
243 * @return String the summary text
245 public function getTextForSummary( $maxlength = 250 )
251 * Returns native represenation of the data. Interpretation depends on the data model used,
252 * as given by getDataModel().
254 * @return mixed the native representation of the content. Could be a string, a nested array
255 * structure, an object, a binary blob... anything, really.
257 public function getNativeData()
263 * returns the content's nominal size in bogo-bytes.
267 public function getSize()
273 * Return a copy of this Content object. The following must be true for the object returned
274 * if $copy = $original->copy()
276 * * get_class($original) === get_class($copy)
277 * * $original->getModelName() === $copy->getModelName()
278 * * $original->equals( $copy )
280 * If and only if the Content object is imutable, the copy() method can and should
281 * return $this. That is, $copy === $original may be true, but only for imutable content
284 * @return Content. A copy of this object
286 public function copy()
292 * Returns true if this content is countable as a "real" wiki page, provided
293 * that it's also in a countable location (e.g. a current revision in the main namespace).
295 * @param $hasLinks Bool: if it is known whether this content contains links, provide this information here,
296 * to avoid redundant parsing to find out.
299 public function isCountable( $hasLinks = null )
305 * @param IContextSource $context
307 * @param null|ParserOptions $options
308 * @param Boolean $generateHtml whether to generate Html (default: true). If false,
309 * the result of calling getText() on the ParserOutput object returned by
310 * this method is undefined.
312 * @return ParserOutput
314 public function getParserOutput( IContextSource
$context, $revId = null, ParserOptions
$options = NULL, $generateHtml = true )
316 return new ParserOutput( $this->data
);