DifferenceEngineTest.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378
  1. <?php
  2. use MediaWiki\Storage\MutableRevisionRecord;
  3. use MediaWiki\Storage\RevisionRecord;
  4. use MediaWiki\Storage\SlotRecord;
  5. use Wikimedia\TestingAccessWrapper;
  6. /**
  7. * @covers DifferenceEngine
  8. *
  9. * @todo tests for the rest of DifferenceEngine!
  10. *
  11. * @group Database
  12. * @group Diff
  13. *
  14. * @author Katie Filbert < aude.wiki@gmail.com >
  15. */
  16. class DifferenceEngineTest extends MediaWikiTestCase {
  17. protected $context;
  18. private static $revisions;
  19. protected function setUp() {
  20. parent::setUp();
  21. $title = $this->getTitle();
  22. $this->context = new RequestContext();
  23. $this->context->setTitle( $title );
  24. if ( !self::$revisions ) {
  25. self::$revisions = $this->doEdits();
  26. }
  27. }
  28. /**
  29. * @return Title
  30. */
  31. protected function getTitle() {
  32. $namespace = $this->getDefaultWikitextNS();
  33. return Title::newFromText( 'Kitten', $namespace );
  34. }
  35. /**
  36. * @return int[] Revision ids
  37. */
  38. protected function doEdits() {
  39. $title = $this->getTitle();
  40. $page = WikiPage::factory( $title );
  41. $strings = [ "it is a kitten", "two kittens", "three kittens", "four kittens" ];
  42. $revisions = [];
  43. foreach ( $strings as $string ) {
  44. $content = ContentHandler::makeContent( $string, $title );
  45. $page->doEditContent( $content, 'edit page' );
  46. $revisions[] = $page->getLatest();
  47. }
  48. return $revisions;
  49. }
  50. public function testMapDiffPrevNext() {
  51. $cases = $this->getMapDiffPrevNextCases();
  52. foreach ( $cases as $case ) {
  53. list( $expected, $old, $new, $message ) = $case;
  54. $diffEngine = new DifferenceEngine( $this->context, $old, $new, 2, true, false );
  55. $diffMap = $diffEngine->mapDiffPrevNext( $old, $new );
  56. $this->assertEquals( $expected, $diffMap, $message );
  57. }
  58. }
  59. private function getMapDiffPrevNextCases() {
  60. $revs = self::$revisions;
  61. return [
  62. [ [ $revs[1], $revs[2] ], $revs[2], 'prev', 'diff=prev' ],
  63. [ [ $revs[2], $revs[3] ], $revs[2], 'next', 'diff=next' ],
  64. [ [ $revs[1], $revs[3] ], $revs[1], $revs[3], 'diff=' . $revs[3] ]
  65. ];
  66. }
  67. public function testLoadRevisionData() {
  68. $cases = $this->getLoadRevisionDataCases();
  69. foreach ( $cases as $testName => $case ) {
  70. list( $expectedOld, $expectedNew, $expectedRet, $old, $new ) = $case;
  71. $diffEngine = new DifferenceEngine( $this->context, $old, $new, 2, true, false );
  72. $ret = $diffEngine->loadRevisionData();
  73. $ret2 = $diffEngine->loadRevisionData();
  74. $this->assertEquals( $expectedOld, $diffEngine->getOldid(), $testName );
  75. $this->assertEquals( $expectedNew, $diffEngine->getNewid(), $testName );
  76. $this->assertEquals( $expectedRet, $ret, $testName );
  77. $this->assertEquals( $expectedRet, $ret2, $testName );
  78. }
  79. }
  80. private function getLoadRevisionDataCases() {
  81. $revs = self::$revisions;
  82. return [
  83. 'diff=prev' => [ $revs[2], $revs[3], true, $revs[3], 'prev' ],
  84. 'diff=next' => [ $revs[2], $revs[3], true, $revs[2], 'next' ],
  85. 'diff=' . $revs[3] => [ $revs[1], $revs[3], true, $revs[1], $revs[3] ],
  86. 'diff=0' => [ $revs[1], $revs[3], true, $revs[1], 0 ],
  87. 'diff=prev&oldid=<first>' => [ false, $revs[0], true, $revs[0], 'prev' ],
  88. 'invalid' => [ 123456789, $revs[1], false, 123456789, $revs[1] ],
  89. ];
  90. }
  91. public function testGetOldid() {
  92. $revs = self::$revisions;
  93. $diffEngine = new DifferenceEngine( $this->context, $revs[1], $revs[2], 2, true, false );
  94. $this->assertEquals( $revs[1], $diffEngine->getOldid(), 'diff get old id' );
  95. }
  96. public function testGetNewid() {
  97. $revs = self::$revisions;
  98. $diffEngine = new DifferenceEngine( $this->context, $revs[1], $revs[2], 2, true, false );
  99. $this->assertEquals( $revs[2], $diffEngine->getNewid(), 'diff get new id' );
  100. }
  101. public function provideLocaliseTitleTooltipsTestData() {
  102. return [
  103. 'moved paragraph left shoud get new location title' => [
  104. '<a class="mw-diff-movedpara-left">⚫</a>',
  105. '<a class="mw-diff-movedpara-left" title="(diff-paragraph-moved-tonew)">⚫</a>',
  106. ],
  107. 'moved paragraph right shoud get old location title' => [
  108. '<a class="mw-diff-movedpara-right">⚫</a>',
  109. '<a class="mw-diff-movedpara-right" title="(diff-paragraph-moved-toold)">⚫</a>',
  110. ],
  111. 'nothing changed when key not hit' => [
  112. '<a class="mw-diff-movedpara-rightis">⚫</a>',
  113. '<a class="mw-diff-movedpara-rightis">⚫</a>',
  114. ],
  115. ];
  116. }
  117. /**
  118. * @dataProvider provideLocaliseTitleTooltipsTestData
  119. */
  120. public function testAddLocalisedTitleTooltips( $input, $expected ) {
  121. $this->setContentLang( 'qqx' );
  122. $diffEngine = TestingAccessWrapper::newFromObject( new DifferenceEngine() );
  123. $this->assertEquals( $expected, $diffEngine->addLocalisedTitleTooltips( $input ) );
  124. }
  125. /**
  126. * @dataProvider provideGenerateContentDiffBody
  127. */
  128. public function testGenerateContentDiffBody(
  129. Content $oldContent, Content $newContent, $expectedDiff
  130. ) {
  131. // Set $wgExternalDiffEngine to something bogus to try to force use of
  132. // the PHP engine rather than wikidiff2.
  133. $this->setMwGlobals( [
  134. 'wgExternalDiffEngine' => '/dev/null',
  135. ] );
  136. $differenceEngine = new DifferenceEngine();
  137. $diff = $differenceEngine->generateContentDiffBody( $oldContent, $newContent );
  138. $this->assertSame( $expectedDiff, $this->getPlainDiff( $diff ) );
  139. }
  140. public function provideGenerateContentDiffBody() {
  141. $this->mergeMwGlobalArrayValue( 'wgContentHandlers', [
  142. 'testing-nontext' => DummyNonTextContentHandler::class,
  143. ] );
  144. $content1 = ContentHandler::makeContent( 'xxx', null, CONTENT_MODEL_TEXT );
  145. $content2 = ContentHandler::makeContent( 'yyy', null, CONTENT_MODEL_TEXT );
  146. return [
  147. 'self-diff' => [ $content1, $content1, '' ],
  148. 'text diff' => [ $content1, $content2, '-xxx+yyy' ],
  149. ];
  150. }
  151. public function testGenerateTextDiffBody() {
  152. // Set $wgExternalDiffEngine to something bogus to try to force use of
  153. // the PHP engine rather than wikidiff2.
  154. $this->setMwGlobals( [
  155. 'wgExternalDiffEngine' => '/dev/null',
  156. ] );
  157. $oldText = "aaa\nbbb\nccc";
  158. $newText = "aaa\nxxx\nccc";
  159. $expectedDiff = " aaa aaa\n-bbb+xxx\n ccc ccc";
  160. $differenceEngine = new DifferenceEngine();
  161. $diff = $differenceEngine->generateTextDiffBody( $oldText, $newText );
  162. $this->assertSame( $expectedDiff, $this->getPlainDiff( $diff ) );
  163. }
  164. public function testSetContent() {
  165. // Set $wgExternalDiffEngine to something bogus to try to force use of
  166. // the PHP engine rather than wikidiff2.
  167. $this->setMwGlobals( [
  168. 'wgExternalDiffEngine' => '/dev/null',
  169. ] );
  170. $oldContent = ContentHandler::makeContent( 'xxx', null, CONTENT_MODEL_TEXT );
  171. $newContent = ContentHandler::makeContent( 'yyy', null, CONTENT_MODEL_TEXT );
  172. $differenceEngine = new DifferenceEngine();
  173. $differenceEngine->setContent( $oldContent, $newContent );
  174. $diff = $differenceEngine->getDiffBody();
  175. $this->assertSame( "Line 1:\nLine 1:\n-xxx+yyy", $this->getPlainDiff( $diff ) );
  176. }
  177. public function testSetRevisions() {
  178. $main1 = SlotRecord::newUnsaved( 'main',
  179. ContentHandler::makeContent( 'xxx', null, CONTENT_MODEL_TEXT ) );
  180. $main2 = SlotRecord::newUnsaved( 'main',
  181. ContentHandler::makeContent( 'yyy', null, CONTENT_MODEL_TEXT ) );
  182. $rev1 = $this->getRevisionRecord( $main1 );
  183. $rev2 = $this->getRevisionRecord( $main2 );
  184. $differenceEngine = new DifferenceEngine();
  185. $differenceEngine->setRevisions( $rev1, $rev2 );
  186. $this->assertSame( $rev1, $differenceEngine->getOldRevision() );
  187. $this->assertSame( $rev2, $differenceEngine->getNewRevision() );
  188. $this->assertSame( true, $differenceEngine->loadRevisionData() );
  189. $this->assertSame( true, $differenceEngine->loadText() );
  190. $differenceEngine->setRevisions( null, $rev2 );
  191. $this->assertSame( null, $differenceEngine->getOldRevision() );
  192. }
  193. /**
  194. * @dataProvider provideGetDiffBody
  195. */
  196. public function testGetDiffBody(
  197. RevisionRecord $oldRevision = null, RevisionRecord $newRevision = null, $expectedDiff
  198. ) {
  199. // Set $wgExternalDiffEngine to something bogus to try to force use of
  200. // the PHP engine rather than wikidiff2.
  201. $this->setMwGlobals( [
  202. 'wgExternalDiffEngine' => '/dev/null',
  203. ] );
  204. if ( $expectedDiff instanceof Exception ) {
  205. $this->setExpectedException( get_class( $expectedDiff ), $expectedDiff->getMessage() );
  206. }
  207. $differenceEngine = new DifferenceEngine();
  208. $differenceEngine->setRevisions( $oldRevision, $newRevision );
  209. if ( $expectedDiff instanceof Exception ) {
  210. return;
  211. }
  212. $diff = $differenceEngine->getDiffBody();
  213. $this->assertSame( $expectedDiff, $this->getPlainDiff( $diff ) );
  214. }
  215. public function provideGetDiffBody() {
  216. $main1 = SlotRecord::newUnsaved( 'main',
  217. ContentHandler::makeContent( 'xxx', null, CONTENT_MODEL_TEXT ) );
  218. $main2 = SlotRecord::newUnsaved( 'main',
  219. ContentHandler::makeContent( 'yyy', null, CONTENT_MODEL_TEXT ) );
  220. $slot1 = SlotRecord::newUnsaved( 'slot',
  221. ContentHandler::makeContent( 'aaa', null, CONTENT_MODEL_TEXT ) );
  222. $slot2 = SlotRecord::newUnsaved( 'slot',
  223. ContentHandler::makeContent( 'bbb', null, CONTENT_MODEL_TEXT ) );
  224. return [
  225. 'revision vs. null' => [
  226. null,
  227. $this->getRevisionRecord( $main1, $slot1 ),
  228. '',
  229. ],
  230. 'revision vs. itself' => [
  231. $this->getRevisionRecord( $main1, $slot1 ),
  232. $this->getRevisionRecord( $main1, $slot1 ),
  233. '',
  234. ],
  235. 'different text in one slot' => [
  236. $this->getRevisionRecord( $main1, $slot1 ),
  237. $this->getRevisionRecord( $main1, $slot2 ),
  238. "slotLine 1:\nLine 1:\n-aaa+bbb",
  239. ],
  240. 'different text in two slots' => [
  241. $this->getRevisionRecord( $main1, $slot1 ),
  242. $this->getRevisionRecord( $main2, $slot2 ),
  243. "Line 1:\nLine 1:\n-xxx+yyy\nslotLine 1:\nLine 1:\n-aaa+bbb",
  244. ],
  245. 'new slot' => [
  246. $this->getRevisionRecord( $main1 ),
  247. $this->getRevisionRecord( $main1, $slot1 ),
  248. "slotLine 1:\nLine 1:\n- +aaa",
  249. ],
  250. ];
  251. }
  252. public function testRecursion() {
  253. // Set up a ContentHandler which will return a wrapped DifferenceEngine as
  254. // SlotDiffRenderer, then pass it a content which uses the same ContentHandler.
  255. // This tests the anti-recursion logic in DifferenceEngine::generateContentDiffBody.
  256. $customDifferenceEngine = $this->getMockBuilder( DifferenceEngine::class )
  257. ->enableProxyingToOriginalMethods()
  258. ->getMock();
  259. $customContentHandler = $this->getMockBuilder( ContentHandler::class )
  260. ->setConstructorArgs( [ 'foo', [] ] )
  261. ->setMethods( [ 'createDifferenceEngine' ] )
  262. ->getMockForAbstractClass();
  263. $customContentHandler->expects( $this->any() )
  264. ->method( 'createDifferenceEngine' )
  265. ->willReturn( $customDifferenceEngine );
  266. /** @var $customContentHandler ContentHandler */
  267. $customContent = $this->getMockBuilder( Content::class )
  268. ->setMethods( [ 'getContentHandler' ] )
  269. ->getMockForAbstractClass();
  270. $customContent->expects( $this->any() )
  271. ->method( 'getContentHandler' )
  272. ->willReturn( $customContentHandler );
  273. /** @var $customContent Content */
  274. $customContent2 = clone $customContent;
  275. $slotDiffRenderer = $customContentHandler->getSlotDiffRenderer( RequestContext::getMain() );
  276. $this->setExpectedException( Exception::class,
  277. ': could not maintain backwards compatibility. Please use a SlotDiffRenderer.' );
  278. $slotDiffRenderer->getDiff( $customContent, $customContent2 );
  279. }
  280. /**
  281. * Convert a HTML diff to a human-readable format and hopefully make the test less fragile.
  282. * @param string diff
  283. * @return string
  284. */
  285. private function getPlainDiff( $diff ) {
  286. $replacements = [
  287. html_entity_decode( '&nbsp;' ) => ' ',
  288. html_entity_decode( '&minus;' ) => '-',
  289. ];
  290. return str_replace( array_keys( $replacements ), array_values( $replacements ),
  291. trim( strip_tags( $diff ), "\n" ) );
  292. }
  293. /**
  294. * @param int $id
  295. * @return Title
  296. */
  297. private function getMockTitle( $id = 23 ) {
  298. $mock = $this->getMockBuilder( Title::class )
  299. ->disableOriginalConstructor()
  300. ->getMock();
  301. $mock->expects( $this->any() )
  302. ->method( 'getDBkey' )
  303. ->will( $this->returnValue( __CLASS__ ) );
  304. $mock->expects( $this->any() )
  305. ->method( 'getArticleID' )
  306. ->will( $this->returnValue( $id ) );
  307. return $mock;
  308. }
  309. /**
  310. * @param SlotRecord[] $slots
  311. * @return MutableRevisionRecord
  312. */
  313. private function getRevisionRecord( ...$slots ) {
  314. $title = $this->getMockTitle();
  315. $revision = new MutableRevisionRecord( $title );
  316. foreach ( $slots as $slot ) {
  317. $revision->setSlot( $slot );
  318. }
  319. return $revision;
  320. }
  321. }