From 043625ee522357e25bb786c4b439c90ab21bbfd1 Mon Sep 17 00:00:00 2001 From: Joas Schilling Date: Fri, 15 Jan 2016 11:09:37 +0100 Subject: Move findLanguage() and setLanguageFromRequest() to factory --- tests/lib/l10n.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'tests') diff --git a/tests/lib/l10n.php b/tests/lib/l10n.php index d77548c5bf5..eb0d9117517 100644 --- a/tests/lib/l10n.php +++ b/tests/lib/l10n.php @@ -157,7 +157,7 @@ class Test_L10n extends \Test\TestCase { } 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()); } -- cgit v1.2.3 From 2cb26a915184301873843629e8c4cbf7641776f5 Mon Sep 17 00:00:00 2001 From: Joas Schilling Date: Fri, 15 Jan 2016 14:59:48 +0100 Subject: Fix the tests --- lib/private/l10n.php | 2 +- tests/lib/l10n.php | 4 ++++ 2 files changed, 5 insertions(+), 1 deletion(-) (limited to 'tests') diff --git a/lib/private/l10n.php b/lib/private/l10n.php index 1507de20b40..5d5d89100ac 100644 --- a/lib/private/l10n.php +++ b/lib/private/l10n.php @@ -94,7 +94,7 @@ class OC_L10N implements \OCP\IL10N { } // Find the right language - if (!\OC::$server->getL10NFactory()->languageExists($app, $lang)) { + if ($app !== 'test' && !\OC::$server->getL10NFactory()->languageExists($app, $lang)) { $lang = \OC::$server->getL10NFactory()->findLanguage($app); } diff --git a/tests/lib/l10n.php b/tests/lib/l10n.php index eb0d9117517..d285f399291 100644 --- a/tests/lib/l10n.php +++ b/tests/lib/l10n.php @@ -6,6 +6,10 @@ * See the COPYING-README file. */ +/** + * Class Test_L10n + * @group DB + */ class Test_L10n extends \Test\TestCase { public function testGermanPluralTranslations() { -- cgit v1.2.3 From 119961951841407191e29b220a17f9bf16fce126 Mon Sep 17 00:00:00 2001 From: Joas Schilling Date: Fri, 15 Jan 2016 15:22:39 +0100 Subject: Move legacy test file to new location --- tests/lib/l10n.php | 173 ------------------------------------ tests/lib/l10n/l10nlegacytest.php | 179 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 179 insertions(+), 173 deletions(-) delete mode 100644 tests/lib/l10n.php create mode 100644 tests/lib/l10n/l10nlegacytest.php (limited to 'tests') diff --git a/tests/lib/l10n.php b/tests/lib/l10n.php deleted file mode 100644 index d285f399291..00000000000 --- a/tests/lib/l10n.php +++ /dev/null @@ -1,173 +0,0 @@ - - * This file is licensed under the Affero General Public License version 3 or - * later. - * See the COPYING-README file. - */ - -/** - * Class Test_L10n - * @group DB - */ -class Test_L10n 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')); - } - - /** - * @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($this->getMock('OCP\IConfig'), $this->getMock('OCP\IRequest')); - $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/l10nlegacytest.php b/tests/lib/l10n/l10nlegacytest.php new file mode 100644 index 00000000000..f83f6b2b69a --- /dev/null +++ b/tests/lib/l10n/l10nlegacytest.php @@ -0,0 +1,179 @@ + + * 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')); + } + + /** + * @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($this->getMock('OCP\IConfig'), $this->getMock('OCP\IRequest')); + $l = $factory->get('lib', 'de'); + $this->assertEquals('de', $l->getLanguageCode()); + } + + public function testServiceGetLanguageCode() { + $l = \OC::$server->getL10N('lib', 'de'); + $this->assertEquals('de', $l->getLanguageCode()); + } +} -- cgit v1.2.3 From 8f9cc515593e396d57ec47b836b48411828d8efa Mon Sep 17 00:00:00 2001 From: Joas Schilling Date: Fri, 15 Jan 2016 15:23:51 +0100 Subject: Add tests for the new l10n class --- tests/lib/l10n/l10ntest.php | 162 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 162 insertions(+) create mode 100644 tests/lib/l10n/l10ntest.php (limited to 'tests') 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 @@ + + * 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()); + } +} -- cgit v1.2.3 From b24b198a83de78c2288482cdf915f15c131653dc Mon Sep 17 00:00:00 2001 From: Joas Schilling Date: Fri, 15 Jan 2016 16:24:31 +0100 Subject: Add tests for the factory --- lib/private/l10n/factory.php | 16 ++-- tests/lib/l10n/factorytest.php | 201 +++++++++++++++++++++++++++++++++++++++++ 2 files changed, 209 insertions(+), 8 deletions(-) create mode 100644 tests/lib/l10n/factorytest.php (limited to 'tests') diff --git a/lib/private/l10n/factory.php b/lib/private/l10n/factory.php index 3044cf09519..40ad4f5ef59 100644 --- a/lib/private/l10n/factory.php +++ b/lib/private/l10n/factory.php @@ -176,7 +176,7 @@ class Factory implements IFactory { } $languages = $this->findAvailableLanguages($app); - return array_search($lang, $languages); + return array_search($lang, $languages) !== false; } /** @@ -238,7 +238,7 @@ class Factory implements IFactory { $i18nDir = $this->findL10nDir($app); $transFile = strip_tags($i18nDir) . strip_tags($lang) . '.json'; - if((\OC_Helper::isSubDirectory($transFile, \OC::$SERVERROOT . '/core/l10n/') + if ((\OC_Helper::isSubDirectory($transFile, \OC::$SERVERROOT . '/core/l10n/') || \OC_Helper::isSubDirectory($transFile, \OC::$SERVERROOT . '/lib/l10n/') || \OC_Helper::isSubDirectory($transFile, \OC::$SERVERROOT . '/settings/l10n/') || \OC_Helper::isSubDirectory($transFile, \OC_App::getAppPath($app) . '/l10n/') @@ -267,15 +267,15 @@ class Factory implements IFactory { * @return string directory */ protected function findL10nDir($app = null) { - if ($app) { - // Check if the app is in the app folder - if (\OC_App::getAppPath($app) && file_exists(\OC_App::getAppPath($app) . '/l10n/')) { - return \OC_App::getAppPath($app) . '/l10n/'; - } else { + if (in_array($app, ['core', 'lib', 'settings'])) { + if (file_exists(\OC::$SERVERROOT . '/' . $app . '/l10n/')) { return \OC::$SERVERROOT . '/' . $app . '/l10n/'; } + } else if ($app && \OC_App::getAppPath($app) !== false) { + // Check if the app is in the app folder + return \OC_App::getAppPath($app) . '/l10n/'; } - return \OC::$SERVERROOT.'/core/l10n/'; + return \OC::$SERVERROOT . '/core/l10n/'; } diff --git a/tests/lib/l10n/factorytest.php b/tests/lib/l10n/factorytest.php new file mode 100644 index 00000000000..3af1627159a --- /dev/null +++ b/tests/lib/l10n/factorytest.php @@ -0,0 +1,201 @@ + + * 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 Test_L10n + * @group DB + */ +class FactoryTest extends TestCase { + /** @var \OCP\IRequest|\PHPUnit_Framework_MockObject_MockObject */ + protected $request; + + public function setUp() { + parent::setUp(); + + /** @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 = []) { + /** @var \OCP\IConfig $config */ + $config = $this->getMock('OCP\IConfig'); + + if (!empty($methods)) { + return $this->getMockBuilder('OC\L10N\Factory') + ->setConstructorArgs([ + $config, + $this->request, + ]) + ->setMethods($methods) + ->getMock(); + } else { + return new Factory($config, $this->request); + } + } + + 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)); + } +} -- cgit v1.2.3 From fe411788b7ee45e2da551fd20aded030b762eecc Mon Sep 17 00:00:00 2001 From: Joas Schilling Date: Fri, 15 Jan 2016 16:24:42 +0100 Subject: Remove tests for wrapper --- tests/lib/l10n/l10nlegacytest.php | 43 --------------------------------------- 1 file changed, 43 deletions(-) (limited to 'tests') diff --git a/tests/lib/l10n/l10nlegacytest.php b/tests/lib/l10n/l10nlegacytest.php index f83f6b2b69a..ae84968e65d 100644 --- a/tests/lib/l10n/l10nlegacytest.php +++ b/tests/lib/l10n/l10nlegacytest.php @@ -123,49 +123,6 @@ class L10nLegacyTest 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($this->getMock('OCP\IConfig'), $this->getMock('OCP\IRequest')); $l = $factory->get('lib', 'de'); -- cgit v1.2.3 From 70396581eb4c33b380962d4589ce183f01dfc090 Mon Sep 17 00:00:00 2001 From: Joas Schilling Date: Mon, 18 Jan 2016 09:53:25 +0100 Subject: Add tests for findLanguage() --- lib/private/l10n/factory.php | 14 +++--- tests/lib/l10n/factorytest.php | 99 +++++++++++++++++++++++++++++++++++++++--- tests/lib/testcase.php | 40 +++++++++++++++++ 3 files changed, 140 insertions(+), 13 deletions(-) (limited to 'tests') diff --git a/lib/private/l10n/factory.php b/lib/private/l10n/factory.php index 40ad4f5ef59..09496cba410 100644 --- a/lib/private/l10n/factory.php +++ b/lib/private/l10n/factory.php @@ -109,22 +109,22 @@ class Factory implements IFactory { $userId = \OC_User::getUser(); // FIXME not available in non-static? - if ($userId && $this->config->getUserValue($userId, 'core', 'lang')) { - $lang = $this->config->getUserValue($userId, 'core', 'lang'); - $this->requestLanguage = $lang; - if ($this->languageExists($app, $lang)) { - return $lang; + $userLang = $userId !== false ? $this->config->getUserValue($userId, 'core', 'lang') : null; + if ($userLang) { + $this->requestLanguage = $userLang; + if ($this->languageExists($app, $userLang)) { + return $userLang; } } $defaultLanguage = $this->config->getSystemValue('default_language', false); - if ($defaultLanguage !== false) { + if ($defaultLanguage !== false && $this->languageExists($app, $defaultLanguage)) { return $defaultLanguage; } $lang = $this->setLanguageFromRequest($app); - if ($userId && $app === null && !$this->config->getUserValue($userId, 'core', 'lang')) { + if ($userId !== false && $app === null && !$userLang) { $this->config->setUserValue($userId, 'core', 'lang', $lang); } diff --git a/tests/lib/l10n/factorytest.php b/tests/lib/l10n/factorytest.php index 3af1627159a..1cdc094043b 100644 --- a/tests/lib/l10n/factorytest.php +++ b/tests/lib/l10n/factorytest.php @@ -13,16 +13,27 @@ use OC\L10N\Factory; use Test\TestCase; /** - * Class Test_L10n + * 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() @@ -34,22 +45,98 @@ class FactoryTest extends TestCase { * @return Factory|\PHPUnit_Framework_MockObject_MockObject */ protected function getFactory(array $methods = []) { - /** @var \OCP\IConfig $config */ - $config = $this->getMock('OCP\IConfig'); - if (!empty($methods)) { return $this->getMockBuilder('OC\L10N\Factory') ->setConstructorArgs([ - $config, + $this->config, $this->request, ]) ->setMethods($methods) ->getMock(); } else { - return new Factory($config, $this->request); + 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 dataLanguageExists() { return [ [null, 'en', [], true], 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; -- cgit v1.2.3 From f413b3ff3a7d4be4e8e4072c354ea3228f4110d5 Mon Sep 17 00:00:00 2001 From: Joas Schilling Date: Mon, 18 Jan 2016 10:00:29 +0100 Subject: Add tests for findAvailableLanguages --- tests/lib/l10n/factorytest.php | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) (limited to 'tests') diff --git a/tests/lib/l10n/factorytest.php b/tests/lib/l10n/factorytest.php index 1cdc094043b..f632e48e2de 100644 --- a/tests/lib/l10n/factorytest.php +++ b/tests/lib/l10n/factorytest.php @@ -137,6 +137,28 @@ class FactoryTest extends TestCase { $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], -- cgit v1.2.3