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();
36 $this->setMwGlobals( [ 'wgDiffEngine' => 'php' ] );
42 protected function getTitle() {
43 $namespace = $this->getDefaultWikitextNS();
44 return Title
::newFromText( 'Kitten', $namespace );
48 * @return int[] Revision ids
50 protected function doEdits() {
51 $title = $this->getTitle();
52 $page = WikiPage
::factory( $title );
54 $strings = [ "it is a kitten", "two kittens", "three kittens", "four kittens" ];
57 foreach ( $strings as $string ) {
58 $content = ContentHandler
::makeContent( $string, $title );
59 $page->doEditContent( $content, 'edit page' );
60 $revisions[] = $page->getLatest();
66 public function testMapDiffPrevNext() {
67 $cases = $this->getMapDiffPrevNextCases();
69 foreach ( $cases as $case ) {
70 list( $expected, $old, $new, $message ) = $case;
72 $diffEngine = new DifferenceEngine( $this->context
, $old, $new, 2, true, false );
73 $diffMap = $diffEngine->mapDiffPrevNext( $old, $new );
74 $this->assertEquals( $expected, $diffMap, $message );
78 private function getMapDiffPrevNextCases() {
79 $revs = self
::$revisions;
82 [ [ $revs[1], $revs[2] ], $revs[2], 'prev', 'diff=prev' ],
83 [ [ $revs[2], $revs[3] ], $revs[2], 'next', 'diff=next' ],
84 [ [ $revs[1], $revs[3] ], $revs[1], $revs[3], 'diff=' . $revs[3] ]
88 public function testLoadRevisionData() {
89 $cases = $this->getLoadRevisionDataCases();
91 foreach ( $cases as $testName => $case ) {
92 list( $expectedOld, $expectedNew, $expectedRet, $old, $new ) = $case;
94 $diffEngine = new DifferenceEngine( $this->context
, $old, $new, 2, true, false );
95 $ret = $diffEngine->loadRevisionData();
96 $ret2 = $diffEngine->loadRevisionData();
98 $this->assertEquals( $expectedOld, $diffEngine->getOldid(), $testName );
99 $this->assertEquals( $expectedNew, $diffEngine->getNewid(), $testName );
100 $this->assertEquals( $expectedRet, $ret, $testName );
101 $this->assertEquals( $expectedRet, $ret2, $testName );
105 private function getLoadRevisionDataCases() {
106 $revs = self
::$revisions;
109 'diff=prev' => [ $revs[2], $revs[3], true, $revs[3], 'prev' ],
110 'diff=next' => [ $revs[2], $revs[3], true, $revs[2], 'next' ],
111 'diff=' . $revs[3] => [ $revs[1], $revs[3], true, $revs[1], $revs[3] ],
112 'diff=0' => [ $revs[1], $revs[3], true, $revs[1], 0 ],
113 'diff=prev&oldid=<first>' => [ false, $revs[0], true, $revs[0], 'prev' ],
114 'invalid' => [ 123456789, $revs[1], false, 123456789, $revs[1] ],
118 public function testGetOldid() {
119 $revs = self
::$revisions;
121 $diffEngine = new DifferenceEngine( $this->context
, $revs[1], $revs[2], 2, true, false );
122 $this->assertEquals( $revs[1], $diffEngine->getOldid(), 'diff get old id' );
125 public function testGetNewid() {
126 $revs = self
::$revisions;
128 $diffEngine = new DifferenceEngine( $this->context
, $revs[1], $revs[2], 2, true, false );
129 $this->assertEquals( $revs[2], $diffEngine->getNewid(), 'diff get new id' );
132 public function provideLocaliseTitleTooltipsTestData() {
134 'moved paragraph left shoud get new location title' => [
135 '<a class="mw-diff-movedpara-left">⚫</a>',
136 '<a class="mw-diff-movedpara-left" title="(diff-paragraph-moved-tonew)">⚫</a>',
138 'moved paragraph right shoud get old location title' => [
139 '<a class="mw-diff-movedpara-right">⚫</a>',
140 '<a class="mw-diff-movedpara-right" title="(diff-paragraph-moved-toold)">⚫</a>',
142 'nothing changed when key not hit' => [
143 '<a class="mw-diff-movedpara-rightis">⚫</a>',
144 '<a class="mw-diff-movedpara-rightis">⚫</a>',
150 * @dataProvider provideLocaliseTitleTooltipsTestData
152 public function testAddLocalisedTitleTooltips( $input, $expected ) {
153 $this->setContentLang( 'qqx' );
154 $diffEngine = TestingAccessWrapper
::newFromObject( new DifferenceEngine() );
155 $this->assertEquals( $expected, $diffEngine->addLocalisedTitleTooltips( $input ) );
159 * @dataProvider provideGenerateContentDiffBody
161 public function testGenerateContentDiffBody(
162 array $oldContentArgs, array $newContentArgs, $expectedDiff
164 $this->mergeMwGlobalArrayValue( 'wgContentHandlers', [
165 'testing-nontext' => DummyNonTextContentHandler
::class,
167 $oldContent = ContentHandler
::makeContent( ...$oldContentArgs );
168 $newContent = ContentHandler
::makeContent( ...$newContentArgs );
170 $differenceEngine = new DifferenceEngine();
171 $diff = $differenceEngine->generateContentDiffBody( $oldContent, $newContent );
172 $this->assertSame( $expectedDiff, $this->getPlainDiff( $diff ) );
175 public static function provideGenerateContentDiffBody() {
176 $content1 = [ 'xxx', null, CONTENT_MODEL_TEXT
];
177 $content2 = [ 'yyy', null, CONTENT_MODEL_TEXT
];
180 'self-diff' => [ $content1, $content1, '' ],
181 'text diff' => [ $content1, $content2, '-xxx+yyy' ],
185 public function testGenerateTextDiffBody() {
186 $oldText = "aaa\nbbb\nccc";
187 $newText = "aaa\nxxx\nccc";
188 $expectedDiff = " aaa aaa\n-bbb+xxx\n ccc ccc";
190 $differenceEngine = new DifferenceEngine();
191 $diff = $differenceEngine->generateTextDiffBody( $oldText, $newText );
192 $this->assertSame( $expectedDiff, $this->getPlainDiff( $diff ) );
195 public function testSetContent() {
196 $oldContent = ContentHandler
::makeContent( 'xxx', null, CONTENT_MODEL_TEXT
);
197 $newContent = ContentHandler
::makeContent( 'yyy', null, CONTENT_MODEL_TEXT
);
199 $differenceEngine = new DifferenceEngine();
200 $differenceEngine->setContent( $oldContent, $newContent );
201 $diff = $differenceEngine->getDiffBody();
202 $this->assertSame( "Line 1:\nLine 1:\n-xxx+yyy", $this->getPlainDiff( $diff ) );
205 public function testSetRevisions() {
206 $main1 = SlotRecord
::newUnsaved( SlotRecord
::MAIN
,
207 ContentHandler
::makeContent( 'xxx', null, CONTENT_MODEL_TEXT
) );
208 $main2 = SlotRecord
::newUnsaved( SlotRecord
::MAIN
,
209 ContentHandler
::makeContent( 'yyy', null, CONTENT_MODEL_TEXT
) );
210 $rev1 = $this->getRevisionRecord( $main1 );
211 $rev2 = $this->getRevisionRecord( $main2 );
213 $differenceEngine = new DifferenceEngine();
214 $differenceEngine->setRevisions( $rev1, $rev2 );
215 $this->assertSame( $rev1, $differenceEngine->getOldRevision() );
216 $this->assertSame( $rev2, $differenceEngine->getNewRevision() );
217 $this->assertSame( true, $differenceEngine->loadRevisionData() );
218 $this->assertSame( true, $differenceEngine->loadText() );
220 $differenceEngine->setRevisions( null, $rev2 );
221 $this->assertSame( null, $differenceEngine->getOldRevision() );
225 * @dataProvider provideGetDiffBody
227 public function testGetDiffBody(
228 RevisionRecord
$oldRevision = null, RevisionRecord
$newRevision = null, $expectedDiff
230 if ( $expectedDiff instanceof Exception
) {
231 $this->setExpectedException( get_class( $expectedDiff ), $expectedDiff->getMessage() );
233 $differenceEngine = new DifferenceEngine();
234 $differenceEngine->setRevisions( $oldRevision, $newRevision );
235 if ( $expectedDiff instanceof Exception
) {
239 $diff = $differenceEngine->getDiffBody();
240 $this->assertSame( $expectedDiff, $this->getPlainDiff( $diff ) );
243 public function provideGetDiffBody() {
244 $main1 = SlotRecord
::newUnsaved( SlotRecord
::MAIN
,
245 ContentHandler
::makeContent( 'xxx', null, CONTENT_MODEL_TEXT
) );
246 $main2 = SlotRecord
::newUnsaved( SlotRecord
::MAIN
,
247 ContentHandler
::makeContent( 'yyy', null, CONTENT_MODEL_TEXT
) );
248 $slot1 = SlotRecord
::newUnsaved( 'slot',
249 ContentHandler
::makeContent( 'aaa', null, CONTENT_MODEL_TEXT
) );
250 $slot2 = SlotRecord
::newUnsaved( 'slot',
251 ContentHandler
::makeContent( 'bbb', null, CONTENT_MODEL_TEXT
) );
254 'revision vs. null' => [
256 $this->getRevisionRecord( $main1, $slot1 ),
259 'revision vs. itself' => [
260 $this->getRevisionRecord( $main1, $slot1 ),
261 $this->getRevisionRecord( $main1, $slot1 ),
264 'different text in one slot' => [
265 $this->getRevisionRecord( $main1, $slot1 ),
266 $this->getRevisionRecord( $main1, $slot2 ),
267 "slotLine 1:\nLine 1:\n-aaa+bbb",
269 'different text in two slots' => [
270 $this->getRevisionRecord( $main1, $slot1 ),
271 $this->getRevisionRecord( $main2, $slot2 ),
272 "Line 1:\nLine 1:\n-xxx+yyy\nslotLine 1:\nLine 1:\n-aaa+bbb",
275 $this->getRevisionRecord( $main1 ),
276 $this->getRevisionRecord( $main1, $slot1 ),
277 "slotLine 1:\nLine 1:\n- +aaa",
282 public function testRecursion() {
283 // Set up a ContentHandler which will return a wrapped DifferenceEngine as
284 // SlotDiffRenderer, then pass it a content which uses the same ContentHandler.
285 // This tests the anti-recursion logic in DifferenceEngine::generateContentDiffBody.
287 $customDifferenceEngine = $this->getMockBuilder( DifferenceEngine
::class )
288 ->enableProxyingToOriginalMethods()
290 $customContentHandler = $this->getMockBuilder( ContentHandler
::class )
291 ->setConstructorArgs( [ 'foo', [] ] )
292 ->setMethods( [ 'createDifferenceEngine' ] )
293 ->getMockForAbstractClass();
294 $customContentHandler->expects( $this->any() )
295 ->method( 'createDifferenceEngine' )
296 ->willReturn( $customDifferenceEngine );
297 /** @var ContentHandler $customContentHandler */
298 $customContent = $this->getMockBuilder( Content
::class )
299 ->setMethods( [ 'getContentHandler' ] )
300 ->getMockForAbstractClass();
301 $customContent->expects( $this->any() )
302 ->method( 'getContentHandler' )
303 ->willReturn( $customContentHandler );
304 /** @var Content $customContent */
305 $customContent2 = clone $customContent;
307 $slotDiffRenderer = $customContentHandler->getSlotDiffRenderer( RequestContext
::getMain() );
308 $this->setExpectedException( Exception
::class,
309 ': could not maintain backwards compatibility. Please use a SlotDiffRenderer.' );
310 $slotDiffRenderer->getDiff( $customContent, $customContent2 );
314 * Convert a HTML diff to a human-readable format and hopefully make the test less fragile.
318 private function getPlainDiff( $diff ) {
320 html_entity_decode( ' ' ) => ' ',
321 html_entity_decode( '−' ) => '-',
323 return str_replace( array_keys( $replacements ), array_values( $replacements ),
324 trim( strip_tags( $diff ), "\n" ) );
331 private function getMockTitle( $id = 23 ) {
332 $mock = $this->getMockBuilder( Title
::class )
333 ->disableOriginalConstructor()
335 $mock->expects( $this->any() )
336 ->method( 'getDBkey' )
337 ->will( $this->returnValue( __CLASS__
) );
338 $mock->expects( $this->any() )
339 ->method( 'getArticleID' )
340 ->will( $this->returnValue( $id ) );
346 * @param SlotRecord[] $slots
347 * @return MutableRevisionRecord
349 private function getRevisionRecord( ...$slots ) {
350 $title = $this->getMockTitle();
351 $revision = new MutableRevisionRecord( $title );
352 foreach ( $slots as $slot ) {
353 $revision->setSlot( $slot );