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