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

/**
 * @group Database
 * ^ See AggregateMessageGroup::getGroups -> MessageGroups::getPriority
 */
class MessageGroupsTest extends MediaWikiTestCase {
	protected function setUp() {
		parent::setUp();

		$conf = [
			__DIR__ . '/data/ParentGroups.yaml',
		];

		global $wgHooks;
		$this->setMwGlobals( [
			'wgHooks' => $wgHooks,
			'wgTranslateGroupFiles' => $conf,
			'wgTranslateTranslationServices' => [],
			'wgTranslateMessageNamespaces' => [ NS_MEDIAWIKI ],
		] );
		$wgHooks['TranslatePostInitGroups'] = [ 'MessageGroups::getConfiguredGroups' ];

		$mg = MessageGroups::singleton();
		$mg->setCache( new WANObjectCache( [ 'cache' => wfGetCache( 'hash' ) ] ) );
		$mg->recache();

		MessageIndex::setInstance( new HashMessageIndex() );
		MessageIndex::singleton()->rebuild();
	}

	/**
	 * @dataProvider provideGroups
	 */
	public function testGetParentGroups( $expected, $target ) {
		$group = MessageGroups::getGroup( $target );
		$got = MessageGroups::getParentGroups( $group );
		$this->assertEquals( $expected, $got );
	}

	public static function provideGroups() {
		$cases = [];
		$cases[] = [
			[ [ 'root1' ], [ 'root2' ] ],
			'twoparents'
		];

		$cases[] = [
			[ [ 'root3', 'sub1' ], [ 'root3', 'sub2' ] ],
			'oneparent-twopaths'
		];

		$cases[] = [
			[
				[ 'root4' ],
				[ 'root4', 'nested1' ],
				[ 'root4', 'nested1', 'nested2' ],
				[ 'root4', 'nested2' ],
			],
			'multilevelnested'
		];

		return $cases;
	}

	public function testHaveSingleSourceLanguage() {
		$this->setMwGlobals( [
			'wgTranslateGroupFiles' => [ __DIR__ . '/data/MixedSourceLanguageGroups.yaml' ],
		] );
		MessageGroups::singleton()->recache();

		$enGroup1 = MessageGroups::getGroup( 'EnglishGroup1' );
		$enGroup2 = MessageGroups::getGroup( 'EnglishGroup2' );
		$teGroup1 = MessageGroups::getGroup( 'TeluguGroup1' );

		$this->assertEquals( 'en', MessageGroups::haveSingleSourceLanguage(
			[ $enGroup1, $enGroup2 ] )
		);
		$this->assertEquals( '', MessageGroups::haveSingleSourceLanguage(
			[ $enGroup1, $enGroup2, $teGroup1 ] )
		);
	}
}