Add $wgDiffEngine
[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 $this->setMwGlobals( [ 'wgDiffEngine' => 'php' ] );
37 }
38
39 /**
40 * @return Title
41 */
42 protected function getTitle() {
43 $namespace = $this->getDefaultWikitextNS();
44 return Title::newFromText( 'Kitten', $namespace );
45 }
46
47 /**
48 * @return int[] Revision ids
49 */
50 protected function doEdits() {
51 $title = $this->getTitle();
52 $page = WikiPage::factory( $title );
53
54 $strings = [ "it is a kitten", "two kittens", "three kittens", "four kittens" ];
55 $revisions = [];
56
57 foreach ( $strings as $string ) {
58 $content = ContentHandler::makeContent( $string, $title );
59 $page->doEditContent( $content, 'edit page' );
60 $revisions[] = $page->getLatest();
61 }
62
63 return $revisions;
64 }
65
66 public function testMapDiffPrevNext() {
67 $cases = $this->getMapDiffPrevNextCases();
68
69 foreach ( $cases as $case ) {
70 list( $expected, $old, $new, $message ) = $case;
71
72 $diffEngine = new DifferenceEngine( $this->context, $old, $new, 2, true, false );
73 $diffMap = $diffEngine->mapDiffPrevNext( $old, $new );
74 $this->assertEquals( $expected, $diffMap, $message );
75 }
76 }
77
78 private function getMapDiffPrevNextCases() {
79 $revs = self::$revisions;
80
81 return [
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] ]
85 ];
86 }
87
88 public function testLoadRevisionData() {
89 $cases = $this->getLoadRevisionDataCases();
90
91 foreach ( $cases as $testName => $case ) {
92 list( $expectedOld, $expectedNew, $expectedRet, $old, $new ) = $case;
93
94 $diffEngine = new DifferenceEngine( $this->context, $old, $new, 2, true, false );
95 $ret = $diffEngine->loadRevisionData();
96 $ret2 = $diffEngine->loadRevisionData();
97
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 );
102 }
103 }
104
105 private function getLoadRevisionDataCases() {
106 $revs = self::$revisions;
107
108 return [
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] ],
115 ];
116 }
117
118 public function testGetOldid() {
119 $revs = self::$revisions;
120
121 $diffEngine = new DifferenceEngine( $this->context, $revs[1], $revs[2], 2, true, false );
122 $this->assertEquals( $revs[1], $diffEngine->getOldid(), 'diff get old id' );
123 }
124
125 public function testGetNewid() {
126 $revs = self::$revisions;
127
128 $diffEngine = new DifferenceEngine( $this->context, $revs[1], $revs[2], 2, true, false );
129 $this->assertEquals( $revs[2], $diffEngine->getNewid(), 'diff get new id' );
130 }
131
132 public function provideLocaliseTitleTooltipsTestData() {
133 return [
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>',
137 ],
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>',
141 ],
142 'nothing changed when key not hit' => [
143 '<a class="mw-diff-movedpara-rightis">⚫</a>',
144 '<a class="mw-diff-movedpara-rightis">⚫</a>',
145 ],
146 ];
147 }
148
149 /**
150 * @dataProvider provideLocaliseTitleTooltipsTestData
151 */
152 public function testAddLocalisedTitleTooltips( $input, $expected ) {
153 $this->setContentLang( 'qqx' );
154 $diffEngine = TestingAccessWrapper::newFromObject( new DifferenceEngine() );
155 $this->assertEquals( $expected, $diffEngine->addLocalisedTitleTooltips( $input ) );
156 }
157
158 /**
159 * @dataProvider provideGenerateContentDiffBody
160 */
161 public function testGenerateContentDiffBody(
162 array $oldContentArgs, array $newContentArgs, $expectedDiff
163 ) {
164 $this->mergeMwGlobalArrayValue( 'wgContentHandlers', [
165 'testing-nontext' => DummyNonTextContentHandler::class,
166 ] );
167 $oldContent = ContentHandler::makeContent( ...$oldContentArgs );
168 $newContent = ContentHandler::makeContent( ...$newContentArgs );
169
170 $differenceEngine = new DifferenceEngine();
171 $diff = $differenceEngine->generateContentDiffBody( $oldContent, $newContent );
172 $this->assertSame( $expectedDiff, $this->getPlainDiff( $diff ) );
173 }
174
175 public static function provideGenerateContentDiffBody() {
176 $content1 = [ 'xxx', null, CONTENT_MODEL_TEXT ];
177 $content2 = [ 'yyy', null, CONTENT_MODEL_TEXT ];
178
179 return [
180 'self-diff' => [ $content1, $content1, '' ],
181 'text diff' => [ $content1, $content2, '-xxx+yyy' ],
182 ];
183 }
184
185 public function testGenerateTextDiffBody() {
186 $oldText = "aaa\nbbb\nccc";
187 $newText = "aaa\nxxx\nccc";
188 $expectedDiff = " aaa aaa\n-bbb+xxx\n ccc ccc";
189
190 $differenceEngine = new DifferenceEngine();
191 $diff = $differenceEngine->generateTextDiffBody( $oldText, $newText );
192 $this->assertSame( $expectedDiff, $this->getPlainDiff( $diff ) );
193 }
194
195 public function testSetContent() {
196 $oldContent = ContentHandler::makeContent( 'xxx', null, CONTENT_MODEL_TEXT );
197 $newContent = ContentHandler::makeContent( 'yyy', null, CONTENT_MODEL_TEXT );
198
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 ) );
203 }
204
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 );
212
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() );
219
220 $differenceEngine->setRevisions( null, $rev2 );
221 $this->assertSame( null, $differenceEngine->getOldRevision() );
222 }
223
224 /**
225 * @dataProvider provideGetDiffBody
226 */
227 public function testGetDiffBody(
228 RevisionRecord $oldRevision = null, RevisionRecord $newRevision = null, $expectedDiff
229 ) {
230 if ( $expectedDiff instanceof Exception ) {
231 $this->setExpectedException( get_class( $expectedDiff ), $expectedDiff->getMessage() );
232 }
233 $differenceEngine = new DifferenceEngine();
234 $differenceEngine->setRevisions( $oldRevision, $newRevision );
235 if ( $expectedDiff instanceof Exception ) {
236 return;
237 }
238
239 $diff = $differenceEngine->getDiffBody();
240 $this->assertSame( $expectedDiff, $this->getPlainDiff( $diff ) );
241 }
242
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 ) );
252
253 return [
254 'revision vs. null' => [
255 null,
256 $this->getRevisionRecord( $main1, $slot1 ),
257 '',
258 ],
259 'revision vs. itself' => [
260 $this->getRevisionRecord( $main1, $slot1 ),
261 $this->getRevisionRecord( $main1, $slot1 ),
262 '',
263 ],
264 'different text in one slot' => [
265 $this->getRevisionRecord( $main1, $slot1 ),
266 $this->getRevisionRecord( $main1, $slot2 ),
267 "slotLine 1:\nLine 1:\n-aaa+bbb",
268 ],
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",
273 ],
274 'new slot' => [
275 $this->getRevisionRecord( $main1 ),
276 $this->getRevisionRecord( $main1, $slot1 ),
277 "slotLine 1:\nLine 1:\n- +aaa",
278 ],
279 ];
280 }
281
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.
286
287 $customDifferenceEngine = $this->getMockBuilder( DifferenceEngine::class )
288 ->enableProxyingToOriginalMethods()
289 ->getMock();
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;
306
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 );
311 }
312
313 /**
314 * Convert a HTML diff to a human-readable format and hopefully make the test less fragile.
315 * @param string diff
316 * @return string
317 */
318 private function getPlainDiff( $diff ) {
319 $replacements = [
320 html_entity_decode( '&nbsp;' ) => ' ',
321 html_entity_decode( '&minus;' ) => '-',
322 ];
323 return str_replace( array_keys( $replacements ), array_values( $replacements ),
324 trim( strip_tags( $diff ), "\n" ) );
325 }
326
327 /**
328 * @param int $id
329 * @return Title
330 */
331 private function getMockTitle( $id = 23 ) {
332 $mock = $this->getMockBuilder( Title::class )
333 ->disableOriginalConstructor()
334 ->getMock();
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 ) );
341
342 return $mock;
343 }
344
345 /**
346 * @param SlotRecord[] $slots
347 * @return MutableRevisionRecord
348 */
349 private function getRevisionRecord( ...$slots ) {
350 $title = $this->getMockTitle();
351 $revision = new MutableRevisionRecord( $title );
352 foreach ( $slots as $slot ) {
353 $revision->setSlot( $slot );
354 }
355 return $revision;
356 }
357
358 }