Merge "Add DROP INDEX support to DatabaseSqlite::replaceVars method"
[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 * @covers ContentHandler::getDefaultModelFor
74 */
75 public function testGetDefaultModelFor( $title, $expectedModelId ) {
76 $title = Title::newFromText( $title );
77 $this->assertEquals( $expectedModelId, ContentHandler::getDefaultModelFor( $title ) );
78 }
79
80 /**
81 * @dataProvider dataGetDefaultModelFor
82 * @covers ContentHandler::getForTitle
83 */
84 public function testGetForTitle( $title, $expectedContentModel ) {
85 $title = Title::newFromText( $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 }
143
144 $expected = wfGetLangObj( $expected );
145
146 $handler = ContentHandler::getForTitle( $title );
147 $lang = $handler->getPageLanguage( $title );
148
149 $this->assertEquals( $expected->getCode(), $lang->getCode() );
150 }
151
152 public static function dataGetContentText_Null() {
153 return array(
154 array( 'fail' ),
155 array( 'serialize' ),
156 array( 'ignore' ),
157 );
158 }
159
160 /**
161 * @dataProvider dataGetContentText_Null
162 * @covers ContentHandler::getContentText
163 */
164 public function testGetContentText_Null( $contentHandlerTextFallback ) {
165 $this->setMwGlobals( 'wgContentHandlerTextFallback', $contentHandlerTextFallback );
166
167 $content = null;
168
169 $text = ContentHandler::getContentText( $content );
170 $this->assertEquals( '', $text );
171 }
172
173 public static function dataGetContentText_TextContent() {
174 return array(
175 array( 'fail' ),
176 array( 'serialize' ),
177 array( 'ignore' ),
178 );
179 }
180
181 /**
182 * @dataProvider dataGetContentText_TextContent
183 * @covers ContentHandler::getContentText
184 */
185 public function testGetContentText_TextContent( $contentHandlerTextFallback ) {
186 $this->setMwGlobals( 'wgContentHandlerTextFallback', $contentHandlerTextFallback );
187
188 $content = new WikitextContent( "hello world" );
189
190 $text = ContentHandler::getContentText( $content );
191 $this->assertEquals( $content->getNativeData(), $text );
192 }
193
194 /**
195 * ContentHandler::getContentText should have thrown an exception for non-text Content object
196 * @expectedException MWException
197 * @covers ContentHandler::getContentText
198 */
199 public function testGetContentText_NonTextContent_fail() {
200 $this->setMwGlobals( 'wgContentHandlerTextFallback', 'fail' );
201
202 $content = new DummyContentForTesting( "hello world" );
203
204 ContentHandler::getContentText( $content );
205 }
206
207 /**
208 * @covers ContentHandler::getContentText
209 */
210 public function testGetContentText_NonTextContent_serialize() {
211 $this->setMwGlobals( 'wgContentHandlerTextFallback', 'serialize' );
212
213 $content = new DummyContentForTesting( "hello world" );
214
215 $text = ContentHandler::getContentText( $content );
216 $this->assertEquals( $content->serialize(), $text );
217 }
218
219 /**
220 * @covers ContentHandler::getContentText
221 */
222 public function testGetContentText_NonTextContent_ignore() {
223 $this->setMwGlobals( 'wgContentHandlerTextFallback', 'ignore' );
224
225 $content = new DummyContentForTesting( "hello world" );
226
227 $text = ContentHandler::getContentText( $content );
228 $this->assertNull( $text );
229 }
230
231 /*
232 public static function makeContent( $text, Title $title, $modelId = null, $format = null ) {}
233 */
234
235 public static function dataMakeContent() {
236 return array(
237 array( 'hallo', 'Help:Test', null, null, CONTENT_MODEL_WIKITEXT, 'hallo', false ),
238 array( 'hallo', 'MediaWiki:Test.js', null, null, CONTENT_MODEL_JAVASCRIPT, 'hallo', false ),
239 array( serialize( 'hallo' ), 'Dummy:Test', null, null, "testing", 'hallo', false ),
240
241 array( 'hallo', 'Help:Test', null, CONTENT_FORMAT_WIKITEXT, CONTENT_MODEL_WIKITEXT, 'hallo', false ),
242 array( 'hallo', 'MediaWiki:Test.js', null, CONTENT_FORMAT_JAVASCRIPT, CONTENT_MODEL_JAVASCRIPT, 'hallo', false ),
243 array( serialize( 'hallo' ), 'Dummy:Test', null, "testing", "testing", 'hallo', false ),
244
245 array( 'hallo', 'Help:Test', CONTENT_MODEL_CSS, null, CONTENT_MODEL_CSS, 'hallo', false ),
246 array( 'hallo', 'MediaWiki:Test.js', CONTENT_MODEL_CSS, null, CONTENT_MODEL_CSS, 'hallo', false ),
247 array( serialize( 'hallo' ), 'Dummy:Test', CONTENT_MODEL_CSS, null, CONTENT_MODEL_CSS, serialize( 'hallo' ), false ),
248
249 array( 'hallo', 'Help:Test', CONTENT_MODEL_WIKITEXT, "testing", null, null, true ),
250 array( 'hallo', 'MediaWiki:Test.js', CONTENT_MODEL_CSS, "testing", null, null, true ),
251 array( 'hallo', 'Dummy:Test', CONTENT_MODEL_JAVASCRIPT, "testing", null, null, true ),
252 );
253 }
254
255 /**
256 * @dataProvider dataMakeContent
257 * @covers ContentHandler::makeContent
258 */
259 public function testMakeContent( $data, $title, $modelId, $format, $expectedModelId, $expectedNativeData, $shouldFail ) {
260 $title = Title::newFromText( $title );
261
262 try {
263 $content = ContentHandler::makeContent( $data, $title, $modelId, $format );
264
265 if ( $shouldFail ) {
266 $this->fail( "ContentHandler::makeContent should have failed!" );
267 }
268
269 $this->assertEquals( $expectedModelId, $content->getModel(), 'bad model id' );
270 $this->assertEquals( $expectedNativeData, $content->getNativeData(), 'bads native data' );
271 } catch ( MWException $ex ) {
272 if ( !$shouldFail ) {
273 $this->fail( "ContentHandler::makeContent failed unexpectedly: " . $ex->getMessage() );
274 } else {
275 // dummy, so we don't get the "test did not perform any assertions" message.
276 $this->assertTrue( true );
277 }
278 }
279 }
280
281 /*
282 public function testSupportsSections() {
283 $this->markTestIncomplete( "not yet implemented" );
284 }
285 */
286
287 /**
288 * @covers ContentHandler::runLegacyHooks
289 */
290 public function testRunLegacyHooks() {
291 Hooks::register( 'testRunLegacyHooks', __CLASS__ . '::dummyHookHandler' );
292
293 $content = new WikitextContent( 'test text' );
294 $ok = ContentHandler::runLegacyHooks( 'testRunLegacyHooks', array( 'foo', &$content, 'bar' ), false );
295
296 $this->assertTrue( $ok, "runLegacyHooks should have returned true" );
297 $this->assertEquals( "TEST TEXT", $content->getNativeData() );
298 }
299
300 public static function dummyHookHandler( $foo, &$text, $bar ) {
301 if ( $text === null || $text === false ) {
302 return false;
303 }
304
305 $text = strtoupper( $text );
306
307 return true;
308 }
309 }
310
311 class DummyContentHandlerForTesting extends ContentHandler {
312
313 public function __construct( $dataModel ) {
314 parent::__construct( $dataModel, array( "testing" ) );
315 }
316
317 /**
318 * Serializes Content object of the type supported by this ContentHandler.
319 *
320 * @param Content $content the Content object to serialize
321 * @param null $format the desired serialization format
322 * @return String serialized form of the content
323 */
324 public function serializeContent( Content $content, $format = null ) {
325 return $content->serialize();
326 }
327
328 /**
329 * Unserializes a Content object of the type supported by this ContentHandler.
330 *
331 * @param $blob String serialized form of the content
332 * @param null $format the format used for serialization
333 * @return Content the Content object created by deserializing $blob
334 */
335 public function unserializeContent( $blob, $format = null ) {
336 $d = unserialize( $blob );
337
338 return new DummyContentForTesting( $d );
339 }
340
341 /**
342 * Creates an empty Content object of the type supported by this ContentHandler.
343 *
344 */
345 public function makeEmptyContent() {
346 return new DummyContentForTesting( '' );
347 }
348 }
349
350 class DummyContentForTesting extends AbstractContent {
351
352 public function __construct( $data ) {
353 parent::__construct( "testing" );
354
355 $this->data = $data;
356 }
357
358 public function serialize( $format = null ) {
359 return serialize( $this->data );
360 }
361
362 /**
363 * @return String a string representing the content in a way useful for building a full text search index.
364 * If no useful representation exists, this method returns an empty string.
365 */
366 public function getTextForSearchIndex() {
367 return '';
368 }
369
370 /**
371 * @return String the wikitext to include when another page includes this content, or false if the content is not
372 * includable in a wikitext page.
373 */
374 public function getWikitextForTransclusion() {
375 return false;
376 }
377
378 /**
379 * Returns a textual representation of the content suitable for use in edit summaries and log messages.
380 *
381 * @param int $maxlength Maximum length of the summary text.
382 * @return string The summary text.
383 */
384 public function getTextForSummary( $maxlength = 250 ) {
385 return '';
386 }
387
388 /**
389 * Returns native represenation of the data. Interpretation depends on the data model used,
390 * as given by getDataModel().
391 *
392 * @return mixed the native representation of the content. Could be a string, a nested array
393 * structure, an object, a binary blob... anything, really.
394 */
395 public function getNativeData() {
396 return $this->data;
397 }
398
399 /**
400 * returns the content's nominal size in bogo-bytes.
401 *
402 * @return int
403 */
404 public function getSize() {
405 return strlen( $this->data );
406 }
407
408 /**
409 * Return a copy of this Content object. The following must be true for the object returned
410 * if $copy = $original->copy()
411 *
412 * * get_class($original) === get_class($copy)
413 * * $original->getModel() === $copy->getModel()
414 * * $original->equals( $copy )
415 *
416 * If and only if the Content object is imutable, the copy() method can and should
417 * return $this. That is, $copy === $original may be true, but only for imutable content
418 * objects.
419 *
420 * @return Content. A copy of this object.
421 */
422 public function copy() {
423 return $this;
424 }
425
426 /**
427 * Returns true if this content is countable as a "real" wiki page, provided
428 * that it's also in a countable location (e.g. a current revision in the main namespace).
429 *
430 * @param boolean $hasLinks if it is known whether this content contains links, provide this information here,
431 * to avoid redundant parsing to find out.
432 * @return boolean
433 */
434 public function isCountable( $hasLinks = null ) {
435 return false;
436 }
437
438 /**
439 * @param Title $title
440 * @param null $revId
441 * @param null|ParserOptions $options
442 * @param boolean $generateHtml whether to generate Html (default: true). If false,
443 * the result of calling getText() on the ParserOutput object returned by
444 * this method is undefined.
445 *
446 * @return ParserOutput
447 */
448 public function getParserOutput( Title $title, $revId = null, ParserOptions $options = null, $generateHtml = true ) {
449 return new ParserOutput( $this->getNativeData() );
450 }
451 }