Merge "Force phan-taint-check to think LogFormatter stuff is safe for html"
[lhc/web/wiklou.git] / tests / phpunit / includes / content / ContentHandlerTest.php
1 <?php
2 use MediaWiki\MediaWikiServices;
3
4 /**
5 * @group ContentHandler
6 * @group Database
7 */
8 class ContentHandlerTest extends MediaWikiTestCase {
9
10 protected function setUp() {
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::class,
25 CONTENT_MODEL_JAVASCRIPT => JavaScriptContentHandler::class,
26 CONTENT_MODEL_JSON => JsonContentHandler::class,
27 CONTENT_MODEL_CSS => CssContentHandler::class,
28 CONTENT_MODEL_TEXT => TextContentHandler::class,
29 'testing' => DummyContentHandlerForTesting::class,
30 'testing-callbacks' => function ( $modelId ) {
31 return new DummyContentHandlerForTesting( $modelId );
32 }
33 ],
34 ] );
35
36 // Reset LinkCache
37 MediaWikiServices::getInstance()->resetServiceForTesting( 'LinkCache' );
38 }
39
40 protected function tearDown() {
41 // Reset LinkCache
42 MediaWikiServices::getInstance()->resetServiceForTesting( 'LinkCache' );
43
44 parent::tearDown();
45 }
46
47 public function addDBDataOnce() {
48 $this->insertPage( 'Not_Main_Page', 'This is not a main page' );
49 $this->insertPage( 'Smithee', 'A smithee is one who smiths. See also [[Alan Smithee]]' );
50 }
51
52 public static function dataGetDefaultModelFor() {
53 return [
54 [ 'Help:Foo', CONTENT_MODEL_WIKITEXT ],
55 [ 'Help:Foo.js', CONTENT_MODEL_WIKITEXT ],
56 [ 'Help:Foo.css', CONTENT_MODEL_WIKITEXT ],
57 [ 'Help:Foo.json', CONTENT_MODEL_WIKITEXT ],
58 [ 'Help:Foo/bar.js', CONTENT_MODEL_WIKITEXT ],
59 [ 'User:Foo', CONTENT_MODEL_WIKITEXT ],
60 [ 'User:Foo.js', CONTENT_MODEL_WIKITEXT ],
61 [ 'User:Foo.css', CONTENT_MODEL_WIKITEXT ],
62 [ 'User:Foo.json', CONTENT_MODEL_WIKITEXT ],
63 [ 'User:Foo/bar.js', CONTENT_MODEL_JAVASCRIPT ],
64 [ 'User:Foo/bar.css', CONTENT_MODEL_CSS ],
65 [ 'User:Foo/bar.json', CONTENT_MODEL_JSON ],
66 [ 'User:Foo/bar.json.nope', CONTENT_MODEL_WIKITEXT ],
67 [ 'User talk:Foo/bar.css', CONTENT_MODEL_WIKITEXT ],
68 [ 'User:Foo/bar.js.xxx', CONTENT_MODEL_WIKITEXT ],
69 [ 'User:Foo/bar.xxx', CONTENT_MODEL_WIKITEXT ],
70 [ 'MediaWiki:Foo.js', CONTENT_MODEL_JAVASCRIPT ],
71 [ 'MediaWiki:Foo.JS', CONTENT_MODEL_WIKITEXT ],
72 [ 'MediaWiki:Foo.css', CONTENT_MODEL_CSS ],
73 [ 'MediaWiki:Foo.css.xxx', CONTENT_MODEL_WIKITEXT ],
74 [ 'MediaWiki:Foo.CSS', CONTENT_MODEL_WIKITEXT ],
75 [ 'MediaWiki:Foo.json', CONTENT_MODEL_JSON ],
76 [ 'MediaWiki:Foo.JSON', CONTENT_MODEL_WIKITEXT ],
77 ];
78 }
79
80 /**
81 * @dataProvider dataGetDefaultModelFor
82 * @covers ContentHandler::getDefaultModelFor
83 */
84 public function testGetDefaultModelFor( $title, $expectedModelId ) {
85 $title = Title::newFromText( $title );
86 $this->assertEquals( $expectedModelId, ContentHandler::getDefaultModelFor( $title ) );
87 }
88
89 /**
90 * @dataProvider dataGetDefaultModelFor
91 * @covers ContentHandler::getForTitle
92 */
93 public function testGetForTitle( $title, $expectedContentModel ) {
94 $title = Title::newFromText( $title );
95 MediaWikiServices::getInstance()->getLinkCache()->addBadLinkObj( $title );
96 $handler = ContentHandler::getForTitle( $title );
97 $this->assertEquals( $expectedContentModel, $handler->getModelID() );
98 }
99
100 public static function dataGetLocalizedName() {
101 return [
102 [ null, null ],
103 [ "xyzzy", null ],
104
105 // XXX: depends on content language
106 [ CONTENT_MODEL_JAVASCRIPT, '/javascript/i' ],
107 ];
108 }
109
110 /**
111 * @dataProvider dataGetLocalizedName
112 * @covers ContentHandler::getLocalizedName
113 */
114 public function testGetLocalizedName( $id, $expected ) {
115 $name = ContentHandler::getLocalizedName( $id );
116
117 if ( $expected ) {
118 $this->assertNotNull( $name, "no name found for content model $id" );
119 $this->assertTrue( preg_match( $expected, $name ) > 0,
120 "content model name for #$id did not match pattern $expected"
121 );
122 } else {
123 $this->assertEquals( $id, $name, "localization of unknown model $id should have "
124 . "fallen back to use the model id directly."
125 );
126 }
127 }
128
129 public static function dataGetPageLanguage() {
130 global $wgLanguageCode;
131
132 return [
133 [ "Main", $wgLanguageCode ],
134 [ "Dummy:Foo", $wgLanguageCode ],
135 [ "MediaWiki:common.js", 'en' ],
136 [ "User:Foo/common.js", 'en' ],
137 [ "MediaWiki:common.css", 'en' ],
138 [ "User:Foo/common.css", 'en' ],
139 [ "User:Foo", $wgLanguageCode ],
140
141 [ CONTENT_MODEL_JAVASCRIPT, 'javascript' ],
142 ];
143 }
144
145 /**
146 * @dataProvider dataGetPageLanguage
147 * @covers ContentHandler::getPageLanguage
148 */
149 public function testGetPageLanguage( $title, $expected ) {
150 if ( is_string( $title ) ) {
151 $title = Title::newFromText( $title );
152 MediaWikiServices::getInstance()->getLinkCache()->addBadLinkObj( $title );
153 }
154
155 $expected = wfGetLangObj( $expected );
156
157 $handler = ContentHandler::getForTitle( $title );
158 $lang = $handler->getPageLanguage( $title );
159
160 $this->assertEquals( $expected->getCode(), $lang->getCode() );
161 }
162
163 public static function dataGetContentText_Null() {
164 return [
165 [ 'fail' ],
166 [ 'serialize' ],
167 [ 'ignore' ],
168 ];
169 }
170
171 /**
172 * @dataProvider dataGetContentText_Null
173 * @covers ContentHandler::getContentText
174 */
175 public function testGetContentText_Null( $contentHandlerTextFallback ) {
176 $this->setMwGlobals( 'wgContentHandlerTextFallback', $contentHandlerTextFallback );
177
178 $content = null;
179
180 $text = ContentHandler::getContentText( $content );
181 $this->assertEquals( '', $text );
182 }
183
184 public static function dataGetContentText_TextContent() {
185 return [
186 [ 'fail' ],
187 [ 'serialize' ],
188 [ 'ignore' ],
189 ];
190 }
191
192 /**
193 * @dataProvider dataGetContentText_TextContent
194 * @covers ContentHandler::getContentText
195 */
196 public function testGetContentText_TextContent( $contentHandlerTextFallback ) {
197 $this->setMwGlobals( 'wgContentHandlerTextFallback', $contentHandlerTextFallback );
198
199 $content = new WikitextContent( "hello world" );
200
201 $text = ContentHandler::getContentText( $content );
202 $this->assertEquals( $content->getNativeData(), $text );
203 }
204
205 /**
206 * ContentHandler::getContentText should have thrown an exception for non-text Content object
207 * @expectedException MWException
208 * @covers ContentHandler::getContentText
209 */
210 public function testGetContentText_NonTextContent_fail() {
211 $this->setMwGlobals( 'wgContentHandlerTextFallback', 'fail' );
212
213 $content = new DummyContentForTesting( "hello world" );
214
215 ContentHandler::getContentText( $content );
216 }
217
218 /**
219 * @covers ContentHandler::getContentText
220 */
221 public function testGetContentText_NonTextContent_serialize() {
222 $this->setMwGlobals( 'wgContentHandlerTextFallback', 'serialize' );
223
224 $content = new DummyContentForTesting( "hello world" );
225
226 $text = ContentHandler::getContentText( $content );
227 $this->assertEquals( $content->serialize(), $text );
228 }
229
230 /**
231 * @covers ContentHandler::getContentText
232 */
233 public function testGetContentText_NonTextContent_ignore() {
234 $this->setMwGlobals( 'wgContentHandlerTextFallback', 'ignore' );
235
236 $content = new DummyContentForTesting( "hello world" );
237
238 $text = ContentHandler::getContentText( $content );
239 $this->assertNull( $text );
240 }
241
242 public static function dataMakeContent() {
243 return [
244 [ 'hallo', 'Help:Test', null, null, CONTENT_MODEL_WIKITEXT, 'hallo', false ],
245 [ 'hallo', 'MediaWiki:Test.js', null, null, CONTENT_MODEL_JAVASCRIPT, 'hallo', false ],
246 [ serialize( 'hallo' ), 'Dummy:Test', null, null, "testing", 'hallo', false ],
247
248 [
249 'hallo',
250 'Help:Test',
251 null,
252 CONTENT_FORMAT_WIKITEXT,
253 CONTENT_MODEL_WIKITEXT,
254 'hallo',
255 false
256 ],
257 [
258 'hallo',
259 'MediaWiki:Test.js',
260 null,
261 CONTENT_FORMAT_JAVASCRIPT,
262 CONTENT_MODEL_JAVASCRIPT,
263 'hallo',
264 false
265 ],
266 [ serialize( 'hallo' ), 'Dummy:Test', null, "testing", "testing", 'hallo', false ],
267
268 [ 'hallo', 'Help:Test', CONTENT_MODEL_CSS, null, CONTENT_MODEL_CSS, 'hallo', false ],
269 [
270 'hallo',
271 'MediaWiki:Test.js',
272 CONTENT_MODEL_CSS,
273 null,
274 CONTENT_MODEL_CSS,
275 'hallo',
276 false
277 ],
278 [
279 serialize( 'hallo' ),
280 'Dummy:Test',
281 CONTENT_MODEL_CSS,
282 null,
283 CONTENT_MODEL_CSS,
284 serialize( 'hallo' ),
285 false
286 ],
287
288 [ 'hallo', 'Help:Test', CONTENT_MODEL_WIKITEXT, "testing", null, null, true ],
289 [ 'hallo', 'MediaWiki:Test.js', CONTENT_MODEL_CSS, "testing", null, null, true ],
290 [ 'hallo', 'Dummy:Test', CONTENT_MODEL_JAVASCRIPT, "testing", null, null, true ],
291 ];
292 }
293
294 /**
295 * @dataProvider dataMakeContent
296 * @covers ContentHandler::makeContent
297 */
298 public function testMakeContent( $data, $title, $modelId, $format,
299 $expectedModelId, $expectedNativeData, $shouldFail
300 ) {
301 $title = Title::newFromText( $title );
302 MediaWikiServices::getInstance()->getLinkCache()->addBadLinkObj( $title );
303 try {
304 $content = ContentHandler::makeContent( $data, $title, $modelId, $format );
305
306 if ( $shouldFail ) {
307 $this->fail( "ContentHandler::makeContent should have failed!" );
308 }
309
310 $this->assertEquals( $expectedModelId, $content->getModel(), 'bad model id' );
311 $this->assertEquals( $expectedNativeData, $content->getNativeData(), 'bads native data' );
312 } catch ( MWException $ex ) {
313 if ( !$shouldFail ) {
314 $this->fail( "ContentHandler::makeContent failed unexpectedly: " . $ex->getMessage() );
315 } else {
316 // dummy, so we don't get the "test did not perform any assertions" message.
317 $this->assertTrue( true );
318 }
319 }
320 }
321
322 /**
323 * @covers ContentHandler::getAutosummary
324 *
325 * Test if we become a "Created blank page" summary from getAutoSummary if no Content added to
326 * page.
327 */
328 public function testGetAutosummary() {
329 $this->setContentLang( 'en' );
330
331 $content = new DummyContentHandlerForTesting( CONTENT_MODEL_WIKITEXT );
332 $title = Title::newFromText( 'Help:Test' );
333 // Create a new content object with no content
334 $newContent = ContentHandler::makeContent( '', $title, CONTENT_MODEL_WIKITEXT, null );
335 // first check, if we become a blank page created summary with the right bitmask
336 $autoSummary = $content->getAutosummary( null, $newContent, 97 );
337 $this->assertEquals( $autoSummary,
338 wfMessage( 'autosumm-newblank' )->inContentLanguage()->text() );
339 // now check, what we become with another bitmask
340 $autoSummary = $content->getAutosummary( null, $newContent, 92 );
341 $this->assertEquals( $autoSummary, '' );
342 }
343
344 /**
345 * Test software tag that is added when content model of the page changes
346 * @covers ContentHandler::getChangeTag
347 */
348 public function testGetChangeTag() {
349 $this->setMwGlobals( 'wgSoftwareTags', [ 'mw-contentmodelchange' => true ] );
350 $wikitextContentHandler = new DummyContentHandlerForTesting( CONTENT_MODEL_WIKITEXT );
351 // Create old content object with javascript content model
352 $oldContent = ContentHandler::makeContent( '', null, CONTENT_MODEL_JAVASCRIPT, null );
353 // Create new content object with wikitext content model
354 $newContent = ContentHandler::makeContent( '', null, CONTENT_MODEL_WIKITEXT, null );
355 // Get the tag for this edit
356 $tag = $wikitextContentHandler->getChangeTag( $oldContent, $newContent, EDIT_UPDATE );
357 $this->assertSame( $tag, 'mw-contentmodelchange' );
358 }
359
360 /**
361 * @covers ContentHandler::supportsCategories
362 */
363 public function testSupportsCategories() {
364 $handler = new DummyContentHandlerForTesting( CONTENT_MODEL_WIKITEXT );
365 $this->assertTrue( $handler->supportsCategories(), 'content model supports categories' );
366 }
367
368 /**
369 * @covers ContentHandler::supportsDirectEditing
370 */
371 public function testSupportsDirectEditing() {
372 $handler = new DummyContentHandlerForTesting( CONTENT_MODEL_JSON );
373 $this->assertFalse( $handler->supportsDirectEditing(), 'direct editing is not supported' );
374 }
375
376 public static function dummyHookHandler( $foo, &$text, $bar ) {
377 if ( $text === null || $text === false ) {
378 return false;
379 }
380
381 $text = strtoupper( $text );
382
383 return true;
384 }
385
386 public function provideGetModelForID() {
387 return [
388 [ CONTENT_MODEL_WIKITEXT, WikitextContentHandler::class ],
389 [ CONTENT_MODEL_JAVASCRIPT, JavaScriptContentHandler::class ],
390 [ CONTENT_MODEL_JSON, JsonContentHandler::class ],
391 [ CONTENT_MODEL_CSS, CssContentHandler::class ],
392 [ CONTENT_MODEL_TEXT, TextContentHandler::class ],
393 [ 'testing', DummyContentHandlerForTesting::class ],
394 [ 'testing-callbacks', DummyContentHandlerForTesting::class ],
395 ];
396 }
397
398 /**
399 * @covers ContentHandler::getForModelID
400 * @dataProvider provideGetModelForID
401 */
402 public function testGetModelForID( $modelId, $handlerClass ) {
403 $handler = ContentHandler::getForModelID( $modelId );
404
405 $this->assertInstanceOf( $handlerClass, $handler );
406 }
407
408 /**
409 * @covers ContentHandler::getFieldsForSearchIndex
410 */
411 public function testGetFieldsForSearchIndex() {
412 $searchEngine = $this->newSearchEngine();
413
414 $handler = ContentHandler::getForModelID( CONTENT_MODEL_WIKITEXT );
415
416 $fields = $handler->getFieldsForSearchIndex( $searchEngine );
417
418 $this->assertArrayHasKey( 'category', $fields );
419 $this->assertArrayHasKey( 'external_link', $fields );
420 $this->assertArrayHasKey( 'outgoing_link', $fields );
421 $this->assertArrayHasKey( 'template', $fields );
422 $this->assertArrayHasKey( 'content_model', $fields );
423 }
424
425 private function newSearchEngine() {
426 $searchEngine = $this->getMockBuilder( SearchEngine::class )
427 ->getMock();
428
429 $searchEngine->expects( $this->any() )
430 ->method( 'makeSearchFieldMapping' )
431 ->will( $this->returnCallback( function ( $name, $type ) {
432 return new DummySearchIndexFieldDefinition( $name, $type );
433 } ) );
434
435 return $searchEngine;
436 }
437
438 /**
439 * @covers ContentHandler::getDataForSearchIndex
440 */
441 public function testDataIndexFields() {
442 $mockEngine = $this->createMock( SearchEngine::class );
443 $title = Title::newFromText( 'Not_Main_Page', NS_MAIN );
444 $page = new WikiPage( $title );
445
446 $this->setTemporaryHook( 'SearchDataForIndex',
447 function (
448 &$fields,
449 ContentHandler $handler,
450 WikiPage $page,
451 ParserOutput $output,
452 SearchEngine $engine
453 ) {
454 $fields['testDataField'] = 'test content';
455 } );
456
457 $output = $page->getContent()->getParserOutput( $title );
458 $data = $page->getContentHandler()->getDataForSearchIndex( $page, $output, $mockEngine );
459 $this->assertArrayHasKey( 'text', $data );
460 $this->assertArrayHasKey( 'text_bytes', $data );
461 $this->assertArrayHasKey( 'language', $data );
462 $this->assertArrayHasKey( 'testDataField', $data );
463 $this->assertEquals( 'test content', $data['testDataField'] );
464 $this->assertEquals( 'wikitext', $data['content_model'] );
465 }
466
467 /**
468 * @covers ContentHandler::getParserOutputForIndexing
469 */
470 public function testParserOutputForIndexing() {
471 $title = Title::newFromText( 'Smithee', NS_MAIN );
472 $page = new WikiPage( $title );
473
474 $out = $page->getContentHandler()->getParserOutputForIndexing( $page );
475 $this->assertInstanceOf( ParserOutput::class, $out );
476 $this->assertContains( 'one who smiths', $out->getRawText() );
477 }
478
479 /**
480 * @covers ContentHandler::getContentModels
481 */
482 public function testGetContentModelsHook() {
483 $this->setTemporaryHook( 'GetContentModels', function ( &$models ) {
484 $models[] = 'Ferrari';
485 } );
486 $this->assertContains( 'Ferrari', ContentHandler::getContentModels() );
487 }
488 }