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