3 use MediaWiki\Revision\MutableRevisionRecord
;
4 use MediaWiki\Revision\RevisionRecord
;
5 use MediaWiki\Revision\SlotRecord
;
6 use Wikimedia\TestingAccessWrapper
;
9 * @covers DifferenceEngine
11 * @todo tests for the rest of DifferenceEngine!
16 * @author Katie Filbert < aude.wiki@gmail.com >
18 class DifferenceEngineTest
extends MediaWikiTestCase
{
22 private static $revisions;
24 protected function setUp() {
27 $title = $this->getTitle();
29 $this->context
= new RequestContext();
30 $this->context
->setTitle( $title );
32 if ( !self
::$revisions ) {
33 self
::$revisions = $this->doEdits();
40 protected function getTitle() {
41 $namespace = $this->getDefaultWikitextNS();
42 return Title
::newFromText( 'Kitten', $namespace );
46 * @return int[] Revision ids
48 protected function doEdits() {
49 $title = $this->getTitle();
50 $page = WikiPage
::factory( $title );
52 $strings = [ "it is a kitten", "two kittens", "three kittens", "four kittens" ];
55 foreach ( $strings as $string ) {
56 $content = ContentHandler
::makeContent( $string, $title );
57 $page->doEditContent( $content, 'edit page' );
58 $revisions[] = $page->getLatest();
64 public function testMapDiffPrevNext() {
65 $cases = $this->getMapDiffPrevNextCases();
67 foreach ( $cases as $case ) {
68 list( $expected, $old, $new, $message ) = $case;
70 $diffEngine = new DifferenceEngine( $this->context
, $old, $new, 2, true, false );
71 $diffMap = $diffEngine->mapDiffPrevNext( $old, $new );
72 $this->assertEquals( $expected, $diffMap, $message );
76 private function getMapDiffPrevNextCases() {
77 $revs = self
::$revisions;
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] ]
86 public function testLoadRevisionData() {
87 $cases = $this->getLoadRevisionDataCases();
89 foreach ( $cases as $testName => $case ) {
90 list( $expectedOld, $expectedNew, $expectedRet, $old, $new ) = $case;
92 $diffEngine = new DifferenceEngine( $this->context
, $old, $new, 2, true, false );
93 $ret = $diffEngine->loadRevisionData();
94 $ret2 = $diffEngine->loadRevisionData();
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 );
103 private function getLoadRevisionDataCases() {
104 $revs = self
::$revisions;
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] ],
116 public function testGetOldid() {
117 $revs = self
::$revisions;
119 $diffEngine = new DifferenceEngine( $this->context
, $revs[1], $revs[2], 2, true, false );
120 $this->assertEquals( $revs[1], $diffEngine->getOldid(), 'diff get old id' );
123 public function testGetNewid() {
124 $revs = self
::$revisions;
126 $diffEngine = new DifferenceEngine( $this->context
, $revs[1], $revs[2], 2, true, false );
127 $this->assertEquals( $revs[2], $diffEngine->getNewid(), 'diff get new id' );
130 public function provideLocaliseTitleTooltipsTestData() {
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>',
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>',
140 'nothing changed when key not hit' => [
141 '<a class="mw-diff-movedpara-rightis">⚫</a>',
142 '<a class="mw-diff-movedpara-rightis">⚫</a>',
148 * @dataProvider provideLocaliseTitleTooltipsTestData
150 public function testAddLocalisedTitleTooltips( $input, $expected ) {
151 $this->setContentLang( 'qqx' );
152 $diffEngine = TestingAccessWrapper
::newFromObject( new DifferenceEngine() );
153 $this->assertEquals( $expected, $diffEngine->addLocalisedTitleTooltips( $input ) );
157 * @dataProvider provideGenerateContentDiffBody
159 public function testGenerateContentDiffBody(
160 array $oldContentArgs, array $newContentArgs, $expectedDiff
162 $this->mergeMwGlobalArrayValue( 'wgContentHandlers', [
163 'testing-nontext' => DummyNonTextContentHandler
::class,
165 $oldContent = ContentHandler
::makeContent( ...$oldContentArgs );
166 $newContent = ContentHandler
::makeContent( ...$newContentArgs );
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',
174 $differenceEngine = new DifferenceEngine();
175 $diff = $differenceEngine->generateContentDiffBody( $oldContent, $newContent );
176 $this->assertSame( $expectedDiff, $this->getPlainDiff( $diff ) );
179 public static function provideGenerateContentDiffBody() {
180 $content1 = [ 'xxx', null, CONTENT_MODEL_TEXT
];
181 $content2 = [ 'yyy', null, CONTENT_MODEL_TEXT
];
184 'self-diff' => [ $content1, $content1, '' ],
185 'text diff' => [ $content1, $content2, '-xxx+yyy' ],
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',
196 $oldText = "aaa\nbbb\nccc";
197 $newText = "aaa\nxxx\nccc";
198 $expectedDiff = " aaa aaa\n-bbb+xxx\n ccc ccc";
200 $differenceEngine = new DifferenceEngine();
201 $diff = $differenceEngine->generateTextDiffBody( $oldText, $newText );
202 $this->assertSame( $expectedDiff, $this->getPlainDiff( $diff ) );
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',
212 $oldContent = ContentHandler
::makeContent( 'xxx', null, CONTENT_MODEL_TEXT
);
213 $newContent = ContentHandler
::makeContent( 'yyy', null, CONTENT_MODEL_TEXT
);
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 ) );
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 );
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() );
236 $differenceEngine->setRevisions( null, $rev2 );
237 $this->assertSame( null, $differenceEngine->getOldRevision() );
241 * @dataProvider provideGetDiffBody
243 public function testGetDiffBody(
244 RevisionRecord
$oldRevision = null, RevisionRecord
$newRevision = null, $expectedDiff
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',
252 if ( $expectedDiff instanceof Exception
) {
253 $this->setExpectedException( get_class( $expectedDiff ), $expectedDiff->getMessage() );
255 $differenceEngine = new DifferenceEngine();
256 $differenceEngine->setRevisions( $oldRevision, $newRevision );
257 if ( $expectedDiff instanceof Exception
) {
261 $diff = $differenceEngine->getDiffBody();
262 $this->assertSame( $expectedDiff, $this->getPlainDiff( $diff ) );
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
) );
276 'revision vs. null' => [
278 $this->getRevisionRecord( $main1, $slot1 ),
281 'revision vs. itself' => [
282 $this->getRevisionRecord( $main1, $slot1 ),
283 $this->getRevisionRecord( $main1, $slot1 ),
286 'different text in one slot' => [
287 $this->getRevisionRecord( $main1, $slot1 ),
288 $this->getRevisionRecord( $main1, $slot2 ),
289 "slotLine 1:\nLine 1:\n-aaa+bbb",
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",
297 $this->getRevisionRecord( $main1 ),
298 $this->getRevisionRecord( $main1, $slot1 ),
299 "slotLine 1:\nLine 1:\n- +aaa",
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.
309 $customDifferenceEngine = $this->getMockBuilder( DifferenceEngine
::class )
310 ->enableProxyingToOriginalMethods()
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;
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 );
336 * Convert a HTML diff to a human-readable format and hopefully make the test less fragile.
340 private function getPlainDiff( $diff ) {
342 html_entity_decode( ' ' ) => ' ',
343 html_entity_decode( '−' ) => '-',
345 return str_replace( array_keys( $replacements ), array_values( $replacements ),
346 trim( strip_tags( $diff ), "\n" ) );
353 private function getMockTitle( $id = 23 ) {
354 $mock = $this->getMockBuilder( Title
::class )
355 ->disableOriginalConstructor()
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 ) );
368 * @param SlotRecord[] $slots
369 * @return MutableRevisionRecord
371 private function getRevisionRecord( ...$slots ) {
372 $title = $this->getMockTitle();
373 $revision = new MutableRevisionRecord( $title );
374 foreach ( $slots as $slot ) {
375 $revision->setSlot( $slot );