summaryrefslogtreecommitdiff
path: root/www/wiki/extensions/LocalisationUpdate/tests/phpunit/UpdaterTest.php
blob: 00c45e12336d8c7a0fa6522927d56b444731ca0c (plain)
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
<?php
/**
 * @file
 * @author Niklas Laxström
 * @license GPL-2.0-or-later
 */

namespace LocalisationUpdate;

use PHPUnit4And6Compat;

/**
 * @covers \LocalisationUpdate\Updater
 */
class UpdaterTest extends \PHPUnit\Framework\TestCase {
	use PHPUnit4And6Compat;

	public function testIsDirectory() {
		$updater = new Updater();

		$this->assertTrue(
			$updater->isDirectory( '/IP/extensions/Translate/i18n/*.json' ),
			'Extension json files are a file pattern'
		);

		$this->assertFalse(
			$updater->isDirectory( '/IP/extensions/Translate/Translate.i18n.php' ),
			'Extension php file is not a pattern'
		);
	}

	public function testExpandRemotePath() {
		$updater = new Updater();
		$repos = [ 'main' => 'file:///repos/%NAME%/%SOME-VAR%' ];

		$info = [
			'repo' => 'main',
			'name' => 'product',
			'some-var' => 'file',
		];
		$this->assertEquals(
			'file:///repos/product/file',
			$updater->expandRemotePath( $info, $repos ),
			'Variables are expanded correctly'
		);
	}

	public function testReadMessages() {
		$updater = $updater = new Updater();

		$input = [ 'file' => 'Hello World!' ];
		$output = [ 'en' => [ 'key' => $input['file'] ] ];

		$reader = $this->getMock( 'LocalisationUpdate\Reader' );
		$reader
			->expects( $this->once() )
			->method( 'parse' )
			->will( $this->returnValue( $output ) );

		$factory = $this->getMock( 'LocalisationUpdate\ReaderFactory' );
		$factory
			->expects( $this->once() )
			->method( 'getReader' )
			->will( $this->returnValue( $reader ) );

		$observed = $updater->readMessages( $factory, $input );
		$this->assertEquals( $output, $observed, 'Tries to parse given file' );
	}

	public function testFindChangedTranslations() {
		$updater = $updater = new Updater();

		$origin = [
			'A' => '1',
			'C' => '3',
			'D' => '4',
		];
		$remote = [
			'A' => '1', // No change key
			'B' => '2', // New key
			'C' => '33', // Changed key
			'D' => '44', // Blacklisted key
		];
		$blacklist = [ 'D' => 0 ];
		$expected = [ 'B' => '2', 'C' => '33' ];
		$observed = $updater->findChangedTranslations( $origin, $remote, $blacklist );
		$this->assertEquals( $expected, $observed, 'Changed and new keys returned' );
	}
}