A new inline mode is provided for diffs. It is only available when wikidiff2 is installed. There is no PHP implementation (one can be added later if necessary) For now, it is accessed by passing diff-type as a query string parameter e.g ?diff-type=inline A control for switching between the two is added as a follow up. see Ie9bb17789d90b7492559782021937f3f3e4356f8 * The final method getSlotDiffRenderer now takes a second parameter options * The method `getSlotDiffRendererInternal` is deprecated and replaced with the more flexible `getSlotDiffRendererWithOptions` This has potential to be a breaking change but is unlikely to impact any existing clients. Note: PHP implementation can be added later if necessary Bug: T117279 Change-Id: I4f81c8ccf253dd4aa6cf43c3fad257b4b0dd1ebd
129 lines
3.7 KiB
PHP
129 lines
3.7 KiB
PHP
<?php
|
|
|
|
use MediaWiki\MediaWikiServices;
|
|
use Wikimedia\Assert\ParameterTypeException;
|
|
|
|
/**
|
|
* @covers TextSlotDiffRenderer
|
|
*/
|
|
class TextSlotDiffRendererTest extends MediaWikiTestCase {
|
|
|
|
public function testGetExtraCacheKeys() {
|
|
$slotDiffRenderer = $this->getTextSlotDiffRenderer();
|
|
$key = $slotDiffRenderer->getExtraCacheKeys();
|
|
$slotDiffRenderer->setEngine( TextSlotDiffRenderer::ENGINE_WIKIDIFF2_INLINE );
|
|
$inlineKey = $slotDiffRenderer->getExtraCacheKeys();
|
|
$this->assertSame( $key, [] );
|
|
$this->assertSame( $inlineKey, [ phpversion( 'wikidiff2' ), 'inline' ] );
|
|
}
|
|
|
|
/**
|
|
* @dataProvider provideGetDiff
|
|
* @param array|null $oldContentArgs To pass to makeContent() (if not null)
|
|
* @param array|null $newContentArgs
|
|
* @param string|Exception $expectedResult
|
|
* @throws Exception
|
|
*/
|
|
public function testGetDiff(
|
|
?array $oldContentArgs, ?array $newContentArgs, $expectedResult
|
|
) {
|
|
$this->mergeMwGlobalArrayValue( 'wgContentHandlers', [
|
|
'testing' => DummyContentHandlerForTesting::class,
|
|
'testing-nontext' => DummyNonTextContentHandler::class,
|
|
] );
|
|
|
|
$oldContent = $oldContentArgs ? self::makeContent( ...$oldContentArgs ) : null;
|
|
$newContent = $newContentArgs ? self::makeContent( ...$newContentArgs ) : null;
|
|
|
|
if ( $expectedResult instanceof Exception ) {
|
|
$this->expectException( get_class( $expectedResult ) );
|
|
$this->expectExceptionMessage( $expectedResult->getMessage() );
|
|
}
|
|
|
|
$slotDiffRenderer = $this->getTextSlotDiffRenderer();
|
|
$diff = $slotDiffRenderer->getDiff( $oldContent, $newContent );
|
|
if ( $expectedResult instanceof Exception ) {
|
|
return;
|
|
}
|
|
$plainDiff = $this->getPlainDiff( $diff );
|
|
$this->assertSame( $expectedResult, $plainDiff );
|
|
}
|
|
|
|
public static function provideGetDiff() {
|
|
return [
|
|
'same text' => [
|
|
[ "aaa\nbbb\nccc" ],
|
|
[ "aaa\nbbb\nccc" ],
|
|
"",
|
|
],
|
|
'different text' => [
|
|
[ "aaa\nbbb\nccc" ],
|
|
[ "aaa\nxxx\nccc" ],
|
|
" aaa aaa\n-bbb+xxx\n ccc ccc",
|
|
],
|
|
'no right content' => [
|
|
[ "aaa\nbbb\nccc" ],
|
|
null,
|
|
"-aaa+ \n-bbb \n-ccc ",
|
|
],
|
|
'no left content' => [
|
|
null,
|
|
[ "aaa\nbbb\nccc" ],
|
|
"- +aaa\n +bbb\n +ccc",
|
|
],
|
|
'no content' => [
|
|
null,
|
|
null,
|
|
new InvalidArgumentException( '$oldContent and $newContent cannot both be null' ),
|
|
],
|
|
'non-text left content' => [
|
|
[ '', 'testing-nontext' ],
|
|
[ "aaa\nbbb\nccc" ],
|
|
new ParameterTypeException( '$oldContent', 'TextContent|null' ),
|
|
],
|
|
'non-text right content' => [
|
|
[ "aaa\nbbb\nccc" ],
|
|
[ '', 'testing-nontext' ],
|
|
new ParameterTypeException( '$newContent', 'TextContent|null' ),
|
|
],
|
|
];
|
|
}
|
|
|
|
// no separate test for getTextDiff() as getDiff() is just a thin wrapper around it
|
|
|
|
/**
|
|
* @return TextSlotDiffRenderer
|
|
*/
|
|
private function getTextSlotDiffRenderer() {
|
|
$slotDiffRenderer = new TextSlotDiffRenderer();
|
|
$slotDiffRenderer->setStatsdDataFactory( new NullStatsdDataFactory() );
|
|
$slotDiffRenderer->setLanguage(
|
|
MediaWikiServices::getInstance()->getLanguageFactory()->getLanguage( 'en' ) );
|
|
$slotDiffRenderer->setEngine( TextSlotDiffRenderer::ENGINE_PHP );
|
|
return $slotDiffRenderer;
|
|
}
|
|
|
|
/**
|
|
* Convert a HTML diff to a human-readable format and hopefully make the test less fragile.
|
|
* @param string diff
|
|
* @return string
|
|
*/
|
|
private function getPlainDiff( $diff ) {
|
|
$replacements = [
|
|
html_entity_decode( ' ' ) => ' ',
|
|
html_entity_decode( '−' ) => '-',
|
|
];
|
|
return str_replace( array_keys( $replacements ), array_values( $replacements ),
|
|
trim( strip_tags( $diff ), "\n" ) );
|
|
}
|
|
|
|
/**
|
|
* @param string $str
|
|
* @param string $model
|
|
* @return null|TextContent
|
|
*/
|
|
private static function makeContent( $str, $model = CONTENT_MODEL_TEXT ) {
|
|
return ContentHandler::makeContent( $str, null, $model );
|
|
}
|
|
|
|
}
|