From 13486ca80ea1fe3aa7c3564e4be67c4273d00323 Mon Sep 17 00:00:00 2001 From: Maxime Rainville Date: Wed, 16 Jan 2019 10:36:53 +1300 Subject: [PATCH] MINOR Add unit test for Requirements::add_i18n_javascript() --- src/View/Requirements_Backend.php | 4 +- tests/php/View/RequirementsTest.php | 74 +++++++++++++++++++++++ tests/php/View/SSViewerTest/i18n/en-gb.js | 0 tests/php/View/SSViewerTest/i18n/en-us.js | 0 tests/php/View/SSViewerTest/i18n/en.js | 0 tests/php/View/SSViewerTest/i18n/en_GB.js | 0 tests/php/View/SSViewerTest/i18n/en_US.js | 0 tests/php/View/SSViewerTest/i18n/fr-ca.js | 0 tests/php/View/SSViewerTest/i18n/fr.js | 0 tests/php/View/SSViewerTest/i18n/fr_CA.js | 0 tests/php/View/SSViewerTest/i18n/mi.js | 0 11 files changed, 77 insertions(+), 1 deletion(-) create mode 100644 tests/php/View/SSViewerTest/i18n/en-gb.js create mode 100644 tests/php/View/SSViewerTest/i18n/en-us.js create mode 100644 tests/php/View/SSViewerTest/i18n/en.js create mode 100644 tests/php/View/SSViewerTest/i18n/en_GB.js create mode 100644 tests/php/View/SSViewerTest/i18n/en_US.js create mode 100644 tests/php/View/SSViewerTest/i18n/fr-ca.js create mode 100644 tests/php/View/SSViewerTest/i18n/fr.js create mode 100644 tests/php/View/SSViewerTest/i18n/fr_CA.js create mode 100644 tests/php/View/SSViewerTest/i18n/mi.js diff --git a/src/View/Requirements_Backend.php b/src/View/Requirements_Backend.php index 272a61988..70bdba384 100644 --- a/src/View/Requirements_Backend.php +++ b/src/View/Requirements_Backend.php @@ -1016,7 +1016,9 @@ class Requirements_Backend ); $candidates = array_map( - function ($candiate) { return $candiate . '.js'; }, + function ($candidate) { + return $candidate . '.js'; + }, $candidates ); diff --git a/tests/php/View/RequirementsTest.php b/tests/php/View/RequirementsTest.php index db4b534fa..f2ccd5902 100644 --- a/tests/php/View/RequirementsTest.php +++ b/tests/php/View/RequirementsTest.php @@ -6,6 +6,7 @@ use InvalidArgumentException; use SilverStripe\Control\Director; use SilverStripe\Core\Injector\Injector; use SilverStripe\Dev\SapphireTest; +use SilverStripe\i18n\i18n; use SilverStripe\View\Requirements; use SilverStripe\View\ArrayData; use Silverstripe\Assets\Dev\TestAssetStore; @@ -1109,4 +1110,77 @@ EOS } return array(); } + + public function testAddI18nJavascript() + { + /** @var Requirements_Backend $backend */ + $backend = Injector::inst()->create(Requirements_Backend::class); + $this->setupRequirements($backend); + $backend->add_i18n_javascript('i18n'); + + $actual = $backend->getJavascript(); + + // English and English US should always be loaded no matter what + $this->assertArrayHasKey('i18n/en.js', $actual); + $this->assertArrayHasKey('i18n/en_US.js', $actual); + $this->assertArrayHasKey('i18n/en-us.js', $actual); + } + + public function testAddI18nJavascriptWithDefaultLocale() + { + i18n::config()->set('default_locale', 'fr_CA'); + + /** @var Requirements_Backend $backend */ + $backend = Injector::inst()->create(Requirements_Backend::class); + $this->setupRequirements($backend); + $backend->add_i18n_javascript('i18n'); + + $actual = $backend->getJavascript(); + + + $this->assertArrayHasKey('i18n/en.js', $actual); + $this->assertArrayHasKey('i18n/en_US.js', $actual); + $this->assertArrayHasKey('i18n/en-us.js', $actual); + // Default locale should be loaded + $this->assertArrayHasKey('i18n/fr.js', $actual); + $this->assertArrayHasKey('i18n/fr_CA.js', $actual); + $this->assertArrayHasKey('i18n/fr-ca.js', $actual); + } + + public function testAddI18nJavascriptWithMemberLocale() + { + i18n::set_locale('en_GB'); + + /** @var Requirements_Backend $backend */ + $backend = Injector::inst()->create(Requirements_Backend::class); + $this->setupRequirements($backend); + $backend->add_i18n_javascript('i18n'); + + $actual = $backend->getJavascript(); + + // The current member's Locale as defined by i18n::get_locale should be loaded + $this->assertArrayHasKey('i18n/en.js', $actual); + $this->assertArrayHasKey('i18n/en_US.js', $actual); + $this->assertArrayHasKey('i18n/en-us.js', $actual); + $this->assertArrayHasKey('i18n/en-gb.js', $actual); + $this->assertArrayHasKey('i18n/en_GB.js', $actual); + } + + public function testAddI18nJavascriptWithMissingLocale() + { + i18n::set_locale('fr_BE'); + + /** @var Requirements_Backend $backend */ + $backend = Injector::inst()->create(Requirements_Backend::class); + $this->setupRequirements($backend); + $backend->add_i18n_javascript('i18n'); + + $actual = $backend->getJavascript(); + + // We don't have a file for French Belgium. Regular french should be loaded anyway. + $this->assertArrayHasKey('i18n/en.js', $actual); + $this->assertArrayHasKey('i18n/en_US.js', $actual); + $this->assertArrayHasKey('i18n/en-us.js', $actual); + $this->assertArrayHasKey('i18n/fr.js', $actual); + } } diff --git a/tests/php/View/SSViewerTest/i18n/en-gb.js b/tests/php/View/SSViewerTest/i18n/en-gb.js new file mode 100644 index 000000000..e69de29bb diff --git a/tests/php/View/SSViewerTest/i18n/en-us.js b/tests/php/View/SSViewerTest/i18n/en-us.js new file mode 100644 index 000000000..e69de29bb diff --git a/tests/php/View/SSViewerTest/i18n/en.js b/tests/php/View/SSViewerTest/i18n/en.js new file mode 100644 index 000000000..e69de29bb diff --git a/tests/php/View/SSViewerTest/i18n/en_GB.js b/tests/php/View/SSViewerTest/i18n/en_GB.js new file mode 100644 index 000000000..e69de29bb diff --git a/tests/php/View/SSViewerTest/i18n/en_US.js b/tests/php/View/SSViewerTest/i18n/en_US.js new file mode 100644 index 000000000..e69de29bb diff --git a/tests/php/View/SSViewerTest/i18n/fr-ca.js b/tests/php/View/SSViewerTest/i18n/fr-ca.js new file mode 100644 index 000000000..e69de29bb diff --git a/tests/php/View/SSViewerTest/i18n/fr.js b/tests/php/View/SSViewerTest/i18n/fr.js new file mode 100644 index 000000000..e69de29bb diff --git a/tests/php/View/SSViewerTest/i18n/fr_CA.js b/tests/php/View/SSViewerTest/i18n/fr_CA.js new file mode 100644 index 000000000..e69de29bb diff --git a/tests/php/View/SSViewerTest/i18n/mi.js b/tests/php/View/SSViewerTest/i18n/mi.js new file mode 100644 index 000000000..e69de29bb