4 * @covers TextSlotDiffRenderer
6 class TextSlotDiffRendererTest
extends MediaWikiTestCase
{
9 * @dataProvider provideGetDiff
10 * @param Content|null $oldContent
11 * @param Content|null $newContent
12 * @param string|Exception $expectedResult
15 public function testGetDiff(
16 Content
$oldContent = null, Content
$newContent = null, $expectedResult
18 if ( $expectedResult instanceof Exception
) {
19 $this->setExpectedException( get_class( $expectedResult ), $expectedResult->getMessage() );
22 $slotDiffRenderer = $this->getTextSlotDiffRenderer();
23 $diff = $slotDiffRenderer->getDiff( $oldContent, $newContent );
24 if ( $expectedResult instanceof Exception
) {
27 $plainDiff = $this->getPlainDiff( $diff );
28 $this->assertSame( $expectedResult, $plainDiff );
31 public function provideGetDiff() {
32 $this->mergeMwGlobalArrayValue( 'wgContentHandlers', [
33 'testing' => DummyContentHandlerForTesting
::class,
34 'testing-nontext' => DummyNonTextContentHandler
::class,
39 $this->makeContent( "aaa\nbbb\nccc" ),
40 $this->makeContent( "aaa\nbbb\nccc" ),
44 $this->makeContent( "aaa\nbbb\nccc" ),
45 $this->makeContent( "aaa\nxxx\nccc" ),
46 " aaa aaa\n-bbb+xxx\n ccc ccc",
48 'no right content' => [
49 $this->makeContent( "aaa\nbbb\nccc" ),
51 "-aaa+ \n-bbb \n-ccc ",
53 'no left content' => [
55 $this->makeContent( "aaa\nbbb\nccc" ),
56 "- +aaa\n +bbb\n +ccc",
61 new InvalidArgumentException( '$oldContent and $newContent cannot both be null' ),
63 'non-text left content' => [
64 $this->makeContent( '', 'testing-nontext' ),
65 $this->makeContent( "aaa\nbbb\nccc" ),
66 new InvalidArgumentException( 'TextSlotDiffRenderer does not handle DummyNonTextContent' ),
68 'non-text right content' => [
69 $this->makeContent( "aaa\nbbb\nccc" ),
70 $this->makeContent( '', 'testing-nontext' ),
71 new InvalidArgumentException( 'TextSlotDiffRenderer does not handle DummyNonTextContent' ),
76 // no separate test for getTextDiff() as getDiff() is just a thin wrapper around it
79 * @return TextSlotDiffRenderer
81 private function getTextSlotDiffRenderer() {
82 $slotDiffRenderer = new TextSlotDiffRenderer();
83 $slotDiffRenderer->setStatsdDataFactory( new NullStatsdDataFactory() );
84 $slotDiffRenderer->setLanguage( Language
::factory( 'en' ) );
85 $slotDiffRenderer->setWikiDiff2MovedParagraphDetectionCutoff( 0 );
86 $slotDiffRenderer->setEngine( TextSlotDiffRenderer
::ENGINE_PHP
);
87 return $slotDiffRenderer;
91 * Convert a HTML diff to a human-readable format and hopefully make the test less fragile.
95 private function getPlainDiff( $diff ) {
97 html_entity_decode( ' ' ) => ' ',
98 html_entity_decode( '−' ) => '-',
100 return str_replace( array_keys( $replacements ), array_values( $replacements ),
101 trim( strip_tags( $diff ), "\n" ) );
106 * @param string $model
107 * @return null|TextContent
109 private function makeContent( $str, $model = CONTENT_MODEL_TEXT
) {
110 return ContentHandler
::makeContent( $str, null, $model );