TextSlotDiffRendererTest.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. <?php
  2. /**
  3. * @covers TextSlotDiffRenderer
  4. */
  5. class TextSlotDiffRendererTest extends MediaWikiTestCase {
  6. /**
  7. * @dataProvider provideGetDiff
  8. * @param Content|null $oldContent
  9. * @param Content|null $newContent
  10. * @param string|Exception $expectedResult
  11. * @throws Exception
  12. */
  13. public function testGetDiff(
  14. Content $oldContent = null, Content $newContent = null, $expectedResult
  15. ) {
  16. if ( $expectedResult instanceof Exception ) {
  17. $this->setExpectedException( get_class( $expectedResult ), $expectedResult->getMessage() );
  18. }
  19. $slotDiffRenderer = $this->getTextSlotDiffRenderer();
  20. $diff = $slotDiffRenderer->getDiff( $oldContent, $newContent );
  21. if ( $expectedResult instanceof Exception ) {
  22. return;
  23. }
  24. $plainDiff = $this->getPlainDiff( $diff );
  25. $this->assertSame( $expectedResult, $plainDiff );
  26. }
  27. public function provideGetDiff() {
  28. $this->mergeMwGlobalArrayValue( 'wgContentHandlers', [
  29. 'testing' => DummyContentHandlerForTesting::class,
  30. 'testing-nontext' => DummyNonTextContentHandler::class,
  31. ] );
  32. return [
  33. 'same text' => [
  34. $this->makeContent( "aaa\nbbb\nccc" ),
  35. $this->makeContent( "aaa\nbbb\nccc" ),
  36. "",
  37. ],
  38. 'different text' => [
  39. $this->makeContent( "aaa\nbbb\nccc" ),
  40. $this->makeContent( "aaa\nxxx\nccc" ),
  41. " aaa aaa\n-bbb+xxx\n ccc ccc",
  42. ],
  43. 'no right content' => [
  44. $this->makeContent( "aaa\nbbb\nccc" ),
  45. null,
  46. "-aaa+ \n-bbb \n-ccc ",
  47. ],
  48. 'no left content' => [
  49. null,
  50. $this->makeContent( "aaa\nbbb\nccc" ),
  51. "- +aaa\n +bbb\n +ccc",
  52. ],
  53. 'no content' => [
  54. null,
  55. null,
  56. new InvalidArgumentException( '$oldContent and $newContent cannot both be null' ),
  57. ],
  58. 'non-text left content' => [
  59. $this->makeContent( '', 'testing-nontext' ),
  60. $this->makeContent( "aaa\nbbb\nccc" ),
  61. new InvalidArgumentException( 'TextSlotDiffRenderer does not handle DummyNonTextContent' ),
  62. ],
  63. 'non-text right content' => [
  64. $this->makeContent( "aaa\nbbb\nccc" ),
  65. $this->makeContent( '', 'testing-nontext' ),
  66. new InvalidArgumentException( 'TextSlotDiffRenderer does not handle DummyNonTextContent' ),
  67. ],
  68. ];
  69. }
  70. // no separate test for getTextDiff() as getDiff() is just a thin wrapper around it
  71. /**
  72. * @return TextSlotDiffRenderer
  73. */
  74. private function getTextSlotDiffRenderer() {
  75. $slotDiffRenderer = new TextSlotDiffRenderer();
  76. $slotDiffRenderer->setStatsdDataFactory( new NullStatsdDataFactory() );
  77. $slotDiffRenderer->setLanguage( Language::factory( 'en' ) );
  78. $slotDiffRenderer->setWikiDiff2MovedParagraphDetectionCutoff( 0 );
  79. $slotDiffRenderer->setEngine( TextSlotDiffRenderer::ENGINE_PHP );
  80. return $slotDiffRenderer;
  81. }
  82. /**
  83. * Convert a HTML diff to a human-readable format and hopefully make the test less fragile.
  84. * @param string diff
  85. * @return string
  86. */
  87. private function getPlainDiff( $diff ) {
  88. $replacements = [
  89. html_entity_decode( '&nbsp;' ) => ' ',
  90. html_entity_decode( '&minus;' ) => '-',
  91. ];
  92. return str_replace( array_keys( $replacements ), array_values( $replacements ),
  93. trim( strip_tags( $diff ), "\n" ) );
  94. }
  95. /**
  96. * @param string $str
  97. * @param string $model
  98. * @return null|TextContent
  99. */
  100. private function makeContent( $str, $model = CONTENT_MODEL_TEXT ) {
  101. return ContentHandler::makeContent( $str, null, $model );
  102. }
  103. }