| 1 |
<?php |
| 2 |
|
| 3 |
use MediaWiki\Context\RequestContext; |
| 4 |
use MediaWiki\Diff\TextDiffer\ManifoldTextDiffer; |
| 5 |
use MediaWiki\Tests\Diff\TextDiffer\TextDifferData; |
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
class ManifoldTextDifferTest extends MediaWikiIntegrationTestCase { |
| 12 |
private function createDiffer( $configVars = [] ) { |
| 13 |
$services = $this->getServiceContainer(); |
| 14 |
return new ManifoldTextDiffer( |
| 15 |
RequestContext::getMain(), |
| 16 |
$services->getLanguageFactory()->getLanguage( 'en' ), |
| 17 |
$configVars['DiffEngine'] ?? null, |
| 18 |
$configVars['ExternalDiffEngine'] ?? null, |
| 19 |
$configVars['Wikidiff2Options'] ?? [] |
| 20 |
); |
| 21 |
} |
| 22 |
|
| 23 |
public function testGetName() { |
| 24 |
$this->assertSame( 'manifold', $this->createDiffer()->getName() ); |
| 25 |
} |
| 26 |
|
| 27 |
public function testGetFormats() { |
| 28 |
if ( extension_loaded( 'wikidiff2' ) ) { |
| 29 |
$formats = [ 'table', 'inline', 'unified' ]; |
| 30 |
} else { |
| 31 |
$formats = [ 'table', 'unified' ]; |
| 32 |
} |
| 33 |
$this->assertSame( |
| 34 |
$formats, |
| 35 |
$this->createDiffer()->getFormats() |
| 36 |
); |
| 37 |
} |
| 38 |
|
| 39 |
public function testHasFormat() { |
| 40 |
$differ = $this->createDiffer(); |
| 41 |
$this->assertTrue( $differ->hasFormat( 'table' ) ); |
| 42 |
if ( extension_loaded( 'wikidiff2' ) ) { |
| 43 |
$this->assertTrue( $differ->hasFormat( 'inline' ) ); |
| 44 |
} |
| 45 |
$this->assertFalse( $differ->hasFormat( 'external' ) ); |
| 46 |
$this->assertFalse( $differ->hasFormat( 'nonexistent' ) ); |
| 47 |
} |
| 48 |
|
| 49 |
public function testHasFormatExternal() { |
| 50 |
if ( wfIsWindows() ) { |
| 51 |
$this->markTestSkipped( 'This test only works on non-Windows platforms' ); |
| 52 |
} |
| 53 |
$differ = $this->createDiffer( [ |
| 54 |
'ExternalDiffEngine' => __DIR__ . '/externalDiffTest.sh' |
| 55 |
] ); |
| 56 |
$this->assertTrue( $differ->hasFormat( 'external' ) ); |
| 57 |
} |
| 58 |
|
| 59 |
public function testRenderForcePhp() { |
| 60 |
$differ = $this->createDiffer( [ |
| 61 |
'DiffEngine' => 'php' |
| 62 |
] ); |
| 63 |
$result = $differ->render( 'foo', 'bar', 'table' ); |
| 64 |
$this->assertSame( |
| 65 |
TextDifferData::PHP_TABLE, |
| 66 |
$result |
| 67 |
); |
| 68 |
} |
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
public function testRenderUnforcedWikidiff2() { |
| 74 |
$differ = $this->createDiffer(); |
| 75 |
$result = $differ->render( 'foo', 'bar', 'table' ); |
| 76 |
$this->assertSame( |
| 77 |
TextDifferData::WIKIDIFF2_TABLE, |
| 78 |
$result |
| 79 |
); |
| 80 |
} |
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
public function testRenderBatchWikidiff2External() { |
| 86 |
if ( !is_executable( '/bin/sh' ) ) { |
| 87 |
$this->markTestSkipped( 'ExternalTextDiffer can\'t pass extra ' . |
| 88 |
'arguments like $wgPhpCli, so it\'s hard to be platform-independent' ); |
| 89 |
} |
| 90 |
$differ = $this->createDiffer( [ |
| 91 |
'ExternalDiffEngine' => __DIR__ . '/externalDiffTest.sh' |
| 92 |
] ); |
| 93 |
$result = $differ->renderBatch( 'foo', 'bar', |
| 94 |
[ 'table', 'inline', 'external', 'unified' ] ); |
| 95 |
$this->assertSame( |
| 96 |
[ |
| 97 |
'table' => TextDifferData::WIKIDIFF2_TABLE, |
| 98 |
'inline' => TextDifferData::WIKIDIFF2_INLINE, |
| 99 |
'external' => TextDifferData::EXTERNAL, |
| 100 |
'unified' => TextDifferData::PHP_UNIFIED |
| 101 |
], |
| 102 |
$result |
| 103 |
); |
| 104 |
} |
| 105 |
|
| 106 |
public static function provideAddRowWrapper() { |
| 107 |
return [ |
| 108 |
[ 'table', false ], |
| 109 |
[ 'external', false ], |
| 110 |
[ 'unified', true ] |
| 111 |
]; |
| 112 |
} |
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
public function testAddRowWrapper( $format, $isWrap ) { |
| 120 |
if ( wfIsWindows() ) { |
| 121 |
$this->markTestSkipped( 'This test only works on non-Windows platforms' ); |
| 122 |
} |
| 123 |
$differ = $this->createDiffer( [ |
| 124 |
'ExternalDiffEngine' => __DIR__ . '/externalDiffTest.sh' |
| 125 |
] ); |
| 126 |
$result = $differ->addRowWrapper( $format, 'foo' ); |
| 127 |
if ( $isWrap ) { |
| 128 |
$this->assertSame( '<tr><td colspan="4"><pre>foo</pre></td></tr>', $result ); |
| 129 |
} else { |
| 130 |
$this->assertSame( 'foo', $result ); |
| 131 |
} |
| 132 |
} |
| 133 |
} |