Merge "Add missing @throws in Importers"
[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 * @var ContentHandler
9 */
10 private $handler;
11
12 protected function setUp() {
13 parent::setUp();
14
15 $this->handler = ContentHandler::getForModelID( CONTENT_MODEL_WIKITEXT );
16 }
17
18 /**
19 * @covers WikitextContentHandler::serializeContent
20 */
21 public function testSerializeContent() {
22 $content = new WikitextContent( 'hello world' );
23
24 $this->assertEquals( 'hello world', $this->handler->serializeContent( $content ) );
25 $this->assertEquals(
26 'hello world',
27 $this->handler->serializeContent( $content, CONTENT_FORMAT_WIKITEXT )
28 );
29
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
35 }
36 }
37
38 /**
39 * @covers WikitextContentHandler::unserializeContent
40 */
41 public function testUnserializeContent() {
42 $content = $this->handler->unserializeContent( 'hello world' );
43 $this->assertEquals( 'hello world', $content->getNativeData() );
44
45 $content = $this->handler->unserializeContent( 'hello world', CONTENT_FORMAT_WIKITEXT );
46 $this->assertEquals( 'hello world', $content->getNativeData() );
47
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
53 }
54 }
55
56 /**
57 * @covers WikitextContentHandler::makeEmptyContent
58 */
59 public function testMakeEmptyContent() {
60 $content = $this->handler->makeEmptyContent();
61
62 $this->assertTrue( $content->isEmpty() );
63 $this->assertEquals( '', $content->getNativeData() );
64 }
65
66 public static function dataIsSupportedFormat() {
67 return [
68 [ null, true ],
69 [ CONTENT_FORMAT_WIKITEXT, true ],
70 [ 99887766, false ],
71 ];
72 }
73
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
79 */
80 public function testMakeRedirectContent( $title, $expected ) {
81 global $wgContLang;
82 $wgContLang->resetNamespaces();
83
84 MagicWord::clearCache();
85
86 if ( is_string( $title ) ) {
87 $title = Title::newFromText( $title );
88 }
89 $content = $this->handler->makeRedirectContent( $title );
90 $this->assertEquals( $expected, $content->serialize() );
91 }
92
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]]' ],
103 [
104 Title::makeTitle( NS_MAIN, 'Bar', 'fragment', 'google' ),
105 '#REDIRECT [[google:Bar#fragment]]'
106 ],
107 ];
108 }
109
110 /**
111 * @dataProvider dataIsSupportedFormat
112 * @covers WikitextContentHandler::isSupportedFormat
113 */
114 public function testIsSupportedFormat( $format, $supported ) {
115 $this->assertEquals( $supported, $this->handler->isSupportedFormat( $format ) );
116 }
117
118 public function testSupportsDirectEditing() {
119 $handler = new WikiTextContentHandler();
120 $this->assertTrue( $handler->supportsDirectEditing(), 'direct editing is supported' );
121 }
122
123 public static function dataMerge3() {
124 return [
125 [
126 "first paragraph
127
128 second paragraph\n",
129
130 "FIRST paragraph
131
132 second paragraph\n",
133
134 "first paragraph
135
136 SECOND paragraph\n",
137
138 "FIRST paragraph
139
140 SECOND paragraph\n",
141 ],
142
143 [ "first paragraph
144 second paragraph\n",
145
146 "Bla bla\n",
147
148 "Blubberdibla\n",
149
150 false,
151 ],
152 ];
153 }
154
155 /**
156 * @dataProvider dataMerge3
157 * @covers WikitextContentHandler::merge3
158 */
159 public function testMerge3( $old, $mine, $yours, $expected ) {
160 $this->markTestSkippedIfNoDiff3();
161
162 // test merge
163 $oldContent = new WikitextContent( $old );
164 $myContent = new WikitextContent( $mine );
165 $yourContent = new WikitextContent( $yours );
166
167 $merged = $this->handler->merge3( $oldContent, $myContent, $yourContent );
168
169 $this->assertEquals( $expected, $merged ? $merged->getNativeData() : $merged );
170 }
171
172 public static function dataGetAutosummary() {
173 return [
174 [
175 'Hello there, world!',
176 '#REDIRECT [[Foo]]',
177 0,
178 '/^Redirected page .*Foo/'
179 ],
180
181 [
182 null,
183 'Hello world!',
184 EDIT_NEW,
185 '/^Created page .*Hello/'
186 ],
187
188 [
189 null,
190 '',
191 EDIT_NEW,
192 '/^Created blank page$/'
193 ],
194
195 [
196 'Hello there, world!',
197 '',
198 0,
199 '/^Blanked/'
200 ],
201
202 [
203 'Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy
204 eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam
205 voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet
206 clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet.',
207 'Hello world!',
208 0,
209 '/^Replaced .*Hello/'
210 ],
211
212 [
213 'foo',
214 'bar',
215 0,
216 '/^$/'
217 ],
218 ];
219 }
220
221 /**
222 * @dataProvider dataGetAutosummary
223 * @covers WikitextContentHandler::getAutosummary
224 */
225 public function testGetAutosummary( $old, $new, $flags, $expected ) {
226 $oldContent = is_null( $old ) ? null : new WikitextContent( $old );
227 $newContent = is_null( $new ) ? null : new WikitextContent( $new );
228
229 $summary = $this->handler->getAutosummary( $oldContent, $newContent, $flags );
230
231 $this->assertTrue(
232 (bool)preg_match( $expected, $summary ),
233 "Autosummary didn't match expected pattern $expected: $summary"
234 );
235 }
236
237 public static function dataGetChangeTag() {
238 return [
239 [
240 null,
241 '#REDIRECT [[Foo]]',
242 0,
243 'mw-new-redirect'
244 ],
245
246 [
247 'Lorem ipsum dolor',
248 '#REDIRECT [[Foo]]',
249 0,
250 'mw-new-redirect'
251 ],
252
253 [
254 '#REDIRECT [[Foo]]',
255 'Lorem ipsum dolor',
256 0,
257 'mw-removed-redirect'
258 ],
259
260 [
261 '#REDIRECT [[Foo]]',
262 '#REDIRECT [[Bar]]',
263 0,
264 'mw-changed-redirect-target'
265 ],
266
267 [
268 null,
269 'Lorem ipsum dolor',
270 EDIT_NEW,
271 null // mw-newpage is not defined as a tag
272 ],
273
274 [
275 null,
276 '',
277 EDIT_NEW,
278 null // mw-newblank is not defined as a tag
279 ],
280
281 [
282 'Lorem ipsum dolor',
283 '',
284 0,
285 'mw-blank'
286 ],
287
288 [
289 'Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy
290 eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam
291 voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet
292 clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet.',
293 'Ipsum',
294 0,
295 'mw-replace'
296 ],
297
298 [
299 'Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy
300 eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam
301 voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet
302 clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet.',
303 'Duis purus odio, rhoncus et finibus dapibus, facilisis ac urna. Pellentesque
304 arcu, tristique nec tempus nec, suscipit vel arcu. Sed non dolor nec ligula
305 congue tempor. Quisque pellentesque finibus orci a molestie. Nam maximus, purus
306 euismod finibus mollis, dui ante malesuada felis, dignissim rutrum diam sapien.',
307 0,
308 null
309 ],
310 ];
311 }
312
313 /**
314 * @dataProvider dataGetChangeTag
315 * @covers WikitextContentHandler::getChangeTag
316 */
317 public function testGetChangeTag( $old, $new, $flags, $expected ) {
318 $this->setMwGlobals( 'wgSoftwareTags', [
319 'mw-new-redirect' => true,
320 'mw-removed-redirect' => true,
321 'mw-changed-redirect-target' => true,
322 'mw-newpage' => true,
323 'mw-newblank' => true,
324 'mw-blank' => true,
325 'mw-replace' => true,
326 ] );
327 $oldContent = is_null( $old ) ? null : new WikitextContent( $old );
328 $newContent = is_null( $new ) ? null : new WikitextContent( $new );
329
330 $tag = $this->handler->getChangeTag( $oldContent, $newContent, $flags );
331
332 $this->assertSame( $expected, $tag );
333 }
334
335 /**
336 * @todo Text case requires database, should be done by a test class in the Database group
337 */
338 /*
339 public function testGetAutoDeleteReason( Title $title, &$hasHistory ) {}
340 */
341
342 /**
343 * @todo Text case requires database, should be done by a test class in the Database group
344 */
345 /*
346 public function testGetUndoContent( Revision $current, Revision $undo,
347 Revision $undoafter = null
348 ) {
349 }
350 */
351
352 public function testDataIndexFieldsFile() {
353 $mockEngine = $this->createMock( 'SearchEngine' );
354 $title = Title::newFromText( 'Somefile.jpg', NS_FILE );
355 $page = new WikiPage( $title );
356
357 $fileHandler = $this->getMockBuilder( FileContentHandler::class )
358 ->disableOriginalConstructor()
359 ->setMethods( [ 'getDataForSearchIndex' ] )
360 ->getMock();
361
362 $handler = $this->getMockBuilder( WikitextContentHandler::class )
363 ->disableOriginalConstructor()
364 ->setMethods( [ 'getFileHandler' ] )
365 ->getMock();
366
367 $handler->method( 'getFileHandler' )->will( $this->returnValue( $fileHandler ) );
368 $fileHandler->expects( $this->once() )
369 ->method( 'getDataForSearchIndex' )
370 ->will( $this->returnValue( [ 'file_text' => 'This is file content' ] ) );
371
372 $data = $handler->getDataForSearchIndex( $page, new ParserOutput(), $mockEngine );
373 $this->assertArrayHasKey( 'file_text', $data );
374 $this->assertEquals( 'This is file content', $data['file_text'] );
375 }
376 }