123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394 |
- <?php
- use MediaWiki\MediaWikiServices;
- use MediaWiki\Revision\SlotRenderingProvider;
- /**
- * @group ContentHandler
- */
- class WikitextContentHandlerTest extends MediaWikiLangTestCase {
- /**
- * @var ContentHandler
- */
- private $handler;
- protected function setUp() {
- parent::setUp();
- $this->handler = ContentHandler::getForModelID( CONTENT_MODEL_WIKITEXT );
- }
- /**
- * @covers WikitextContentHandler::serializeContent
- */
- public function testSerializeContent() {
- $content = new WikitextContent( 'hello world' );
- $this->assertEquals( 'hello world', $this->handler->serializeContent( $content ) );
- $this->assertEquals(
- 'hello world',
- $this->handler->serializeContent( $content, CONTENT_FORMAT_WIKITEXT )
- );
- try {
- $this->handler->serializeContent( $content, 'dummy/foo' );
- $this->fail( "serializeContent() should have failed on unknown format" );
- } catch ( MWException $e ) {
- // ok, as expected
- }
- }
- /**
- * @covers WikitextContentHandler::unserializeContent
- */
- public function testUnserializeContent() {
- $content = $this->handler->unserializeContent( 'hello world' );
- $this->assertEquals( 'hello world', $content->getNativeData() );
- $content = $this->handler->unserializeContent( 'hello world', CONTENT_FORMAT_WIKITEXT );
- $this->assertEquals( 'hello world', $content->getNativeData() );
- try {
- $this->handler->unserializeContent( 'hello world', 'dummy/foo' );
- $this->fail( "unserializeContent() should have failed on unknown format" );
- } catch ( MWException $e ) {
- // ok, as expected
- }
- }
- /**
- * @covers WikitextContentHandler::makeEmptyContent
- */
- public function testMakeEmptyContent() {
- $content = $this->handler->makeEmptyContent();
- $this->assertTrue( $content->isEmpty() );
- $this->assertEquals( '', $content->getNativeData() );
- }
- public static function dataIsSupportedFormat() {
- return [
- [ null, true ],
- [ CONTENT_FORMAT_WIKITEXT, true ],
- [ 99887766, false ],
- ];
- }
- /**
- * @dataProvider provideMakeRedirectContent
- * @param Title|string $title Title object or string for Title::newFromText()
- * @param string $expected Serialized form of the content object built
- * @covers WikitextContentHandler::makeRedirectContent
- */
- public function testMakeRedirectContent( $title, $expected ) {
- MediaWikiServices::getInstance()->getContentLanguage()->resetNamespaces();
- MediaWikiServices::getInstance()->resetServiceForTesting( 'MagicWordFactory' );
- if ( is_string( $title ) ) {
- $title = Title::newFromText( $title );
- }
- $content = $this->handler->makeRedirectContent( $title );
- $this->assertEquals( $expected, $content->serialize() );
- }
- public static function provideMakeRedirectContent() {
- return [
- [ 'Hello', '#REDIRECT [[Hello]]' ],
- [ 'Template:Hello', '#REDIRECT [[Template:Hello]]' ],
- [ 'Hello#section', '#REDIRECT [[Hello#section]]' ],
- [ 'user:john_doe#section', '#REDIRECT [[User:John doe#section]]' ],
- [ 'MEDIAWIKI:FOOBAR', '#REDIRECT [[MediaWiki:FOOBAR]]' ],
- [ 'Category:Foo', '#REDIRECT [[:Category:Foo]]' ],
- [ Title::makeTitle( NS_MAIN, 'en:Foo' ), '#REDIRECT [[en:Foo]]' ],
- [ Title::makeTitle( NS_MAIN, 'Foo', '', 'en' ), '#REDIRECT [[:en:Foo]]' ],
- [
- Title::makeTitle( NS_MAIN, 'Bar', 'fragment', 'google' ),
- '#REDIRECT [[google:Bar#fragment]]'
- ],
- ];
- }
- /**
- * @dataProvider dataIsSupportedFormat
- * @covers WikitextContentHandler::isSupportedFormat
- */
- public function testIsSupportedFormat( $format, $supported ) {
- $this->assertEquals( $supported, $this->handler->isSupportedFormat( $format ) );
- }
- /**
- * @covers WikitextContentHandler::supportsDirectEditing
- */
- public function testSupportsDirectEditing() {
- $handler = new WikiTextContentHandler();
- $this->assertTrue( $handler->supportsDirectEditing(), 'direct editing is supported' );
- }
- public static function dataMerge3() {
- return [
- [
- "first paragraph
- second paragraph\n",
- "FIRST paragraph
- second paragraph\n",
- "first paragraph
- SECOND paragraph\n",
- "FIRST paragraph
- SECOND paragraph\n",
- ],
- [ "first paragraph
- second paragraph\n",
- "Bla bla\n",
- "Blubberdibla\n",
- false,
- ],
- ];
- }
- /**
- * @dataProvider dataMerge3
- * @covers WikitextContentHandler::merge3
- */
- public function testMerge3( $old, $mine, $yours, $expected ) {
- $this->markTestSkippedIfNoDiff3();
- // test merge
- $oldContent = new WikitextContent( $old );
- $myContent = new WikitextContent( $mine );
- $yourContent = new WikitextContent( $yours );
- $merged = $this->handler->merge3( $oldContent, $myContent, $yourContent );
- $this->assertEquals( $expected, $merged ? $merged->getNativeData() : $merged );
- }
- public static function dataGetAutosummary() {
- return [
- [
- 'Hello there, world!',
- '#REDIRECT [[Foo]]',
- 0,
- '/^Redirected page .*Foo/'
- ],
- [
- null,
- 'Hello world!',
- EDIT_NEW,
- '/^Created page .*Hello/'
- ],
- [
- null,
- '',
- EDIT_NEW,
- '/^Created blank page$/'
- ],
- [
- 'Hello there, world!',
- '',
- 0,
- '/^Blanked/'
- ],
- [
- 'Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy
- eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam
- voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet
- clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet.',
- 'Hello world!',
- 0,
- '/^Replaced .*Hello/'
- ],
- [
- 'foo',
- 'bar',
- 0,
- '/^$/'
- ],
- ];
- }
- /**
- * @dataProvider dataGetAutosummary
- * @covers WikitextContentHandler::getAutosummary
- */
- public function testGetAutosummary( $old, $new, $flags, $expected ) {
- $oldContent = is_null( $old ) ? null : new WikitextContent( $old );
- $newContent = is_null( $new ) ? null : new WikitextContent( $new );
- $summary = $this->handler->getAutosummary( $oldContent, $newContent, $flags );
- $this->assertTrue(
- (bool)preg_match( $expected, $summary ),
- "Autosummary didn't match expected pattern $expected: $summary"
- );
- }
- public static function dataGetChangeTag() {
- return [
- [
- null,
- '#REDIRECT [[Foo]]',
- 0,
- 'mw-new-redirect'
- ],
- [
- 'Lorem ipsum dolor',
- '#REDIRECT [[Foo]]',
- 0,
- 'mw-new-redirect'
- ],
- [
- '#REDIRECT [[Foo]]',
- 'Lorem ipsum dolor',
- 0,
- 'mw-removed-redirect'
- ],
- [
- '#REDIRECT [[Foo]]',
- '#REDIRECT [[Bar]]',
- 0,
- 'mw-changed-redirect-target'
- ],
- [
- null,
- 'Lorem ipsum dolor',
- EDIT_NEW,
- null // mw-newpage is not defined as a tag
- ],
- [
- null,
- '',
- EDIT_NEW,
- null // mw-newblank is not defined as a tag
- ],
- [
- 'Lorem ipsum dolor',
- '',
- 0,
- 'mw-blank'
- ],
- [
- 'Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy
- eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam
- voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet
- clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet.',
- 'Ipsum',
- 0,
- 'mw-replace'
- ],
- [
- 'Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy
- eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam
- voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet
- clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet.',
- 'Duis purus odio, rhoncus et finibus dapibus, facilisis ac urna. Pellentesque
- arcu, tristique nec tempus nec, suscipit vel arcu. Sed non dolor nec ligula
- congue tempor. Quisque pellentesque finibus orci a molestie. Nam maximus, purus
- euismod finibus mollis, dui ante malesuada felis, dignissim rutrum diam sapien.',
- 0,
- null
- ],
- ];
- }
- /**
- * @dataProvider dataGetChangeTag
- * @covers WikitextContentHandler::getChangeTag
- */
- public function testGetChangeTag( $old, $new, $flags, $expected ) {
- $this->setMwGlobals( 'wgSoftwareTags', [
- 'mw-new-redirect' => true,
- 'mw-removed-redirect' => true,
- 'mw-changed-redirect-target' => true,
- 'mw-newpage' => true,
- 'mw-newblank' => true,
- 'mw-blank' => true,
- 'mw-replace' => true,
- ] );
- $oldContent = is_null( $old ) ? null : new WikitextContent( $old );
- $newContent = is_null( $new ) ? null : new WikitextContent( $new );
- $tag = $this->handler->getChangeTag( $oldContent, $newContent, $flags );
- $this->assertSame( $expected, $tag );
- }
- /**
- * @covers WikitextContentHandler::getDataForSearchIndex
- */
- public function testDataIndexFieldsFile() {
- $mockEngine = $this->createMock( SearchEngine::class );
- $title = Title::newFromText( 'Somefile.jpg', NS_FILE );
- $page = new WikiPage( $title );
- $fileHandler = $this->getMockBuilder( FileContentHandler::class )
- ->disableOriginalConstructor()
- ->setMethods( [ 'getDataForSearchIndex' ] )
- ->getMock();
- $handler = $this->getMockBuilder( WikitextContentHandler::class )
- ->disableOriginalConstructor()
- ->setMethods( [ 'getFileHandler' ] )
- ->getMock();
- $handler->method( 'getFileHandler' )->will( $this->returnValue( $fileHandler ) );
- $fileHandler->expects( $this->once() )
- ->method( 'getDataForSearchIndex' )
- ->will( $this->returnValue( [ 'file_text' => 'This is file content' ] ) );
- $data = $handler->getDataForSearchIndex( $page, new ParserOutput(), $mockEngine );
- $this->assertArrayHasKey( 'file_text', $data );
- $this->assertEquals( 'This is file content', $data['file_text'] );
- }
- public function testGetSecondaryDataUpdates() {
- $title = Title::newFromText( 'Somefile.jpg', NS_FILE );
- $content = new WikitextContent( '' );
- /** @var SlotRenderingProvider $srp */
- $srp = $this->getMock( SlotRenderingProvider::class );
- $handler = new WikitextContentHandler();
- $updates = $handler->getSecondaryDataUpdates( $title, $content, 'main', $srp );
- $this->assertEquals( [], $updates );
- }
- public function testGetDeletionUpdates() {
- $title = Title::newFromText( 'Somefile.jpg', NS_FILE );
- $content = new WikitextContent( '' );
- $srp = $this->getMock( SlotRenderingProvider::class );
- $handler = new WikitextContentHandler();
- $updates = $handler->getDeletionUpdates( $title, 'main' );
- $this->assertEquals( [], $updates );
- }
- }
|