diff options
author | Thomas Müller <thomas.mueller@tmit.eu> | 2016-01-27 09:37:00 +0100 |
---|---|---|
committer | Thomas Müller <thomas.mueller@tmit.eu> | 2016-01-27 09:37:00 +0100 |
commit | 1594371c8c0124af2469bd70b9b917bfd845ae19 (patch) | |
tree | a7fb0546707e05984fa78f45a53563514b9c9d4d /tests/lib | |
parent | 5b7a1b13f3514f60650e1da41d8dc046ee0252c5 (diff) | |
parent | 3a6390031cb7e6575edd8d4a5271614eb8ba9470 (diff) | |
download | nextcloud-server-1594371c8c0124af2469bd70b9b917bfd845ae19.tar.gz nextcloud-server-1594371c8c0124af2469bd70b9b917bfd845ae19.zip |
Merge pull request #21741 from owncloud/l10n-improvements
Move methods to the factory that are not related to translating, but to guessing/finding the language
Diffstat (limited to 'tests/lib')
-rw-r--r-- | tests/lib/l10n/factorytest.php | 310 | ||||
-rw-r--r-- | tests/lib/l10n/l10nlegacytest.php (renamed from tests/lib/l10n.php) | 63 | ||||
-rw-r--r-- | tests/lib/l10n/l10ntest.php | 162 | ||||
-rw-r--r-- | tests/lib/testcase.php | 40 |
4 files changed, 527 insertions, 48 deletions
diff --git a/tests/lib/l10n/factorytest.php b/tests/lib/l10n/factorytest.php new file mode 100644 index 00000000000..f632e48e2de --- /dev/null +++ b/tests/lib/l10n/factorytest.php @@ -0,0 +1,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)); + } +} diff --git a/tests/lib/l10n.php b/tests/lib/l10n/l10nlegacytest.php index d77548c5bf5..ae84968e65d 100644 --- a/tests/lib/l10n.php +++ b/tests/lib/l10n/l10nlegacytest.php @@ -6,11 +6,21 @@ * See the COPYING-README file. */ -class Test_L10n extends \Test\TestCase { +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'; + $transFile = \OC::$SERVERROOT.'/tests/data/l10n/de.json'; $l->load($transFile); $this->assertEquals('1 Datei', (string)$l->n('%n file', '%n files', 1)); @@ -19,7 +29,7 @@ class Test_L10n extends \Test\TestCase { public function testRussianPluralTranslations() { $l = new OC_L10N('test'); - $transFile = OC::$SERVERROOT.'/tests/data/l10n/ru.json'; + $transFile = \OC::$SERVERROOT.'/tests/data/l10n/ru.json'; $l->load($transFile); $this->assertEquals('1 файл', (string)$l->n('%n file', '%n files', 1)); @@ -44,7 +54,7 @@ class Test_L10n extends \Test\TestCase { public function testCzechPluralTranslations() { $l = new OC_L10N('test'); - $transFile = OC::$SERVERROOT.'/tests/data/l10n/cs.json'; + $transFile = \OC::$SERVERROOT.'/tests/data/l10n/cs.json'; $l->load($transFile); $this->assertEquals('1 okno', (string)$l->n('%n window', '%n windows', 1)); @@ -113,51 +123,8 @@ class Test_L10n extends \Test\TestCase { $this->assertSame($expected, $l->l('firstday', 'firstday')); } - /** - * @dataProvider findLanguageData - */ - public function testFindLanguage($default, $preference, $expected) { - OC_User::setUserId(null); - - $config = \OC::$server->getConfig(); - if (is_null($default)) { - $config->deleteSystemValue('default_language'); - } else { - $config->setSystemValue('default_language', $default); - } - $_SERVER['HTTP_ACCEPT_LANGUAGE'] = $preference; - - $reflection = new \ReflectionClass('OC_L10N'); - $prop = $reflection->getProperty('language'); - $prop->setAccessible(1); - $prop->setValue(''); - $prop->setAccessible(0); - - $this->assertSame($expected, OC_L10N::findLanguage()); - } - - public function findLanguageData() { - return array( - // Exact match - array(null, 'de-DE,en;q=0.5', 'de_DE'), - array(null, 'de-DE,en-US;q=0.8,en;q=0.6', 'de_DE'), - - // Best match - array(null, 'de-US,en;q=0.5', 'de'), - array(null, 'de-US,en-US;q=0.8,en;q=0.6', 'de'), - - // The default_language config setting overrides browser preferences. - array('es_AR', 'de-DE,en;q=0.5', 'es_AR'), - array('es_AR', 'de-DE,en-US;q=0.8,en;q=0.6', 'es_AR'), - - // Worst case default to english - array(null, '', 'en'), - array(null, null, 'en'), - ); - } - public function testFactoryGetLanguageCode() { - $factory = new \OC\L10N\Factory(); + $factory = new \OC\L10N\Factory($this->getMock('OCP\IConfig'), $this->getMock('OCP\IRequest')); $l = $factory->get('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..95546b4f788 --- /dev/null +++ b/tests/lib/l10n/l10ntest.php @@ -0,0 +1,162 @@ +<?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 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'); + return new Factory($config, $request); + } + + 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()); + } +} diff --git a/tests/lib/testcase.php b/tests/lib/testcase.php index 38d5cf49320..008b96b3417 100644 --- a/tests/lib/testcase.php +++ b/tests/lib/testcase.php @@ -36,6 +36,46 @@ abstract class TestCase extends \PHPUnit_Framework_TestCase { static protected $realDatabase = null; static private $wasDatabaseAllowed = false; + /** @var array */ + protected $services = []; + + /** + * @param string $name + * @param mixed $newService + * @return bool + */ + public function overwriteService($name, $newService) { + if (isset($this->services[$name])) { + return false; + } + + $this->services[$name] = \OC::$server->query($name); + \OC::$server->registerService($name, function () use ($newService) { + return $newService; + }); + + return true; + } + + /** + * @param string $name + * @return bool + */ + public function restoreService($name) { + if (isset($this->services[$name])) { + $oldService = $this->services[$name]; + \OC::$server->registerService($name, function () use ($oldService) { + return $oldService; + }); + + + unset($this->services[$name]); + return true; + } + + return false; + } + protected function getTestTraits() { $traits = []; $class = $this; |