Release notes for Iabf4873f
[mediawiki.git] / tests / phpunit / includes / content / WikitextContentHandlerTest.php
blob73b975baa8885823e4852bb8f891e5619fb005e1
1 <?php
3 /**
4 * @group ContentHandler
5 */
6 class WikitextContentHandlerTest extends MediaWikiLangTestCase {
8 /**
9 * @var ContentHandler
11 var $handler;
13 protected function setUp() {
14 parent::setUp();
16 $this->handler = ContentHandler::getForModelID( CONTENT_MODEL_WIKITEXT );
19 /**
20 * @covers WikitextContentHandler::serializeContent
22 public function testSerializeContent() {
23 $content = new WikitextContent( 'hello world' );
25 $this->assertEquals( 'hello world', $this->handler->serializeContent( $content ) );
26 $this->assertEquals( 'hello world', $this->handler->serializeContent( $content, CONTENT_FORMAT_WIKITEXT ) );
28 try {
29 $this->handler->serializeContent( $content, 'dummy/foo' );
30 $this->fail( "serializeContent() should have failed on unknown format" );
31 } catch ( MWException $e ) {
32 // ok, as expected
36 /**
37 * @covers WikitextContentHandler::unserializeContent
39 public function testUnserializeContent() {
40 $content = $this->handler->unserializeContent( 'hello world' );
41 $this->assertEquals( 'hello world', $content->getNativeData() );
43 $content = $this->handler->unserializeContent( 'hello world', CONTENT_FORMAT_WIKITEXT );
44 $this->assertEquals( 'hello world', $content->getNativeData() );
46 try {
47 $this->handler->unserializeContent( 'hello world', 'dummy/foo' );
48 $this->fail( "unserializeContent() should have failed on unknown format" );
49 } catch ( MWException $e ) {
50 // ok, as expected
54 /**
55 * @covers WikitextContentHandler::makeEmptyContent
57 public function testMakeEmptyContent() {
58 $content = $this->handler->makeEmptyContent();
60 $this->assertTrue( $content->isEmpty() );
61 $this->assertEquals( '', $content->getNativeData() );
64 public static function dataIsSupportedFormat() {
65 return array(
66 array( null, true ),
67 array( CONTENT_FORMAT_WIKITEXT, true ),
68 array( 99887766, false ),
72 /**
73 * @dataProvider provideMakeRedirectContent
74 * @param Title|string $title Title object or string for Title::newFromText()
75 * @param string $expected Serialized form of the content object built
76 * @covers WikitextContentHandler::makeRedirectContent
78 public function testMakeRedirectContent( $title, $expected ) {
79 global $wgContLang;
80 $wgContLang->resetNamespaces();
82 MagicWord::clearCache();
84 if ( is_string( $title ) ) {
85 $title = Title::newFromText( $title );
87 $content = $this->handler->makeRedirectContent( $title );
88 $this->assertEquals( $expected, $content->serialize() );
91 public static function provideMakeRedirectContent() {
92 return array(
93 array( 'Hello', '#REDIRECT [[Hello]]' ),
94 array( 'Template:Hello', '#REDIRECT [[Template:Hello]]' ),
95 array( 'Hello#section', '#REDIRECT [[Hello#section]]' ),
96 array( 'user:john_doe#section', '#REDIRECT [[User:John doe#section]]' ),
97 array( 'MEDIAWIKI:FOOBAR', '#REDIRECT [[MediaWiki:FOOBAR]]' ),
98 array( 'Category:Foo', '#REDIRECT [[:Category:Foo]]' ),
99 array( Title::makeTitle( NS_MAIN, 'en:Foo' ), '#REDIRECT [[en:Foo]]' ),
100 array( Title::makeTitle( NS_MAIN, 'Foo', '', 'en' ), '#REDIRECT [[:en:Foo]]' ),
101 array( Title::makeTitle( NS_MAIN, 'Bar', 'fragment', 'google' ), '#REDIRECT [[google:Bar#fragment]]' ),
106 * @dataProvider dataIsSupportedFormat
107 * @covers WikitextContentHandler::isSupportedFormat
109 public function testIsSupportedFormat( $format, $supported ) {
110 $this->assertEquals( $supported, $this->handler->isSupportedFormat( $format ) );
113 public static function dataMerge3() {
114 return array(
115 array(
116 "first paragraph
118 second paragraph\n",
120 "FIRST paragraph
122 second paragraph\n",
124 "first paragraph
126 SECOND paragraph\n",
128 "FIRST paragraph
130 SECOND paragraph\n",
133 array( "first paragraph
134 second paragraph\n",
136 "Bla bla\n",
138 "Blubberdibla\n",
140 false,
146 * @dataProvider dataMerge3
147 * @covers WikitextContentHandler::merge3
149 public function testMerge3( $old, $mine, $yours, $expected ) {
150 $this->checkHasDiff3();
152 // test merge
153 $oldContent = new WikitextContent( $old );
154 $myContent = new WikitextContent( $mine );
155 $yourContent = new WikitextContent( $yours );
157 $merged = $this->handler->merge3( $oldContent, $myContent, $yourContent );
159 $this->assertEquals( $expected, $merged ? $merged->getNativeData() : $merged );
162 public static function dataGetAutosummary() {
163 return array(
164 array(
165 'Hello there, world!',
166 '#REDIRECT [[Foo]]',
168 '/^Redirected page .*Foo/'
171 array(
172 null,
173 'Hello world!',
174 EDIT_NEW,
175 '/^Created page .*Hello/'
178 array(
179 'Hello there, world!',
182 '/^Blanked/'
185 array(
186 'Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut
187 labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et
188 ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet.',
189 'Hello world!',
191 '/^Replaced .*Hello/'
194 array(
195 'foo',
196 'bar',
198 '/^$/'
204 * @dataProvider dataGetAutosummary
205 * @covers WikitextContentHandler::getAutosummary
207 public function testGetAutosummary( $old, $new, $flags, $expected ) {
208 $oldContent = is_null( $old ) ? null : new WikitextContent( $old );
209 $newContent = is_null( $new ) ? null : new WikitextContent( $new );
211 $summary = $this->handler->getAutosummary( $oldContent, $newContent, $flags );
213 $this->assertTrue( (bool)preg_match( $expected, $summary ), "Autosummary didn't match expected pattern $expected: $summary" );
217 * @todo Text case requires database, should be done by a test class in the Database group
220 public function testGetAutoDeleteReason( Title $title, &$hasHistory ) {}
224 * @todo Text case requires database, should be done by a test class in the Database group
227 public function testGetUndoContent( Revision $current, Revision $undo, Revision $undoafter = null ) {}