3 use Wikimedia\Assert\ParameterTypeException
;
6 * @covers TextSlotDiffRenderer
8 class TextSlotDiffRendererTest
extends MediaWikiTestCase
{
11 * @dataProvider provideGetDiff
12 * @param array|null $oldContentArgs To pass to makeContent() (if not null)
13 * @param array|null $newContentArgs
14 * @param string|Exception $expectedResult
17 public function testGetDiff(
18 array $oldContentArgs = null, array $newContentArgs = null, $expectedResult
20 $this->mergeMwGlobalArrayValue( 'wgContentHandlers', [
21 'testing' => DummyContentHandlerForTesting
::class,
22 'testing-nontext' => DummyNonTextContentHandler
::class,
25 $oldContent = $oldContentArgs ? self
::makeContent( ...$oldContentArgs ) : null;
26 $newContent = $newContentArgs ? self
::makeContent( ...$newContentArgs ) : null;
28 if ( $expectedResult instanceof Exception
) {
29 $this->setExpectedException( get_class( $expectedResult ), $expectedResult->getMessage() );
32 $slotDiffRenderer = $this->getTextSlotDiffRenderer();
33 $diff = $slotDiffRenderer->getDiff( $oldContent, $newContent );
34 if ( $expectedResult instanceof Exception
) {
37 $plainDiff = $this->getPlainDiff( $diff );
38 $this->assertSame( $expectedResult, $plainDiff );
41 public static function provideGetDiff() {
51 " aaa aaa\n-bbb+xxx\n ccc ccc",
53 'no right content' => [
56 "-aaa+ \n-bbb \n-ccc ",
58 'no left content' => [
61 "- +aaa\n +bbb\n +ccc",
66 new InvalidArgumentException( '$oldContent and $newContent cannot both be null' ),
68 'non-text left content' => [
69 [ '', 'testing-nontext' ],
71 new ParameterTypeException( '$oldContent', 'TextContent|null' ),
73 'non-text right content' => [
75 [ '', 'testing-nontext' ],
76 new ParameterTypeException( '$newContent', 'TextContent|null' ),
81 // no separate test for getTextDiff() as getDiff() is just a thin wrapper around it
84 * @return TextSlotDiffRenderer
86 private function getTextSlotDiffRenderer() {
87 $slotDiffRenderer = new TextSlotDiffRenderer();
88 $slotDiffRenderer->setStatsdDataFactory( new NullStatsdDataFactory() );
89 $slotDiffRenderer->setLanguage( Language
::factory( 'en' ) );
90 $slotDiffRenderer->setEngine( TextSlotDiffRenderer
::ENGINE_PHP
);
91 return $slotDiffRenderer;
95 * Convert a HTML diff to a human-readable format and hopefully make the test less fragile.
99 private function getPlainDiff( $diff ) {
101 html_entity_decode( ' ' ) => ' ',
102 html_entity_decode( '−' ) => '-',
104 return str_replace( array_keys( $replacements ), array_values( $replacements ),
105 trim( strip_tags( $diff ), "\n" ) );
110 * @param string $model
111 * @return null|TextContent
113 private static function makeContent( $str, $model = CONTENT_MODEL_TEXT
) {
114 return ContentHandler
::makeContent( $str, null, $model );