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