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