aboutsummaryrefslogtreecommitdiffstats
path: root/tests/phpunit/includes/diff/TextDiffer/ManifoldTextDifferTest.php
blob: d882f539c51dd11b2165711540dda502def88be5 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
<?php

use MediaWiki\Diff\TextDiffer\ManifoldTextDiffer;
use MediaWiki\Tests\Diff\TextDiffer\TextDifferData;

/**
 * @covers \MediaWiki\Diff\TextDiffer\ManifoldTextDiffer
 * @covers \MediaWiki\Diff\TextDiffer\BaseTextDiffer
 */
class ManifoldTextDifferTest extends MediaWikiIntegrationTestCase {
	private function createDiffer( $configVars = [] ) {
		$services = $this->getServiceContainer();
		return new ManifoldTextDiffer(
			RequestContext::getMain(),
			$services->getLanguageFactory()->getLanguage( 'en' ),
			$configVars['DiffEngine'] ?? null,
			$configVars['ExternalDiffEngine'] ?? null,
			$configVars['Wikidiff2Options'] ?? []
		);
	}

	public function testGetName() {
		$this->assertSame( 'manifold', $this->createDiffer()->getName() );
	}

	public function testGetFormats() {
		if ( extension_loaded( 'wikidiff2' ) ) {
			$formats = [ 'table', 'inline', 'unified' ];
		} else {
			$formats = [ 'table', 'unified' ];
		}
		$this->assertSame(
			$formats,
			$this->createDiffer()->getFormats()
		);
	}

	public function testHasFormat() {
		$differ = $this->createDiffer();
		$this->assertTrue( $differ->hasFormat( 'table' ) );
		if ( extension_loaded( 'wikidiff2' ) ) {
			$this->assertTrue( $differ->hasFormat( 'inline' ) );
		}
		$this->assertFalse( $differ->hasFormat( 'external' ) );
		$this->assertFalse( $differ->hasFormat( 'nonexistent' ) );
	}

	public function testHasFormatExternal() {
		if ( wfIsWindows() ) {
			$this->markTestSkipped( 'This test only works on non-Windows platforms' );
		}
		$differ = $this->createDiffer( [
			'ExternalDiffEngine' => __DIR__ . '/externalDiffTest.sh'
		] );
		$this->assertTrue( $differ->hasFormat( 'external' ) );
	}

	public function testRenderForcePhp() {
		$differ = $this->createDiffer( [
			'DiffEngine' => 'php'
		] );
		$result = $differ->render( 'foo', 'bar', 'table' );
		$this->assertSame(
			TextDifferData::PHP_TABLE,
			$result
		);
	}

	/**
	 * @requires extension wikidiff2
	 */
	public function testRenderUnforcedWikidiff2() {
		$differ = $this->createDiffer();
		$result = $differ->render( 'foo', 'bar', 'table' );
		$this->assertSame(
			TextDifferData::WIKIDIFF2_TABLE,
			$result
		);
	}

	/**
	 * @requires extension wikidiff2
	 */
	public function testRenderBatchWikidiff2External() {
		if ( !is_executable( '/bin/sh' ) ) {
			$this->markTestSkipped( 'ExternalTextDiffer can\'t pass extra ' .
				'arguments like $wgPhpCli, so it\'s hard to be platform-independent' );
		}
		$differ = $this->createDiffer( [
			'ExternalDiffEngine' => __DIR__ . '/externalDiffTest.sh'
		] );
		$result = $differ->renderBatch( 'foo', 'bar',
			[ 'table', 'inline', 'external', 'unified' ] );
		$this->assertSame(
			[
				'table' => TextDifferData::WIKIDIFF2_TABLE,
				'inline' => TextDifferData::WIKIDIFF2_INLINE,
				'external' => TextDifferData::EXTERNAL,
				'unified' => TextDifferData::PHP_UNIFIED
			],
			$result
		);
	}

	public static function provideAddRowWrapper() {
		return [
			[ 'table', false ],
			[ 'external', false ],
			[ 'unified', true ]
		];
	}

	/**
	 * @dataProvider provideAddRowWrapper
	 * @param string $format
	 * @param bool $isWrap
	 */
	public function testAddRowWrapper( $format, $isWrap ) {
		if ( wfIsWindows() ) {
			$this->markTestSkipped( 'This test only works on non-Windows platforms' );
		}
		$differ = $this->createDiffer( [
			'ExternalDiffEngine' => __DIR__ . '/externalDiffTest.sh'
		] );
		$result = $differ->addRowWrapper( $format, 'foo' );
		if ( $isWrap ) {
			$this->assertSame( '<tr><td colspan="4"><pre>foo</pre></td></tr>', $result );
		} else {
			$this->assertSame( 'foo', $result );
		}
	}
}