3 use MediaWiki\MediaWikiServices
;
4 use MediaWiki\Revision\SlotRecord
;
5 use MediaWiki\Revision\SlotRenderingProvider
;
8 * @group ContentHandler
10 class WikitextContentHandlerTest
extends MediaWikiLangTestCase
{
16 protected function setUp() {
19 $this->handler
= ContentHandler
::getForModelID( CONTENT_MODEL_WIKITEXT
);
23 * @covers WikitextContentHandler::serializeContent
25 public function testSerializeContent() {
26 $content = new WikitextContent( 'hello world' );
28 $this->assertEquals( 'hello world', $this->handler
->serializeContent( $content ) );
31 $this->handler
->serializeContent( $content, CONTENT_FORMAT_WIKITEXT
)
35 $this->handler
->serializeContent( $content, 'dummy/foo' );
36 $this->fail( "serializeContent() should have failed on unknown format" );
37 } catch ( MWException
$e ) {
43 * @covers WikitextContentHandler::unserializeContent
45 public function testUnserializeContent() {
46 $content = $this->handler
->unserializeContent( 'hello world' );
47 $this->assertEquals( 'hello world', $content->getText() );
49 $content = $this->handler
->unserializeContent( 'hello world', CONTENT_FORMAT_WIKITEXT
);
50 $this->assertEquals( 'hello world', $content->getText() );
53 $this->handler
->unserializeContent( 'hello world', 'dummy/foo' );
54 $this->fail( "unserializeContent() should have failed on unknown format" );
55 } catch ( MWException
$e ) {
61 * @covers WikitextContentHandler::makeEmptyContent
63 public function testMakeEmptyContent() {
64 $content = $this->handler
->makeEmptyContent();
66 $this->assertTrue( $content->isEmpty() );
67 $this->assertEquals( '', $content->getText() );
70 public static function dataIsSupportedFormat() {
73 [ CONTENT_FORMAT_WIKITEXT
, true ],
79 * @dataProvider provideMakeRedirectContent
80 * @param Title|string $title Title object or string for Title::newFromText()
81 * @param string $expected Serialized form of the content object built
82 * @covers WikitextContentHandler::makeRedirectContent
84 public function testMakeRedirectContent( $title, $expected ) {
85 MediaWikiServices
::getInstance()->getContentLanguage()->resetNamespaces();
87 MediaWikiServices
::getInstance()->resetServiceForTesting( 'MagicWordFactory' );
89 if ( is_string( $title ) ) {
90 $title = Title
::newFromText( $title );
92 $content = $this->handler
->makeRedirectContent( $title );
93 $this->assertEquals( $expected, $content->serialize() );
96 public static function provideMakeRedirectContent() {
98 [ 'Hello', '#REDIRECT [[Hello]]' ],
99 [ 'Template:Hello', '#REDIRECT [[Template:Hello]]' ],
100 [ 'Hello#section', '#REDIRECT [[Hello#section]]' ],
101 [ 'user:john_doe#section', '#REDIRECT [[User:John doe#section]]' ],
102 [ 'MEDIAWIKI:FOOBAR', '#REDIRECT [[MediaWiki:FOOBAR]]' ],
103 [ 'Category:Foo', '#REDIRECT [[:Category:Foo]]' ],
104 [ Title
::makeTitle( NS_MAIN
, 'en:Foo' ), '#REDIRECT [[en:Foo]]' ],
105 [ Title
::makeTitle( NS_MAIN
, 'Foo', '', 'en' ), '#REDIRECT [[:en:Foo]]' ],
107 Title
::makeTitle( NS_MAIN
, 'Bar', 'fragment', 'google' ),
108 '#REDIRECT [[google:Bar#fragment]]'
114 * @dataProvider dataIsSupportedFormat
115 * @covers WikitextContentHandler::isSupportedFormat
117 public function testIsSupportedFormat( $format, $supported ) {
118 $this->assertEquals( $supported, $this->handler
->isSupportedFormat( $format ) );
122 * @covers WikitextContentHandler::supportsDirectEditing
124 public function testSupportsDirectEditing() {
125 $handler = new WikiTextContentHandler();
126 $this->assertTrue( $handler->supportsDirectEditing(), 'direct editing is supported' );
129 public static function dataMerge3() {
162 * @dataProvider dataMerge3
163 * @covers WikitextContentHandler::merge3
165 public function testMerge3( $old, $mine, $yours, $expected ) {
166 $this->markTestSkippedIfNoDiff3();
169 $oldContent = new WikitextContent( $old );
170 $myContent = new WikitextContent( $mine );
171 $yourContent = new WikitextContent( $yours );
173 $merged = $this->handler
->merge3( $oldContent, $myContent, $yourContent );
175 $this->assertEquals( $expected, $merged ?
$merged->getText() : $merged );
178 public static function dataGetAutosummary() {
181 'Hello there, world!',
184 '/^Redirected page .*Foo/'
191 '/^Created page .*Hello/'
198 '/^Created blank page$/'
202 'Hello there, world!',
209 'Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy
210 eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam
211 voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet
212 clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet.',
215 '/^Replaced .*Hello/'
228 * @dataProvider dataGetAutosummary
229 * @covers WikitextContentHandler::getAutosummary
231 public function testGetAutosummary( $old, $new, $flags, $expected ) {
232 $oldContent = is_null( $old ) ?
null : new WikitextContent( $old );
233 $newContent = is_null( $new ) ?
null : new WikitextContent( $new );
235 $summary = $this->handler
->getAutosummary( $oldContent, $newContent, $flags );
238 (bool)preg_match( $expected, $summary ),
239 "Autosummary didn't match expected pattern $expected: $summary"
243 public static function dataGetChangeTag() {
263 'mw-removed-redirect'
270 'mw-changed-redirect-target'
277 null // mw-newpage is not defined as a tag
284 null // mw-newblank is not defined as a tag
295 'Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy
296 eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam
297 voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet
298 clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet.',
305 'Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy
306 eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam
307 voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet
308 clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet.',
309 'Duis purus odio, rhoncus et finibus dapibus, facilisis ac urna. Pellentesque
310 arcu, tristique nec tempus nec, suscipit vel arcu. Sed non dolor nec ligula
311 congue tempor. Quisque pellentesque finibus orci a molestie. Nam maximus, purus
312 euismod finibus mollis, dui ante malesuada felis, dignissim rutrum diam sapien.',
320 * @dataProvider dataGetChangeTag
321 * @covers WikitextContentHandler::getChangeTag
323 public function testGetChangeTag( $old, $new, $flags, $expected ) {
324 $this->setMwGlobals( 'wgSoftwareTags', [
325 'mw-new-redirect' => true,
326 'mw-removed-redirect' => true,
327 'mw-changed-redirect-target' => true,
328 'mw-newpage' => true,
329 'mw-newblank' => true,
331 'mw-replace' => true,
333 $oldContent = is_null( $old ) ?
null : new WikitextContent( $old );
334 $newContent = is_null( $new ) ?
null : new WikitextContent( $new );
336 $tag = $this->handler
->getChangeTag( $oldContent, $newContent, $flags );
338 $this->assertSame( $expected, $tag );
342 * @covers WikitextContentHandler::getDataForSearchIndex
344 public function testDataIndexFieldsFile() {
345 $mockEngine = $this->createMock( SearchEngine
::class );
346 $title = Title
::newFromText( 'Somefile.jpg', NS_FILE
);
347 $page = new WikiPage( $title );
349 $fileHandler = $this->getMockBuilder( FileContentHandler
::class )
350 ->disableOriginalConstructor()
351 ->setMethods( [ 'getDataForSearchIndex' ] )
354 $handler = $this->getMockBuilder( WikitextContentHandler
::class )
355 ->disableOriginalConstructor()
356 ->setMethods( [ 'getFileHandler' ] )
359 $handler->method( 'getFileHandler' )->will( $this->returnValue( $fileHandler ) );
360 $fileHandler->expects( $this->once() )
361 ->method( 'getDataForSearchIndex' )
362 ->will( $this->returnValue( [ 'file_text' => 'This is file content' ] ) );
364 $data = $handler->getDataForSearchIndex( $page, new ParserOutput(), $mockEngine );
365 $this->assertArrayHasKey( 'file_text', $data );
366 $this->assertEquals( 'This is file content', $data['file_text'] );
369 public function testGetSecondaryDataUpdates() {
370 $title = Title
::newFromText( 'Somefile.jpg', NS_FILE
);
371 $content = new WikitextContent( '' );
373 /** @var SlotRenderingProvider $srp */
374 $srp = $this->getMock( SlotRenderingProvider
::class );
376 $handler = new WikitextContentHandler();
377 $updates = $handler->getSecondaryDataUpdates( $title, $content, SlotRecord
::MAIN
, $srp );
379 $this->assertEquals( [], $updates );
382 public function testGetDeletionUpdates() {
383 $title = Title
::newFromText( 'Somefile.jpg', NS_FILE
);
384 $content = new WikitextContent( '' );
386 $srp = $this->getMock( SlotRenderingProvider
::class );
388 $handler = new WikitextContentHandler();
389 $updates = $handler->getDeletionUpdates( $title, SlotRecord
::MAIN
);
391 $this->assertEquals( [], $updates );