From 3b554764733779692d43c2f3843213c987a50cf3 Mon Sep 17 00:00:00 2001 From: Javier Spagnoletti Date: Tue, 16 Jan 2018 13:14:31 -0300 Subject: [PATCH] Add tests --- src/Twig/Extension/SonataAdminExtension.php | 2 +- .../Extension/SonataAdminExtensionTest.php | 205 ++++++++++++++++++ 2 files changed, 206 insertions(+), 1 deletion(-) diff --git a/src/Twig/Extension/SonataAdminExtension.php b/src/Twig/Extension/SonataAdminExtension.php index ff742b704d8..4dd6029c9b8 100644 --- a/src/Twig/Extension/SonataAdminExtension.php +++ b/src/Twig/Extension/SonataAdminExtension.php @@ -440,7 +440,7 @@ final public function getCanonicalizedLocaleForMoment(array $context) $locale = strtolower(str_replace('_', '-', $context['app']->getRequest()->getLocale())); // "en" language doesn't require localization. - if ('en' === $lang = substr($locale, 0, 2)) { + if (('en' === $lang = substr($locale, 0, 2)) && !in_array($locale, ['en-au', 'en-ca', 'en-gb', 'en-ie', 'en-nz'], true)) { return null; } diff --git a/tests/Twig/Extension/SonataAdminExtensionTest.php b/tests/Twig/Extension/SonataAdminExtensionTest.php index 2e75c203067..f8de0e18ce4 100644 --- a/tests/Twig/Extension/SonataAdminExtensionTest.php +++ b/tests/Twig/Extension/SonataAdminExtensionTest.php @@ -25,6 +25,7 @@ use Symfony\Bridge\Twig\Tests\Extension\Fixtures\StubFilesystemLoader; use Symfony\Component\Config\FileLocator; use Symfony\Component\DependencyInjection\ContainerInterface; +use Symfony\Component\HttpFoundation\Request; use Symfony\Component\Routing\Generator\UrlGenerator; use Symfony\Component\Routing\Loader\XmlFileLoader; use Symfony\Component\Routing\RequestContext; @@ -33,6 +34,7 @@ use Symfony\Component\Translation\MessageSelector; use Symfony\Component\Translation\Translator; use Symfony\Component\Translation\TranslatorInterface; +use Symfony\Bridge\Twig\AppVariable; /** * Test for SonataAdminExtension. @@ -2290,6 +2292,199 @@ public function testGetXEditableChoicesIsIdempotent(array $input) ); } + public function select2LocalesProvider() + { + return [ + ['ar', 'ar'], + ['az', 'az'], + ['bg', 'bg'], + ['ca', 'ca'], + ['cs', 'cs'], + ['da', 'da'], + ['de', 'de'], + ['el', 'el'], + ['en', null], + ['es', 'es'], + ['et', 'et'], + ['eu', 'eu'], + ['fa', 'fa'], + ['fi', 'fi'], + ['fr', 'fr'], + ['gl', 'gl'], + ['he', 'he'], + ['hr', 'hr'], + ['hu', 'hu'], + ['id', 'id'], + ['is', 'is'], + ['it', 'it'], + ['ja', 'ja'], + ['ka', 'ka'], + ['ko', 'ko'], + ['lt', 'lt'], + ['lv', 'lv'], + ['mk', 'mk'], + ['ms', 'ms'], + ['nb', 'nb'], + ['nl', 'nl'], + ['pl', 'pl'], + ['pt', 'pt-PT'], + ['pt-BR', 'pt-BR'], + ['pt-PT', 'pt-PT'], + ['ro', 'ro'], + ['rs', 'rs'], + ['ru', 'ru'], + ['sk', 'sk'], + ['sv', 'sv'], + ['th', 'th'], + ['tr', 'tr'], + ['ug', 'ug-CN'], + ['ug-CN', 'ug-CN'], + ['uk', 'uk'], + ['vi', 'vi'], + ['zh', 'zh-CN'], + ['zh-CN', 'zh-CN'], + ['zh-TW', 'zh-TW'], + ]; + } + + /** + * @dataProvider select2LocalesProvider + */ + public function testCanonicalizedLocaleForSelect2($original, $canonicalized) + { + $context = $this->mockExtensionContext($original); + + $this->assertSame($this->twigExtension->getCanonicalizedLocaleForSelect2($context), $canonicalized); + } + + public function momentLocalesProvider() + { + return [ + ['af', 'af'], + ['ar-dz', 'ar-dz'], + ['ar', 'ar'], + ['ar-ly', 'ar-ly'], + ['ar-ma', 'ar-ma'], + ['ar-sa', 'ar-sa'], + ['ar-tn', 'ar-tn'], + ['az', 'az'], + ['be', 'be'], + ['bg', 'bg'], + ['bn', 'bn'], + ['bo', 'bo'], + ['br', 'br'], + ['bs', 'bs'], + ['ca', 'ca'], + ['cs', 'cs'], + ['cv', 'cv'], + ['cy', 'cy'], + ['da', 'da'], + ['de-at', 'de-at'], + ['de', 'de'], + ['dv', 'dv'], + ['el', 'el'], + ['en', null], + ['en-us', null], + ['en-au', 'en-au'], + ['en-ca', 'en-ca'], + ['en-gb', 'en-gb'], + ['en-ie', 'en-ie'], + ['en-nz', 'en-nz'], + ['eo', 'eo'], + ['es-do', 'es-do'], + ['es-ar', 'es'], + ['es-mx', 'es'], + ['es', 'es'], + ['et', 'et'], + ['eu', 'eu'], + ['fa', 'fa'], + ['fi', 'fi'], + ['fo', 'fo'], + ['fr-ca', 'fr-ca'], + ['fr-ch', 'fr-ch'], + ['fr', 'fr'], + ['fy', 'fy'], + ['gd', 'gd'], + ['gl', 'gl'], + ['he', 'he'], + ['hi', 'hi'], + ['hr', 'hr'], + ['hu', 'hu'], + ['hy-am', 'hy-am'], + ['id', 'id'], + ['is', 'is'], + ['it', 'it'], + ['ja', 'ja'], + ['jv', 'jv'], + ['ka', 'ka'], + ['kk', 'kk'], + ['km', 'km'], + ['ko', 'ko'], + ['ky', 'ky'], + ['lb', 'lb'], + ['lo', 'lo'], + ['lt', 'lt'], + ['lv', 'lv'], + ['me', 'me'], + ['mi', 'mi'], + ['mk', 'mk'], + ['ml', 'ml'], + ['mr', 'mr'], + ['ms', 'ms'], + ['ms-my', 'ms-my'], + ['my', 'my'], + ['nb', 'nb'], + ['ne', 'ne'], + ['nl-be', 'nl-be'], + ['nl', 'nl'], + ['nn', 'nn'], + ['pa-in', 'pa-in'], + ['pl', 'pl'], + ['pt-br', 'pt-br'], + ['pt', 'pt'], + ['ro', 'ro'], + ['ru', 'ru'], + ['se', 'se'], + ['si', 'si'], + ['sk', 'sk'], + ['sl', 'sl'], + ['sq', 'sq'], + ['sr-cyrl', 'sr-cyrl'], + ['sr', 'sr'], + ['ss', 'ss'], + ['sv', 'sv'], + ['sw', 'sw'], + ['ta', 'ta'], + ['te', 'te'], + ['tet', 'tet'], + ['th', 'th'], + ['tlh', 'tlh'], + ['tl-ph', 'tl-ph'], + ['tr', 'tr'], + ['tzl', 'tzl'], + ['tzm', 'tzm'], + ['tzm-latn', 'tzm-latn'], + ['uk', 'uk'], + ['uz', 'uz'], + ['vi', 'vi'], + ['x-pseudo', 'x-pseudo'], + ['yo', 'yo'], + ['zh-cn', 'zh-cn'], + ['zh-hk', 'zh-hk'], + ['zh-tw', 'zh-tw'], + ]; + } + + /** + * @dataProvider momentLocalesProvider + */ + public function testCanonicalizedLocaleForMoment($original, $canonicalized) + { + $context = $this->mockExtensionContext($original); + + $this->assertSame($this->twigExtension->getCanonicalizedLocaleForMoment($context), $canonicalized); + } + /** * This method generates url part for Twig layout. * @@ -2310,4 +2505,14 @@ private function removeExtraWhitespace($string) $string )); } + + private function mockExtensionContext($locale) + { + $request = $this->createMock(Request::class); + $request->method('getLocale')->willReturn($locale); + $appVariable = $this->createMock(AppVariable::class); + $appVariable->method('getRequest')->willReturn($request); + + return ['app' => $appVariable]; + } }