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