3 use MediaWiki\Storage\MutableRevisionRecord
;
4 use MediaWiki\Storage\RevisionRecord
;
5 use MediaWiki\Storage\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 $case ) {
90 list( $expectedOld, $expectedNew, $old, $new, $message ) = $case;
92 $diffEngine = new DifferenceEngine( $this->context
, $old, $new, 2, true, false );
93 $diffEngine->loadRevisionData();
95 $this->assertEquals( $diffEngine->getOldid(), $expectedOld, $message );
96 $this->assertEquals( $diffEngine->getNewid(), $expectedNew, $message );
100 private function getLoadRevisionDataCases() {
101 $revs = self
::$revisions;
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' ]
111 public function testGetOldid() {
112 $revs = self
::$revisions;
114 $diffEngine = new DifferenceEngine( $this->context
, $revs[1], $revs[2], 2, true, false );
115 $this->assertEquals( $revs[1], $diffEngine->getOldid(), 'diff get old id' );
118 public function testGetNewid() {
119 $revs = self
::$revisions;
121 $diffEngine = new DifferenceEngine( $this->context
, $revs[1], $revs[2], 2, true, false );
122 $this->assertEquals( $revs[2], $diffEngine->getNewid(), 'diff get new id' );
125 public function provideLocaliseTitleTooltipsTestData() {
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>',
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>',
135 'nothing changed when key not hit' => [
136 '<a class="mw-diff-movedpara-rightis">⚫</a>',
137 '<a class="mw-diff-movedpara-rightis">⚫</a>',
143 * @dataProvider provideLocaliseTitleTooltipsTestData
145 public function testAddLocalisedTitleTooltips( $input, $expected ) {
146 $this->setContentLang( 'qqx' );
147 $diffEngine = TestingAccessWrapper
::newFromObject( new DifferenceEngine() );
148 $this->assertEquals( $expected, $diffEngine->addLocalisedTitleTooltips( $input ) );
152 * @dataProvider provideGenerateContentDiffBody
154 public function testGenerateContentDiffBody(
155 Content
$oldContent, Content
$newContent, $expectedDiff
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',
163 $differenceEngine = new DifferenceEngine();
164 $diff = $differenceEngine->generateContentDiffBody( $oldContent, $newContent );
165 $this->assertSame( $expectedDiff, $this->getPlainDiff( $diff ) );
168 public function provideGenerateContentDiffBody() {
169 $this->mergeMwGlobalArrayValue( 'wgContentHandlers', [
170 'testing-nontext' => DummyNonTextContentHandler
::class,
172 $content1 = ContentHandler
::makeContent( 'xxx', null, CONTENT_MODEL_TEXT
);
173 $content2 = ContentHandler
::makeContent( 'yyy', null, CONTENT_MODEL_TEXT
);
176 'self-diff' => [ $content1, $content1, '' ],
177 'text diff' => [ $content1, $content2, '-xxx+yyy' ],
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',
188 $oldText = "aaa\nbbb\nccc";
189 $newText = "aaa\nxxx\nccc";
190 $expectedDiff = " aaa aaa\n-bbb+xxx\n ccc ccc";
192 $differenceEngine = new DifferenceEngine();
193 $diff = $differenceEngine->generateTextDiffBody( $oldText, $newText );
194 $this->assertSame( $expectedDiff, $this->getPlainDiff( $diff ) );
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',
204 $oldContent = ContentHandler
::makeContent( 'xxx', null, CONTENT_MODEL_TEXT
);
205 $newContent = ContentHandler
::makeContent( 'yyy', null, CONTENT_MODEL_TEXT
);
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 ) );
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 );
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() );
228 $differenceEngine->setRevisions( null, $rev2 );
229 $this->assertSame( null, $differenceEngine->getOldRevision() );
233 * @dataProvider provideGetDiffBody
235 public function testGetDiffBody(
236 RevisionRecord
$oldRevision = null, RevisionRecord
$newRevision = null, $expectedDiff
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',
244 if ( $expectedDiff instanceof Exception
) {
245 $this->setExpectedException( get_class( $expectedDiff ), $expectedDiff->getMessage() );
247 $differenceEngine = new DifferenceEngine();
248 $differenceEngine->setRevisions( $oldRevision, $newRevision );
249 if ( $expectedDiff instanceof Exception
) {
253 $diff = $differenceEngine->getDiffBody();
254 $this->assertSame( $expectedDiff, $this->getPlainDiff( $diff ) );
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
) );
268 'revision vs. null' => [
270 $this->getRevisionRecord( $main1, $slot1 ),
273 'revision vs. itself' => [
274 $this->getRevisionRecord( $main1, $slot1 ),
275 $this->getRevisionRecord( $main1, $slot1 ),
278 'different text in one slot' => [
279 $this->getRevisionRecord( $main1, $slot1 ),
280 $this->getRevisionRecord( $main1, $slot2 ),
281 "slotLine 1:\nLine 1:\n-aaa+bbb",
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",
289 $this->getRevisionRecord( $main1 ),
290 $this->getRevisionRecord( $main1, $slot1 ),
291 "slotLine 1:\nLine 1:\n- +aaa",
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.
301 $customDifferenceEngine = $this->getMockBuilder( DifferenceEngine
::class )
302 ->enableProxyingToOriginalMethods()
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;
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 );
328 * Convert a HTML diff to a human-readable format and hopefully make the test less fragile.
332 private function getPlainDiff( $diff ) {
334 html_entity_decode( ' ' ) => ' ',
335 html_entity_decode( '−' ) => '-',
337 return str_replace( array_keys( $replacements ), array_values( $replacements ),
338 trim( strip_tags( $diff ), "\n" ) );
342 * @param SlotRecord[] $slots
343 * @return MutableRevisionRecord
345 private function getRevisionRecord( ...$slots ) {
346 $title = Title
::newFromText( 'Foo' );
347 $revision = new MutableRevisionRecord( $title );
348 foreach ( $slots as $slot ) {
349 $revision->setSlot( $slot );