Merge "Add parserTests for language converter markup."
[lhc/web/wiklou.git] / tests / phpunit / includes / content / WikitextContentHandlerTest.php
1 <?php
2
3 /**
4 * @group ContentHandler
5 */
6 class WikitextContentHandlerTest extends MediaWikiLangTestCase {
7
8 /**
9 * @var ContentHandler
10 */
11 var $handler;
12
13 public function setUp() {
14 parent::setUp();
15
16 $this->handler = ContentHandler::getForModelID( CONTENT_MODEL_WIKITEXT );
17 }
18
19 /**
20 * @covers WikitextContentHandler::serializeContent
21 */
22 public function testSerializeContent() {
23 $content = new WikitextContent( 'hello world' );
24
25 $this->assertEquals( 'hello world', $this->handler->serializeContent( $content ) );
26 $this->assertEquals( 'hello world', $this->handler->serializeContent( $content, CONTENT_FORMAT_WIKITEXT ) );
27
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
33 }
34 }
35
36 /**
37 * @covers WikitextContentHandler::unserializeContent
38 */
39 public function testUnserializeContent() {
40 $content = $this->handler->unserializeContent( 'hello world' );
41 $this->assertEquals( 'hello world', $content->getNativeData() );
42
43 $content = $this->handler->unserializeContent( 'hello world', CONTENT_FORMAT_WIKITEXT );
44 $this->assertEquals( 'hello world', $content->getNativeData() );
45
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
51 }
52 }
53
54 /**
55 * @covers WikitextContentHandler::makeEmptyContent
56 */
57 public function testMakeEmptyContent() {
58 $content = $this->handler->makeEmptyContent();
59
60 $this->assertTrue( $content->isEmpty() );
61 $this->assertEquals( '', $content->getNativeData() );
62 }
63
64 public static function dataIsSupportedFormat() {
65 return array(
66 array( null, true ),
67 array( CONTENT_FORMAT_WIKITEXT, true ),
68 array( 99887766, false ),
69 );
70 }
71
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
77 */
78 public function testMakeRedirectContent( $title, $expected ) {
79 global $wgContLang;
80 $wgContLang->resetNamespaces();
81
82 if ( is_string( $title ) ) {
83 $title = Title::newFromText( $title );
84 }
85 $content = $this->handler->makeRedirectContent( $title );
86 $this->assertEquals( $expected, $content->serialize() );
87 }
88
89 public static function provideMakeRedirectContent() {
90 return array(
91 array( 'Hello', '#REDIRECT [[Hello]]' ),
92 array( 'Template:Hello', '#REDIRECT [[Template:Hello]]' ),
93 array( 'Hello#section', '#REDIRECT [[Hello#section]]' ),
94 array( 'user:john_doe#section', '#REDIRECT [[User:John doe#section]]' ),
95 array( 'MEDIAWIKI:FOOBAR', '#REDIRECT [[MediaWiki:FOOBAR]]' ),
96 array( 'Category:Foo', '#REDIRECT [[:Category:Foo]]' ),
97 array( Title::makeTitle( NS_MAIN, 'en:Foo' ), '#REDIRECT [[en:Foo]]' ),
98 array( Title::makeTitle( NS_MAIN, 'Foo', '', 'en' ), '#REDIRECT [[:en:Foo]]' ),
99 array( Title::makeTitle( NS_MAIN, 'Bar', 'fragment', 'google' ), '#REDIRECT [[google:Bar#fragment]]' ),
100 );
101 }
102
103 /**
104 * @dataProvider dataIsSupportedFormat
105 * @covers WikitextContentHandler::isSupportedFormat
106 */
107 public function testIsSupportedFormat( $format, $supported ) {
108 $this->assertEquals( $supported, $this->handler->isSupportedFormat( $format ) );
109 }
110
111 public static function dataMerge3() {
112 return array(
113 array(
114 "first paragraph
115
116 second paragraph\n",
117
118 "FIRST paragraph
119
120 second paragraph\n",
121
122 "first paragraph
123
124 SECOND paragraph\n",
125
126 "FIRST paragraph
127
128 SECOND paragraph\n",
129 ),
130
131 array( "first paragraph
132 second paragraph\n",
133
134 "Bla bla\n",
135
136 "Blubberdibla\n",
137
138 false,
139 ),
140 );
141 }
142
143 /**
144 * @dataProvider dataMerge3
145 * @covers WikitextContentHandler::merge3
146 */
147 public function testMerge3( $old, $mine, $yours, $expected ) {
148 $this->checkHasDiff3();
149
150 // test merge
151 $oldContent = new WikitextContent( $old );
152 $myContent = new WikitextContent( $mine );
153 $yourContent = new WikitextContent( $yours );
154
155 $merged = $this->handler->merge3( $oldContent, $myContent, $yourContent );
156
157 $this->assertEquals( $expected, $merged ? $merged->getNativeData() : $merged );
158 }
159
160 public static function dataGetAutosummary() {
161 return array(
162 array(
163 'Hello there, world!',
164 '#REDIRECT [[Foo]]',
165 0,
166 '/^Redirected page .*Foo/'
167 ),
168
169 array(
170 null,
171 'Hello world!',
172 EDIT_NEW,
173 '/^Created page .*Hello/'
174 ),
175
176 array(
177 'Hello there, world!',
178 '',
179 0,
180 '/^Blanked/'
181 ),
182
183 array(
184 'Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut
185 labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et
186 ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet.',
187 'Hello world!',
188 0,
189 '/^Replaced .*Hello/'
190 ),
191
192 array(
193 'foo',
194 'bar',
195 0,
196 '/^$/'
197 ),
198 );
199 }
200
201 /**
202 * @dataProvider dataGetAutosummary
203 * @covers WikitextContentHandler::getAutosummary
204 */
205 public function testGetAutosummary( $old, $new, $flags, $expected ) {
206 $oldContent = is_null( $old ) ? null : new WikitextContent( $old );
207 $newContent = is_null( $new ) ? null : new WikitextContent( $new );
208
209 $summary = $this->handler->getAutosummary( $oldContent, $newContent, $flags );
210
211 $this->assertTrue( (bool)preg_match( $expected, $summary ), "Autosummary didn't match expected pattern $expected: $summary" );
212 }
213
214 /**
215 * @todo Text case requires database, should be done by a test class in the Database group
216 */
217 /*
218 public function testGetAutoDeleteReason( Title $title, &$hasHistory ) {}
219 */
220
221 /**
222 * @todo Text case requires database, should be done by a test class in the Database group
223 */
224 /*
225 public function testGetUndoContent( Revision $current, Revision $undo, Revision $undoafter = null ) {}
226 */
227 }