Merge "Remove extra line breaks in memcached debug output"
[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_JSON => 'JsonContentHandler',
26 CONTENT_MODEL_CSS => 'CssContentHandler',
27 CONTENT_MODEL_TEXT => 'TextContentHandler',
28 'testing' => 'DummyContentHandlerForTesting',
29 'testing-callbacks' => function( $modelId ) {
30 return new DummyContentHandlerForTesting( $modelId );
31 }
32 ),
33 ) );
34
35 // Reset namespace cache
36 MWNamespace::getCanonicalNamespaces( true );
37 $wgContLang->resetNamespaces();
38 // And LinkCache
39 LinkCache::destroySingleton();
40 }
41
42 protected function tearDown() {
43 global $wgContLang;
44
45 // Reset namespace cache
46 MWNamespace::getCanonicalNamespaces( true );
47 $wgContLang->resetNamespaces();
48 // And LinkCache
49 LinkCache::destroySingleton();
50
51 parent::tearDown();
52 }
53
54 public static function dataGetDefaultModelFor() {
55 return array(
56 array( 'Help:Foo', CONTENT_MODEL_WIKITEXT ),
57 array( 'Help:Foo.js', CONTENT_MODEL_WIKITEXT ),
58 array( 'Help:Foo.css', CONTENT_MODEL_WIKITEXT ),
59 array( 'Help:Foo.json', CONTENT_MODEL_WIKITEXT ),
60 array( 'Help:Foo/bar.js', CONTENT_MODEL_WIKITEXT ),
61 array( 'User:Foo', CONTENT_MODEL_WIKITEXT ),
62 array( 'User:Foo.js', CONTENT_MODEL_WIKITEXT ),
63 array( 'User:Foo.css', CONTENT_MODEL_WIKITEXT ),
64 array( 'User:Foo.json', CONTENT_MODEL_WIKITEXT ),
65 array( 'User:Foo/bar.js', CONTENT_MODEL_JAVASCRIPT ),
66 array( 'User:Foo/bar.css', CONTENT_MODEL_CSS ),
67 array( 'User:Foo/bar.json', CONTENT_MODEL_JSON ),
68 array( 'User:Foo/bar.json.nope', CONTENT_MODEL_WIKITEXT ),
69 array( 'User talk:Foo/bar.css', CONTENT_MODEL_WIKITEXT ),
70 array( 'User:Foo/bar.js.xxx', CONTENT_MODEL_WIKITEXT ),
71 array( 'User:Foo/bar.xxx', CONTENT_MODEL_WIKITEXT ),
72 array( 'MediaWiki:Foo.js', CONTENT_MODEL_JAVASCRIPT ),
73 array( 'MediaWiki:Foo.JS', CONTENT_MODEL_WIKITEXT ),
74 array( 'MediaWiki:Foo.css', CONTENT_MODEL_CSS ),
75 array( 'MediaWiki:Foo.css.xxx', CONTENT_MODEL_WIKITEXT ),
76 array( 'MediaWiki:Foo.CSS', CONTENT_MODEL_WIKITEXT ),
77 array( 'MediaWiki:Foo.json', CONTENT_MODEL_JSON ),
78 array( 'MediaWiki:Foo.JSON', CONTENT_MODEL_WIKITEXT ),
79 );
80 }
81
82 /**
83 * @dataProvider dataGetDefaultModelFor
84 * @covers ContentHandler::getDefaultModelFor
85 */
86 public function testGetDefaultModelFor( $title, $expectedModelId ) {
87 $title = Title::newFromText( $title );
88 $this->assertEquals( $expectedModelId, ContentHandler::getDefaultModelFor( $title ) );
89 }
90
91 /**
92 * @dataProvider dataGetDefaultModelFor
93 * @covers ContentHandler::getForTitle
94 */
95 public function testGetForTitle( $title, $expectedContentModel ) {
96 $title = Title::newFromText( $title );
97 LinkCache::singleton()->addBadLinkObj( $title );
98 $handler = ContentHandler::getForTitle( $title );
99 $this->assertEquals( $expectedContentModel, $handler->getModelID() );
100 }
101
102 public static function dataGetLocalizedName() {
103 return array(
104 array( null, null ),
105 array( "xyzzy", null ),
106
107 // XXX: depends on content language
108 array( CONTENT_MODEL_JAVASCRIPT, '/javascript/i' ),
109 );
110 }
111
112 /**
113 * @dataProvider dataGetLocalizedName
114 * @covers ContentHandler::getLocalizedName
115 */
116 public function testGetLocalizedName( $id, $expected ) {
117 $name = ContentHandler::getLocalizedName( $id );
118
119 if ( $expected ) {
120 $this->assertNotNull( $name, "no name found for content model $id" );
121 $this->assertTrue( preg_match( $expected, $name ) > 0,
122 "content model name for #$id did not match pattern $expected"
123 );
124 } else {
125 $this->assertEquals( $id, $name, "localization of unknown model $id should have "
126 . "fallen back to use the model id directly."
127 );
128 }
129 }
130
131 public static function dataGetPageLanguage() {
132 global $wgLanguageCode;
133
134 return array(
135 array( "Main", $wgLanguageCode ),
136 array( "Dummy:Foo", $wgLanguageCode ),
137 array( "MediaWiki:common.js", 'en' ),
138 array( "User:Foo/common.js", 'en' ),
139 array( "MediaWiki:common.css", 'en' ),
140 array( "User:Foo/common.css", 'en' ),
141 array( "User:Foo", $wgLanguageCode ),
142
143 array( CONTENT_MODEL_JAVASCRIPT, 'javascript' ),
144 );
145 }
146
147 /**
148 * @dataProvider dataGetPageLanguage
149 * @covers ContentHandler::getPageLanguage
150 */
151 public function testGetPageLanguage( $title, $expected ) {
152 if ( is_string( $title ) ) {
153 $title = Title::newFromText( $title );
154 LinkCache::singleton()->addBadLinkObj( $title );
155 }
156
157 $expected = wfGetLangObj( $expected );
158
159 $handler = ContentHandler::getForTitle( $title );
160 $lang = $handler->getPageLanguage( $title );
161
162 $this->assertEquals( $expected->getCode(), $lang->getCode() );
163 }
164
165 public static function dataGetContentText_Null() {
166 return array(
167 array( 'fail' ),
168 array( 'serialize' ),
169 array( 'ignore' ),
170 );
171 }
172
173 /**
174 * @dataProvider dataGetContentText_Null
175 * @covers ContentHandler::getContentText
176 */
177 public function testGetContentText_Null( $contentHandlerTextFallback ) {
178 $this->setMwGlobals( 'wgContentHandlerTextFallback', $contentHandlerTextFallback );
179
180 $content = null;
181
182 $text = ContentHandler::getContentText( $content );
183 $this->assertEquals( '', $text );
184 }
185
186 public static function dataGetContentText_TextContent() {
187 return array(
188 array( 'fail' ),
189 array( 'serialize' ),
190 array( 'ignore' ),
191 );
192 }
193
194 /**
195 * @dataProvider dataGetContentText_TextContent
196 * @covers ContentHandler::getContentText
197 */
198 public function testGetContentText_TextContent( $contentHandlerTextFallback ) {
199 $this->setMwGlobals( 'wgContentHandlerTextFallback', $contentHandlerTextFallback );
200
201 $content = new WikitextContent( "hello world" );
202
203 $text = ContentHandler::getContentText( $content );
204 $this->assertEquals( $content->getNativeData(), $text );
205 }
206
207 /**
208 * ContentHandler::getContentText should have thrown an exception for non-text Content object
209 * @expectedException MWException
210 * @covers ContentHandler::getContentText
211 */
212 public function testGetContentText_NonTextContent_fail() {
213 $this->setMwGlobals( 'wgContentHandlerTextFallback', 'fail' );
214
215 $content = new DummyContentForTesting( "hello world" );
216
217 ContentHandler::getContentText( $content );
218 }
219
220 /**
221 * @covers ContentHandler::getContentText
222 */
223 public function testGetContentText_NonTextContent_serialize() {
224 $this->setMwGlobals( 'wgContentHandlerTextFallback', 'serialize' );
225
226 $content = new DummyContentForTesting( "hello world" );
227
228 $text = ContentHandler::getContentText( $content );
229 $this->assertEquals( $content->serialize(), $text );
230 }
231
232 /**
233 * @covers ContentHandler::getContentText
234 */
235 public function testGetContentText_NonTextContent_ignore() {
236 $this->setMwGlobals( 'wgContentHandlerTextFallback', 'ignore' );
237
238 $content = new DummyContentForTesting( "hello world" );
239
240 $text = ContentHandler::getContentText( $content );
241 $this->assertNull( $text );
242 }
243
244 /*
245 public static function makeContent( $text, Title $title, $modelId = null, $format = null ) {}
246 */
247
248 public static function dataMakeContent() {
249 return array(
250 array( 'hallo', 'Help:Test', null, null, CONTENT_MODEL_WIKITEXT, 'hallo', false ),
251 array( 'hallo', 'MediaWiki:Test.js', null, null, CONTENT_MODEL_JAVASCRIPT, 'hallo', false ),
252 array( serialize( 'hallo' ), 'Dummy:Test', null, null, "testing", 'hallo', false ),
253
254 array(
255 'hallo',
256 'Help:Test',
257 null,
258 CONTENT_FORMAT_WIKITEXT,
259 CONTENT_MODEL_WIKITEXT,
260 'hallo',
261 false
262 ),
263 array(
264 'hallo',
265 'MediaWiki:Test.js',
266 null,
267 CONTENT_FORMAT_JAVASCRIPT,
268 CONTENT_MODEL_JAVASCRIPT,
269 'hallo',
270 false
271 ),
272 array( serialize( 'hallo' ), 'Dummy:Test', null, "testing", "testing", 'hallo', false ),
273
274 array( 'hallo', 'Help:Test', CONTENT_MODEL_CSS, null, CONTENT_MODEL_CSS, 'hallo', false ),
275 array(
276 'hallo',
277 'MediaWiki:Test.js',
278 CONTENT_MODEL_CSS,
279 null,
280 CONTENT_MODEL_CSS,
281 'hallo',
282 false
283 ),
284 array(
285 serialize( 'hallo' ),
286 'Dummy:Test',
287 CONTENT_MODEL_CSS,
288 null,
289 CONTENT_MODEL_CSS,
290 serialize( 'hallo' ),
291 false
292 ),
293
294 array( 'hallo', 'Help:Test', CONTENT_MODEL_WIKITEXT, "testing", null, null, true ),
295 array( 'hallo', 'MediaWiki:Test.js', CONTENT_MODEL_CSS, "testing", null, null, true ),
296 array( 'hallo', 'Dummy:Test', CONTENT_MODEL_JAVASCRIPT, "testing", null, null, true ),
297 );
298 }
299
300 /**
301 * @dataProvider dataMakeContent
302 * @covers ContentHandler::makeContent
303 */
304 public function testMakeContent( $data, $title, $modelId, $format,
305 $expectedModelId, $expectedNativeData, $shouldFail
306 ) {
307 $title = Title::newFromText( $title );
308 LinkCache::singleton()->addBadLinkObj( $title );
309 try {
310 $content = ContentHandler::makeContent( $data, $title, $modelId, $format );
311
312 if ( $shouldFail ) {
313 $this->fail( "ContentHandler::makeContent should have failed!" );
314 }
315
316 $this->assertEquals( $expectedModelId, $content->getModel(), 'bad model id' );
317 $this->assertEquals( $expectedNativeData, $content->getNativeData(), 'bads native data' );
318 } catch ( MWException $ex ) {
319 if ( !$shouldFail ) {
320 $this->fail( "ContentHandler::makeContent failed unexpectedly: " . $ex->getMessage() );
321 } else {
322 // dummy, so we don't get the "test did not perform any assertions" message.
323 $this->assertTrue( true );
324 }
325 }
326 }
327
328 /*
329 * Test if we become a "Created blank page" summary from getAutoSummary if no Content added to
330 * page.
331 */
332 public function testGetAutosummary() {
333 $this->setMwGlobals( 'wgContLang', Language::factory( 'en' ) );
334
335 $content = new DummyContentHandlerForTesting( CONTENT_MODEL_WIKITEXT );
336 $title = Title::newFromText( 'Help:Test' );
337 // Create a new content object with no content
338 $newContent = ContentHandler::makeContent( '', $title, null, null, CONTENT_MODEL_WIKITEXT );
339 // first check, if we become a blank page created summary with the right bitmask
340 $autoSummary = $content->getAutosummary( null, $newContent, 97 );
341 $this->assertEquals( $autoSummary, 'Created blank page' );
342 // now check, what we become with another bitmask
343 $autoSummary = $content->getAutosummary( null, $newContent, 92 );
344 $this->assertEquals( $autoSummary, '' );
345 }
346
347 /*
348 public function testSupportsSections() {
349 $this->markTestIncomplete( "not yet implemented" );
350 }
351 */
352
353 public function testSupportsDirectEditing() {
354 $handler = new DummyContentHandlerForTesting( CONTENT_MODEL_JSON );
355 $this->assertFalse( $handler->supportsDirectEditing(), 'direct editing is not supported' );
356 }
357
358 /**
359 * @covers ContentHandler::runLegacyHooks
360 */
361 public function testRunLegacyHooks() {
362 Hooks::register( 'testRunLegacyHooks', __CLASS__ . '::dummyHookHandler' );
363
364 $content = new WikitextContent( 'test text' );
365 $ok = ContentHandler::runLegacyHooks(
366 'testRunLegacyHooks',
367 array( 'foo', &$content, 'bar' ),
368 false
369 );
370
371 $this->assertTrue( $ok, "runLegacyHooks should have returned true" );
372 $this->assertEquals( "TEST TEXT", $content->getNativeData() );
373 }
374
375 public static function dummyHookHandler( $foo, &$text, $bar ) {
376 if ( $text === null || $text === false ) {
377 return false;
378 }
379
380 $text = strtoupper( $text );
381
382 return true;
383 }
384
385 public function provideGetModelForID() {
386 return array(
387 array( CONTENT_MODEL_WIKITEXT, 'WikitextContentHandler' ),
388 array( CONTENT_MODEL_JAVASCRIPT, 'JavaScriptContentHandler' ),
389 array( CONTENT_MODEL_JSON, 'JsonContentHandler' ),
390 array( CONTENT_MODEL_CSS, 'CssContentHandler' ),
391 array( CONTENT_MODEL_TEXT, 'TextContentHandler' ),
392 array( 'testing', 'DummyContentHandlerForTesting' ),
393 array( 'testing-callbacks', 'DummyContentHandlerForTesting' ),
394 );
395 }
396
397 /**
398 * @dataProvider provideGetModelForID
399 */
400 public function testGetModelForID( $modelId, $handlerClass ) {
401 $handler = ContentHandler::getForModelID( $modelId );
402
403 $this->assertInstanceOf( $handlerClass, $handler );
404 }
405
406 }