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