| 1 |
<?php |
| 2 |
|
| 3 |
use MediaWiki\Context\RequestContext; |
| 4 |
use MediaWiki\Diff\TextDiffer\PhpTextDiffer; |
| 5 |
use MediaWiki\Tests\Diff\TextDiffer\TextDifferData; |
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
class PhpTextDifferTest extends MediaWikiIntegrationTestCase { |
| 12 |
private function createDiffer() { |
| 13 |
$lang = $this->getServiceContainer() |
| 14 |
->getLanguageFactory() |
| 15 |
->getLanguage( 'en' ); |
| 16 |
$differ = new PhpTextDiffer( $lang ); |
| 17 |
|
| 18 |
$localizer = RequestContext::getMain(); |
| 19 |
$localizer->setLanguage( $lang ); |
| 20 |
|
| 21 |
$differ->setLocalizer( $localizer ); |
| 22 |
return $differ; |
| 23 |
} |
| 24 |
|
| 25 |
public function testRender() { |
| 26 |
$differ = $this->createDiffer(); |
| 27 |
$result = $differ->render( 'foo', 'bar', 'table' ); |
| 28 |
$this->assertSame( TextDifferData::PHP_TABLE, $result ); |
| 29 |
} |
| 30 |
|
| 31 |
public static function provideRenderBatch() { |
| 32 |
return [ |
| 33 |
'empty' => [ |
| 34 |
[], |
| 35 |
[] |
| 36 |
], |
| 37 |
'one format' => [ |
| 38 |
[ 'table' ], |
| 39 |
[ |
| 40 |
'table' => TextDifferData::PHP_TABLE, |
| 41 |
] |
| 42 |
], |
| 43 |
'multiple formats' => [ |
| 44 |
[ 'table', 'unified' ], |
| 45 |
[ |
| 46 |
'table' => TextDifferData::PHP_TABLE, |
| 47 |
'unified' => TextDifferData::PHP_UNIFIED, |
| 48 |
] |
| 49 |
], |
| 50 |
]; |
| 51 |
} |
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
public function testRenderBatch( $formats, $expected ) { |
| 59 |
$oldText = 'foo'; |
| 60 |
$newText = 'bar'; |
| 61 |
$differ = $this->createDiffer(); |
| 62 |
$result = $differ->renderBatch( $oldText, $newText, $formats ); |
| 63 |
$this->assertSame( $expected, $result ); |
| 64 |
} |
| 65 |
|
| 66 |
public function testHasFormat() { |
| 67 |
$differ = $this->createDiffer(); |
| 68 |
$this->assertTrue( $differ->hasFormat( 'table' ) ); |
| 69 |
$this->assertFalse( $differ->hasFormat( 'external' ) ); |
| 70 |
} |
| 71 |
|
| 72 |
public function testAddModules() { |
| 73 |
$out = RequestContext::getMain()->getOutput(); |
| 74 |
$differ = $this->createDiffer(); |
| 75 |
$differ->addModules( $out, 'table' ); |
| 76 |
$this->assertSame( [], $out->getModules() ); |
| 77 |
} |
| 78 |
|
| 79 |
public function testGetCacheKeys() { |
| 80 |
$differ = $this->createDiffer(); |
| 81 |
$result = $differ->getCacheKeys( [ 'table' ] ); |
| 82 |
$this->assertSame( [], $result ); |
| 83 |
} |
| 84 |
|
| 85 |
public static function provideLocalize() { |
| 86 |
return [ |
| 87 |
[ 1, [], 'Line 1:' ], |
| 88 |
[ 2, [], 'Line 2:' ], |
| 89 |
[ 1, [ 'reducedLineNumbers' => true ], '' ], |
| 90 |
[ [ 3, 5 ], [ 'diff-type' => 'inline' ], 'Line 3 ⟶ 5:' ], |
| 91 |
[ [ 1, 5 ], [ 'diff-type' => 'inline', 'reducedLineNumbers' => true ], 'Line 1 ⟶ 5:' ], |
| 92 |
[ [ 1, 1 ], [ 'diff-type' => 'inline', 'reducedLineNumbers' => true ], '' ] |
| 93 |
]; |
| 94 |
} |
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
public function testLocalize( $line, $options, $expected ) { |
| 103 |
$content = is_array( $line ) |
| 104 |
? "<!-- LINES $line[0],$line[1] -->" |
| 105 |
: "<!--LINE $line-->"; |
| 106 |
$differ = $this->createDiffer(); |
| 107 |
$result = $differ->localize( |
| 108 |
'table', |
| 109 |
$content, |
| 110 |
$options |
| 111 |
); |
| 112 |
$this->assertSame( $expected, $result ); |
| 113 |
} |
| 114 |
|
| 115 |
public function testGetTablePrefixes() { |
| 116 |
$this->assertSame( [], $this->createDiffer()->getTablePrefixes( 'table' ) ); |
| 117 |
} |
| 118 |
|
| 119 |
public function testGetPreferredFormatBatch() { |
| 120 |
$this->assertSame( |
| 121 |
[ 'table' ], |
| 122 |
$this->createDiffer()->getPreferredFormatBatch( 'table' ) |
| 123 |
); |
| 124 |
} |
| 125 |
} |