summaryrefslogtreecommitdiffstats
path: root/tests/lib/l10n/factorytest.php
blob: f632e48e2de3433a99085a98457f084299a75ae8 (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
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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
<?php
/**
 * Copyright (c) 2016 Joas Schilling <nickvergessen@owncloud.com>
 * This file is licensed under the Affero General Public License version 3 or
 * later.
 * See the COPYING-README file.
 */

namespace Test\L10N;


use OC\L10N\Factory;
use Test\TestCase;

/**
 * Class FactoryTest
 *
 * @package Test\L10N
 * @group DB
 */
class FactoryTest extends TestCase {

	/** @var \OCP\IConfig|\PHPUnit_Framework_MockObject_MockObject */
	protected $config;

	/** @var \OCP\IRequest|\PHPUnit_Framework_MockObject_MockObject */
	protected $request;

	public function setUp() {
		parent::setUp();

		/** @var \OCP\IConfig $request */
		$this->config = $this->getMockBuilder('OCP\IConfig')
			->disableOriginalConstructor()
			->getMock();

		/** @var \OCP\IRequest $request */
		$this->request = $this->getMockBuilder('OCP\IRequest')
			->disableOriginalConstructor()
			->getMock();
	}

	/**
	 * @param array $methods
	 * @return Factory|\PHPUnit_Framework_MockObject_MockObject
	 */
	protected function getFactory(array $methods = []) {
		if (!empty($methods)) {
			return $this->getMockBuilder('OC\L10N\Factory')
				->setConstructorArgs([
					$this->config,
					$this->request,
				])
				->setMethods($methods)
				->getMock();
		} else {
			return new Factory($this->config, $this->request);
		}
	}

	public function dataFindLanguage() {
		return [
			[null, false, 1, 'de', true, null, null, null, null, null, 'de'],
			[null, 'test', 2, 'de', false, 'ru', true, null, null, null, 'ru'],
			[null, 'test', 1, '', null, 'ru', true, null, null, null, 'ru'],
			[null, 'test', 3, 'de', false, 'ru', false, 'cz', true, null, 'cz'],
			[null, 'test', 2, '', null, 'ru', false, 'cz', true, null, 'cz'],
			[null, 'test', 1, '', null, '', null, 'cz', true, null, 'cz'],
			[null, 'test', 3, 'de', false, 'ru', false, 'cz', false, 'ar', 'ar'],
			[null, 'test', 2, '', null, 'ru', false, 'cz', false, 'ar', 'ar'],
			[null, 'test', 1, '', null, '', null, 'cz', false, 'ar', 'ar'],
			[null, 'test', 0, '', null, '', null, false, null, 'ar', 'ar'],
		];
	}

	/**
	 * @dataProvider dataFindLanguage
	 *
	 * @param string|null $app
	 * @param string|null $user
	 * @param int $existsCalls
	 * @param string $storedRequestLang
	 * @param bool $srlExists
	 * @param string|null $userLang
	 * @param bool $ulExists
	 * @param string|false $defaultLang
	 * @param bool $dlExists
	 * @param string|null $requestLang
	 * @param string $expected
	 */
	public function testFindLanguage($app, $user, $existsCalls, $storedRequestLang, $srlExists, $userLang, $ulExists, $defaultLang, $dlExists, $requestLang, $expected) {
		$factory = $this->getFactory([
			'languageExists',
			'setLanguageFromRequest',
		]);

		$session = $this->getMockBuilder('OCP\ISession')
			->disableOriginalConstructor()
			->getMock();
		$session->expects($this->any())
			->method('get')
			->with('user_id')
			->willReturn($user);
		$userSession = $this->getMockBuilder('OC\User\Session')
			->disableOriginalConstructor()
			->getMock();
		$userSession->expects($this->any())
			->method('getSession')
			->willReturn($session);

		$this->invokePrivate($factory, 'requestLanguage', [$storedRequestLang]);

		$factory->expects($this->exactly($existsCalls))
			->method('languageExists')
			->willReturnMap([
				[$app, $storedRequestLang, $srlExists],
				[$app, $userLang, $ulExists],
				[$app, $defaultLang, $dlExists],
			]);

		$factory->expects($requestLang !== null ? $this->once() : $this->never())
			->method('setLanguageFromRequest')
			->willReturn($requestLang);

		$this->config->expects($userLang !== null ? $this->any() : $this->never())
			->method('getUserValue')
			->with($this->anything(), 'core', 'lang')
			->willReturn($userLang);

		$this->config->expects($defaultLang !== null ? $this->once() : $this->never())
			->method('getSystemValue')
			->with('default_language', false)
			->willReturn($defaultLang);

		$this->overwriteService('UserSession', $userSession);
		$this->assertSame($expected, $factory->findLanguage($app));
		$this->restoreService('UserSession');
	}

	public function dataFindAvailableLanguages() {
		return [
			[null],
			['files'],
		];
	}

	/**
	 * @dataProvider dataFindAvailableLanguages
	 *
	 * @param string|null $app
	 */
	public function testFindAvailableLanguages($app) {
		$factory = $this->getFactory(['findL10nDir']);
		$factory->expects($this->once())
			->method('findL10nDir')
			->with($app)
			->willReturn(\OC::$SERVERROOT . '/tests/data/l10n/');

		$this->assertEquals(['cs', 'de', 'en', 'ru'], $factory->findAvailableLanguages($app), '', 0.0, 10, true);
	}

	public function dataLanguageExists() {
		return [
			[null, 'en', [], true],
			[null, 'de', [], false],
			[null, 'de', ['ru'], false],
			[null, 'de', ['ru', 'de'], true],
			['files', 'en', [], true],
			['files', 'de', [], false],
			['files', 'de', ['ru'], false],
			['files', 'de', ['de', 'ru'], true],
		];
	}

	/**
	 * @dataProvider dataLanguageExists
	 *
	 * @param string|null $app
	 * @param string $lang
	 * @param string[] $availableLanguages
	 * @param string $expected
	 */
	public function testLanguageExists($app, $lang, array $availableLanguages, $expected) {
		$factory = $this->getFactory(['findAvailableLanguages']);
		$factory->expects(($lang === 'en') ? $this->never() : $this->once())
			->method('findAvailableLanguages')
			->with($app)
			->willReturn($availableLanguages);

		$this->assertSame($expected, $factory->languageExists($app, $lang));
	}

	public function dataSetLanguageFromRequest() {
		return [
			// Language is available
			[null, 'de', null, ['de'], 'de', 'de'],
			[null, 'de,en', null, ['de'], 'de', 'de'],
			[null, 'de-DE,en-US;q=0.8,en;q=0.6', null, ['de'], 'de', 'de'],
			// Language is not available
			[null, 'de', null, ['ru'], 'en', 'en'],
			[null, 'de,en', null, ['ru', 'en'], 'en', 'en'],
			[null, 'de-DE,en-US;q=0.8,en;q=0.6', null, ['ru', 'en'], 'en', 'en'],
			// Language is available, but request language is set
			[null, 'de', 'ru', ['de'], 'de', 'ru'],
			[null, 'de,en', 'ru', ['de'], 'de', 'ru'],
			[null, 'de-DE,en-US;q=0.8,en;q=0.6', 'ru', ['de'], 'de', 'ru'],
		];
	}

	/**
	 * @dataProvider dataSetLanguageFromRequest
	 *
	 * @param string|null $app
	 * @param string $header
	 * @param string|null $requestLanguage
	 * @param string[] $availableLanguages
	 * @param string $expected
	 * @param string $expectedLang
	 */
	public function testSetLanguageFromRequest($app, $header, $requestLanguage, array $availableLanguages, $expected, $expectedLang) {
		$factory = $this->getFactory(['findAvailableLanguages']);
		$factory->expects($this->once())
			->method('findAvailableLanguages')
			->with($app)
			->willReturn($availableLanguages);

		$this->request->expects($this->once())
			->method('getHeader')
			->with('ACCEPT_LANGUAGE')
			->willReturn($header);

		if ($requestLanguage !== null) {
			$this->invokePrivate($factory, 'requestLanguage', [$requestLanguage]);
		}
		$this->assertSame($expected, $factory->setLanguageFromRequest($app), 'Asserting returned language');
		$this->assertSame($expectedLang, $this->invokePrivate($factory, 'requestLanguage'), 'Asserting stored language');
	}

	public function dataGetL10nFilesForApp() {
		return [
			[null, 'de', [\OC::$SERVERROOT . '/core/l10n/de.json']],
			['core', 'ru', [\OC::$SERVERROOT . '/core/l10n/ru.json']],
			['lib', 'ru', [\OC::$SERVERROOT . '/lib/l10n/ru.json']],
			['settings', 'de', [\OC::$SERVERROOT . '/settings/l10n/de.json']],
			['files', 'de', [\OC::$SERVERROOT . '/apps/files/l10n/de.json']],
			['files', '_lang_never_exists_', []],
			['_app_never_exists_', 'de', [\OC::$SERVERROOT . '/core/l10n/de.json']],
		];
	}

	/**
	 * @dataProvider dataGetL10nFilesForApp
	 *
	 * @param string|null $app
	 * @param string $expected
	 */
	public function testGetL10nFilesForApp($app, $lang, $expected) {
		$factory = $this->getFactory();
		$this->assertSame($expected, $this->invokePrivate($factory, 'getL10nFilesForApp', [$app, $lang]));
	}

	public function dataFindL10NDir() {
		return [
			[null, \OC::$SERVERROOT . '/core/l10n/'],
			['core', \OC::$SERVERROOT . '/core/l10n/'],
			['lib', \OC::$SERVERROOT . '/lib/l10n/'],
			['settings', \OC::$SERVERROOT . '/settings/l10n/'],
			['files', \OC::$SERVERROOT . '/apps/files/l10n/'],
			['_app_never_exists_', \OC::$SERVERROOT . '/core/l10n/'],
		];
	}

	/**
	 * @dataProvider dataFindL10NDir
	 *
	 * @param string|null $app
	 * @param string $expected
	 */
	public function testFindL10NDir($app, $expected) {
		$factory = $this->getFactory();
		$this->assertSame($expected, $this->invokePrivate($factory, 'findL10nDir', [$app]));
	}

	public function dataCreatePluralFunction() {
		return [
			['nplurals=2; plural=(n != 1);', 0, 1],
			['nplurals=2; plural=(n != 1);', 1, 0],
			['nplurals=2; plural=(n != 1);', 2, 1],
			['nplurals=3; plural=(n==1) ? 0 : (n>=2 && n<=4) ? 1 : 2;', 0, 2],
			['nplurals=3; plural=(n==1) ? 0 : (n>=2 && n<=4) ? 1 : 2;', 1, 0],
			['nplurals=3; plural=(n==1) ? 0 : (n>=2 && n<=4) ? 1 : 2;', 2, 1],
			['nplurals=3; plural=(n==1) ? 0 : (n>=2 && n<=4) ? 1 : 2;', 3, 1],
			['nplurals=3; plural=(n==1) ? 0 : (n>=2 && n<=4) ? 1 : 2;', 4, 1],
			['nplurals=3; plural=(n==1) ? 0 : (n>=2 && n<=4) ? 1 : 2;', 5, 2],
		];
	}

	/**
	 * @dataProvider dataCreatePluralFunction
	 *
	 * @param string $function
	 * @param int $count
	 * @param int $expected
	 */
	public function testCreatePluralFunction($function, $count, $expected) {
		$factory = $this->getFactory();
		$fn = $factory->createPluralFunction($function);
		$this->assertEquals($expected, $fn($count));
	}
}