diff options
author | Joas Schilling <nickvergessen@gmx.de> | 2016-05-20 15:38:20 +0200 |
---|---|---|
committer | Thomas Müller <DeepDiver1975@users.noreply.github.com> | 2016-05-20 15:38:20 +0200 |
commit | 94ad54ec9b96d41a614fbbad4a97b34c41a6901f (patch) | |
tree | f3eb7cdda2704aaf0cd59d58efe66bcbd34cb67d /tests/lib/L10N | |
parent | 2ef751b1ec28f7b5c7113af60ec8c9fa0ae1cf87 (diff) | |
download | nextcloud-server-94ad54ec9b96d41a614fbbad4a97b34c41a6901f.tar.gz nextcloud-server-94ad54ec9b96d41a614fbbad4a97b34c41a6901f.zip |
Move tests/ to PSR-4 (#24731)
* Move a-b to PSR-4
* Move c-d to PSR-4
* Move e+g to PSR-4
* Move h-l to PSR-4
* Move m-r to PSR-4
* Move s-u to PSR-4
* Move files/ to PSR-4
* Move remaining tests to PSR-4
* Remove Test\ from old autoloader
Diffstat (limited to 'tests/lib/L10N')
-rw-r--r-- | tests/lib/L10N/FactoryTest.php | 458 | ||||
-rw-r--r-- | tests/lib/L10N/L10nLegacyTest.php | 136 | ||||
-rw-r--r-- | tests/lib/L10N/L10nTest.php | 165 |
3 files changed, 759 insertions, 0 deletions
diff --git a/tests/lib/L10N/FactoryTest.php b/tests/lib/L10N/FactoryTest.php new file mode 100644 index 00000000000..98bb5ec13c9 --- /dev/null +++ b/tests/lib/L10N/FactoryTest.php @@ -0,0 +1,458 @@ +<?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; + + /** @var \OCP\IUserSession|\PHPUnit_Framework_MockObject_MockObject */ + protected $userSession; + + /** @var string */ + protected $serverRoot; + + 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(); + + $this->userSession = $this->getMock('\OCP\IUserSession'); + + $this->serverRoot = \OC::$SERVERROOT; + } + + /** + * @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, + $this->userSession, + $this->serverRoot, + ]) + ->setMethods($methods) + ->getMock(); + } else { + return new Factory($this->config, $this->request, $this->userSession, $this->serverRoot); + } + } + + public function dataFindAvailableLanguages() { + return [ + [null], + ['files'], + ]; + } + + public function testFindLanguageWithExistingRequestLanguageAndNoApp() { + $factory = $this->getFactory(['languageExists']); + $this->invokePrivate($factory, 'requestLanguage', ['de']); + $factory->expects($this->once()) + ->method('languageExists') + ->with(null, 'de') + ->willReturn(true); + + $this->assertSame('de', $factory->findLanguage()); + } + + public function testFindLanguageWithExistingRequestLanguageAndApp() { + $factory = $this->getFactory(['languageExists']); + $this->invokePrivate($factory, 'requestLanguage', ['de']); + $factory->expects($this->once()) + ->method('languageExists') + ->with('MyApp', 'de') + ->willReturn(true); + + $this->assertSame('de', $factory->findLanguage('MyApp')); + } + + public function testFindLanguageWithNotExistingRequestLanguageAndExistingStoredUserLanguage() { + $factory = $this->getFactory(['languageExists']); + $this->invokePrivate($factory, 'requestLanguage', ['de']); + $factory->expects($this->at(0)) + ->method('languageExists') + ->with('MyApp', 'de') + ->willReturn(false); + $this->config + ->expects($this->once()) + ->method('getSystemValue') + ->with('installed', false) + ->willReturn(true); + $user = $this->getMock('\OCP\IUser'); + $user->expects($this->once()) + ->method('getUID') + ->willReturn('MyUserUid'); + $this->userSession + ->expects($this->exactly(2)) + ->method('getUser') + ->willReturn($user); + $this->config + ->expects($this->once()) + ->method('getUserValue') + ->with('MyUserUid', 'core', 'lang', null) + ->willReturn('jp'); + $factory->expects($this->at(1)) + ->method('languageExists') + ->with('MyApp', 'jp') + ->willReturn(true); + + $this->assertSame('jp', $factory->findLanguage('MyApp')); + } + + public function testFindLanguageWithNotExistingRequestLanguageAndNotExistingStoredUserLanguage() { + $factory = $this->getFactory(['languageExists']); + $this->invokePrivate($factory, 'requestLanguage', ['de']); + $factory->expects($this->at(0)) + ->method('languageExists') + ->with('MyApp', 'de') + ->willReturn(false); + $this->config + ->expects($this->at(0)) + ->method('getSystemValue') + ->with('installed', false) + ->willReturn(true); + $user = $this->getMock('\OCP\IUser'); + $user->expects($this->once()) + ->method('getUID') + ->willReturn('MyUserUid'); + $this->userSession + ->expects($this->exactly(2)) + ->method('getUser') + ->willReturn($user); + $this->config + ->expects($this->once()) + ->method('getUserValue') + ->with('MyUserUid', 'core', 'lang', null) + ->willReturn('jp'); + $factory->expects($this->at(1)) + ->method('languageExists') + ->with('MyApp', 'jp') + ->willReturn(false); + $this->config + ->expects($this->at(2)) + ->method('getSystemValue') + ->with('default_language', false) + ->willReturn('es'); + $factory->expects($this->at(2)) + ->method('languageExists') + ->with('MyApp', 'es') + ->willReturn(true); + + $this->assertSame('es', $factory->findLanguage('MyApp')); + } + + public function testFindLanguageWithNotExistingRequestLanguageAndNotExistingStoredUserLanguageAndNotExistingDefault() { + $factory = $this->getFactory(['languageExists']); + $this->invokePrivate($factory, 'requestLanguage', ['de']); + $factory->expects($this->at(0)) + ->method('languageExists') + ->with('MyApp', 'de') + ->willReturn(false); + $this->config + ->expects($this->at(0)) + ->method('getSystemValue') + ->with('installed', false) + ->willReturn(true); + $user = $this->getMock('\OCP\IUser'); + $user->expects($this->once()) + ->method('getUID') + ->willReturn('MyUserUid'); + $this->userSession + ->expects($this->exactly(2)) + ->method('getUser') + ->willReturn($user); + $this->config + ->expects($this->once()) + ->method('getUserValue') + ->with('MyUserUid', 'core', 'lang', null) + ->willReturn('jp'); + $factory->expects($this->at(1)) + ->method('languageExists') + ->with('MyApp', 'jp') + ->willReturn(false); + $this->config + ->expects($this->at(2)) + ->method('getSystemValue') + ->with('default_language', false) + ->willReturn('es'); + $factory->expects($this->at(2)) + ->method('languageExists') + ->with('MyApp', 'es') + ->willReturn(false); + $this->config + ->expects($this->never()) + ->method('setUserValue'); + + $this->assertSame('en', $factory->findLanguage('MyApp')); + } + + public function testFindLanguageWithNotExistingRequestLanguageAndNotExistingStoredUserLanguageAndNotExistingDefaultAndNoAppInScope() { + $factory = $this->getFactory(['languageExists']); + $this->invokePrivate($factory, 'requestLanguage', ['de']); + $factory->expects($this->at(0)) + ->method('languageExists') + ->with('MyApp', 'de') + ->willReturn(false); + $this->config + ->expects($this->at(0)) + ->method('getSystemValue') + ->with('installed', false) + ->willReturn(true); + $user = $this->getMock('\OCP\IUser'); + $user->expects($this->once()) + ->method('getUID') + ->willReturn('MyUserUid'); + $this->userSession + ->expects($this->exactly(2)) + ->method('getUser') + ->willReturn($user); + $this->config + ->expects($this->once()) + ->method('getUserValue') + ->with('MyUserUid', 'core', 'lang', null) + ->willReturn('jp'); + $factory->expects($this->at(1)) + ->method('languageExists') + ->with('MyApp', 'jp') + ->willReturn(false); + $this->config + ->expects($this->at(2)) + ->method('getSystemValue') + ->with('default_language', false) + ->willReturn('es'); + $factory->expects($this->at(2)) + ->method('languageExists') + ->with('MyApp', 'es') + ->willReturn(false); + $this->config + ->expects($this->never()) + ->method('setUserValue') + ->with('MyUserUid', 'core', 'lang', 'en'); + + + $this->assertSame('en', $factory->findLanguage('MyApp')); + } + + /** + * @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], + ]; + } + + public function testFindAvailableLanguagesWithThemes() { + $this->serverRoot .= '/tests/data'; + $app = 'files'; + + $factory = $this->getFactory(['findL10nDir']); + $factory->expects($this->once()) + ->method('findL10nDir') + ->with($app) + ->willReturn($this->serverRoot . '/apps/files/l10n/'); + $this->config + ->expects($this->once()) + ->method('getSystemValue') + ->with('theme') + ->willReturn('abc'); + + $this->assertEquals(['en', 'zz'], $factory->findAvailableLanguages($app), '', 0.0, 10, 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'], + + // Request lang should not be set for apps: Language is available + ['files_pdfviewer', 'de', null, ['de'], 'de', ''], + ['files_pdfviewer', 'de,en', null, ['de'], 'de', ''], + ['files_pdfviewer', 'de-DE,en-US;q=0.8,en;q=0.6', null, ['de'], 'de', ''], + // Request lang should not be set for apps: Language is not available + ['files_pdfviewer', 'de', null, ['ru'], 'en', ''], + ['files_pdfviewer', 'de,en', null, ['ru', 'en'], 'en', ''], + ['files_pdfviewer', 'de-DE,en-US;q=0.8,en;q=0.6', null, ['ru', 'en'], 'en', ''], + ]; + } + + /** + * @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)); + } +} diff --git a/tests/lib/L10N/L10nLegacyTest.php b/tests/lib/L10N/L10nLegacyTest.php new file mode 100644 index 00000000000..1df22ba36bd --- /dev/null +++ b/tests/lib/L10N/L10nLegacyTest.php @@ -0,0 +1,136 @@ +<?php +/** + * Copyright (c) 2013 Thomas Müller <thomas.mueller@tmit.eu> + * 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; +use DateTime; + +/** + * Class Test_L10n + * @group DB + */ +class L10nLegacyTest extends \Test\TestCase { + + public function testGermanPluralTranslations() { + $l = new OC_L10N('test'); + $transFile = \OC::$SERVERROOT.'/tests/data/l10n/de.json'; + + $l->load($transFile); + $this->assertEquals('1 Datei', (string)$l->n('%n file', '%n files', 1)); + $this->assertEquals('2 Dateien', (string)$l->n('%n file', '%n files', 2)); + } + + public function testRussianPluralTranslations() { + $l = new OC_L10N('test'); + $transFile = \OC::$SERVERROOT.'/tests/data/l10n/ru.json'; + + $l->load($transFile); + $this->assertEquals('1 файл', (string)$l->n('%n file', '%n files', 1)); + $this->assertEquals('2 файла', (string)$l->n('%n file', '%n files', 2)); + $this->assertEquals('6 файлов', (string)$l->n('%n file', '%n files', 6)); + $this->assertEquals('21 файл', (string)$l->n('%n file', '%n files', 21)); + $this->assertEquals('22 файла', (string)$l->n('%n file', '%n files', 22)); + $this->assertEquals('26 файлов', (string)$l->n('%n file', '%n files', 26)); + + /* + 1 file 1 файл 1 папка + 2-4 files 2-4 файла 2-4 папки + 5-20 files 5-20 файлов 5-20 папок + 21 files 21 файл 21 папка + 22-24 files 22-24 файла 22-24 папки + 25-30 files 25-30 файлов 25-30 папок + etc + 100 files 100 файлов, 100 папок + 1000 files 1000 файлов 1000 папок + */ + } + + public function testCzechPluralTranslations() { + $l = new OC_L10N('test'); + $transFile = \OC::$SERVERROOT.'/tests/data/l10n/cs.json'; + + $l->load($transFile); + $this->assertEquals('1 okno', (string)$l->n('%n window', '%n windows', 1)); + $this->assertEquals('2 okna', (string)$l->n('%n window', '%n windows', 2)); + $this->assertEquals('5 oken', (string)$l->n('%n window', '%n windows', 5)); + } + + public function localizationDataProvider() { + return array( + // timestamp as string + array('February 13, 2009 at 11:31:30 PM GMT+0', 'en', 'datetime', '1234567890'), + array('13. Februar 2009 um 23:31:30 GMT+0', 'de', 'datetime', '1234567890'), + array('February 13, 2009', 'en', 'date', '1234567890'), + array('13. Februar 2009', 'de', 'date', '1234567890'), + array('11:31:30 PM GMT+0', 'en', 'time', '1234567890'), + array('23:31:30 GMT+0', 'de', 'time', '1234567890'), + + // timestamp as int + array('February 13, 2009 at 11:31:30 PM GMT+0', 'en', 'datetime', 1234567890), + array('13. Februar 2009 um 23:31:30 GMT+0', 'de', 'datetime', 1234567890), + array('February 13, 2009', 'en', 'date', 1234567890), + array('13. Februar 2009', 'de', 'date', 1234567890), + array('11:31:30 PM GMT+0', 'en', 'time', 1234567890), + array('23:31:30 GMT+0', 'de', 'time', 1234567890), + + // DateTime object + array('February 13, 2009 at 11:31:30 PM GMT+0', 'en', 'datetime', new DateTime('@1234567890')), + array('13. Februar 2009 um 23:31:30 GMT+0', 'de', 'datetime', new DateTime('@1234567890')), + array('February 13, 2009', 'en', 'date', new DateTime('@1234567890')), + array('13. Februar 2009', 'de', 'date', new DateTime('@1234567890')), + array('11:31:30 PM GMT+0', 'en', 'time', new DateTime('@1234567890')), + array('23:31:30 GMT+0', 'de', 'time', new DateTime('@1234567890')), + + // en_GB + array('13 February 2009 at 23:31:30 GMT+0', 'en_GB', 'datetime', new DateTime('@1234567890')), + array('13 February 2009', 'en_GB', 'date', new DateTime('@1234567890')), + array('23:31:30 GMT+0', 'en_GB', 'time', new DateTime('@1234567890')), + array('13 February 2009 at 23:31:30 GMT+0', 'en-GB', 'datetime', new DateTime('@1234567890')), + array('13 February 2009', 'en-GB', 'date', new DateTime('@1234567890')), + array('23:31:30 GMT+0', 'en-GB', 'time', new DateTime('@1234567890')), + ); + } + + /** + * @dataProvider localizationDataProvider + */ + public function testNumericStringLocalization($expectedDate, $lang, $type, $value) { + $l = new OC_L10N('test', $lang); + $this->assertSame($expectedDate, $l->l($type, $value)); + } + + public function firstDayDataProvider() { + return array( + array(1, 'de'), + array(0, 'en'), + ); + } + + /** + * @dataProvider firstDayDataProvider + * @param $expected + * @param $lang + */ + public function testFirstWeekDay($expected, $lang) { + $l = new OC_L10N('test', $lang); + $this->assertSame($expected, $l->l('firstday', 'firstday')); + } + + public function testFactoryGetLanguageCode() { + $factory = new \OC\L10N\Factory($this->getMock('OCP\IConfig'), $this->getMock('OCP\IRequest'), $this->getMock('OCP\IUserSession'), \OC::$SERVERROOT); + $l = $factory->get('lib', 'de'); + $this->assertEquals('de', $l->getLanguageCode()); + } + + public function testServiceGetLanguageCode() { + $l = \OC::$server->getL10N('lib', 'de'); + $this->assertEquals('de', $l->getLanguageCode()); + } +} diff --git a/tests/lib/L10N/L10nTest.php b/tests/lib/L10N/L10nTest.php new file mode 100644 index 00000000000..227e07056a8 --- /dev/null +++ b/tests/lib/L10N/L10nTest.php @@ -0,0 +1,165 @@ +<?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 DateTime; +use OC\L10N\Factory; +use OC\L10N\L10N; +use OCP\IUserSession; +use Test\TestCase; + +/** + * Class L10nTest + * + * @package Test\L10N + */ +class L10nTest extends TestCase { + /** + * @return Factory + */ + protected function getFactory() { + /** @var \OCP\IConfig $config */ + $config = $this->getMock('OCP\IConfig'); + /** @var \OCP\IRequest $request */ + $request = $this->getMock('OCP\IRequest'); + /** @var IUserSession $userSession */ + $userSession = $this->getMock('OCP\IUserSession'); + return new Factory($config, $request, $userSession, \OC::$SERVERROOT); + } + + public function testGermanPluralTranslations() { + $transFile = \OC::$SERVERROOT.'/tests/data/l10n/de.json'; + $l = new L10N($this->getFactory(), 'test', 'de', [$transFile]); + + $this->assertEquals('1 Datei', (string) $l->n('%n file', '%n files', 1)); + $this->assertEquals('2 Dateien', (string) $l->n('%n file', '%n files', 2)); + } + + public function testRussianPluralTranslations() { + $transFile = \OC::$SERVERROOT.'/tests/data/l10n/ru.json'; + $l = new L10N($this->getFactory(), 'test', 'ru', [$transFile]); + + $this->assertEquals('1 файл', (string)$l->n('%n file', '%n files', 1)); + $this->assertEquals('2 файла', (string)$l->n('%n file', '%n files', 2)); + $this->assertEquals('6 файлов', (string)$l->n('%n file', '%n files', 6)); + $this->assertEquals('21 файл', (string)$l->n('%n file', '%n files', 21)); + $this->assertEquals('22 файла', (string)$l->n('%n file', '%n files', 22)); + $this->assertEquals('26 файлов', (string)$l->n('%n file', '%n files', 26)); + + /* + 1 file 1 файл 1 папка + 2-4 files 2-4 файла 2-4 папки + 5-20 files 5-20 файлов 5-20 папок + 21 files 21 файл 21 папка + 22-24 files 22-24 файла 22-24 папки + 25-30 files 25-30 файлов 25-30 папок + etc + 100 files 100 файлов, 100 папок + 1000 files 1000 файлов 1000 папок + */ + } + + public function testCzechPluralTranslations() { + $transFile = \OC::$SERVERROOT.'/tests/data/l10n/cs.json'; + $l = new L10N($this->getFactory(), 'test', 'cs', [$transFile]); + + $this->assertEquals('1 okno', (string)$l->n('%n window', '%n windows', 1)); + $this->assertEquals('2 okna', (string)$l->n('%n window', '%n windows', 2)); + $this->assertEquals('5 oken', (string)$l->n('%n window', '%n windows', 5)); + } + + public function localizationData() { + return array( + // timestamp as string + array('February 13, 2009 at 11:31:30 PM GMT+0', 'en', 'datetime', '1234567890'), + array('13. Februar 2009 um 23:31:30 GMT+0', 'de', 'datetime', '1234567890'), + array('February 13, 2009', 'en', 'date', '1234567890'), + array('13. Februar 2009', 'de', 'date', '1234567890'), + array('11:31:30 PM GMT+0', 'en', 'time', '1234567890'), + array('23:31:30 GMT+0', 'de', 'time', '1234567890'), + + // timestamp as int + array('February 13, 2009 at 11:31:30 PM GMT+0', 'en', 'datetime', 1234567890), + array('13. Februar 2009 um 23:31:30 GMT+0', 'de', 'datetime', 1234567890), + array('February 13, 2009', 'en', 'date', 1234567890), + array('13. Februar 2009', 'de', 'date', 1234567890), + array('11:31:30 PM GMT+0', 'en', 'time', 1234567890), + array('23:31:30 GMT+0', 'de', 'time', 1234567890), + + // DateTime object + array('February 13, 2009 at 11:31:30 PM GMT+0', 'en', 'datetime', new DateTime('@1234567890')), + array('13. Februar 2009 um 23:31:30 GMT+0', 'de', 'datetime', new DateTime('@1234567890')), + array('February 13, 2009', 'en', 'date', new DateTime('@1234567890')), + array('13. Februar 2009', 'de', 'date', new DateTime('@1234567890')), + array('11:31:30 PM GMT+0', 'en', 'time', new DateTime('@1234567890')), + array('23:31:30 GMT+0', 'de', 'time', new DateTime('@1234567890')), + + // en_GB + array('13 February 2009 at 23:31:30 GMT+0', 'en_GB', 'datetime', new DateTime('@1234567890')), + array('13 February 2009', 'en_GB', 'date', new DateTime('@1234567890')), + array('23:31:30 GMT+0', 'en_GB', 'time', new DateTime('@1234567890')), + array('13 February 2009 at 23:31:30 GMT+0', 'en-GB', 'datetime', new DateTime('@1234567890')), + array('13 February 2009', 'en-GB', 'date', new DateTime('@1234567890')), + array('23:31:30 GMT+0', 'en-GB', 'time', new DateTime('@1234567890')), + ); + } + + /** + * @dataProvider localizationData + */ + public function testNumericStringLocalization($expectedDate, $lang, $type, $value) { + $l = new L10N($this->getFactory(), 'test', $lang, []); + $this->assertSame($expectedDate, $l->l($type, $value)); + } + + public function firstDayData() { + return array( + array(1, 'de'), + array(0, 'en'), + ); + } + + /** + * @dataProvider firstDayData + * @param $expected + * @param $lang + */ + public function testFirstWeekDay($expected, $lang) { + $l = new L10N($this->getFactory(), 'test', $lang, []); + $this->assertSame($expected, $l->l('firstday', 'firstday')); + } + + public function jsDateData() { + return array( + array('dd.MM.yy', 'de'), + array('M/d/yy', 'en'), + ); + } + + /** + * @dataProvider jsDateData + * @param $expected + * @param $lang + */ + public function testJSDate($expected, $lang) { + $l = new L10N($this->getFactory(), 'test', $lang, []); + $this->assertSame($expected, $l->l('jsdate', 'jsdate')); + } + + public function testFactoryGetLanguageCode() { + $l = $this->getFactory()->get('lib', 'de'); + $this->assertEquals('de', $l->getLanguageCode()); + } + + public function testServiceGetLanguageCode() { + $l = \OC::$server->getL10N('lib', 'de'); + $this->assertEquals('de', $l->getLanguageCode()); + } +} |