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