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 $differenceEngine = new DifferenceEngine();
169 $diff = $differenceEngine->generateContentDiffBody( $oldContent, $newContent );
170 $this->assertSame( $expectedDiff, $this->getPlainDiff( $diff ) );
173 public static function provideGenerateContentDiffBody() {
174 $content1 = [ 'xxx', null, CONTENT_MODEL_TEXT
];
175 $content2 = [ 'yyy', null, CONTENT_MODEL_TEXT
];
178 'self-diff' => [ $content1, $content1, '' ],
179 'text diff' => [ $content1, $content2, '-xxx+yyy' ],
183 public function testGenerateTextDiffBody() {
184 $oldText = "aaa\nbbb\nccc";
185 $newText = "aaa\nxxx\nccc";
186 $expectedDiff = " aaa aaa\n-bbb+xxx\n ccc ccc";
188 $differenceEngine = new DifferenceEngine();
189 $diff = $differenceEngine->generateTextDiffBody( $oldText, $newText );
190 $this->assertSame( $expectedDiff, $this->getPlainDiff( $diff ) );
193 public function testSetContent() {
194 $oldContent = ContentHandler
::makeContent( 'xxx', null, CONTENT_MODEL_TEXT
);
195 $newContent = ContentHandler
::makeContent( 'yyy', null, CONTENT_MODEL_TEXT
);
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 ) );
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 );
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() );
218 $differenceEngine->setRevisions( null, $rev2 );
219 $this->assertSame( null, $differenceEngine->getOldRevision() );
223 * @dataProvider provideGetDiffBody
225 public function testGetDiffBody(
226 RevisionRecord
$oldRevision = null, RevisionRecord
$newRevision = null, $expectedDiff
228 if ( $expectedDiff instanceof Exception
) {
229 $this->setExpectedException( get_class( $expectedDiff ), $expectedDiff->getMessage() );
231 $differenceEngine = new DifferenceEngine();
232 $differenceEngine->setRevisions( $oldRevision, $newRevision );
233 if ( $expectedDiff instanceof Exception
) {
237 $diff = $differenceEngine->getDiffBody();
238 $this->assertSame( $expectedDiff, $this->getPlainDiff( $diff ) );
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
) );
252 'revision vs. null' => [
254 $this->getRevisionRecord( $main1, $slot1 ),
257 'revision vs. itself' => [
258 $this->getRevisionRecord( $main1, $slot1 ),
259 $this->getRevisionRecord( $main1, $slot1 ),
262 'different text in one slot' => [
263 $this->getRevisionRecord( $main1, $slot1 ),
264 $this->getRevisionRecord( $main1, $slot2 ),
265 "slotLine 1:\nLine 1:\n-aaa+bbb",
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",
273 $this->getRevisionRecord( $main1 ),
274 $this->getRevisionRecord( $main1, $slot1 ),
275 "slotLine 1:\nLine 1:\n- +aaa",
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.
285 $customDifferenceEngine = $this->getMockBuilder( DifferenceEngine
::class )
286 ->enableProxyingToOriginalMethods()
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;
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 );
312 * Convert a HTML diff to a human-readable format and hopefully make the test less fragile.
316 private function getPlainDiff( $diff ) {
318 html_entity_decode( ' ' ) => ' ',
319 html_entity_decode( '−' ) => '-',
321 return str_replace( array_keys( $replacements ), array_values( $replacements ),
322 trim( strip_tags( $diff ), "\n" ) );
329 private function getMockTitle( $id = 23 ) {
330 $mock = $this->getMockBuilder( Title
::class )
331 ->disableOriginalConstructor()
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 ) );
344 * @param SlotRecord[] $slots
345 * @return MutableRevisionRecord
347 private function getRevisionRecord( ...$slots ) {
348 $title = $this->getMockTitle();
349 $revision = new MutableRevisionRecord( $title );
350 foreach ( $slots as $slot ) {
351 $revision->setSlot( $slot );