c1bb1a0d386a0df9beda9278bb50900680c6ef70
[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 array $oldContentArgs, array $newContentArgs, $expectedDiff
161 ) {
162 $this->mergeMwGlobalArrayValue( 'wgContentHandlers', [
163 'testing-nontext' => DummyNonTextContentHandler::class,
164 ] );
165 $oldContent = ContentHandler::makeContent( ...$oldContentArgs );
166 $newContent = ContentHandler::makeContent( ...$newContentArgs );
167
168 // Set $wgExternalDiffEngine to something bogus to try to force use of
169 // the PHP engine rather than wikidiff2.
170 $this->setMwGlobals( [
171 'wgExternalDiffEngine' => '/dev/null',
172 ] );
173
174 $differenceEngine = new DifferenceEngine();
175 $diff = $differenceEngine->generateContentDiffBody( $oldContent, $newContent );
176 $this->assertSame( $expectedDiff, $this->getPlainDiff( $diff ) );
177 }
178
179 public static function provideGenerateContentDiffBody() {
180 $content1 = [ 'xxx', null, CONTENT_MODEL_TEXT ];
181 $content2 = [ 'yyy', null, CONTENT_MODEL_TEXT ];
182
183 return [
184 'self-diff' => [ $content1, $content1, '' ],
185 'text diff' => [ $content1, $content2, '-xxx+yyy' ],
186 ];
187 }
188
189 public function testGenerateTextDiffBody() {
190 // Set $wgExternalDiffEngine to something bogus to try to force use of
191 // the PHP engine rather than wikidiff2.
192 $this->setMwGlobals( [
193 'wgExternalDiffEngine' => '/dev/null',
194 ] );
195
196 $oldText = "aaa\nbbb\nccc";
197 $newText = "aaa\nxxx\nccc";
198 $expectedDiff = " aaa aaa\n-bbb+xxx\n ccc ccc";
199
200 $differenceEngine = new DifferenceEngine();
201 $diff = $differenceEngine->generateTextDiffBody( $oldText, $newText );
202 $this->assertSame( $expectedDiff, $this->getPlainDiff( $diff ) );
203 }
204
205 public function testSetContent() {
206 // Set $wgExternalDiffEngine to something bogus to try to force use of
207 // the PHP engine rather than wikidiff2.
208 $this->setMwGlobals( [
209 'wgExternalDiffEngine' => '/dev/null',
210 ] );
211
212 $oldContent = ContentHandler::makeContent( 'xxx', null, CONTENT_MODEL_TEXT );
213 $newContent = ContentHandler::makeContent( 'yyy', null, CONTENT_MODEL_TEXT );
214
215 $differenceEngine = new DifferenceEngine();
216 $differenceEngine->setContent( $oldContent, $newContent );
217 $diff = $differenceEngine->getDiffBody();
218 $this->assertSame( "Line 1:\nLine 1:\n-xxx+yyy", $this->getPlainDiff( $diff ) );
219 }
220
221 public function testSetRevisions() {
222 $main1 = SlotRecord::newUnsaved( SlotRecord::MAIN,
223 ContentHandler::makeContent( 'xxx', null, CONTENT_MODEL_TEXT ) );
224 $main2 = SlotRecord::newUnsaved( SlotRecord::MAIN,
225 ContentHandler::makeContent( 'yyy', null, CONTENT_MODEL_TEXT ) );
226 $rev1 = $this->getRevisionRecord( $main1 );
227 $rev2 = $this->getRevisionRecord( $main2 );
228
229 $differenceEngine = new DifferenceEngine();
230 $differenceEngine->setRevisions( $rev1, $rev2 );
231 $this->assertSame( $rev1, $differenceEngine->getOldRevision() );
232 $this->assertSame( $rev2, $differenceEngine->getNewRevision() );
233 $this->assertSame( true, $differenceEngine->loadRevisionData() );
234 $this->assertSame( true, $differenceEngine->loadText() );
235
236 $differenceEngine->setRevisions( null, $rev2 );
237 $this->assertSame( null, $differenceEngine->getOldRevision() );
238 }
239
240 /**
241 * @dataProvider provideGetDiffBody
242 */
243 public function testGetDiffBody(
244 RevisionRecord $oldRevision = null, RevisionRecord $newRevision = null, $expectedDiff
245 ) {
246 // Set $wgExternalDiffEngine to something bogus to try to force use of
247 // the PHP engine rather than wikidiff2.
248 $this->setMwGlobals( [
249 'wgExternalDiffEngine' => '/dev/null',
250 ] );
251
252 if ( $expectedDiff instanceof Exception ) {
253 $this->setExpectedException( get_class( $expectedDiff ), $expectedDiff->getMessage() );
254 }
255 $differenceEngine = new DifferenceEngine();
256 $differenceEngine->setRevisions( $oldRevision, $newRevision );
257 if ( $expectedDiff instanceof Exception ) {
258 return;
259 }
260
261 $diff = $differenceEngine->getDiffBody();
262 $this->assertSame( $expectedDiff, $this->getPlainDiff( $diff ) );
263 }
264
265 public function provideGetDiffBody() {
266 $main1 = SlotRecord::newUnsaved( SlotRecord::MAIN,
267 ContentHandler::makeContent( 'xxx', null, CONTENT_MODEL_TEXT ) );
268 $main2 = SlotRecord::newUnsaved( SlotRecord::MAIN,
269 ContentHandler::makeContent( 'yyy', null, CONTENT_MODEL_TEXT ) );
270 $slot1 = SlotRecord::newUnsaved( 'slot',
271 ContentHandler::makeContent( 'aaa', null, CONTENT_MODEL_TEXT ) );
272 $slot2 = SlotRecord::newUnsaved( 'slot',
273 ContentHandler::makeContent( 'bbb', null, CONTENT_MODEL_TEXT ) );
274
275 return [
276 'revision vs. null' => [
277 null,
278 $this->getRevisionRecord( $main1, $slot1 ),
279 '',
280 ],
281 'revision vs. itself' => [
282 $this->getRevisionRecord( $main1, $slot1 ),
283 $this->getRevisionRecord( $main1, $slot1 ),
284 '',
285 ],
286 'different text in one slot' => [
287 $this->getRevisionRecord( $main1, $slot1 ),
288 $this->getRevisionRecord( $main1, $slot2 ),
289 "slotLine 1:\nLine 1:\n-aaa+bbb",
290 ],
291 'different text in two slots' => [
292 $this->getRevisionRecord( $main1, $slot1 ),
293 $this->getRevisionRecord( $main2, $slot2 ),
294 "Line 1:\nLine 1:\n-xxx+yyy\nslotLine 1:\nLine 1:\n-aaa+bbb",
295 ],
296 'new slot' => [
297 $this->getRevisionRecord( $main1 ),
298 $this->getRevisionRecord( $main1, $slot1 ),
299 "slotLine 1:\nLine 1:\n- +aaa",
300 ],
301 ];
302 }
303
304 public function testRecursion() {
305 // Set up a ContentHandler which will return a wrapped DifferenceEngine as
306 // SlotDiffRenderer, then pass it a content which uses the same ContentHandler.
307 // This tests the anti-recursion logic in DifferenceEngine::generateContentDiffBody.
308
309 $customDifferenceEngine = $this->getMockBuilder( DifferenceEngine::class )
310 ->enableProxyingToOriginalMethods()
311 ->getMock();
312 $customContentHandler = $this->getMockBuilder( ContentHandler::class )
313 ->setConstructorArgs( [ 'foo', [] ] )
314 ->setMethods( [ 'createDifferenceEngine' ] )
315 ->getMockForAbstractClass();
316 $customContentHandler->expects( $this->any() )
317 ->method( 'createDifferenceEngine' )
318 ->willReturn( $customDifferenceEngine );
319 /** @var ContentHandler $customContentHandler */
320 $customContent = $this->getMockBuilder( Content::class )
321 ->setMethods( [ 'getContentHandler' ] )
322 ->getMockForAbstractClass();
323 $customContent->expects( $this->any() )
324 ->method( 'getContentHandler' )
325 ->willReturn( $customContentHandler );
326 /** @var Content $customContent */
327 $customContent2 = clone $customContent;
328
329 $slotDiffRenderer = $customContentHandler->getSlotDiffRenderer( RequestContext::getMain() );
330 $this->setExpectedException( Exception::class,
331 ': could not maintain backwards compatibility. Please use a SlotDiffRenderer.' );
332 $slotDiffRenderer->getDiff( $customContent, $customContent2 );
333 }
334
335 /**
336 * Convert a HTML diff to a human-readable format and hopefully make the test less fragile.
337 * @param string diff
338 * @return string
339 */
340 private function getPlainDiff( $diff ) {
341 $replacements = [
342 html_entity_decode( '&nbsp;' ) => ' ',
343 html_entity_decode( '&minus;' ) => '-',
344 ];
345 return str_replace( array_keys( $replacements ), array_values( $replacements ),
346 trim( strip_tags( $diff ), "\n" ) );
347 }
348
349 /**
350 * @param int $id
351 * @return Title
352 */
353 private function getMockTitle( $id = 23 ) {
354 $mock = $this->getMockBuilder( Title::class )
355 ->disableOriginalConstructor()
356 ->getMock();
357 $mock->expects( $this->any() )
358 ->method( 'getDBkey' )
359 ->will( $this->returnValue( __CLASS__ ) );
360 $mock->expects( $this->any() )
361 ->method( 'getArticleID' )
362 ->will( $this->returnValue( $id ) );
363
364 return $mock;
365 }
366
367 /**
368 * @param SlotRecord[] $slots
369 * @return MutableRevisionRecord
370 */
371 private function getRevisionRecord( ...$slots ) {
372 $title = $this->getMockTitle();
373 $revision = new MutableRevisionRecord( $title );
374 foreach ( $slots as $slot ) {
375 $revision->setSlot( $slot );
376 }
377 return $revision;
378 }
379
380 }