[MCR] Render multi-slot diffs
[lhc/web/wiklou.git] / tests / phpunit / includes / diff / DifferenceEngineTest.php
1 <?php
2
3 use MediaWiki\Storage\MutableRevisionRecord;
4 use MediaWiki\Storage\RevisionRecord;
5 use MediaWiki\Storage\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 $case ) {
90 list( $expectedOld, $expectedNew, $old, $new, $message ) = $case;
91
92 $diffEngine = new DifferenceEngine( $this->context, $old, $new, 2, true, false );
93 $diffEngine->loadRevisionData();
94
95 $this->assertEquals( $diffEngine->getOldid(), $expectedOld, $message );
96 $this->assertEquals( $diffEngine->getNewid(), $expectedNew, $message );
97 }
98 }
99
100 private function getLoadRevisionDataCases() {
101 $revs = self::$revisions;
102
103 return [
104 [ $revs[2], $revs[3], $revs[3], 'prev', 'diff=prev' ],
105 [ $revs[2], $revs[3], $revs[2], 'next', 'diff=next' ],
106 [ $revs[1], $revs[3], $revs[1], $revs[3], 'diff=' . $revs[3] ],
107 [ $revs[1], $revs[3], $revs[1], 0, 'diff=0' ]
108 ];
109 }
110
111 public function testGetOldid() {
112 $revs = self::$revisions;
113
114 $diffEngine = new DifferenceEngine( $this->context, $revs[1], $revs[2], 2, true, false );
115 $this->assertEquals( $revs[1], $diffEngine->getOldid(), 'diff get old id' );
116 }
117
118 public function testGetNewid() {
119 $revs = self::$revisions;
120
121 $diffEngine = new DifferenceEngine( $this->context, $revs[1], $revs[2], 2, true, false );
122 $this->assertEquals( $revs[2], $diffEngine->getNewid(), 'diff get new id' );
123 }
124
125 public function provideLocaliseTitleTooltipsTestData() {
126 return [
127 'moved paragraph left shoud get new location title' => [
128 '<a class="mw-diff-movedpara-left">⚫</a>',
129 '<a class="mw-diff-movedpara-left" title="(diff-paragraph-moved-tonew)">⚫</a>',
130 ],
131 'moved paragraph right shoud get old location title' => [
132 '<a class="mw-diff-movedpara-right">⚫</a>',
133 '<a class="mw-diff-movedpara-right" title="(diff-paragraph-moved-toold)">⚫</a>',
134 ],
135 'nothing changed when key not hit' => [
136 '<a class="mw-diff-movedpara-rightis">⚫</a>',
137 '<a class="mw-diff-movedpara-rightis">⚫</a>',
138 ],
139 ];
140 }
141
142 /**
143 * @dataProvider provideLocaliseTitleTooltipsTestData
144 */
145 public function testAddLocalisedTitleTooltips( $input, $expected ) {
146 $this->setContentLang( 'qqx' );
147 $diffEngine = TestingAccessWrapper::newFromObject( new DifferenceEngine() );
148 $this->assertEquals( $expected, $diffEngine->addLocalisedTitleTooltips( $input ) );
149 }
150
151 /**
152 * @dataProvider provideGenerateContentDiffBody
153 */
154 public function testGenerateContentDiffBody(
155 Content $oldContent, Content $newContent, $expectedDiff
156 ) {
157 // Set $wgExternalDiffEngine to something bogus to try to force use of
158 // the PHP engine rather than wikidiff2.
159 $this->setMwGlobals( [
160 'wgExternalDiffEngine' => '/dev/null',
161 ] );
162
163 $differenceEngine = new DifferenceEngine();
164 $diff = $differenceEngine->generateContentDiffBody( $oldContent, $newContent );
165 $this->assertSame( $expectedDiff, $this->getPlainDiff( $diff ) );
166 }
167
168 public function provideGenerateContentDiffBody() {
169 $this->mergeMwGlobalArrayValue( 'wgContentHandlers', [
170 'testing-nontext' => DummyNonTextContentHandler::class,
171 ] );
172 $content1 = ContentHandler::makeContent( 'xxx', null, CONTENT_MODEL_TEXT );
173 $content2 = ContentHandler::makeContent( 'yyy', null, CONTENT_MODEL_TEXT );
174
175 return [
176 'self-diff' => [ $content1, $content1, '' ],
177 'text diff' => [ $content1, $content2, '-xxx+yyy' ],
178 ];
179 }
180
181 public function testGenerateTextDiffBody() {
182 // Set $wgExternalDiffEngine to something bogus to try to force use of
183 // the PHP engine rather than wikidiff2.
184 $this->setMwGlobals( [
185 'wgExternalDiffEngine' => '/dev/null',
186 ] );
187
188 $oldText = "aaa\nbbb\nccc";
189 $newText = "aaa\nxxx\nccc";
190 $expectedDiff = " aaa aaa\n-bbb+xxx\n ccc ccc";
191
192 $differenceEngine = new DifferenceEngine();
193 $diff = $differenceEngine->generateTextDiffBody( $oldText, $newText );
194 $this->assertSame( $expectedDiff, $this->getPlainDiff( $diff ) );
195 }
196
197 public function testSetContent() {
198 // Set $wgExternalDiffEngine to something bogus to try to force use of
199 // the PHP engine rather than wikidiff2.
200 $this->setMwGlobals( [
201 'wgExternalDiffEngine' => '/dev/null',
202 ] );
203
204 $oldContent = ContentHandler::makeContent( 'xxx', null, CONTENT_MODEL_TEXT );
205 $newContent = ContentHandler::makeContent( 'yyy', null, CONTENT_MODEL_TEXT );
206
207 $differenceEngine = new DifferenceEngine();
208 $differenceEngine->setContent( $oldContent, $newContent );
209 $diff = $differenceEngine->getDiffBody();
210 $this->assertSame( "Line 1:\nLine 1:\n-xxx+yyy", $this->getPlainDiff( $diff ) );
211 }
212
213 public function testSetRevisions() {
214 $main1 = SlotRecord::newUnsaved( 'main',
215 ContentHandler::makeContent( 'xxx', null, CONTENT_MODEL_TEXT ) );
216 $main2 = SlotRecord::newUnsaved( 'main',
217 ContentHandler::makeContent( 'yyy', null, CONTENT_MODEL_TEXT ) );
218 $rev1 = $this->getRevisionRecord( $main1 );
219 $rev2 = $this->getRevisionRecord( $main2 );
220
221 $differenceEngine = new DifferenceEngine();
222 $differenceEngine->setRevisions( $rev1, $rev2 );
223 $this->assertSame( $rev1, $differenceEngine->getOldRevision() );
224 $this->assertSame( $rev2, $differenceEngine->getNewRevision() );
225 $this->assertSame( true, $differenceEngine->loadRevisionData() );
226 $this->assertSame( true, $differenceEngine->loadText() );
227
228 $differenceEngine->setRevisions( null, $rev2 );
229 $this->assertSame( null, $differenceEngine->getOldRevision() );
230 }
231
232 /**
233 * @dataProvider provideGetDiffBody
234 */
235 public function testGetDiffBody(
236 RevisionRecord $oldRevision = null, RevisionRecord $newRevision = null, $expectedDiff
237 ) {
238 // Set $wgExternalDiffEngine to something bogus to try to force use of
239 // the PHP engine rather than wikidiff2.
240 $this->setMwGlobals( [
241 'wgExternalDiffEngine' => '/dev/null',
242 ] );
243
244 if ( $expectedDiff instanceof Exception ) {
245 $this->setExpectedException( get_class( $expectedDiff ), $expectedDiff->getMessage() );
246 }
247 $differenceEngine = new DifferenceEngine();
248 $differenceEngine->setRevisions( $oldRevision, $newRevision );
249 if ( $expectedDiff instanceof Exception ) {
250 return;
251 }
252
253 $diff = $differenceEngine->getDiffBody();
254 $this->assertSame( $expectedDiff, $this->getPlainDiff( $diff ) );
255 }
256
257 public function provideGetDiffBody() {
258 $main1 = SlotRecord::newUnsaved( 'main',
259 ContentHandler::makeContent( 'xxx', null, CONTENT_MODEL_TEXT ) );
260 $main2 = SlotRecord::newUnsaved( 'main',
261 ContentHandler::makeContent( 'yyy', null, CONTENT_MODEL_TEXT ) );
262 $slot1 = SlotRecord::newUnsaved( 'slot',
263 ContentHandler::makeContent( 'aaa', null, CONTENT_MODEL_TEXT ) );
264 $slot2 = SlotRecord::newUnsaved( 'slot',
265 ContentHandler::makeContent( 'bbb', null, CONTENT_MODEL_TEXT ) );
266
267 return [
268 'revision vs. null' => [
269 null,
270 $this->getRevisionRecord( $main1, $slot1 ),
271 '',
272 ],
273 'revision vs. itself' => [
274 $this->getRevisionRecord( $main1, $slot1 ),
275 $this->getRevisionRecord( $main1, $slot1 ),
276 '',
277 ],
278 'different text in one slot' => [
279 $this->getRevisionRecord( $main1, $slot1 ),
280 $this->getRevisionRecord( $main1, $slot2 ),
281 "slotLine 1:\nLine 1:\n-aaa+bbb",
282 ],
283 'different text in two slots' => [
284 $this->getRevisionRecord( $main1, $slot1 ),
285 $this->getRevisionRecord( $main2, $slot2 ),
286 "Line 1:\nLine 1:\n-xxx+yyy\nslotLine 1:\nLine 1:\n-aaa+bbb",
287 ],
288 'new slot' => [
289 $this->getRevisionRecord( $main1 ),
290 $this->getRevisionRecord( $main1, $slot1 ),
291 "slotLine 1:\nLine 1:\n- +aaa",
292 ],
293 ];
294 }
295
296 public function testRecursion() {
297 // Set up a ContentHandler which will return a wrapped DifferenceEngine as
298 // SlotDiffRenderer, then pass it a content which uses the same ContentHandler.
299 // This tests the anti-recursion logic in DifferenceEngine::generateContentDiffBody.
300
301 $customDifferenceEngine = $this->getMockBuilder( DifferenceEngine::class )
302 ->enableProxyingToOriginalMethods()
303 ->getMock();
304 $customContentHandler = $this->getMockBuilder( ContentHandler::class )
305 ->setConstructorArgs( [ 'foo', [] ] )
306 ->setMethods( [ 'createDifferenceEngine' ] )
307 ->getMockForAbstractClass();
308 $customContentHandler->expects( $this->any() )
309 ->method( 'createDifferenceEngine' )
310 ->willReturn( $customDifferenceEngine );
311 /** @var $customContentHandler ContentHandler */
312 $customContent = $this->getMockBuilder( Content::class )
313 ->setMethods( [ 'getContentHandler' ] )
314 ->getMockForAbstractClass();
315 $customContent->expects( $this->any() )
316 ->method( 'getContentHandler' )
317 ->willReturn( $customContentHandler );
318 /** @var $customContent Content */
319 $customContent2 = clone $customContent;
320
321 $slotDiffRenderer = $customContentHandler->getSlotDiffRenderer( RequestContext::getMain() );
322 $this->setExpectedException( Exception::class,
323 ': could not maintain backwards compatibility. Please use a SlotDiffRenderer.' );
324 $slotDiffRenderer->getDiff( $customContent, $customContent2 );
325 }
326
327 /**
328 * Convert a HTML diff to a human-readable format and hopefully make the test less fragile.
329 * @param string diff
330 * @return string
331 */
332 private function getPlainDiff( $diff ) {
333 $replacements = [
334 html_entity_decode( '&nbsp;' ) => ' ',
335 html_entity_decode( '&minus;' ) => '-',
336 ];
337 return str_replace( array_keys( $replacements ), array_values( $replacements ),
338 trim( strip_tags( $diff ), "\n" ) );
339 }
340
341 /**
342 * @param SlotRecord[] $slots
343 * @return MutableRevisionRecord
344 */
345 private function getRevisionRecord( ...$slots ) {
346 $title = Title::newFromText( 'Foo' );
347 $revision = new MutableRevisionRecord( $title );
348 foreach ( $slots as $slot ) {
349 $revision->setSlot( $slot );
350 }
351 return $revision;
352 }
353
354 }