4 * @group ContentHandler
6 class WikitextContentHandlerTest
extends MediaWikiLangTestCase
{
12 protected function setUp() {
15 $this->handler
= ContentHandler
::getForModelID( CONTENT_MODEL_WIKITEXT
);
19 * @covers WikitextContentHandler::serializeContent
21 public function testSerializeContent() {
22 $content = new WikitextContent( 'hello world' );
24 $this->assertEquals( 'hello world', $this->handler
->serializeContent( $content ) );
27 $this->handler
->serializeContent( $content, CONTENT_FORMAT_WIKITEXT
)
31 $this->handler
->serializeContent( $content, 'dummy/foo' );
32 $this->fail( "serializeContent() should have failed on unknown format" );
33 } catch ( MWException
$e ) {
39 * @covers WikitextContentHandler::unserializeContent
41 public function testUnserializeContent() {
42 $content = $this->handler
->unserializeContent( 'hello world' );
43 $this->assertEquals( 'hello world', $content->getNativeData() );
45 $content = $this->handler
->unserializeContent( 'hello world', CONTENT_FORMAT_WIKITEXT
);
46 $this->assertEquals( 'hello world', $content->getNativeData() );
49 $this->handler
->unserializeContent( 'hello world', 'dummy/foo' );
50 $this->fail( "unserializeContent() should have failed on unknown format" );
51 } catch ( MWException
$e ) {
57 * @covers WikitextContentHandler::makeEmptyContent
59 public function testMakeEmptyContent() {
60 $content = $this->handler
->makeEmptyContent();
62 $this->assertTrue( $content->isEmpty() );
63 $this->assertEquals( '', $content->getNativeData() );
66 public static function dataIsSupportedFormat() {
69 [ CONTENT_FORMAT_WIKITEXT
, true ],
75 * @dataProvider provideMakeRedirectContent
76 * @param Title|string $title Title object or string for Title::newFromText()
77 * @param string $expected Serialized form of the content object built
78 * @covers WikitextContentHandler::makeRedirectContent
80 public function testMakeRedirectContent( $title, $expected ) {
82 $wgContLang->resetNamespaces();
84 MagicWord
::clearCache();
86 if ( is_string( $title ) ) {
87 $title = Title
::newFromText( $title );
89 $content = $this->handler
->makeRedirectContent( $title );
90 $this->assertEquals( $expected, $content->serialize() );
93 public static function provideMakeRedirectContent() {
95 [ 'Hello', '#REDIRECT [[Hello]]' ],
96 [ 'Template:Hello', '#REDIRECT [[Template:Hello]]' ],
97 [ 'Hello#section', '#REDIRECT [[Hello#section]]' ],
98 [ 'user:john_doe#section', '#REDIRECT [[User:John doe#section]]' ],
99 [ 'MEDIAWIKI:FOOBAR', '#REDIRECT [[MediaWiki:FOOBAR]]' ],
100 [ 'Category:Foo', '#REDIRECT [[:Category:Foo]]' ],
101 [ Title
::makeTitle( NS_MAIN
, 'en:Foo' ), '#REDIRECT [[en:Foo]]' ],
102 [ Title
::makeTitle( NS_MAIN
, 'Foo', '', 'en' ), '#REDIRECT [[:en:Foo]]' ],
104 Title
::makeTitle( NS_MAIN
, 'Bar', 'fragment', 'google' ),
105 '#REDIRECT [[google:Bar#fragment]]'
111 * @dataProvider dataIsSupportedFormat
112 * @covers WikitextContentHandler::isSupportedFormat
114 public function testIsSupportedFormat( $format, $supported ) {
115 $this->assertEquals( $supported, $this->handler
->isSupportedFormat( $format ) );
119 * @covers WikitextContentHandler::supportsDirectEditing
121 public function testSupportsDirectEditing() {
122 $handler = new WikiTextContentHandler();
123 $this->assertTrue( $handler->supportsDirectEditing(), 'direct editing is supported' );
126 public static function dataMerge3() {
159 * @dataProvider dataMerge3
160 * @covers WikitextContentHandler::merge3
162 public function testMerge3( $old, $mine, $yours, $expected ) {
163 $this->markTestSkippedIfNoDiff3();
166 $oldContent = new WikitextContent( $old );
167 $myContent = new WikitextContent( $mine );
168 $yourContent = new WikitextContent( $yours );
170 $merged = $this->handler
->merge3( $oldContent, $myContent, $yourContent );
172 $this->assertEquals( $expected, $merged ?
$merged->getNativeData() : $merged );
175 public static function dataGetAutosummary() {
178 'Hello there, world!',
181 '/^Redirected page .*Foo/'
188 '/^Created page .*Hello/'
195 '/^Created blank page$/'
199 'Hello there, world!',
206 'Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy
207 eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam
208 voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet
209 clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet.',
212 '/^Replaced .*Hello/'
225 * @dataProvider dataGetAutosummary
226 * @covers WikitextContentHandler::getAutosummary
228 public function testGetAutosummary( $old, $new, $flags, $expected ) {
229 $oldContent = is_null( $old ) ?
null : new WikitextContent( $old );
230 $newContent = is_null( $new ) ?
null : new WikitextContent( $new );
232 $summary = $this->handler
->getAutosummary( $oldContent, $newContent, $flags );
235 (bool)preg_match( $expected, $summary ),
236 "Autosummary didn't match expected pattern $expected: $summary"
240 public static function dataGetChangeTag() {
260 'mw-removed-redirect'
267 'mw-changed-redirect-target'
274 null // mw-newpage is not defined as a tag
281 null // mw-newblank is not defined as a tag
292 'Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy
293 eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam
294 voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet
295 clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet.',
302 'Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy
303 eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam
304 voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet
305 clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet.',
306 'Duis purus odio, rhoncus et finibus dapibus, facilisis ac urna. Pellentesque
307 arcu, tristique nec tempus nec, suscipit vel arcu. Sed non dolor nec ligula
308 congue tempor. Quisque pellentesque finibus orci a molestie. Nam maximus, purus
309 euismod finibus mollis, dui ante malesuada felis, dignissim rutrum diam sapien.',
317 * @dataProvider dataGetChangeTag
318 * @covers WikitextContentHandler::getChangeTag
320 public function testGetChangeTag( $old, $new, $flags, $expected ) {
321 $this->setMwGlobals( 'wgSoftwareTags', [
322 'mw-new-redirect' => true,
323 'mw-removed-redirect' => true,
324 'mw-changed-redirect-target' => true,
325 'mw-newpage' => true,
326 'mw-newblank' => true,
328 'mw-replace' => true,
330 $oldContent = is_null( $old ) ?
null : new WikitextContent( $old );
331 $newContent = is_null( $new ) ?
null : new WikitextContent( $new );
333 $tag = $this->handler
->getChangeTag( $oldContent, $newContent, $flags );
335 $this->assertSame( $expected, $tag );
339 * @todo Text case requires database, should be done by a test class in the Database group
342 public function testGetAutoDeleteReason( Title $title, &$hasHistory ) {}
346 * @todo Text case requires database, should be done by a test class in the Database group
349 public function testGetUndoContent( Revision $current, Revision $undo,
350 Revision $undoafter = null
356 * @covers WikitextContentHandler::getDataForSearchIndex
358 public function testDataIndexFieldsFile() {
359 $mockEngine = $this->createMock( 'SearchEngine' );
360 $title = Title
::newFromText( 'Somefile.jpg', NS_FILE
);
361 $page = new WikiPage( $title );
363 $fileHandler = $this->getMockBuilder( FileContentHandler
::class )
364 ->disableOriginalConstructor()
365 ->setMethods( [ 'getDataForSearchIndex' ] )
368 $handler = $this->getMockBuilder( WikitextContentHandler
::class )
369 ->disableOriginalConstructor()
370 ->setMethods( [ 'getFileHandler' ] )
373 $handler->method( 'getFileHandler' )->will( $this->returnValue( $fileHandler ) );
374 $fileHandler->expects( $this->once() )
375 ->method( 'getDataForSearchIndex' )
376 ->will( $this->returnValue( [ 'file_text' => 'This is file content' ] ) );
378 $data = $handler->getDataForSearchIndex( $page, new ParserOutput(), $mockEngine );
379 $this->assertArrayHasKey( 'file_text', $data );
380 $this->assertEquals( 'This is file content', $data['file_text'] );