aboutsummaryrefslogtreecommitdiffstats
path: root/tests/phpunit/includes/diff/TextDiffer/PhpTextDifferTest.php
blob: e8009cb12b3ad3595f91123ca654a6f2036d8281 (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
<?php

use MediaWiki\Context\RequestContext;
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,
				]
			],
			'multiple formats' => [
				[ 'table', 'unified' ],
				[
					'table' => TextDifferData::PHP_TABLE,
					'unified' => TextDifferData::PHP_UNIFIED,
				]
			],
		];
	}

	/**
	 * @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 ], '' ],
			[ [ 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 ], '' ]
		];
	}

	/**
	 * @dataProvider provideLocalize
	 * @param int|int[] $line
	 * @param array $options
	 * @param string $expected
	 */
	public function testLocalize( $line, $options, $expected ) {
		$content = is_array( $line )
			? "<!-- LINES $line[0],$line[1] -->"
			: "<!--LINE $line-->";
		$differ = $this->createDiffer();
		$result = $differ->localize(
			'table',
			$content,
			$options
		);
		$this->assertSame( $expected, $result );
	}

	public function testGetTablePrefixes() {
		$this->assertSame( [], $this->createDiffer()->getTablePrefixes( 'table' ) );
	}

	public function testGetPreferredFormatBatch() {
		$this->assertSame(
			[ 'table' ],
			$this->createDiffer()->getPreferredFormatBatch( 'table' )
		);
	}
}