Merge "Output only to stderr in unit tests"
[lhc/web/wiklou.git] / tests / phpunit / includes / diff / DifferenceEngineTest.php
1 <?php
2
3 use MediaWiki\Revision\MutableRevisionRecord;
4 use MediaWiki\Revision\RevisionRecord;
5 use MediaWiki\Revision\SlotRecord;
6 use Wikimedia\TestingAccessWrapper;
7
8 /**
9 * @covers DifferenceEngine
10 *
11 * @todo tests for the rest of DifferenceEngine!
12 *
13 * @group Database
14 * @group Diff
15 *
16 * @author Katie Filbert < aude.wiki@gmail.com >
17 */
18 class DifferenceEngineTest extends MediaWikiTestCase {
19
20 protected $context;
21
22 private static $revisions;
23
24 protected function setUp() {
25 parent::setUp();
26
27 $title = $this->getTitle();
28
29 $this->context = new RequestContext();
30 $this->context->setTitle( $title );
31
32 if ( !self::$revisions ) {
33 self::$revisions = $this->doEdits();
34 }
35 }
36
37 /**
38 * @return Title
39 */
40 protected function getTitle() {
41 $namespace = $this->getDefaultWikitextNS();
42 return Title::newFromText( 'Kitten', $namespace );
43 }
44
45 /**
46 * @return int[] Revision ids
47 */
48 protected function doEdits() {
49 $title = $this->getTitle();
50 $page = WikiPage::factory( $title );
51
52 $strings = [ "it is a kitten", "two kittens", "three kittens", "four kittens" ];
53 $revisions = [];
54
55 foreach ( $strings as $string ) {
56 $content = ContentHandler::makeContent( $string, $title );
57 $page->doEditContent( $content, 'edit page' );
58 $revisions[] = $page->getLatest();
59 }
60
61 return $revisions;
62 }
63
64 public function testMapDiffPrevNext() {
65 $cases = $this->getMapDiffPrevNextCases();
66
67 foreach ( $cases as $case ) {
68 list( $expected, $old, $new, $message ) = $case;
69
70 $diffEngine = new DifferenceEngine( $this->context, $old, $new, 2, true, false );
71 $diffMap = $diffEngine->mapDiffPrevNext( $old, $new );
72 $this->assertEquals( $expected, $diffMap, $message );
73 }
74 }
75
76 private function getMapDiffPrevNextCases() {
77 $revs = self::$revisions;
78
79 return [
80 [ [ $revs[1], $revs[2] ], $revs[2], 'prev', 'diff=prev' ],
81 [ [ $revs[2], $revs[3] ], $revs[2], 'next', 'diff=next' ],
82 [ [ $revs[1], $revs[3] ], $revs[1], $revs[3], 'diff=' . $revs[3] ]
83 ];
84 }
85
86 public function testLoadRevisionData() {
87 $cases = $this->getLoadRevisionDataCases();
88
89 foreach ( $cases as $testName => $case ) {
90 list( $expectedOld, $expectedNew, $expectedRet, $old, $new ) = $case;
91
92 $diffEngine = new DifferenceEngine( $this->context, $old, $new, 2, true, false );
93 $ret = $diffEngine->loadRevisionData();
94 $ret2 = $diffEngine->loadRevisionData();
95
96 $this->assertEquals( $expectedOld, $diffEngine->getOldid(), $testName );
97 $this->assertEquals( $expectedNew, $diffEngine->getNewid(), $testName );
98 $this->assertEquals( $expectedRet, $ret, $testName );
99 $this->assertEquals( $expectedRet, $ret2, $testName );
100 }
101 }
102
103 private function getLoadRevisionDataCases() {
104 $revs = self::$revisions;
105
106 return [
107 'diff=prev' => [ $revs[2], $revs[3], true, $revs[3], 'prev' ],
108 'diff=next' => [ $revs[2], $revs[3], true, $revs[2], 'next' ],
109 'diff=' . $revs[3] => [ $revs[1], $revs[3], true, $revs[1], $revs[3] ],
110 'diff=0' => [ $revs[1], $revs[3], true, $revs[1], 0 ],
111 'diff=prev&oldid=<first>' => [ false, $revs[0], true, $revs[0], 'prev' ],
112 'invalid' => [ 123456789, $revs[1], false, 123456789, $revs[1] ],
113 ];
114 }
115
116 public function testGetOldid() {
117 $revs = self::$revisions;
118
119 $diffEngine = new DifferenceEngine( $this->context, $revs[1], $revs[2], 2, true, false );
120 $this->assertEquals( $revs[1], $diffEngine->getOldid(), 'diff get old id' );
121 }
122
123 public function testGetNewid() {
124 $revs = self::$revisions;
125
126 $diffEngine = new DifferenceEngine( $this->context, $revs[1], $revs[2], 2, true, false );
127 $this->assertEquals( $revs[2], $diffEngine->getNewid(), 'diff get new id' );
128 }
129
130 public function provideLocaliseTitleTooltipsTestData() {
131 return [
132 'moved paragraph left shoud get new location title' => [
133 '<a class="mw-diff-movedpara-left">⚫</a>',
134 '<a class="mw-diff-movedpara-left" title="(diff-paragraph-moved-tonew)">⚫</a>',
135 ],
136 'moved paragraph right shoud get old location title' => [
137 '<a class="mw-diff-movedpara-right">⚫</a>',
138 '<a class="mw-diff-movedpara-right" title="(diff-paragraph-moved-toold)">⚫</a>',
139 ],
140 'nothing changed when key not hit' => [
141 '<a class="mw-diff-movedpara-rightis">⚫</a>',
142 '<a class="mw-diff-movedpara-rightis">⚫</a>',
143 ],
144 ];
145 }
146
147 /**
148 * @dataProvider provideLocaliseTitleTooltipsTestData
149 */
150 public function testAddLocalisedTitleTooltips( $input, $expected ) {
151 $this->setContentLang( 'qqx' );
152 $diffEngine = TestingAccessWrapper::newFromObject( new DifferenceEngine() );
153 $this->assertEquals( $expected, $diffEngine->addLocalisedTitleTooltips( $input ) );
154 }
155
156 /**
157 * @dataProvider provideGenerateContentDiffBody
158 */
159 public function testGenerateContentDiffBody(
160 Content $oldContent, Content $newContent, $expectedDiff
161 ) {
162 // Set $wgExternalDiffEngine to something bogus to try to force use of
163 // the PHP engine rather than wikidiff2.
164 $this->setMwGlobals( [
165 'wgExternalDiffEngine' => '/dev/null',
166 ] );
167
168 $differenceEngine = new DifferenceEngine();
169 $diff = $differenceEngine->generateContentDiffBody( $oldContent, $newContent );
170 $this->assertSame( $expectedDiff, $this->getPlainDiff( $diff ) );
171 }
172
173 public function provideGenerateContentDiffBody() {
174 $this->mergeMwGlobalArrayValue( 'wgContentHandlers', [
175 'testing-nontext' => DummyNonTextContentHandler::class,
176 ] );
177 $content1 = ContentHandler::makeContent( 'xxx', null, CONTENT_MODEL_TEXT );
178 $content2 = ContentHandler::makeContent( 'yyy', null, CONTENT_MODEL_TEXT );
179
180 return [
181 'self-diff' => [ $content1, $content1, '' ],
182 'text diff' => [ $content1, $content2, '-xxx+yyy' ],
183 ];
184 }
185
186 public function testGenerateTextDiffBody() {
187 // Set $wgExternalDiffEngine to something bogus to try to force use of
188 // the PHP engine rather than wikidiff2.
189 $this->setMwGlobals( [
190 'wgExternalDiffEngine' => '/dev/null',
191 ] );
192
193 $oldText = "aaa\nbbb\nccc";
194 $newText = "aaa\nxxx\nccc";
195 $expectedDiff = " aaa aaa\n-bbb+xxx\n ccc ccc";
196
197 $differenceEngine = new DifferenceEngine();
198 $diff = $differenceEngine->generateTextDiffBody( $oldText, $newText );
199 $this->assertSame( $expectedDiff, $this->getPlainDiff( $diff ) );
200 }
201
202 public function testSetContent() {
203 // Set $wgExternalDiffEngine to something bogus to try to force use of
204 // the PHP engine rather than wikidiff2.
205 $this->setMwGlobals( [
206 'wgExternalDiffEngine' => '/dev/null',
207 ] );
208
209 $oldContent = ContentHandler::makeContent( 'xxx', null, CONTENT_MODEL_TEXT );
210 $newContent = ContentHandler::makeContent( 'yyy', null, CONTENT_MODEL_TEXT );
211
212 $differenceEngine = new DifferenceEngine();
213 $differenceEngine->setContent( $oldContent, $newContent );
214 $diff = $differenceEngine->getDiffBody();
215 $this->assertSame( "Line 1:\nLine 1:\n-xxx+yyy", $this->getPlainDiff( $diff ) );
216 }
217
218 public function testSetRevisions() {
219 $main1 = SlotRecord::newUnsaved( SlotRecord::MAIN,
220 ContentHandler::makeContent( 'xxx', null, CONTENT_MODEL_TEXT ) );
221 $main2 = SlotRecord::newUnsaved( SlotRecord::MAIN,
222 ContentHandler::makeContent( 'yyy', null, CONTENT_MODEL_TEXT ) );
223 $rev1 = $this->getRevisionRecord( $main1 );
224 $rev2 = $this->getRevisionRecord( $main2 );
225
226 $differenceEngine = new DifferenceEngine();
227 $differenceEngine->setRevisions( $rev1, $rev2 );
228 $this->assertSame( $rev1, $differenceEngine->getOldRevision() );
229 $this->assertSame( $rev2, $differenceEngine->getNewRevision() );
230 $this->assertSame( true, $differenceEngine->loadRevisionData() );
231 $this->assertSame( true, $differenceEngine->loadText() );
232
233 $differenceEngine->setRevisions( null, $rev2 );
234 $this->assertSame( null, $differenceEngine->getOldRevision() );
235 }
236
237 /**
238 * @dataProvider provideGetDiffBody
239 */
240 public function testGetDiffBody(
241 RevisionRecord $oldRevision = null, RevisionRecord $newRevision = null, $expectedDiff
242 ) {
243 // Set $wgExternalDiffEngine to something bogus to try to force use of
244 // the PHP engine rather than wikidiff2.
245 $this->setMwGlobals( [
246 'wgExternalDiffEngine' => '/dev/null',
247 ] );
248
249 if ( $expectedDiff instanceof Exception ) {
250 $this->setExpectedException( get_class( $expectedDiff ), $expectedDiff->getMessage() );
251 }
252 $differenceEngine = new DifferenceEngine();
253 $differenceEngine->setRevisions( $oldRevision, $newRevision );
254 if ( $expectedDiff instanceof Exception ) {
255 return;
256 }
257
258 $diff = $differenceEngine->getDiffBody();
259 $this->assertSame( $expectedDiff, $this->getPlainDiff( $diff ) );
260 }
261
262 public function provideGetDiffBody() {
263 $main1 = SlotRecord::newUnsaved( SlotRecord::MAIN,
264 ContentHandler::makeContent( 'xxx', null, CONTENT_MODEL_TEXT ) );
265 $main2 = SlotRecord::newUnsaved( SlotRecord::MAIN,
266 ContentHandler::makeContent( 'yyy', null, CONTENT_MODEL_TEXT ) );
267 $slot1 = SlotRecord::newUnsaved( 'slot',
268 ContentHandler::makeContent( 'aaa', null, CONTENT_MODEL_TEXT ) );
269 $slot2 = SlotRecord::newUnsaved( 'slot',
270 ContentHandler::makeContent( 'bbb', null, CONTENT_MODEL_TEXT ) );
271
272 return [
273 'revision vs. null' => [
274 null,
275 $this->getRevisionRecord( $main1, $slot1 ),
276 '',
277 ],
278 'revision vs. itself' => [
279 $this->getRevisionRecord( $main1, $slot1 ),
280 $this->getRevisionRecord( $main1, $slot1 ),
281 '',
282 ],
283 'different text in one slot' => [
284 $this->getRevisionRecord( $main1, $slot1 ),
285 $this->getRevisionRecord( $main1, $slot2 ),
286 "slotLine 1:\nLine 1:\n-aaa+bbb",
287 ],
288 'different text in two slots' => [
289 $this->getRevisionRecord( $main1, $slot1 ),
290 $this->getRevisionRecord( $main2, $slot2 ),
291 "Line 1:\nLine 1:\n-xxx+yyy\nslotLine 1:\nLine 1:\n-aaa+bbb",
292 ],
293 'new slot' => [
294 $this->getRevisionRecord( $main1 ),
295 $this->getRevisionRecord( $main1, $slot1 ),
296 "slotLine 1:\nLine 1:\n- +aaa",
297 ],
298 ];
299 }
300
301 public function testRecursion() {
302 // Set up a ContentHandler which will return a wrapped DifferenceEngine as
303 // SlotDiffRenderer, then pass it a content which uses the same ContentHandler.
304 // This tests the anti-recursion logic in DifferenceEngine::generateContentDiffBody.
305
306 $customDifferenceEngine = $this->getMockBuilder( DifferenceEngine::class )
307 ->enableProxyingToOriginalMethods()
308 ->getMock();
309 $customContentHandler = $this->getMockBuilder( ContentHandler::class )
310 ->setConstructorArgs( [ 'foo', [] ] )
311 ->setMethods( [ 'createDifferenceEngine' ] )
312 ->getMockForAbstractClass();
313 $customContentHandler->expects( $this->any() )
314 ->method( 'createDifferenceEngine' )
315 ->willReturn( $customDifferenceEngine );
316 /** @var $customContentHandler ContentHandler */
317 $customContent = $this->getMockBuilder( Content::class )
318 ->setMethods( [ 'getContentHandler' ] )
319 ->getMockForAbstractClass();
320 $customContent->expects( $this->any() )
321 ->method( 'getContentHandler' )
322 ->willReturn( $customContentHandler );
323 /** @var $customContent Content */
324 $customContent2 = clone $customContent;
325
326 $slotDiffRenderer = $customContentHandler->getSlotDiffRenderer( RequestContext::getMain() );
327 $this->setExpectedException( Exception::class,
328 ': could not maintain backwards compatibility. Please use a SlotDiffRenderer.' );
329 $slotDiffRenderer->getDiff( $customContent, $customContent2 );
330 }
331
332 /**
333 * Convert a HTML diff to a human-readable format and hopefully make the test less fragile.
334 * @param string diff
335 * @return string
336 */
337 private function getPlainDiff( $diff ) {
338 $replacements = [
339 html_entity_decode( '&nbsp;' ) => ' ',
340 html_entity_decode( '&minus;' ) => '-',
341 ];
342 return str_replace( array_keys( $replacements ), array_values( $replacements ),
343 trim( strip_tags( $diff ), "\n" ) );
344 }
345
346 /**
347 * @param int $id
348 * @return Title
349 */
350 private function getMockTitle( $id = 23 ) {
351 $mock = $this->getMockBuilder( Title::class )
352 ->disableOriginalConstructor()
353 ->getMock();
354 $mock->expects( $this->any() )
355 ->method( 'getDBkey' )
356 ->will( $this->returnValue( __CLASS__ ) );
357 $mock->expects( $this->any() )
358 ->method( 'getArticleID' )
359 ->will( $this->returnValue( $id ) );
360
361 return $mock;
362 }
363
364 /**
365 * @param SlotRecord[] $slots
366 * @return MutableRevisionRecord
367 */
368 private function getRevisionRecord( ...$slots ) {
369 $title = $this->getMockTitle();
370 $revision = new MutableRevisionRecord( $title );
371 foreach ( $slots as $slot ) {
372 $revision->setSlot( $slot );
373 }
374 return $revision;
375 }
376
377 }