3 class ContentHandlerTest
extends MediaWikiTestCase
{
5 public function setUp() {
6 global $wgExtraNamespaces, $wgNamespaceContentModels, $wgContentHandlers, $wgContLang;
8 $wgExtraNamespaces[ 12312 ] = 'Dummy';
9 $wgExtraNamespaces[ 12313 ] = 'Dummy_talk';
11 $wgNamespaceContentModels[ 12312 ] = 'DUMMY';
12 $wgContentHandlers[ 'DUMMY' ] = 'DummyContentHandlerForTesting';
14 MWNamespace
::getCanonicalNamespaces( true ); # reset namespace cache
15 $wgContLang->resetNamespaces(); # reset namespace cache
18 public function tearDown() {
19 global $wgExtraNamespaces, $wgNamespaceContentModels, $wgContentHandlers, $wgContLang;
21 unset( $wgExtraNamespaces[ 12312 ] );
22 unset( $wgExtraNamespaces[ 12313 ] );
24 unset( $wgNamespaceContentModels[ 12312 ] );
25 unset( $wgContentHandlers[ 'DUMMY' ] );
27 MWNamespace
::getCanonicalNamespaces( true ); # reset namespace cache
28 $wgContLang->resetNamespaces(); # reset namespace cache
31 public function dataGetDefaultModelFor() {
33 array( 'Foo', CONTENT_MODEL_WIKITEXT
),
34 array( 'Foo.js', CONTENT_MODEL_WIKITEXT
),
35 array( 'Foo/bar.js', CONTENT_MODEL_WIKITEXT
),
36 array( 'User:Foo', CONTENT_MODEL_WIKITEXT
),
37 array( 'User:Foo.js', CONTENT_MODEL_WIKITEXT
),
38 array( 'User:Foo/bar.js', CONTENT_MODEL_JAVASCRIPT
),
39 array( 'User:Foo/bar.css', CONTENT_MODEL_CSS
),
40 array( 'User talk:Foo/bar.css', CONTENT_MODEL_WIKITEXT
),
41 array( 'User:Foo/bar.js.xxx', CONTENT_MODEL_WIKITEXT
),
42 array( 'User:Foo/bar.xxx', CONTENT_MODEL_WIKITEXT
),
43 array( 'MediaWiki:Foo.js', CONTENT_MODEL_JAVASCRIPT
),
44 array( 'MediaWiki:Foo.css', CONTENT_MODEL_CSS
),
45 array( 'MediaWiki:Foo.JS', CONTENT_MODEL_WIKITEXT
),
46 array( 'MediaWiki:Foo.CSS', CONTENT_MODEL_WIKITEXT
),
47 array( 'MediaWiki:Foo.css.xxx', CONTENT_MODEL_WIKITEXT
),
52 * @dataProvider dataGetDefaultModelFor
54 public function testGetDefaultModelFor( $title, $expectedModelName ) {
55 $title = Title
::newFromText( $title );
56 $this->assertEquals( $expectedModelName, ContentHandler
::getDefaultModelFor( $title ) );
59 * @dataProvider dataGetDefaultModelFor
61 public function testGetForTitle( $title, $expectedContentModel ) {
62 $title = Title
::newFromText( $title );
63 $handler = ContentHandler
::getForTitle( $title );
64 $this->assertEquals( $expectedContentModel, $handler->getModelName() );
67 public function testGetContentText_Null( ) {
68 global $wgContentHandlerTextFallback;
72 $wgContentHandlerTextFallback = 'fail';
73 $text = ContentHandler
::getContentText( $content );
74 $this->assertEquals( '', $text );
76 $wgContentHandlerTextFallback = 'serialize';
77 $text = ContentHandler
::getContentText( $content );
78 $this->assertEquals( '', $text );
80 $wgContentHandlerTextFallback = 'ignore';
81 $text = ContentHandler
::getContentText( $content );
82 $this->assertEquals( '', $text );
85 public function testGetContentText_TextContent( ) {
86 global $wgContentHandlerTextFallback;
88 $content = new WikitextContent( "hello world" );
90 $wgContentHandlerTextFallback = 'fail';
91 $text = ContentHandler
::getContentText( $content );
92 $this->assertEquals( $content->getNativeData(), $text );
94 $wgContentHandlerTextFallback = 'serialize';
95 $text = ContentHandler
::getContentText( $content );
96 $this->assertEquals( $content->serialize(), $text );
98 $wgContentHandlerTextFallback = 'ignore';
99 $text = ContentHandler
::getContentText( $content );
100 $this->assertEquals( $content->getNativeData(), $text );
103 public function testGetContentText_NonTextContent( ) {
104 global $wgContentHandlerTextFallback;
106 $content = new DummyContentForTesting( "hello world" );
108 $wgContentHandlerTextFallback = 'fail';
111 $text = ContentHandler
::getContentText( $content );
113 $this->fail( "ContentHandler::getContentText should have thrown an exception for non-text Content object" );
114 } catch (MWException
$ex) {
118 $wgContentHandlerTextFallback = 'serialize';
119 $text = ContentHandler
::getContentText( $content );
120 $this->assertEquals( $content->serialize(), $text );
122 $wgContentHandlerTextFallback = 'ignore';
123 $text = ContentHandler
::getContentText( $content );
124 $this->assertNull( $text );
127 #public static function makeContent( $text, Title $title, $modelName = null, $format = null )
129 public function dataMakeContent() {
131 array( 'hallo', 'Test', null, null, CONTENT_MODEL_WIKITEXT
, 'hallo', false ),
132 array( 'hallo', 'MediaWiki:Test.js', null, null, CONTENT_MODEL_JAVASCRIPT
, 'hallo', false ),
133 array( serialize('hallo'), 'Dummy:Test', null, null, 'DUMMY', 'hallo', false ),
135 array( 'hallo', 'Test', null, 'text/x-wiki', CONTENT_MODEL_WIKITEXT
, 'hallo', false ),
136 array( 'hallo', 'MediaWiki:Test.js', null, 'text/javascript', CONTENT_MODEL_JAVASCRIPT
, 'hallo', false ),
137 array( serialize('hallo'), 'Dummy:Test', null, 'dummy', 'DUMMY', 'hallo', false ),
139 array( 'hallo', 'Test', CONTENT_MODEL_CSS
, null, CONTENT_MODEL_CSS
, 'hallo', false ),
140 array( 'hallo', 'MediaWiki:Test.js', CONTENT_MODEL_CSS
, null, CONTENT_MODEL_CSS
, 'hallo', false ),
141 array( serialize('hallo'), 'Dummy:Test', CONTENT_MODEL_CSS
, null, CONTENT_MODEL_CSS
, serialize('hallo'), false ),
143 array( 'hallo', 'Test', CONTENT_MODEL_WIKITEXT
, 'dummy', null, null, true ),
144 array( 'hallo', 'MediaWiki:Test.js', CONTENT_MODEL_CSS
, 'dummy', null, null, true ),
145 array( 'hallo', 'Dummy:Test', CONTENT_MODEL_JAVASCRIPT
, 'dummy', null, null, true ),
150 * @dataProvider dataMakeContent
152 public function testMakeContent( $data, $title, $modelName, $format, $expectedModelName, $expectedNativeData, $shouldFail ) {
153 global $wgExtraNamespaces, $wgNamespaceContentModels, $wgContentHandlers;
155 $title = Title
::newFromText( $title );
158 $content = ContentHandler
::makeContent( $data, $title, $modelName, $format );
160 if ( $shouldFail ) $this->fail( "ContentHandler::makeContent should have failed!" );
162 $this->assertEquals( $expectedModelName, $content->getModelName(), 'bad model name' );
163 $this->assertEquals( $expectedNativeData, $content->getNativeData(), 'bads native data' );
164 } catch ( MWException
$ex ) {
165 if ( !$shouldFail ) $this->fail( "ContentHandler::makeContent failed unexpectedly!" );
166 else $this->assertTrue( true ); // dummy, so we don't get the "test did not perform any assertions" message.
174 class DummyContentHandlerForTesting
extends ContentHandler
{
176 public function __construct( $dataModel ) {
177 parent
::__construct( $dataModel, array('dummy') );
181 * Serializes Content object of the type supported by this ContentHandler.
183 * @param Content $content the Content object to serialize
184 * @param null $format the desired serialization format
185 * @return String serialized form of the content
187 public function serializeContent( Content
$content, $format = null )
189 return $content->serialize();
193 * Unserializes a Content object of the type supported by this ContentHandler.
195 * @param $blob String serialized form of the content
196 * @param null $format the format used for serialization
197 * @return Content the Content object created by deserializing $blob
199 public function unserializeContent( $blob, $format = null )
201 $d = unserialize( $blob );
202 return new DummyContentForTesting( $d );
206 * Creates an empty Content object of the type supported by this ContentHandler.
209 public function makeEmptyContent()
211 return new DummyContentForTesting( '' );
215 class DummyContentForTesting
extends Content
{
217 public function __construct( $data ) {
218 parent
::__construct( "DUMMY" );
223 public function serialize( $format = null ) {
224 return serialize( $this->data
);
228 * @return String a string representing the content in a way useful for building a full text search index.
229 * If no useful representation exists, this method returns an empty string.
231 public function getTextForSearchIndex()
237 * @return String the wikitext to include when another page includes this content, or false if the content is not
238 * includable in a wikitext page.
240 public function getWikitextForTransclusion()
246 * Returns a textual representation of the content suitable for use in edit summaries and log messages.
248 * @param int $maxlength maximum length of the summary text
249 * @return String the summary text
251 public function getTextForSummary( $maxlength = 250 )
257 * Returns native represenation of the data. Interpretation depends on the data model used,
258 * as given by getDataModel().
260 * @return mixed the native representation of the content. Could be a string, a nested array
261 * structure, an object, a binary blob... anything, really.
263 public function getNativeData()
269 * returns the content's nominal size in bogo-bytes.
273 public function getSize()
275 return strlen( $this->data
);
279 * Return a copy of this Content object. The following must be true for the object returned
280 * if $copy = $original->copy()
282 * * get_class($original) === get_class($copy)
283 * * $original->getModelName() === $copy->getModelName()
284 * * $original->equals( $copy )
286 * If and only if the Content object is imutable, the copy() method can and should
287 * return $this. That is, $copy === $original may be true, but only for imutable content
290 * @return Content. A copy of this object
292 public function copy()
298 * Returns true if this content is countable as a "real" wiki page, provided
299 * that it's also in a countable location (e.g. a current revision in the main namespace).
301 * @param $hasLinks Bool: if it is known whether this content contains links, provide this information here,
302 * to avoid redundant parsing to find out.
305 public function isCountable( $hasLinks = null )
311 * @param IContextSource $context
313 * @param null|ParserOptions $options
314 * @param Boolean $generateHtml whether to generate Html (default: true). If false,
315 * the result of calling getText() on the ParserOutput object returned by
316 * this method is undefined.
318 * @return ParserOutput
320 public function getParserOutput( IContextSource
$context, $revId = null, ParserOptions
$options = NULL, $generateHtml = true )
322 return new ParserOutput( $this->data
);