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