3 use MediaWiki\MediaWikiServices
;
6 * @group ContentHandler
8 class WikitextContentHandlerTest
extends MediaWikiLangTestCase
{
14 protected function setUp() {
17 $this->handler
= ContentHandler
::getForModelID( CONTENT_MODEL_WIKITEXT
);
21 * @covers WikitextContentHandler::serializeContent
23 public function testSerializeContent() {
24 $content = new WikitextContent( 'hello world' );
26 $this->assertEquals( 'hello world', $this->handler
->serializeContent( $content ) );
29 $this->handler
->serializeContent( $content, CONTENT_FORMAT_WIKITEXT
)
33 $this->handler
->serializeContent( $content, 'dummy/foo' );
34 $this->fail( "serializeContent() should have failed on unknown format" );
35 } catch ( MWException
$e ) {
41 * @covers WikitextContentHandler::unserializeContent
43 public function testUnserializeContent() {
44 $content = $this->handler
->unserializeContent( 'hello world' );
45 $this->assertEquals( 'hello world', $content->getNativeData() );
47 $content = $this->handler
->unserializeContent( 'hello world', CONTENT_FORMAT_WIKITEXT
);
48 $this->assertEquals( 'hello world', $content->getNativeData() );
51 $this->handler
->unserializeContent( 'hello world', 'dummy/foo' );
52 $this->fail( "unserializeContent() should have failed on unknown format" );
53 } catch ( MWException
$e ) {
59 * @covers WikitextContentHandler::makeEmptyContent
61 public function testMakeEmptyContent() {
62 $content = $this->handler
->makeEmptyContent();
64 $this->assertTrue( $content->isEmpty() );
65 $this->assertEquals( '', $content->getNativeData() );
68 public static function dataIsSupportedFormat() {
71 [ CONTENT_FORMAT_WIKITEXT
, true ],
77 * @dataProvider provideMakeRedirectContent
78 * @param Title|string $title Title object or string for Title::newFromText()
79 * @param string $expected Serialized form of the content object built
80 * @covers WikitextContentHandler::makeRedirectContent
82 public function testMakeRedirectContent( $title, $expected ) {
83 MediaWikiServices
::getInstance()->getContentLanguage()->resetNamespaces();
85 MediaWikiServices
::getInstance()->resetServiceForTesting( 'MagicWordFactory' );
87 if ( is_string( $title ) ) {
88 $title = Title
::newFromText( $title );
90 $content = $this->handler
->makeRedirectContent( $title );
91 $this->assertEquals( $expected, $content->serialize() );
94 public static function provideMakeRedirectContent() {
96 [ 'Hello', '#REDIRECT [[Hello]]' ],
97 [ 'Template:Hello', '#REDIRECT [[Template:Hello]]' ],
98 [ 'Hello#section', '#REDIRECT [[Hello#section]]' ],
99 [ 'user:john_doe#section', '#REDIRECT [[User:John doe#section]]' ],
100 [ 'MEDIAWIKI:FOOBAR', '#REDIRECT [[MediaWiki:FOOBAR]]' ],
101 [ 'Category:Foo', '#REDIRECT [[:Category:Foo]]' ],
102 [ Title
::makeTitle( NS_MAIN
, 'en:Foo' ), '#REDIRECT [[en:Foo]]' ],
103 [ Title
::makeTitle( NS_MAIN
, 'Foo', '', 'en' ), '#REDIRECT [[:en:Foo]]' ],
105 Title
::makeTitle( NS_MAIN
, 'Bar', 'fragment', 'google' ),
106 '#REDIRECT [[google:Bar#fragment]]'
112 * @dataProvider dataIsSupportedFormat
113 * @covers WikitextContentHandler::isSupportedFormat
115 public function testIsSupportedFormat( $format, $supported ) {
116 $this->assertEquals( $supported, $this->handler
->isSupportedFormat( $format ) );
120 * @covers WikitextContentHandler::supportsDirectEditing
122 public function testSupportsDirectEditing() {
123 $handler = new WikiTextContentHandler();
124 $this->assertTrue( $handler->supportsDirectEditing(), 'direct editing is supported' );
127 public static function dataMerge3() {
160 * @dataProvider dataMerge3
161 * @covers WikitextContentHandler::merge3
163 public function testMerge3( $old, $mine, $yours, $expected ) {
164 $this->markTestSkippedIfNoDiff3();
167 $oldContent = new WikitextContent( $old );
168 $myContent = new WikitextContent( $mine );
169 $yourContent = new WikitextContent( $yours );
171 $merged = $this->handler
->merge3( $oldContent, $myContent, $yourContent );
173 $this->assertEquals( $expected, $merged ?
$merged->getNativeData() : $merged );
176 public static function dataGetAutosummary() {
179 'Hello there, world!',
182 '/^Redirected page .*Foo/'
189 '/^Created page .*Hello/'
196 '/^Created blank page$/'
200 'Hello there, world!',
207 'Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy
208 eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam
209 voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet
210 clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet.',
213 '/^Replaced .*Hello/'
226 * @dataProvider dataGetAutosummary
227 * @covers WikitextContentHandler::getAutosummary
229 public function testGetAutosummary( $old, $new, $flags, $expected ) {
230 $oldContent = is_null( $old ) ?
null : new WikitextContent( $old );
231 $newContent = is_null( $new ) ?
null : new WikitextContent( $new );
233 $summary = $this->handler
->getAutosummary( $oldContent, $newContent, $flags );
236 (bool)preg_match( $expected, $summary ),
237 "Autosummary didn't match expected pattern $expected: $summary"
241 public static function dataGetChangeTag() {
261 'mw-removed-redirect'
268 'mw-changed-redirect-target'
275 null // mw-newpage is not defined as a tag
282 null // mw-newblank is not defined as a tag
293 'Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy
294 eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam
295 voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet
296 clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet.',
303 'Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy
304 eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam
305 voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet
306 clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet.',
307 'Duis purus odio, rhoncus et finibus dapibus, facilisis ac urna. Pellentesque
308 arcu, tristique nec tempus nec, suscipit vel arcu. Sed non dolor nec ligula
309 congue tempor. Quisque pellentesque finibus orci a molestie. Nam maximus, purus
310 euismod finibus mollis, dui ante malesuada felis, dignissim rutrum diam sapien.',
318 * @dataProvider dataGetChangeTag
319 * @covers WikitextContentHandler::getChangeTag
321 public function testGetChangeTag( $old, $new, $flags, $expected ) {
322 $this->setMwGlobals( 'wgSoftwareTags', [
323 'mw-new-redirect' => true,
324 'mw-removed-redirect' => true,
325 'mw-changed-redirect-target' => true,
326 'mw-newpage' => true,
327 'mw-newblank' => true,
329 'mw-replace' => true,
331 $oldContent = is_null( $old ) ?
null : new WikitextContent( $old );
332 $newContent = is_null( $new ) ?
null : new WikitextContent( $new );
334 $tag = $this->handler
->getChangeTag( $oldContent, $newContent, $flags );
336 $this->assertSame( $expected, $tag );
340 * @covers WikitextContentHandler::getDataForSearchIndex
342 public function testDataIndexFieldsFile() {
343 $mockEngine = $this->createMock( SearchEngine
::class );
344 $title = Title
::newFromText( 'Somefile.jpg', NS_FILE
);
345 $page = new WikiPage( $title );
347 $fileHandler = $this->getMockBuilder( FileContentHandler
::class )
348 ->disableOriginalConstructor()
349 ->setMethods( [ 'getDataForSearchIndex' ] )
352 $handler = $this->getMockBuilder( WikitextContentHandler
::class )
353 ->disableOriginalConstructor()
354 ->setMethods( [ 'getFileHandler' ] )
357 $handler->method( 'getFileHandler' )->will( $this->returnValue( $fileHandler ) );
358 $fileHandler->expects( $this->once() )
359 ->method( 'getDataForSearchIndex' )
360 ->will( $this->returnValue( [ 'file_text' => 'This is file content' ] ) );
362 $data = $handler->getDataForSearchIndex( $page, new ParserOutput(), $mockEngine );
363 $this->assertArrayHasKey( 'file_text', $data );
364 $this->assertEquals( 'This is file content', $data['file_text'] );