Skip to content

Commit

Permalink
Add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
phansys committed Jan 16, 2018
1 parent f1691ec commit 3b55476
Show file tree
Hide file tree
Showing 2 changed files with 206 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/Twig/Extension/SonataAdminExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down
205 changes: 205 additions & 0 deletions tests/Twig/Extension/SonataAdminExtensionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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.
Expand Down Expand Up @@ -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.
*
Expand All @@ -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];
}
}

0 comments on commit 3b55476

Please sign in to comment.