Merge "Added release notes for 'ContentHandler::runLegacyHooks' removal"
[mediawiki.git] / tests / phpunit / includes / content / WikitextContentHandlerTest.php
blobec97d763710d4cd85408f4082877d1b0a05615ec
1 <?php
3 /**
4 * @group ContentHandler
5 */
6 class WikitextContentHandlerTest extends MediaWikiLangTestCase {
7 /**
8 * @var ContentHandler
9 */
10 private $handler;
12 protected function setUp() {
13 parent::setUp();
15 $this->handler = ContentHandler::getForModelID( CONTENT_MODEL_WIKITEXT );
18 /**
19 * @covers WikitextContentHandler::serializeContent
21 public function testSerializeContent() {
22 $content = new WikitextContent( 'hello world' );
24 $this->assertEquals( 'hello world', $this->handler->serializeContent( $content ) );
25 $this->assertEquals(
26 'hello world',
27 $this->handler->serializeContent( $content, CONTENT_FORMAT_WIKITEXT )
30 try {
31 $this->handler->serializeContent( $content, 'dummy/foo' );
32 $this->fail( "serializeContent() should have failed on unknown format" );
33 } catch ( MWException $e ) {
34 // ok, as expected
38 /**
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() );
48 try {
49 $this->handler->unserializeContent( 'hello world', 'dummy/foo' );
50 $this->fail( "unserializeContent() should have failed on unknown format" );
51 } catch ( MWException $e ) {
52 // ok, as expected
56 /**
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() {
67 return [
68 [ null, true ],
69 [ CONTENT_FORMAT_WIKITEXT, true ],
70 [ 99887766, false ],
74 /**
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 ) {
81 global $wgContLang;
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() {
94 return [
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 ) );
118 public function testSupportsDirectEditing() {
119 $handler = new WikiTextContentHandler();
120 $this->assertTrue( $handler->supportsDirectEditing(), 'direct editing is supported' );
123 public static function dataMerge3() {
124 return [
126 "first paragraph
128 second paragraph\n",
130 "FIRST paragraph
132 second paragraph\n",
134 "first paragraph
136 SECOND paragraph\n",
138 "FIRST paragraph
140 SECOND paragraph\n",
143 [ "first paragraph
144 second paragraph\n",
146 "Bla bla\n",
148 "Blubberdibla\n",
150 false,
156 * @dataProvider dataMerge3
157 * @covers WikitextContentHandler::merge3
159 public function testMerge3( $old, $mine, $yours, $expected ) {
160 $this->markTestSkippedIfNoDiff3();
162 // test merge
163 $oldContent = new WikitextContent( $old );
164 $myContent = new WikitextContent( $mine );
165 $yourContent = new WikitextContent( $yours );
167 $merged = $this->handler->merge3( $oldContent, $myContent, $yourContent );
169 $this->assertEquals( $expected, $merged ? $merged->getNativeData() : $merged );
172 public static function dataGetAutosummary() {
173 return [
175 'Hello there, world!',
176 '#REDIRECT [[Foo]]',
178 '/^Redirected page .*Foo/'
182 null,
183 'Hello world!',
184 EDIT_NEW,
185 '/^Created page .*Hello/'
189 'Hello there, world!',
192 '/^Blanked/'
196 'Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy
197 eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam
198 voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet
199 clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet.',
200 'Hello world!',
202 '/^Replaced .*Hello/'
206 'foo',
207 'bar',
209 '/^$/'
215 * @dataProvider dataGetAutosummary
216 * @covers WikitextContentHandler::getAutosummary
218 public function testGetAutosummary( $old, $new, $flags, $expected ) {
219 $oldContent = is_null( $old ) ? null : new WikitextContent( $old );
220 $newContent = is_null( $new ) ? null : new WikitextContent( $new );
222 $summary = $this->handler->getAutosummary( $oldContent, $newContent, $flags );
224 $this->assertTrue(
225 (bool)preg_match( $expected, $summary ),
226 "Autosummary didn't match expected pattern $expected: $summary"
231 * @todo Text case requires database, should be done by a test class in the Database group
234 public function testGetAutoDeleteReason( Title $title, &$hasHistory ) {}
238 * @todo Text case requires database, should be done by a test class in the Database group
241 public function testGetUndoContent( Revision $current, Revision $undo,
242 Revision $undoafter = null
247 public function testDataIndexFieldsFile() {
248 $mockEngine = $this->getMock( 'SearchEngine' );
249 $title = Title::newFromText( 'Somefile.jpg', NS_FILE );
250 $page = new WikiPage( $title );
252 $fileHandler = $this->getMockBuilder( FileContentHandler::class )
253 ->disableOriginalConstructor()
254 ->setMethods( [ 'getDataForSearchIndex' ] )
255 ->getMock();
257 $handler = $this->getMockBuilder( WikitextContentHandler::class )
258 ->disableOriginalConstructor()
259 ->setMethods( [ 'getFileHandler' ] )
260 ->getMock();
262 $handler->method( 'getFileHandler' )->will( $this->returnValue( $fileHandler ) );
263 $fileHandler->expects( $this->once() )
264 ->method( 'getDataForSearchIndex' )
265 ->will( $this->returnValue( [ 'file_text' => 'This is file content' ] ) );
267 $data = $handler->getDataForSearchIndex( $page, new ParserOutput(), $mockEngine );
268 $this->assertArrayHasKey( 'file_text', $data );
269 $this->assertEquals( 'This is file content', $data['file_text'] );