2023-07-03 08:27:47 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
use MediaWiki\Diff\TextDiffer\PhpTextDiffer;
|
|
|
|
|
use MediaWiki\Tests\Diff\TextDiffer\TextDifferData;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @covers \MediaWiki\Diff\TextDiffer\PhpTextDiffer
|
|
|
|
|
* @covers \MediaWiki\Diff\TextDiffer\BaseTextDiffer
|
|
|
|
|
*/
|
|
|
|
|
class PhpTextDifferTest extends MediaWikiIntegrationTestCase {
|
|
|
|
|
private function createDiffer() {
|
|
|
|
|
$lang = $this->getServiceContainer()
|
|
|
|
|
->getLanguageFactory()
|
|
|
|
|
->getLanguage( 'en' );
|
|
|
|
|
$differ = new PhpTextDiffer( $lang );
|
|
|
|
|
|
|
|
|
|
$localizer = RequestContext::getMain();
|
|
|
|
|
$localizer->setLanguage( $lang );
|
|
|
|
|
|
|
|
|
|
$differ->setLocalizer( $localizer );
|
|
|
|
|
return $differ;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function testRender() {
|
|
|
|
|
$differ = $this->createDiffer();
|
|
|
|
|
$result = $differ->render( 'foo', 'bar', 'table' );
|
|
|
|
|
$this->assertSame( TextDifferData::PHP_TABLE, $result );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static function provideRenderBatch() {
|
|
|
|
|
return [
|
|
|
|
|
'empty' => [
|
|
|
|
|
[],
|
|
|
|
|
[]
|
|
|
|
|
],
|
|
|
|
|
'one format' => [
|
|
|
|
|
[ 'table' ],
|
|
|
|
|
[
|
|
|
|
|
'table' => TextDifferData::PHP_TABLE,
|
|
|
|
|
]
|
|
|
|
|
],
|
2023-07-06 05:38:16 +00:00
|
|
|
'multiple formats' => [
|
|
|
|
|
[ 'table', 'unified' ],
|
|
|
|
|
[
|
|
|
|
|
'table' => TextDifferData::PHP_TABLE,
|
|
|
|
|
'unified' => TextDifferData::PHP_UNIFIED,
|
|
|
|
|
]
|
|
|
|
|
],
|
2023-07-03 08:27:47 +00:00
|
|
|
];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @dataProvider provideRenderBatch
|
|
|
|
|
* @param array $formats
|
|
|
|
|
* @param array $expected
|
|
|
|
|
*/
|
|
|
|
|
public function testRenderBatch( $formats, $expected ) {
|
|
|
|
|
$oldText = 'foo';
|
|
|
|
|
$newText = 'bar';
|
|
|
|
|
$differ = $this->createDiffer();
|
|
|
|
|
$result = $differ->renderBatch( $oldText, $newText, $formats );
|
|
|
|
|
$this->assertSame( $expected, $result );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function testHasFormat() {
|
|
|
|
|
$differ = $this->createDiffer();
|
|
|
|
|
$this->assertTrue( $differ->hasFormat( 'table' ) );
|
|
|
|
|
$this->assertFalse( $differ->hasFormat( 'external' ) );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function testAddModules() {
|
|
|
|
|
$out = RequestContext::getMain()->getOutput();
|
|
|
|
|
$differ = $this->createDiffer();
|
|
|
|
|
$differ->addModules( $out, 'table' );
|
|
|
|
|
$this->assertSame( [], $out->getModules() );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function testGetCacheKeys() {
|
|
|
|
|
$differ = $this->createDiffer();
|
|
|
|
|
$result = $differ->getCacheKeys( [ 'table' ] );
|
|
|
|
|
$this->assertSame( [], $result );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static function provideLocalize() {
|
|
|
|
|
return [
|
|
|
|
|
[ 1, [], 'Line 1:' ],
|
|
|
|
|
[ 2, [], 'Line 2:' ],
|
|
|
|
|
[ 1, [ 'reducedLineNumbers' => true ], '' ],
|
2023-10-02 23:20:49 +00:00
|
|
|
[ [ 3, 5 ], [ 'diff-type' => 'inline' ], 'Line 3 ⟶ 5:' ],
|
|
|
|
|
[ [ 1, 5 ], [ 'diff-type' => 'inline', 'reducedLineNumbers' => true ], 'Line 1 ⟶ 5:' ],
|
|
|
|
|
[ [ 1, 1 ], [ 'diff-type' => 'inline', 'reducedLineNumbers' => true ], '' ]
|
2023-07-03 08:27:47 +00:00
|
|
|
];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @dataProvider provideLocalize
|
2023-10-02 23:20:49 +00:00
|
|
|
* @param int|int[] $line
|
2023-07-03 08:27:47 +00:00
|
|
|
* @param array $options
|
|
|
|
|
* @param string $expected
|
|
|
|
|
*/
|
|
|
|
|
public function testLocalize( $line, $options, $expected ) {
|
2023-10-02 23:20:49 +00:00
|
|
|
$content = is_array( $line )
|
|
|
|
|
? "<!-- LINES $line[0],$line[1] -->"
|
|
|
|
|
: "<!--LINE $line-->";
|
2023-07-03 08:27:47 +00:00
|
|
|
$differ = $this->createDiffer();
|
|
|
|
|
$result = $differ->localize(
|
|
|
|
|
'table',
|
2023-10-02 23:20:49 +00:00
|
|
|
$content,
|
2023-07-03 08:27:47 +00:00
|
|
|
$options
|
|
|
|
|
);
|
|
|
|
|
$this->assertSame( $expected, $result );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function testGetTablePrefixes() {
|
|
|
|
|
$this->assertSame( [], $this->createDiffer()->getTablePrefixes( 'table' ) );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function testGetPreferredFormatBatch() {
|
|
|
|
|
$this->assertSame(
|
|
|
|
|
[ 'table' ],
|
|
|
|
|
$this->createDiffer()->getPreferredFormatBatch( 'table' )
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
}
|