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