3 use MediaWiki\MediaWikiServices
;
4 use MediaWiki\Revision\SlotRenderingProvider
;
7 * @group ContentHandler
9 class WikitextContentHandlerTest
extends MediaWikiLangTestCase
{
15 protected function setUp() {
18 $this->handler
= ContentHandler
::getForModelID( CONTENT_MODEL_WIKITEXT
);
22 * @covers WikitextContentHandler::serializeContent
24 public function testSerializeContent() {
25 $content = new WikitextContent( 'hello world' );
27 $this->assertEquals( 'hello world', $this->handler
->serializeContent( $content ) );
30 $this->handler
->serializeContent( $content, CONTENT_FORMAT_WIKITEXT
)
34 $this->handler
->serializeContent( $content, 'dummy/foo' );
35 $this->fail( "serializeContent() should have failed on unknown format" );
36 } catch ( MWException
$e ) {
42 * @covers WikitextContentHandler::unserializeContent
44 public function testUnserializeContent() {
45 $content = $this->handler
->unserializeContent( 'hello world' );
46 $this->assertEquals( 'hello world', $content->getNativeData() );
48 $content = $this->handler
->unserializeContent( 'hello world', CONTENT_FORMAT_WIKITEXT
);
49 $this->assertEquals( 'hello world', $content->getNativeData() );
52 $this->handler
->unserializeContent( 'hello world', 'dummy/foo' );
53 $this->fail( "unserializeContent() should have failed on unknown format" );
54 } catch ( MWException
$e ) {
60 * @covers WikitextContentHandler::makeEmptyContent
62 public function testMakeEmptyContent() {
63 $content = $this->handler
->makeEmptyContent();
65 $this->assertTrue( $content->isEmpty() );
66 $this->assertEquals( '', $content->getNativeData() );
69 public static function dataIsSupportedFormat() {
72 [ CONTENT_FORMAT_WIKITEXT
, true ],
78 * @dataProvider provideMakeRedirectContent
79 * @param Title|string $title Title object or string for Title::newFromText()
80 * @param string $expected Serialized form of the content object built
81 * @covers WikitextContentHandler::makeRedirectContent
83 public function testMakeRedirectContent( $title, $expected ) {
84 MediaWikiServices
::getInstance()->getContentLanguage()->resetNamespaces();
86 MediaWikiServices
::getInstance()->resetServiceForTesting( 'MagicWordFactory' );
88 if ( is_string( $title ) ) {
89 $title = Title
::newFromText( $title );
91 $content = $this->handler
->makeRedirectContent( $title );
92 $this->assertEquals( $expected, $content->serialize() );
95 public static function provideMakeRedirectContent() {
97 [ 'Hello', '#REDIRECT [[Hello]]' ],
98 [ 'Template:Hello', '#REDIRECT [[Template:Hello]]' ],
99 [ 'Hello#section', '#REDIRECT [[Hello#section]]' ],
100 [ 'user:john_doe#section', '#REDIRECT [[User:John doe#section]]' ],
101 [ 'MEDIAWIKI:FOOBAR', '#REDIRECT [[MediaWiki:FOOBAR]]' ],
102 [ 'Category:Foo', '#REDIRECT [[:Category:Foo]]' ],
103 [ Title
::makeTitle( NS_MAIN
, 'en:Foo' ), '#REDIRECT [[en:Foo]]' ],
104 [ Title
::makeTitle( NS_MAIN
, 'Foo', '', 'en' ), '#REDIRECT [[:en:Foo]]' ],
106 Title
::makeTitle( NS_MAIN
, 'Bar', 'fragment', 'google' ),
107 '#REDIRECT [[google:Bar#fragment]]'
113 * @dataProvider dataIsSupportedFormat
114 * @covers WikitextContentHandler::isSupportedFormat
116 public function testIsSupportedFormat( $format, $supported ) {
117 $this->assertEquals( $supported, $this->handler
->isSupportedFormat( $format ) );
121 * @covers WikitextContentHandler::supportsDirectEditing
123 public function testSupportsDirectEditing() {
124 $handler = new WikiTextContentHandler();
125 $this->assertTrue( $handler->supportsDirectEditing(), 'direct editing is supported' );
128 public static function dataMerge3() {
161 * @dataProvider dataMerge3
162 * @covers WikitextContentHandler::merge3
164 public function testMerge3( $old, $mine, $yours, $expected ) {
165 $this->markTestSkippedIfNoDiff3();
168 $oldContent = new WikitextContent( $old );
169 $myContent = new WikitextContent( $mine );
170 $yourContent = new WikitextContent( $yours );
172 $merged = $this->handler
->merge3( $oldContent, $myContent, $yourContent );
174 $this->assertEquals( $expected, $merged ?
$merged->getNativeData() : $merged );
177 public static function dataGetAutosummary() {
180 'Hello there, world!',
183 '/^Redirected page .*Foo/'
190 '/^Created page .*Hello/'
197 '/^Created blank page$/'
201 'Hello there, world!',
208 'Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy
209 eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam
210 voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet
211 clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet.',
214 '/^Replaced .*Hello/'
227 * @dataProvider dataGetAutosummary
228 * @covers WikitextContentHandler::getAutosummary
230 public function testGetAutosummary( $old, $new, $flags, $expected ) {
231 $oldContent = is_null( $old ) ?
null : new WikitextContent( $old );
232 $newContent = is_null( $new ) ?
null : new WikitextContent( $new );
234 $summary = $this->handler
->getAutosummary( $oldContent, $newContent, $flags );
237 (bool)preg_match( $expected, $summary ),
238 "Autosummary didn't match expected pattern $expected: $summary"
242 public static function dataGetChangeTag() {
262 'mw-removed-redirect'
269 'mw-changed-redirect-target'
276 null // mw-newpage is not defined as a tag
283 null // mw-newblank is not defined as a tag
294 'Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy
295 eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam
296 voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet
297 clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet.',
304 'Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy
305 eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam
306 voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet
307 clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet.',
308 'Duis purus odio, rhoncus et finibus dapibus, facilisis ac urna. Pellentesque
309 arcu, tristique nec tempus nec, suscipit vel arcu. Sed non dolor nec ligula
310 congue tempor. Quisque pellentesque finibus orci a molestie. Nam maximus, purus
311 euismod finibus mollis, dui ante malesuada felis, dignissim rutrum diam sapien.',
319 * @dataProvider dataGetChangeTag
320 * @covers WikitextContentHandler::getChangeTag
322 public function testGetChangeTag( $old, $new, $flags, $expected ) {
323 $this->setMwGlobals( 'wgSoftwareTags', [
324 'mw-new-redirect' => true,
325 'mw-removed-redirect' => true,
326 'mw-changed-redirect-target' => true,
327 'mw-newpage' => true,
328 'mw-newblank' => true,
330 'mw-replace' => true,
332 $oldContent = is_null( $old ) ?
null : new WikitextContent( $old );
333 $newContent = is_null( $new ) ?
null : new WikitextContent( $new );
335 $tag = $this->handler
->getChangeTag( $oldContent, $newContent, $flags );
337 $this->assertSame( $expected, $tag );
341 * @covers WikitextContentHandler::getDataForSearchIndex
343 public function testDataIndexFieldsFile() {
344 $mockEngine = $this->createMock( SearchEngine
::class );
345 $title = Title
::newFromText( 'Somefile.jpg', NS_FILE
);
346 $page = new WikiPage( $title );
348 $fileHandler = $this->getMockBuilder( FileContentHandler
::class )
349 ->disableOriginalConstructor()
350 ->setMethods( [ 'getDataForSearchIndex' ] )
353 $handler = $this->getMockBuilder( WikitextContentHandler
::class )
354 ->disableOriginalConstructor()
355 ->setMethods( [ 'getFileHandler' ] )
358 $handler->method( 'getFileHandler' )->will( $this->returnValue( $fileHandler ) );
359 $fileHandler->expects( $this->once() )
360 ->method( 'getDataForSearchIndex' )
361 ->will( $this->returnValue( [ 'file_text' => 'This is file content' ] ) );
363 $data = $handler->getDataForSearchIndex( $page, new ParserOutput(), $mockEngine );
364 $this->assertArrayHasKey( 'file_text', $data );
365 $this->assertEquals( 'This is file content', $data['file_text'] );
368 public function testGetSecondaryDataUpdates() {
369 $title = Title
::newFromText( 'Somefile.jpg', NS_FILE
);
370 $content = new WikitextContent( '' );
372 /** @var SlotRenderingProvider $srp */
373 $srp = $this->getMock( SlotRenderingProvider
::class );
375 $handler = new WikitextContentHandler();
376 $updates = $handler->getSecondaryDataUpdates( $title, $content, 'main', $srp );
378 $this->assertEquals( [], $updates );
381 public function testGetDeletionUpdates() {
382 $title = Title
::newFromText( 'Somefile.jpg', NS_FILE
);
383 $content = new WikitextContent( '' );
385 $srp = $this->getMock( SlotRenderingProvider
::class );
387 $handler = new WikitextContentHandler();
388 $updates = $handler->getDeletionUpdates( $title, 'main' );
390 $this->assertEquals( [], $updates );