wiki.techinc.nl/tests/phpunit/includes/content/WikitextContentHandlerTest.php

263 lines
6.7 KiB
PHP
Raw Normal View History

2012-04-23 12:28:57 +00:00
<?php
/**
* @group ContentHandler
*/
class WikitextContentHandlerTest extends MediaWikiLangTestCase {
2012-04-25 16:34:36 +00:00
/**
* @var ContentHandler
*/
private $handler;
2012-04-23 12:28:57 +00:00
protected function setUp() {
parent::setUp();
$this->handler = ContentHandler::getForModelID( CONTENT_MODEL_WIKITEXT );
2012-04-25 16:35:36 +00:00
}
2012-04-23 12:28:57 +00:00
/**
* @covers WikitextContentHandler::serializeContent
*/
public function testSerializeContent() {
2012-04-23 12:28:57 +00:00
$content = new WikitextContent( 'hello world' );
$this->assertEquals( 'hello world', $this->handler->serializeContent( $content ) );
$this->assertEquals(
'hello world',
$this->handler->serializeContent( $content, CONTENT_FORMAT_WIKITEXT )
);
2012-04-23 12:28:57 +00:00
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() {
2012-04-23 12:28:57 +00:00
$content = $this->handler->unserializeContent( 'hello world' );
$this->assertEquals( 'hello world', $content->getNativeData() );
$content = $this->handler->unserializeContent( 'hello world', CONTENT_FORMAT_WIKITEXT );
2012-04-23 12:28:57 +00:00
$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
*/
2012-04-23 12:28:57 +00:00
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 ],
];
2012-04-23 12:28:57 +00:00
}
/**
* @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 ) {
global $wgContLang;
$wgContLang->resetNamespaces();
MagicWord::clearCache();
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]]'
],
];
}
2012-04-23 12:28:57 +00:00
/**
* @dataProvider dataIsSupportedFormat
* @covers WikitextContentHandler::isSupportedFormat
2012-04-23 12:28:57 +00:00
*/
public function testIsSupportedFormat( $format, $supported ) {
$this->assertEquals( $supported, $this->handler->isSupportedFormat( $format ) );
}
public function testSupportsDirectEditing() {
$handler = new WikiTextContentHandler();
$this->assertTrue( $handler->supportsDirectEditing(), 'direct editing is supported' );
}
public static function dataMerge3() {
return [
[
"first paragraph
2012-04-23 12:28:57 +00:00
second paragraph\n",
"FIRST paragraph
2012-04-23 12:28:57 +00:00
second paragraph\n",
"first paragraph
2012-04-23 12:28:57 +00:00
SECOND paragraph\n",
"FIRST paragraph
2012-04-23 12:28:57 +00:00
SECOND paragraph\n",
],
2012-04-23 12:28:57 +00:00
[ "first paragraph
2012-04-23 12:28:57 +00:00
second paragraph\n",
"Bla bla\n",
2012-04-23 12:28:57 +00:00
"Blubberdibla\n",
2012-04-23 12:28:57 +00:00
false,
],
];
2012-04-23 12:28:57 +00:00
}
/**
* @dataProvider dataMerge3
* @covers WikitextContentHandler::merge3
2012-04-23 12:28:57 +00:00
*/
public function testMerge3( $old, $mine, $yours, $expected ) {
$this->markTestSkippedIfNoDiff3();
// test merge
2012-04-23 12:28:57 +00:00
$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 [
[
2012-04-23 12:28:57 +00:00
'Hello there, world!',
'#REDIRECT [[Foo]]',
0,
'/^Redirected page .*Foo/'
],
2012-04-23 12:28:57 +00:00
[
2012-04-23 12:28:57 +00:00
null,
'Hello world!',
EDIT_NEW,
'/^Created page .*Hello/'
],
2012-04-23 12:28:57 +00:00
[
2012-04-23 12:28:57 +00:00
'Hello there, world!',
'',
0,
'/^Blanked/'
],
2012-04-23 12:28:57 +00:00
[
'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.',
2012-04-23 12:28:57 +00:00
'Hello world!',
0,
'/^Replaced .*Hello/'
],
2012-04-23 12:28:57 +00:00
[
2012-04-23 12:28:57 +00:00
'foo',
'bar',
0,
'/^$/'
],
];
2012-04-23 12:28:57 +00:00
}
/**
* @dataProvider dataGetAutosummary
* @covers WikitextContentHandler::getAutosummary
2012-04-23 12:28:57 +00:00
*/
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"
);
2012-04-23 12:28:57 +00:00
}
/**
* @todo Text case requires database, should be done by a test class in the Database group
2012-04-23 12:28:57 +00:00
*/
/*
public function testGetAutoDeleteReason( Title $title, &$hasHistory ) {}
2012-04-23 12:28:57 +00:00
*/
/**
* @todo Text case requires database, should be done by a test class in the Database group
2012-04-23 12:28:57 +00:00
*/
/*
public function testGetUndoContent( Revision $current, Revision $undo,
Revision $undoafter = null
) {
}
2012-04-23 12:28:57 +00:00
*/
public function testDataIndexFieldsFile() {
$mockEngine = $this->getMock( 'SearchEngine' );
$title = Title::newFromText( 'Somefile.jpg', NS_FILE );
$page = new WikiPage( $title );
$handler = $this->getMockBuilder( WikitextContentHandler::class )
->disableOriginalConstructor()
->setMethods( [ 'getFileText' ] )
->getMock();
$handler->method( 'getFileText' )->will( $this->returnValue( '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'] );
}
2012-04-23 12:28:57 +00:00
}