Use integers for content_model and content_format.
[lhc/web/wiklou.git] / tests / phpunit / includes / ContentHandlerTest.php
1 <?php
2
3 /**
4 * @group ContentHandler
5 */
6 class ContentHandlerTest extends MediaWikiTestCase {
7
8 public function setUp() {
9 global $wgExtraNamespaces, $wgNamespaceContentModels, $wgContentHandlers, $wgContLang;
10
11 $wgExtraNamespaces[ 12312 ] = 'Dummy';
12 $wgExtraNamespaces[ 12313 ] = 'Dummy_talk';
13
14 $wgNamespaceContentModels[ 12312 ] = 999999;
15 $wgContentHandlers[ 999999 ] = 'DummyContentHandlerForTesting';
16
17 MWNamespace::getCanonicalNamespaces( true ); # reset namespace cache
18 $wgContLang->resetNamespaces(); # reset namespace cache
19 }
20
21 public function tearDown() {
22 global $wgExtraNamespaces, $wgNamespaceContentModels, $wgContentHandlers, $wgContLang;
23
24 unset( $wgExtraNamespaces[ 12312 ] );
25 unset( $wgExtraNamespaces[ 12313 ] );
26
27 unset( $wgNamespaceContentModels[ 12312 ] );
28 unset( $wgContentHandlers[ 999999 ] );
29
30 MWNamespace::getCanonicalNamespaces( true ); # reset namespace cache
31 $wgContLang->resetNamespaces(); # reset namespace cache
32 }
33
34 public function dataGetDefaultModelFor() {
35 return array(
36 array( 'Foo', CONTENT_MODEL_WIKITEXT ),
37 array( 'Foo.js', CONTENT_MODEL_WIKITEXT ),
38 array( 'Foo/bar.js', CONTENT_MODEL_WIKITEXT ),
39 array( 'User:Foo', CONTENT_MODEL_WIKITEXT ),
40 array( 'User:Foo.js', CONTENT_MODEL_WIKITEXT ),
41 array( 'User:Foo/bar.js', CONTENT_MODEL_JAVASCRIPT ),
42 array( 'User:Foo/bar.css', CONTENT_MODEL_CSS ),
43 array( 'User talk:Foo/bar.css', CONTENT_MODEL_WIKITEXT ),
44 array( 'User:Foo/bar.js.xxx', CONTENT_MODEL_WIKITEXT ),
45 array( 'User:Foo/bar.xxx', CONTENT_MODEL_WIKITEXT ),
46 array( 'MediaWiki:Foo.js', CONTENT_MODEL_JAVASCRIPT ),
47 array( 'MediaWiki:Foo.css', CONTENT_MODEL_CSS ),
48 array( 'MediaWiki:Foo.JS', CONTENT_MODEL_WIKITEXT ),
49 array( 'MediaWiki:Foo.CSS', CONTENT_MODEL_WIKITEXT ),
50 array( 'MediaWiki:Foo.css.xxx', CONTENT_MODEL_WIKITEXT ),
51 );
52 }
53
54 /**
55 * @dataProvider dataGetDefaultModelFor
56 */
57 public function testGetDefaultModelFor( $title, $expectedModelId ) {
58 $title = Title::newFromText( $title );
59 $this->assertEquals( $expectedModelId, ContentHandler::getDefaultModelFor( $title ) );
60 }
61 /**
62 * @dataProvider dataGetDefaultModelFor
63 */
64 public function testGetForTitle( $title, $expectedContentModel ) {
65 $title = Title::newFromText( $title );
66 $handler = ContentHandler::getForTitle( $title );
67 $this->assertEquals( $expectedContentModel, $handler->getModelID() );
68 }
69
70 public function dataGetContentFormatMimeType( ) {
71 return array(
72 array( 0, null ),
73 array( null, null ),
74 array( 99887766, null ),
75
76 array( CONTENT_FORMAT_WIKITEXT, 'text/x-wiki' ),
77 array( CONTENT_FORMAT_JAVASCRIPT, 'text/javascript' ),
78 array( CONTENT_FORMAT_CSS, 'text/css' ),
79 array( CONTENT_FORMAT_JSON, 'application/json' ),
80 array( CONTENT_FORMAT_XML, 'application/xml' ),
81 array( CONTENT_FORMAT_SERIALIZED, 'application/vnd.php.serialized' ),
82 );
83 }
84
85 /**
86 * @dataProvider dataGetContentFormatMimeType
87 */
88 public function testGetContentFormatMimeType( $id, $expectedMime ) {
89 $mime = ContentHandler::getContentFormatMimeType( $id );
90
91 $this->assertEquals( $expectedMime, $mime );
92 }
93
94 public function dataGetContentFormatID( ) {
95 return array(
96 array( '', null ),
97 array( 'foo', null ),
98 array( null, null ),
99
100 array( 'text/x-wiki', CONTENT_FORMAT_WIKITEXT ),
101 array( 'text/javascript', CONTENT_FORMAT_JAVASCRIPT ),
102 array( 'text/css', CONTENT_FORMAT_CSS ),
103 array( 'application/json', CONTENT_FORMAT_JSON ),
104 array( 'application/xml', CONTENT_FORMAT_XML ),
105 array( 'application/vnd.php.serialized', CONTENT_FORMAT_SERIALIZED ),
106 );
107 }
108
109 /**
110 * @dataProvider dataGetContentFormatID
111 */
112 public function testGetContentFormatID( $mime, $expectedId ) {
113 $id = ContentHandler::getContentFormatID( $mime );
114
115 $this->assertEquals( $expectedId, $id );
116 }
117
118 public function dataGetContentModelName() {
119 return array(
120 array( 0, null ),
121 array( null, null ),
122 array( 99887766, null ),
123
124 array( CONTENT_MODEL_JAVASCRIPT, '/javascript/i' ), //XXX: depends on content language
125 );
126 }
127
128 /**
129 * @dataProvider dataGetContentModelName
130 */
131 public function testGetContentModelName( $id, $expected ) {
132 $name = ContentHandler::getContentModelName( $id );
133
134 if ( $expected === null ) {
135 $this->assertNull( $name, "content model name for #$id was expected to be null" );
136 } else {
137 $this->assertNotNull( $name, "no name found for content model #$id" );
138 $this->assertTrue( preg_match( $expected, $name ) > 0 , "content model name for #$id did not match pattern $expected" );
139 }
140 }
141
142 public function testGetContentText_Null( ) {
143 global $wgContentHandlerTextFallback;
144
145 $content = null;
146
147 $wgContentHandlerTextFallback = 'fail';
148 $text = ContentHandler::getContentText( $content );
149 $this->assertEquals( '', $text );
150
151 $wgContentHandlerTextFallback = 'serialize';
152 $text = ContentHandler::getContentText( $content );
153 $this->assertEquals( '', $text );
154
155 $wgContentHandlerTextFallback = 'ignore';
156 $text = ContentHandler::getContentText( $content );
157 $this->assertEquals( '', $text );
158 }
159
160 public function testGetContentText_TextContent( ) {
161 global $wgContentHandlerTextFallback;
162
163 $content = new WikitextContent( "hello world" );
164
165 $wgContentHandlerTextFallback = 'fail';
166 $text = ContentHandler::getContentText( $content );
167 $this->assertEquals( $content->getNativeData(), $text );
168
169 $wgContentHandlerTextFallback = 'serialize';
170 $text = ContentHandler::getContentText( $content );
171 $this->assertEquals( $content->serialize(), $text );
172
173 $wgContentHandlerTextFallback = 'ignore';
174 $text = ContentHandler::getContentText( $content );
175 $this->assertEquals( $content->getNativeData(), $text );
176 }
177
178 public function testGetContentText_NonTextContent( ) {
179 global $wgContentHandlerTextFallback;
180
181 $content = new DummyContentForTesting( "hello world" );
182
183 $wgContentHandlerTextFallback = 'fail';
184
185 try {
186 $text = ContentHandler::getContentText( $content );
187
188 $this->fail( "ContentHandler::getContentText should have thrown an exception for non-text Content object" );
189 } catch (MWException $ex) {
190 // as expected
191 }
192
193 $wgContentHandlerTextFallback = 'serialize';
194 $text = ContentHandler::getContentText( $content );
195 $this->assertEquals( $content->serialize(), $text );
196
197 $wgContentHandlerTextFallback = 'ignore';
198 $text = ContentHandler::getContentText( $content );
199 $this->assertNull( $text );
200 }
201
202 #public static function makeContent( $text, Title $title, $modelId = null, $format = null )
203
204 public function dataMakeContent() {
205 return array(
206 array( 'hallo', 'Test', null, null, CONTENT_MODEL_WIKITEXT, 'hallo', false ),
207 array( 'hallo', 'MediaWiki:Test.js', null, null, CONTENT_MODEL_JAVASCRIPT, 'hallo', false ),
208 array( serialize('hallo'), 'Dummy:Test', null, null, 999999, 'hallo', false ),
209
210 array( 'hallo', 'Test', null, CONTENT_FORMAT_WIKITEXT, CONTENT_MODEL_WIKITEXT, 'hallo', false ),
211 array( 'hallo', 'MediaWiki:Test.js', null, CONTENT_FORMAT_JAVASCRIPT, CONTENT_MODEL_JAVASCRIPT, 'hallo', false ),
212 array( serialize('hallo'), 'Dummy:Test', null, 999999, 999999, 'hallo', false ),
213
214 array( 'hallo', 'Test', CONTENT_MODEL_CSS, null, CONTENT_MODEL_CSS, 'hallo', false ),
215 array( 'hallo', 'MediaWiki:Test.js', CONTENT_MODEL_CSS, null, CONTENT_MODEL_CSS, 'hallo', false ),
216 array( serialize('hallo'), 'Dummy:Test', CONTENT_MODEL_CSS, null, CONTENT_MODEL_CSS, serialize('hallo'), false ),
217
218 array( 'hallo', 'Test', CONTENT_MODEL_WIKITEXT, 999999, null, null, true ),
219 array( 'hallo', 'MediaWiki:Test.js', CONTENT_MODEL_CSS, 999999, null, null, true ),
220 array( 'hallo', 'Dummy:Test', CONTENT_MODEL_JAVASCRIPT, 999999, null, null, true ),
221 );
222 }
223
224 /**
225 * @dataProvider dataMakeContent
226 */
227 public function testMakeContent( $data, $title, $modelId, $format, $expectedModelId, $expectedNativeData, $shouldFail ) {
228 global $wgExtraNamespaces, $wgNamespaceContentModels, $wgContentHandlers;
229
230 $title = Title::newFromText( $title );
231
232 try {
233 $content = ContentHandler::makeContent( $data, $title, $modelId, $format );
234
235 if ( $shouldFail ) $this->fail( "ContentHandler::makeContent should have failed!" );
236
237 $this->assertEquals( $expectedModelId, $content->getModel(), 'bad model id' );
238 $this->assertEquals( $expectedNativeData, $content->getNativeData(), 'bads native data' );
239 } catch ( MWException $ex ) {
240 if ( !$shouldFail ) $this->fail( "ContentHandler::makeContent failed unexpectedly: " . $ex->getMessage() );
241 else $this->assertTrue( true ); // dummy, so we don't get the "test did not perform any assertions" message.
242 }
243
244 }
245
246
247 }
248
249 class DummyContentHandlerForTesting extends ContentHandler {
250
251 public function __construct( $dataModel ) {
252 parent::__construct( $dataModel, array( 999999 ) );
253 }
254
255 /**
256 * Serializes Content object of the type supported by this ContentHandler.
257 *
258 * @param Content $content the Content object to serialize
259 * @param null $format the desired serialization format
260 * @return String serialized form of the content
261 */
262 public function serializeContent( Content $content, $format = null )
263 {
264 return $content->serialize();
265 }
266
267 /**
268 * Unserializes a Content object of the type supported by this ContentHandler.
269 *
270 * @param $blob String serialized form of the content
271 * @param null $format the format used for serialization
272 * @return Content the Content object created by deserializing $blob
273 */
274 public function unserializeContent( $blob, $format = null )
275 {
276 $d = unserialize( $blob );
277 return new DummyContentForTesting( $d );
278 }
279
280 /**
281 * Creates an empty Content object of the type supported by this ContentHandler.
282 *
283 */
284 public function makeEmptyContent()
285 {
286 return new DummyContentForTesting( '' );
287 }
288 }
289
290 class DummyContentForTesting extends Content {
291
292 public function __construct( $data ) {
293 parent::__construct( 999999 );
294
295 $this->data = $data;
296 }
297
298 public function serialize( $format = null ) {
299 return serialize( $this->data );
300 }
301
302 /**
303 * @return String a string representing the content in a way useful for building a full text search index.
304 * If no useful representation exists, this method returns an empty string.
305 */
306 public function getTextForSearchIndex()
307 {
308 return '';
309 }
310
311 /**
312 * @return String the wikitext to include when another page includes this content, or false if the content is not
313 * includable in a wikitext page.
314 */
315 public function getWikitextForTransclusion()
316 {
317 return false;
318 }
319
320 /**
321 * Returns a textual representation of the content suitable for use in edit summaries and log messages.
322 *
323 * @param int $maxlength maximum length of the summary text
324 * @return String the summary text
325 */
326 public function getTextForSummary( $maxlength = 250 )
327 {
328 return '';
329 }
330
331 /**
332 * Returns native represenation of the data. Interpretation depends on the data model used,
333 * as given by getDataModel().
334 *
335 * @return mixed the native representation of the content. Could be a string, a nested array
336 * structure, an object, a binary blob... anything, really.
337 */
338 public function getNativeData()
339 {
340 return $this->data;
341 }
342
343 /**
344 * returns the content's nominal size in bogo-bytes.
345 *
346 * @return int
347 */
348 public function getSize()
349 {
350 return strlen( $this->data );
351 }
352
353 /**
354 * Return a copy of this Content object. The following must be true for the object returned
355 * if $copy = $original->copy()
356 *
357 * * get_class($original) === get_class($copy)
358 * * $original->getModel() === $copy->getModel()
359 * * $original->equals( $copy )
360 *
361 * If and only if the Content object is imutable, the copy() method can and should
362 * return $this. That is, $copy === $original may be true, but only for imutable content
363 * objects.
364 *
365 * @return Content. A copy of this object
366 */
367 public function copy()
368 {
369 return $this;
370 }
371
372 /**
373 * Returns true if this content is countable as a "real" wiki page, provided
374 * that it's also in a countable location (e.g. a current revision in the main namespace).
375 *
376 * @param $hasLinks Bool: if it is known whether this content contains links, provide this information here,
377 * to avoid redundant parsing to find out.
378 * @return boolean
379 */
380 public function isCountable( $hasLinks = null )
381 {
382 return false;
383 }
384
385 /**
386 * @param IContextSource $context
387 * @param null $revId
388 * @param null|ParserOptions $options
389 * @param Boolean $generateHtml whether to generate Html (default: true). If false,
390 * the result of calling getText() on the ParserOutput object returned by
391 * this method is undefined.
392 *
393 * @return ParserOutput
394 */
395 public function getParserOutput( IContextSource $context, $revId = null, ParserOptions $options = NULL, $generateHtml = true )
396 {
397 return new ParserOutput( $this->data );
398 }
399 }
400