From ab11fea09897ab7cb546cb890e7dd3dfce941cb3 Mon Sep 17 00:00:00 2001 From: Raphael Saunier Date: Fri, 17 Nov 2017 10:58:19 +0100 Subject: [PATCH 1/4] Fix failing tests by checking for presence of translations as a subset Tests were failing because the return array also contained an unexpected 'translations' key. --- tests/ScopesTest.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/ScopesTest.php b/tests/ScopesTest.php index 5a6c8c6..5f0b1d7 100644 --- a/tests/ScopesTest.php +++ b/tests/ScopesTest.php @@ -53,7 +53,7 @@ public function test_lists_of_translated_fields() 'id' => '1', 'name' => 'Griechenland', ]]; - $this->assertEquals($list, Country::listsTranslations('name')->get()->toArray()); + $this->assertArraySubset($list, Country::listsTranslations('name')->get()->toArray()); } public function test_lists_of_translated_fields_with_fallback() @@ -69,7 +69,7 @@ public function test_lists_of_translated_fields_with_fallback() 'id' => '2', 'name' => 'France', ]]; - $this->assertEquals($list, $country->listsTranslations('name')->get()->toArray()); + $this->assertArraySubset($list, $country->listsTranslations('name')->get()->toArray()); } public function test_scope_withTranslation_without_fallback() From 186958698839ade5cb14be946c4c0104527694a5 Mon Sep 17 00:00:00 2001 From: Raphael Saunier Date: Fri, 17 Nov 2017 11:07:57 +0100 Subject: [PATCH 2/4] Correctly load country-based fallback when calling withTranslation. When using the `withTranslation` query scope along with country-based locales, the generated query didn't include the country fallback (e.g. `de-DE` should first fall back to `de` before using the fallback defined in the configuration). Also fix #241 by generating a WHERE IN clause instead chaining AND/ORs --- src/Translatable/Translatable.php | 8 +++--- tests/ScopesTest.php | 18 ++++++++++++++ tests/TestsBase.php | 3 ++- tests/models/Vegetable.php | 4 ++- tests/seeds/AddFreshSeeds.php | 41 +++++++++++++++++++++++++++++++ 5 files changed, 69 insertions(+), 5 deletions(-) diff --git a/src/Translatable/Translatable.php b/src/Translatable/Translatable.php index 4590084..5631453 100644 --- a/src/Translatable/Translatable.php +++ b/src/Translatable/Translatable.php @@ -578,11 +578,13 @@ public function scopeWithTranslation(Builder $query) { $query->with([ 'translations' => function (Relation $query) { - $query->where($this->getTranslationsTable().'.'.$this->getLocaleKey(), $this->locale()); - if ($this->useFallback()) { - return $query->orWhere($this->getTranslationsTable().'.'.$this->getLocaleKey(), $this->getFallbackLocale()); + $locale = $this->locale(); + $countryFallbackLocale = $this->getFallbackLocale($locale); // e.g. de-DE => de + $locales = array_unique([$locale, $countryFallbackLocale, $this->getFallbackLocale()]); + return $query->whereIn($this->getTranslationsTable() . '.' . $this->getLocaleKey(), $locales); } + $query->where($this->getTranslationsTable().'.'.$this->getLocaleKey(), $this->locale()); }, ]); } diff --git a/tests/ScopesTest.php b/tests/ScopesTest.php index 5f0b1d7..5041be3 100644 --- a/tests/ScopesTest.php +++ b/tests/ScopesTest.php @@ -1,5 +1,6 @@ assertSame('Griechenland', $loadedTranslations[1]['name']); } + public function test_scope_withTranslation_with_country_based_fallback() { + App::make('config')->set('translatable.fallback_locale', 'en'); + App::make('config')->set('translatable.use_fallback', true); + App::setLocale('en-GB'); + $result = Vegetable::withTranslation()->find(1)->toArray(); + $this->assertSame('courgette', $result['name']); + + App::setLocale('de-CH'); + $result = Vegetable::withTranslation()->find(1)->toArray(); + $expectedTranslations = [ + ['name' => 'zucchini', 'locale' => 'en',], + ['name' => 'Zucchini', 'locale' => 'de',], + ['name' => 'Zucchetti', 'locale' => 'de-CH',], + ]; + $this->assertArraySubset($expectedTranslations, $result['translations']); + } + public function test_whereTranslation_filters_by_translation() { /** @var Country $country */ diff --git a/tests/TestsBase.php b/tests/TestsBase.php index c73f6be..ef10001 100644 --- a/tests/TestsBase.php +++ b/tests/TestsBase.php @@ -123,7 +123,8 @@ protected function getEnvironmentSetUp($app) 'collation' => 'utf8_unicode_ci', 'strict' => false, ]); - $app['config']->set('translatable.locales', ['el', 'en', 'fr', 'de', 'id']); + $locales = ['el', 'en', 'fr', 'de', 'id', 'en-GB', 'en-US', 'de-DE', 'de-CH']; + $app['config']->set('translatable.locales', $locales); } protected function getPackageAliases($app) diff --git a/tests/models/Vegetable.php b/tests/models/Vegetable.php index 900feb9..9d13737 100644 --- a/tests/models/Vegetable.php +++ b/tests/models/Vegetable.php @@ -9,7 +9,9 @@ class Vegetable extends Eloquent { use Translatable; - protected $primaryKey = 'vegetable_identity'; + protected $primaryKey = 'identity'; + + protected $translationForeignKey = 'vegetable_identity'; public $translatedAttributes = ['name']; } diff --git a/tests/seeds/AddFreshSeeds.php b/tests/seeds/AddFreshSeeds.php index 08ddf5e..c5e7afe 100644 --- a/tests/seeds/AddFreshSeeds.php +++ b/tests/seeds/AddFreshSeeds.php @@ -2,8 +2,10 @@ use Dimsav\Translatable\Test\Model\City; use Dimsav\Translatable\Test\Model\Country; +use Dimsav\Translatable\Test\Model\Vegetable; use Dimsav\Translatable\Test\Model\CityTranslation; use Dimsav\Translatable\Test\Model\CountryTranslation; +use Dimsav\Translatable\Test\Model\VegetableTranslation; class AddFreshSeeds { @@ -42,6 +44,25 @@ public function run() ]; $this->createCityTranslations($cityTranslations); + + $vegetables = [ + ['vegetable_identity' => 1], + ['vegetable_identity' => 2], + ]; + + $this->createVegetables($vegetables); + + $vegetableTranslations = [ + ['vegetable_identity' => 1, 'locale' => 'en', 'name' => 'zucchini'], + ['vegetable_identity' => 1, 'locale' => 'en-GB', 'name' => 'courgette'], + ['vegetable_identity' => 1, 'locale' => 'en-US', 'name' => 'zucchini'], + ['vegetable_identity' => 1, 'locale' => 'de', 'name' => 'Zucchini'], + ['vegetable_identity' => 1, 'locale' => 'de-CH', 'name' => 'Zucchetti'], + ['vegetable_identity' => 2, 'locale' => 'en', 'name' => 'aubergine'], + ['vegetable_identity' => 2, 'locale' => 'en-US', 'name' => 'eggplant'], + ]; + + $this->createVegetableTranslations($vegetableTranslations); } private function createCountries($countries) @@ -85,4 +106,24 @@ private function createCityTranslations($translations) $translation->save(); } } + + private function createVegetables($vegetables) + { + foreach ($vegetables as $vegetable) { + $vegetable = new Vegetable(); + $vegetable->identity = $vegetable['identity']; + $vegetable->save(); + } + } + + private function createVegetableTranslations($translations) + { + foreach ($translations as $data) { + $translation = new VegetableTranslation(); + $translation->vegetable_identity = $data['vegetable_identity']; + $translation->locale = $data['locale']; + $translation->name = $data['name']; + $translation->save(); + } + } } From 4654036750f0b8c925d326a8b90c0b8c91f95126 Mon Sep 17 00:00:00 2001 From: Raphael Saunier Date: Fri, 17 Nov 2017 11:46:23 +0100 Subject: [PATCH 3/4] StyleCI --- src/Translatable/Translatable.php | 4 ++-- tests/ScopesTest.php | 8 ++++---- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/src/Translatable/Translatable.php b/src/Translatable/Translatable.php index 5631453..2186da2 100644 --- a/src/Translatable/Translatable.php +++ b/src/Translatable/Translatable.php @@ -582,9 +582,9 @@ public function scopeWithTranslation(Builder $query) $locale = $this->locale(); $countryFallbackLocale = $this->getFallbackLocale($locale); // e.g. de-DE => de $locales = array_unique([$locale, $countryFallbackLocale, $this->getFallbackLocale()]); - return $query->whereIn($this->getTranslationsTable() . '.' . $this->getLocaleKey(), $locales); + return $query->whereIn($this->getTranslationsTable().'.'.$this->getLocaleKey(), $locales); } - $query->where($this->getTranslationsTable().'.'.$this->getLocaleKey(), $this->locale()); + return $query->where($this->getTranslationsTable().'.'.$this->getLocaleKey(), $this->locale()); }, ]); } diff --git a/tests/ScopesTest.php b/tests/ScopesTest.php index 5041be3..2c9fb3a 100644 --- a/tests/ScopesTest.php +++ b/tests/ScopesTest.php @@ -1,7 +1,7 @@ find(1)->toArray(); $expectedTranslations = [ - ['name' => 'zucchini', 'locale' => 'en',], - ['name' => 'Zucchini', 'locale' => 'de',], - ['name' => 'Zucchetti', 'locale' => 'de-CH',], + ['name' => 'zucchini', 'locale' => 'en'], + ['name' => 'Zucchini', 'locale' => 'de'], + ['name' => 'Zucchetti', 'locale' => 'de-CH'], ]; $this->assertArraySubset($expectedTranslations, $result['translations']); } From 15625c8b45413121a73f38b16f3d0bb536abc700 Mon Sep 17 00:00:00 2001 From: Raphael Saunier Date: Fri, 17 Nov 2017 11:48:15 +0100 Subject: [PATCH 4/4] StyleCI 2 --- src/Translatable/Translatable.php | 2 ++ tests/ScopesTest.php | 3 ++- 2 files changed, 4 insertions(+), 1 deletion(-) diff --git a/src/Translatable/Translatable.php b/src/Translatable/Translatable.php index 2186da2..41906cf 100644 --- a/src/Translatable/Translatable.php +++ b/src/Translatable/Translatable.php @@ -582,8 +582,10 @@ public function scopeWithTranslation(Builder $query) $locale = $this->locale(); $countryFallbackLocale = $this->getFallbackLocale($locale); // e.g. de-DE => de $locales = array_unique([$locale, $countryFallbackLocale, $this->getFallbackLocale()]); + return $query->whereIn($this->getTranslationsTable().'.'.$this->getLocaleKey(), $locales); } + return $query->where($this->getTranslationsTable().'.'.$this->getLocaleKey(), $this->locale()); }, ]); diff --git a/tests/ScopesTest.php b/tests/ScopesTest.php index 2c9fb3a..20d2aeb 100644 --- a/tests/ScopesTest.php +++ b/tests/ScopesTest.php @@ -93,7 +93,8 @@ public function test_scope_withTranslation_with_fallback() $this->assertSame('Griechenland', $loadedTranslations[1]['name']); } - public function test_scope_withTranslation_with_country_based_fallback() { + public function test_scope_withTranslation_with_country_based_fallback() + { App::make('config')->set('translatable.fallback_locale', 'en'); App::make('config')->set('translatable.use_fallback', true); App::setLocale('en-GB');