3 use Wikimedia\Assert\ParameterTypeException
;
6 * @covers TextSlotDiffRenderer
8 class TextSlotDiffRendererTest
extends MediaWikiTestCase
{
11 * @dataProvider provideGetDiff
12 * @param Content|null $oldContent
13 * @param Content|null $newContent
14 * @param string|Exception $expectedResult
17 public function testGetDiff(
18 Content
$oldContent = null, Content
$newContent = null, $expectedResult
20 if ( $expectedResult instanceof Exception
) {
21 $this->setExpectedException( get_class( $expectedResult ), $expectedResult->getMessage() );
24 $slotDiffRenderer = $this->getTextSlotDiffRenderer();
25 $diff = $slotDiffRenderer->getDiff( $oldContent, $newContent );
26 if ( $expectedResult instanceof Exception
) {
29 $plainDiff = $this->getPlainDiff( $diff );
30 $this->assertSame( $expectedResult, $plainDiff );
33 public function provideGetDiff() {
34 $this->mergeMwGlobalArrayValue( 'wgContentHandlers', [
35 'testing' => DummyContentHandlerForTesting
::class,
36 'testing-nontext' => DummyNonTextContentHandler
::class,
41 $this->makeContent( "aaa\nbbb\nccc" ),
42 $this->makeContent( "aaa\nbbb\nccc" ),
46 $this->makeContent( "aaa\nbbb\nccc" ),
47 $this->makeContent( "aaa\nxxx\nccc" ),
48 " aaa aaa\n-bbb+xxx\n ccc ccc",
50 'no right content' => [
51 $this->makeContent( "aaa\nbbb\nccc" ),
53 "-aaa+ \n-bbb \n-ccc ",
55 'no left content' => [
57 $this->makeContent( "aaa\nbbb\nccc" ),
58 "- +aaa\n +bbb\n +ccc",
63 new InvalidArgumentException( '$oldContent and $newContent cannot both be null' ),
65 'non-text left content' => [
66 $this->makeContent( '', 'testing-nontext' ),
67 $this->makeContent( "aaa\nbbb\nccc" ),
68 new ParameterTypeException( '$oldContent', 'TextContent|null' ),
70 'non-text right content' => [
71 $this->makeContent( "aaa\nbbb\nccc" ),
72 $this->makeContent( '', 'testing-nontext' ),
73 new ParameterTypeException( '$newContent', 'TextContent|null' ),
78 // no separate test for getTextDiff() as getDiff() is just a thin wrapper around it
81 * @return TextSlotDiffRenderer
83 private function getTextSlotDiffRenderer() {
84 $slotDiffRenderer = new TextSlotDiffRenderer();
85 $slotDiffRenderer->setStatsdDataFactory( new NullStatsdDataFactory() );
86 $slotDiffRenderer->setLanguage( Language
::factory( 'en' ) );
87 $slotDiffRenderer->setEngine( TextSlotDiffRenderer
::ENGINE_PHP
);
88 return $slotDiffRenderer;
92 * Convert a HTML diff to a human-readable format and hopefully make the test less fragile.
96 private function getPlainDiff( $diff ) {
98 html_entity_decode( ' ' ) => ' ',
99 html_entity_decode( '−' ) => '-',
101 return str_replace( array_keys( $replacements ), array_values( $replacements ),
102 trim( strip_tags( $diff ), "\n" ) );
107 * @param string $model
108 * @return null|TextContent
110 private function makeContent( $str, $model = CONTENT_MODEL_TEXT
) {
111 return ContentHandler
::makeContent( $str, null, $model );