This project does not seem to handle request data directly as such no vulnerable execution paths were found.
include, or for example
via PHP's auto-loading mechanism.
These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more
| 1 | <?php |
||
| 2 | |||
| 3 | use Dimsav\Translatable\Test\Model\Food; |
||
| 4 | use Dimsav\Translatable\Test\Model\Person; |
||
| 5 | use Dimsav\Translatable\Test\Model\Country; |
||
| 6 | use Dimsav\Translatable\Test\Model\CountryStrict; |
||
| 7 | use Dimsav\Translatable\Test\Model\CountryWithCustomLocaleKey; |
||
| 8 | use Dimsav\Translatable\Test\Model\CountryWithCustomTranslationModel; |
||
| 9 | |||
| 10 | class TranslatableTest extends TestsBase |
||
| 11 | { |
||
| 12 | public function test_it_finds_the_default_translation_class() |
||
| 13 | { |
||
| 14 | $country = new Country(); |
||
| 15 | $this->assertEquals( |
||
| 16 | 'Dimsav\Translatable\Test\Model\CountryTranslation', |
||
| 17 | $country->getTranslationModelNameDefault()); |
||
| 18 | } |
||
| 19 | |||
| 20 | public function test_it_finds_the_translation_class_with_namespace_set() |
||
| 21 | { |
||
| 22 | $this->app->make('config')->set('translatable.translation_model_namespace', 'App\Models\Translations'); |
||
| 23 | $country = new Country(); |
||
| 24 | $this->assertEquals( |
||
| 25 | 'App\Models\Translations\CountryTranslation', |
||
| 26 | $country->getTranslationModelNameDefault()); |
||
| 27 | } |
||
| 28 | |||
| 29 | public function test_it_finds_the_translation_class_with_suffix_set() |
||
| 30 | { |
||
| 31 | App::make('config')->set('translatable.translation_suffix', 'Trans'); |
||
| 32 | $country = new Country(); |
||
| 33 | $this->assertEquals( |
||
| 34 | 'Dimsav\Translatable\Test\Model\CountryTrans', |
||
| 35 | $country->getTranslationModelName()); |
||
| 36 | } |
||
| 37 | |||
| 38 | public function test_it_returns_custom_TranslationModelName() |
||
| 39 | { |
||
| 40 | $country = new Country(); |
||
| 41 | |||
| 42 | $this->assertEquals( |
||
| 43 | $country->getTranslationModelNameDefault(), |
||
| 44 | $country->getTranslationModelName() |
||
| 45 | ); |
||
| 46 | |||
| 47 | $country->translationModel = 'MyAwesomeCountryTranslation'; |
||
| 48 | $this->assertEquals( |
||
| 49 | 'MyAwesomeCountryTranslation', |
||
| 50 | $country->getTranslationModelName() |
||
| 51 | ); |
||
| 52 | } |
||
| 53 | |||
| 54 | public function test_it_returns_relation_key() |
||
| 55 | { |
||
| 56 | $country = new Country(); |
||
| 57 | $this->assertEquals('country_id', $country->getRelationKey()); |
||
| 58 | |||
| 59 | $country->translationForeignKey = 'my_awesome_key'; |
||
| 60 | $this->assertEquals('my_awesome_key', $country->getRelationKey()); |
||
| 61 | } |
||
| 62 | |||
| 63 | public function test_it_returns_the_translation() |
||
| 64 | { |
||
| 65 | /** @var Country $country */ |
||
| 66 | $country = Country::whereCode('gr')->first(); |
||
| 67 | |||
| 68 | $englishTranslation = $country->translate('el'); |
||
| 69 | $this->assertEquals('Ελλάδα', $englishTranslation->name); |
||
| 70 | |||
| 71 | $englishTranslation = $country->translate('en'); |
||
| 72 | $this->assertEquals('Greece', $englishTranslation->name); |
||
| 73 | |||
| 74 | $this->app->setLocale('el'); |
||
| 75 | $englishTranslation = $country->translate(); |
||
| 76 | $this->assertEquals('Ελλάδα', $englishTranslation->name); |
||
| 77 | |||
| 78 | $this->app->setLocale('en'); |
||
| 79 | $englishTranslation = $country->translate(); |
||
| 80 | $this->assertEquals('Greece', $englishTranslation->name); |
||
| 81 | } |
||
| 82 | |||
| 83 | public function test_it_returns_the_translation_with_accessor() |
||
| 84 | { |
||
| 85 | /** @var Country $country */ |
||
| 86 | $country = Country::whereCode('gr')->first(); |
||
| 87 | |||
| 88 | $this->assertEquals('Ελλάδα', $country->{'name:el'}); |
||
| 89 | $this->assertEquals('Greece', $country->{'name:en'}); |
||
| 90 | } |
||
| 91 | |||
| 92 | public function test_it_returns_null_when_the_locale_doesnt_exist() |
||
| 93 | { |
||
| 94 | /** @var Country $country */ |
||
| 95 | $country = Country::whereCode('gr')->first(); |
||
| 96 | |||
| 97 | $this->assertSame(null, $country->{'name:unknown-locale'}); |
||
| 98 | } |
||
| 99 | |||
| 100 | View Code Duplication | public function test_it_saves_translations() |
|
| 101 | { |
||
| 102 | $country = Country::whereCode('gr')->first(); |
||
| 103 | |||
| 104 | $country->name = '1234'; |
||
| 105 | $country->save(); |
||
| 106 | |||
| 107 | $country = Country::whereCode('gr')->first(); |
||
| 108 | $this->assertEquals('1234', $country->name); |
||
| 109 | } |
||
| 110 | |||
| 111 | public function test_it_saves_translations_with_mutator() |
||
| 112 | { |
||
| 113 | $country = Country::whereCode('gr')->first(); |
||
| 114 | |||
| 115 | $country->{'name:en'} = '1234'; |
||
| 116 | $country->{'name:el'} = '5678'; |
||
| 117 | $country->save(); |
||
| 118 | |||
| 119 | $country = Country::whereCode('gr')->first(); |
||
| 120 | |||
| 121 | $this->app->setLocale('en'); |
||
| 122 | $translation = $country->translate(); |
||
| 123 | $this->assertEquals('1234', $translation->name); |
||
| 124 | |||
| 125 | $this->app->setLocale('el'); |
||
| 126 | $translation = $country->translate(); |
||
| 127 | $this->assertEquals('5678', $translation->name); |
||
| 128 | } |
||
| 129 | |||
| 130 | public function test_it_does_not_lazy_load_translations_when_updating_non_translated_attributes() |
||
| 131 | { |
||
| 132 | DB::enableQueryLog(); |
||
| 133 | |||
| 134 | $country = Country::create(['code' => 'be']); |
||
| 135 | $this->assertFalse($country->relationLoaded('translations')); |
||
| 136 | $this->assertCount(1, DB::getQueryLog()); |
||
| 137 | |||
| 138 | DB::flushQueryLog(); |
||
| 139 | |||
| 140 | $country->update(['code' => 'de']); |
||
| 141 | $this->assertFalse($country->relationLoaded('translations')); |
||
| 142 | $this->assertCount(1, DB::getQueryLog()); |
||
| 143 | |||
| 144 | DB::flushQueryLog(); |
||
| 145 | |||
| 146 | $country->update(['name' => 'Germany']); |
||
| 147 | $this->assertTrue($country->relationLoaded('translations')); |
||
| 148 | $this->assertCount(2, DB::getQueryLog()); |
||
| 149 | |||
| 150 | DB::disableQueryLog(); |
||
| 151 | } |
||
| 152 | |||
| 153 | public function test_it_uses_default_locale_to_return_translations() |
||
| 154 | { |
||
| 155 | $country = Country::whereCode('gr')->first(); |
||
| 156 | |||
| 157 | $country->translate('el')->name = 'abcd'; |
||
| 158 | |||
| 159 | $this->app->setLocale('el'); |
||
| 160 | $this->assertEquals('abcd', $country->name); |
||
| 161 | $country->save(); |
||
| 162 | |||
| 163 | $country = Country::whereCode('gr')->first(); |
||
| 164 | $this->assertEquals('abcd', $country->translate('el')->name); |
||
| 165 | } |
||
| 166 | |||
| 167 | public function test_it_creates_translations() |
||
| 168 | { |
||
| 169 | $country = new Country(); |
||
| 170 | $country->code = 'be'; |
||
| 171 | $country->save(); |
||
| 172 | |||
| 173 | $country = Country::whereCode('be')->first(); |
||
| 174 | $country->name = 'Belgium'; |
||
| 175 | $country->save(); |
||
| 176 | |||
| 177 | $country = Country::whereCode('be')->first(); |
||
| 178 | $this->assertEquals('Belgium', $country->name); |
||
| 179 | } |
||
| 180 | |||
| 181 | View Code Duplication | public function test_it_creates_translations_using_the_shortcut() |
|
| 182 | { |
||
| 183 | $country = new Country(); |
||
| 184 | $country->code = 'be'; |
||
| 185 | $country->name = 'Belgium'; |
||
| 186 | $country->save(); |
||
| 187 | |||
| 188 | $country = Country::whereCode('be')->first(); |
||
| 189 | $this->assertEquals('Belgium', $country->name); |
||
| 190 | } |
||
| 191 | |||
| 192 | public function test_it_creates_translations_using_mass_assignment() |
||
| 193 | { |
||
| 194 | $data = [ |
||
| 195 | 'code' => 'be', |
||
| 196 | 'name' => 'Belgium', |
||
| 197 | ]; |
||
| 198 | $country = Country::create($data); |
||
| 199 | $this->assertEquals('be', $country->code); |
||
| 200 | $this->assertEquals('Belgium', $country->name); |
||
| 201 | } |
||
| 202 | |||
| 203 | public function test_it_creates_translations_using_mass_assignment_and_locales() |
||
| 204 | { |
||
| 205 | $data = [ |
||
| 206 | 'code' => 'be', |
||
| 207 | 'en' => ['name' => 'Belgium'], |
||
| 208 | 'fr' => ['name' => 'Belgique'], |
||
| 209 | ]; |
||
| 210 | $country = Country::create($data); |
||
| 211 | $this->assertEquals('be', $country->code); |
||
| 212 | $this->assertEquals('Belgium', $country->translate('en')->name); |
||
| 213 | $this->assertEquals('Belgique', $country->translate('fr')->name); |
||
| 214 | |||
| 215 | $country = Country::whereCode('be')->first(); |
||
| 216 | $this->assertEquals('Belgium', $country->translate('en')->name); |
||
| 217 | $this->assertEquals('Belgique', $country->translate('fr')->name); |
||
| 218 | } |
||
| 219 | |||
| 220 | public function test_it_skips_mass_assignment_if_attributes_non_fillable() |
||
| 221 | { |
||
| 222 | $this->expectException(Illuminate\Database\Eloquent\MassAssignmentException::class); |
||
| 223 | $data = [ |
||
| 224 | 'code' => 'be', |
||
| 225 | 'en' => ['name' => 'Belgium'], |
||
| 226 | 'fr' => ['name' => 'Belgique'], |
||
| 227 | ]; |
||
| 228 | $country = CountryStrict::create($data); |
||
| 229 | $this->assertEquals('be', $country->code); |
||
| 230 | $this->assertNull($country->translate('en')); |
||
| 231 | $this->assertNull($country->translate('fr')); |
||
| 232 | } |
||
| 233 | |||
| 234 | public function test_it_returns_if_object_has_translation() |
||
| 235 | { |
||
| 236 | $country = Country::find(1); |
||
| 237 | $this->assertTrue($country->hasTranslation('en')); |
||
| 238 | $this->assertFalse($country->hasTranslation('abc')); |
||
| 239 | } |
||
| 240 | |||
| 241 | public function test_it_returns_default_translation() |
||
| 242 | { |
||
| 243 | App::make('config')->set('translatable.fallback_locale', 'de'); |
||
| 244 | |||
| 245 | $country = Country::find(1); |
||
| 246 | $this->assertSame($country->getTranslation('ch', true)->name, 'Griechenland'); |
||
| 247 | $this->assertSame($country->translateOrDefault('ch')->name, 'Griechenland'); |
||
| 248 | $this->assertSame($country->getTranslation('ch', false), null); |
||
| 249 | |||
| 250 | $this->app->setLocale('ch'); |
||
| 251 | $this->assertSame($country->translateOrDefault()->name, 'Griechenland'); |
||
| 252 | } |
||
| 253 | |||
| 254 | public function test_fallback_option_in_config_overrides_models_fallback_option() |
||
| 255 | { |
||
| 256 | App::make('config')->set('translatable.fallback_locale', 'de'); |
||
| 257 | |||
| 258 | $country = Country::find(1); |
||
| 259 | $this->assertEquals($country->getTranslation('ch', true)->locale, 'de'); |
||
| 260 | |||
| 261 | $country->useTranslationFallback = false; |
||
| 262 | $this->assertEquals($country->getTranslation('ch', true)->locale, 'de'); |
||
| 263 | |||
| 264 | $country->useTranslationFallback = true; |
||
| 265 | $this->assertEquals($country->getTranslation('ch')->locale, 'de'); |
||
| 266 | |||
| 267 | $country->useTranslationFallback = false; |
||
| 268 | $this->assertSame($country->getTranslation('ch'), null); |
||
| 269 | } |
||
| 270 | |||
| 271 | public function test_configuration_defines_if_fallback_is_used() |
||
| 272 | { |
||
| 273 | App::make('config')->set('translatable.fallback_locale', 'de'); |
||
| 274 | App::make('config')->set('translatable.use_fallback', true); |
||
| 275 | |||
| 276 | $country = Country::find(1); |
||
| 277 | $this->assertEquals($country->getTranslation('ch')->locale, 'de'); |
||
| 278 | } |
||
| 279 | |||
| 280 | public function test_useTranslationFallback_overrides_configuration() |
||
| 281 | { |
||
| 282 | App::make('config')->set('translatable.fallback_locale', 'de'); |
||
| 283 | App::make('config')->set('translatable.use_fallback', true); |
||
| 284 | $country = Country::find(1); |
||
| 285 | $country->useTranslationFallback = false; |
||
| 286 | $this->assertSame($country->getTranslation('ch'), null); |
||
| 287 | } |
||
| 288 | |||
| 289 | public function test_it_returns_null_if_fallback_is_not_defined() |
||
| 290 | { |
||
| 291 | App::make('config')->set('translatable.fallback_locale', 'ch'); |
||
| 292 | |||
| 293 | $country = Country::find(1); |
||
| 294 | $this->assertSame($country->getTranslation('pl', true), null); |
||
| 295 | } |
||
| 296 | |||
| 297 | public function test_it_fills_a_non_default_language_with_fallback_set() |
||
| 298 | { |
||
| 299 | App::make('config')->set('translatable.fallback_locale', 'en'); |
||
| 300 | |||
| 301 | $country = new Country(); |
||
| 302 | $country->fill([ |
||
| 303 | 'code' => 'gr', |
||
| 304 | 'en' => ['name' => 'Greece'], |
||
| 305 | 'de' => ['name' => 'Griechenland'], |
||
| 306 | ]); |
||
| 307 | |||
| 308 | $this->assertEquals($country->translate('en')->name, 'Greece'); |
||
| 309 | } |
||
| 310 | |||
| 311 | public function test_it_creates_a_new_translation() |
||
| 312 | { |
||
| 313 | App::make('config')->set('translatable.fallback_locale', 'en'); |
||
| 314 | |||
| 315 | $country = Country::create(['code' => 'gr']); |
||
| 316 | $country->getNewTranslation('en')->name = 'Greece'; |
||
| 317 | $country->save(); |
||
| 318 | |||
| 319 | $this->assertEquals($country->translate('en')->name, 'Greece'); |
||
| 320 | } |
||
| 321 | |||
| 322 | public function test_the_locale_key_is_locale_by_default() |
||
| 323 | { |
||
| 324 | $country = Country::find(1); |
||
| 325 | $this->assertEquals($country->getLocaleKey(), 'locale'); |
||
| 326 | } |
||
| 327 | |||
| 328 | public function test_the_locale_key_can_be_overridden_in_configuration() |
||
| 329 | { |
||
| 330 | App::make('config')->set('translatable.locale_key', 'language_id'); |
||
| 331 | |||
| 332 | $country = Country::find(1); |
||
| 333 | $this->assertEquals($country->getLocaleKey(), 'language_id'); |
||
| 334 | } |
||
| 335 | |||
| 336 | public function test_the_locale_key_can_be_customized_per_model() |
||
| 337 | { |
||
| 338 | $country = CountryWithCustomLocaleKey::find(1); |
||
| 339 | $this->assertEquals($country->getLocaleKey(), 'language_id'); |
||
| 340 | } |
||
| 341 | |||
| 342 | public function test_the_translation_model_can_be_customized() |
||
| 343 | { |
||
| 344 | $country = CountryWithCustomTranslationModel::create([ |
||
| 345 | 'code' => 'es', |
||
| 346 | 'name:en' => 'Spain', |
||
| 347 | 'name:de' => 'Spanien', |
||
| 348 | ]); |
||
| 349 | $this->assertTrue($country->exists()); |
||
| 350 | $this->assertEquals($country->translate('en')->name, 'Spain'); |
||
| 351 | $this->assertEquals($country->translate('de')->name, 'Spanien'); |
||
| 352 | } |
||
| 353 | |||
| 354 | public function test_it_reads_the_configuration() |
||
| 355 | { |
||
| 356 | $this->assertEquals(App::make('config')->get('translatable.translation_suffix'), 'Translation'); |
||
| 357 | } |
||
| 358 | |||
| 359 | public function test_getting_translation_does_not_create_translation() |
||
| 360 | { |
||
| 361 | $country = Country::with('translations')->find(1); |
||
|
0 ignored issues
–
show
|
|||
| 362 | $translation = $country->getTranslation('abc', false); |
||
| 363 | $this->assertSame($translation, null); |
||
| 364 | } |
||
| 365 | |||
| 366 | public function test_getting_translated_field_does_not_create_translation() |
||
| 367 | { |
||
| 368 | $this->app->setLocale('en'); |
||
| 369 | $country = new Country(['code' => 'pl']); |
||
| 370 | $country->save(); |
||
| 371 | |||
| 372 | $country->name; |
||
| 373 | |||
| 374 | $this->assertSame($country->getTranslation('en'), null); |
||
| 375 | } |
||
| 376 | |||
| 377 | public function test_if_locales_are_not_defined_throw_exception() |
||
| 378 | { |
||
| 379 | $this->expectException(Dimsav\Translatable\Exception\LocalesNotDefinedException::class); |
||
| 380 | |||
| 381 | $this->app->config->set('translatable.locales', []); |
||
| 382 | $this->app->make('translatable.locales')->load(); |
||
| 383 | |||
| 384 | new Country(['code' => 'pl']); |
||
| 385 | } |
||
| 386 | |||
| 387 | public function test_it_has_methods_that_return_always_a_translation() |
||
| 388 | { |
||
| 389 | $country = Country::find(1)->first(); |
||
| 390 | $this->assertSame('abc', $country->translateOrNew('abc')->locale); |
||
| 391 | |||
| 392 | $this->app->setLocale('xyz'); |
||
| 393 | $this->assertSame('xyz', $country->translateOrNew()->locale); |
||
| 394 | } |
||
| 395 | |||
| 396 | public function test_it_returns_if_attribute_is_translated() |
||
| 397 | { |
||
| 398 | $country = new Country(); |
||
| 399 | |||
| 400 | $this->assertTrue($country->isTranslationAttribute('name')); |
||
| 401 | $this->assertFalse($country->isTranslationAttribute('some-field')); |
||
| 402 | } |
||
| 403 | |||
| 404 | public function test_config_overrides_apps_locale() |
||
| 405 | { |
||
| 406 | $country = Country::find(1); |
||
| 407 | App::make('config')->set('translatable.locale', 'de'); |
||
| 408 | |||
| 409 | $this->assertSame('Griechenland', $country->name); |
||
| 410 | } |
||
| 411 | |||
| 412 | public function test_locales_as_array_keys_are_properly_detected() |
||
| 413 | { |
||
| 414 | $this->app->config->set('translatable.locales', ['en' => ['US', 'GB']]); |
||
| 415 | |||
| 416 | $data = [ |
||
| 417 | 'en' => ['name' => 'French fries'], |
||
| 418 | 'en-US' => ['name' => 'American french fries'], |
||
| 419 | 'en-GB' => ['name' => 'Chips'], |
||
| 420 | ]; |
||
| 421 | $frenchFries = Food::create($data); |
||
| 422 | |||
| 423 | $this->assertSame('French fries', $frenchFries->getTranslation('en')->name); |
||
| 424 | $this->assertSame('Chips', $frenchFries->getTranslation('en-GB')->name); |
||
| 425 | $this->assertSame('American french fries', $frenchFries->getTranslation('en-US')->name); |
||
| 426 | } |
||
| 427 | |||
| 428 | public function test_locale_separator_can_be_configured() |
||
| 429 | { |
||
| 430 | $this->app->config->set('translatable.locales', ['en' => ['GB']]); |
||
| 431 | $this->app->config->set('translatable.locale_separator', '_'); |
||
| 432 | $this->app->make('translatable.locales')->load(); |
||
| 433 | $data = [ |
||
| 434 | 'en_GB' => ['name' => 'Chips'], |
||
| 435 | ]; |
||
| 436 | $frenchFries = Food::create($data); |
||
| 437 | |||
| 438 | $this->assertSame('Chips', $frenchFries->getTranslation('en_GB')->name); |
||
| 439 | } |
||
| 440 | |||
| 441 | public function test_fallback_for_country_based_locales() |
||
| 442 | { |
||
| 443 | $this->app->config->set('translatable.use_fallback', true); |
||
| 444 | $this->app->config->set('translatable.fallback_locale', 'fr'); |
||
| 445 | $this->app->config->set('translatable.locales', ['en' => ['US', 'GB'], 'fr']); |
||
| 446 | $this->app->config->set('translatable.locale_separator', '-'); |
||
| 447 | $this->app->make('translatable.locales')->load(); |
||
| 448 | $data = [ |
||
| 449 | 'id' => 1, |
||
| 450 | 'fr' => ['name' => 'frites'], |
||
| 451 | 'en-GB' => ['name' => 'chips'], |
||
| 452 | 'en' => ['name' => 'french fries'], |
||
| 453 | ]; |
||
| 454 | Food::create($data); |
||
| 455 | $fries = Food::find(1); |
||
| 456 | $this->assertSame('french fries', $fries->getTranslation('en-US')->name); |
||
| 457 | } |
||
| 458 | |||
| 459 | public function test_fallback_for_country_based_locales_with_no_base_locale() |
||
| 460 | { |
||
| 461 | $this->app->config->set('translatable.use_fallback', true); |
||
| 462 | $this->app->config->set('translatable.fallback_locale', 'en'); |
||
| 463 | $this->app->config->set('translatable.locales', ['pt' => ['PT', 'BR'], 'en']); |
||
| 464 | $this->app->config->set('translatable.locale_separator', '-'); |
||
| 465 | $this->app->make('translatable.locales')->load(); |
||
| 466 | $data = [ |
||
| 467 | 'id' => 1, |
||
| 468 | 'en' => ['name' => 'chips'], |
||
| 469 | 'pt-PT' => ['name' => 'batatas fritas'], |
||
| 470 | ]; |
||
| 471 | Food::create($data); |
||
| 472 | $fries = Food::find(1); |
||
| 473 | $this->assertSame('chips', $fries->getTranslation('pt-BR')->name); |
||
| 474 | } |
||
| 475 | |||
| 476 | public function test_to_array_and_fallback_with_country_based_locales_enabled() |
||
| 477 | { |
||
| 478 | $this->app->config->set('translatable.locale', 'en-GB'); |
||
| 479 | $this->app->config->set('translatable.use_fallback', true); |
||
| 480 | $this->app->config->set('translatable.fallback_locale', 'fr'); |
||
| 481 | $this->app->config->set('translatable.locales', ['en' => ['GB'], 'fr']); |
||
| 482 | $this->app->config->set('translatable.locale_separator', '-'); |
||
| 483 | $this->app->make('translatable.locales')->load(); |
||
| 484 | $data = [ |
||
| 485 | 'id' => 1, |
||
| 486 | 'fr' => ['name' => 'frites'], |
||
| 487 | ]; |
||
| 488 | Food::create($data); |
||
| 489 | $fritesArray = Food::find(1)->toArray(); |
||
| 490 | $this->assertSame('frites', $fritesArray['name']); |
||
| 491 | } |
||
| 492 | |||
| 493 | public function test_it_skips_translations_in_to_array_when_config_is_set() |
||
| 494 | { |
||
| 495 | $this->app->config->set('translatable.to_array_always_loads_translations', false); |
||
| 496 | $greece = Country::whereCode('gr')->first()->toArray(); |
||
| 497 | $this->assertFalse(isset($greece['name'])); |
||
| 498 | } |
||
| 499 | |||
| 500 | public function test_it_returns_translations_in_to_array_when_config_is_set_but_translations_are_loaded() |
||
| 501 | { |
||
| 502 | $this->app->config->set('translatable.to_array_always_loads_translations', false); |
||
| 503 | $greece = Country::whereCode('gr')->with('translations')->first()->toArray(); |
||
| 504 | $this->assertTrue(isset($greece['name'])); |
||
| 505 | } |
||
| 506 | |||
| 507 | public function test_it_should_mutate_the_translated_attribute_if_a_mutator_is_set_on_model() |
||
| 508 | { |
||
| 509 | $person = new Person(['name' => 'john doe']); |
||
| 510 | $person->save(); |
||
| 511 | $person = Person::find(1); |
||
| 512 | $this->assertEquals('John doe', $person->name); |
||
| 513 | } |
||
| 514 | |||
| 515 | public function test_it_deletes_all_translations() |
||
| 516 | { |
||
| 517 | $country = Country::whereCode('gr')->first(); |
||
| 518 | $this->assertSame(4, count($country->translations)); |
||
| 519 | |||
| 520 | $country->deleteTranslations(); |
||
| 521 | |||
| 522 | $this->assertSame(0, count($country->translations)); |
||
| 523 | $country = Country::whereCode('gr')->first(); |
||
| 524 | $this->assertSame(0, count($country->translations)); |
||
| 525 | } |
||
| 526 | |||
| 527 | public function test_it_deletes_translations_for_given_locales() |
||
| 528 | { |
||
| 529 | $country = Country::whereCode('gr')->with('translations')->first(); |
||
| 530 | $count = count($country->translations); |
||
| 531 | |||
| 532 | $country->deleteTranslations('fr'); |
||
| 533 | |||
| 534 | $this->assertSame($count - 1, count($country->translations)); |
||
| 535 | $country = Country::whereCode('gr')->with('translations')->first(); |
||
| 536 | $this->assertSame($count - 1, count($country->translations)); |
||
| 537 | $this->assertSame(null, $country->translate('fr')); |
||
| 538 | } |
||
| 539 | |||
| 540 | public function test_passing_an_empty_array_should_not_delete_translations() |
||
| 541 | { |
||
| 542 | $country = Country::whereCode('gr')->with('translations')->first(); |
||
| 543 | $count = count($country->translations); |
||
| 544 | |||
| 545 | $country->deleteTranslations([]); |
||
| 546 | |||
| 547 | $country = Country::whereCode('gr')->with('translations')->first(); |
||
| 548 | $this->assertSame($count, count($country->translations)); |
||
| 549 | } |
||
| 550 | |||
| 551 | public function test_fill_with_translation_key() |
||
| 552 | { |
||
| 553 | $country = new Country(); |
||
| 554 | $country->fill([ |
||
| 555 | 'code' => 'tr', |
||
| 556 | 'name:en' => 'Turkey', |
||
| 557 | 'name:de' => 'Türkei', |
||
| 558 | ]); |
||
| 559 | $this->assertEquals($country->translate('en')->name, 'Turkey'); |
||
| 560 | $this->assertEquals($country->translate('de')->name, 'Türkei'); |
||
| 561 | |||
| 562 | $country->save(); |
||
| 563 | $country = Country::whereCode('tr')->first(); |
||
| 564 | $this->assertEquals($country->translate('en')->name, 'Turkey'); |
||
| 565 | $this->assertEquals($country->translate('de')->name, 'Türkei'); |
||
| 566 | } |
||
| 567 | |||
| 568 | public function test_it_uses_the_default_locale_from_the_model() |
||
| 569 | { |
||
| 570 | $country = new Country(); |
||
| 571 | $country->fill([ |
||
| 572 | 'code' => 'tn', |
||
| 573 | 'name:en' => 'Tunisia', |
||
| 574 | 'name:fr' => 'Tunisie', |
||
| 575 | ]); |
||
| 576 | $this->assertEquals($country->name, 'Tunisia'); |
||
| 577 | $country->setDefaultLocale('fr'); |
||
| 578 | $this->assertEquals($country->name, 'Tunisie'); |
||
| 579 | |||
| 580 | $country->setDefaultLocale(null); |
||
| 581 | $country->save(); |
||
| 582 | $country = Country::whereCode('tn')->first(); |
||
| 583 | $this->assertEquals($country->name, 'Tunisia'); |
||
| 584 | $country->setDefaultLocale('fr'); |
||
| 585 | $this->assertEquals($country->name, 'Tunisie'); |
||
| 586 | } |
||
| 587 | |||
| 588 | public function test_replicate_entity() |
||
| 589 | { |
||
| 590 | $apple = new Food(); |
||
| 591 | $apple->fill([ |
||
| 592 | 'name:fr' => 'Pomme', |
||
| 593 | 'name:en' => 'Apple', |
||
| 594 | 'name:de' => 'Apfel', |
||
| 595 | ]); |
||
| 596 | $apple->save(); |
||
| 597 | |||
| 598 | $replicatedApple = $apple->replicateWithTranslations(); |
||
| 599 | $this->assertNotSame($replicatedApple->id, $apple->id); |
||
| 600 | $this->assertEquals($replicatedApple->translate('fr')->name, $apple->translate('fr')->name); |
||
| 601 | $this->assertEquals($replicatedApple->translate('en')->name, $apple->translate('en')->name); |
||
| 602 | $this->assertEquals($replicatedApple->translate('de')->name, $apple->translate('de')->name); |
||
| 603 | } |
||
| 604 | |||
| 605 | public function test_getTranslationsArray() |
||
| 606 | { |
||
| 607 | Country::create([ |
||
| 608 | 'code' => 'tn', |
||
| 609 | 'name:en' => 'Tunisia', |
||
| 610 | 'name:fr' => 'Tunisie', |
||
| 611 | 'name:de' => 'Tunesien', |
||
| 612 | ]); |
||
| 613 | |||
| 614 | /** @var Country $country */ |
||
| 615 | $country = Country::where('code', 'tn')->first(); |
||
| 616 | |||
| 617 | $this->assertSame([ |
||
| 618 | 'de' => ['name' => 'Tunesien'], |
||
| 619 | 'en' => ['name' => 'Tunisia'], |
||
| 620 | 'fr' => ['name' => 'Tunisie'], |
||
| 621 | ], $country->getTranslationsArray()); |
||
| 622 | } |
||
| 623 | |||
| 624 | public function test_fill_when_locale_key_unknown() |
||
| 625 | { |
||
| 626 | config(['translatable.locales' => ['en']]); |
||
| 627 | |||
| 628 | $country = new Country(); |
||
| 629 | $country->fill([ |
||
| 630 | 'code' => 'ua', |
||
| 631 | 'en' => ['name' => 'Ukraine'], |
||
| 632 | 'ua' => ['name' => 'Україна'], // "ua" is unknown, so must be ignored |
||
| 633 | ]); |
||
| 634 | |||
| 635 | $modelTranslations = []; |
||
| 636 | |||
| 637 | View Code Duplication | foreach ($country->translations as $translation) { |
|
| 638 | foreach ($country->translatedAttributes as $attr) { |
||
| 639 | $modelTranslations[$translation->locale][$attr] = $translation->{$attr}; |
||
| 640 | } |
||
| 641 | } |
||
| 642 | |||
| 643 | $expectedTranslations = [ |
||
| 644 | 'en' => ['name' => 'Ukraine'], |
||
| 645 | ]; |
||
| 646 | |||
| 647 | $this->assertEquals($modelTranslations, $expectedTranslations); |
||
| 648 | } |
||
| 649 | |||
| 650 | public function test_fill_with_translation_key_when_locale_key_unknown() |
||
| 651 | { |
||
| 652 | config(['translatable.locales' => ['en']]); |
||
| 653 | |||
| 654 | $country = new Country(); |
||
| 655 | $country->fill([ |
||
| 656 | 'code' => 'ua', |
||
| 657 | 'name:en' => 'Ukraine', |
||
| 658 | 'name:ua' => 'Україна', // "ua" is unknown, so must be ignored |
||
| 659 | ]); |
||
| 660 | |||
| 661 | $modelTranslations = []; |
||
| 662 | |||
| 663 | View Code Duplication | foreach ($country->translations as $translation) { |
|
| 664 | foreach ($country->translatedAttributes as $attr) { |
||
| 665 | $modelTranslations[$translation->locale][$attr] = $translation->{$attr}; |
||
| 666 | } |
||
| 667 | } |
||
| 668 | |||
| 669 | $expectedTranslations = [ |
||
| 670 | 'en' => ['name' => 'Ukraine'], |
||
| 671 | ]; |
||
| 672 | |||
| 673 | $this->assertEquals($modelTranslations, $expectedTranslations); |
||
| 674 | } |
||
| 675 | |||
| 676 | public function test_it_uses_fallback_locale_if_default_is_empty() |
||
| 677 | { |
||
| 678 | App::make('config')->set('translatable.use_fallback', true); |
||
| 679 | App::make('config')->set('translatable.use_property_fallback', true); |
||
| 680 | App::make('config')->set('translatable.fallback_locale', 'en'); |
||
| 681 | $country = new Country(); |
||
| 682 | $country->fill([ |
||
| 683 | 'code' => 'tn', |
||
| 684 | 'name:en' => 'Tunisia', |
||
| 685 | 'name:fr' => '', |
||
| 686 | ]); |
||
| 687 | $this->app->setLocale('en'); |
||
| 688 | $this->assertEquals('Tunisia', $country->name); |
||
| 689 | $this->app->setLocale('fr'); |
||
| 690 | $this->assertEquals('Tunisia', $country->name); |
||
| 691 | } |
||
| 692 | |||
| 693 | public function test_it_always_uses_value_when_fallback_not_available() |
||
| 694 | { |
||
| 695 | App::make('config')->set('translatable.fallback_locale', 'it'); |
||
| 696 | App::make('config')->set('translatable.use_fallback', true); |
||
| 697 | |||
| 698 | $country = new Country(); |
||
| 699 | $country->fill([ |
||
| 700 | 'code' => 'gr', |
||
| 701 | 'en' => ['name' => ''], |
||
| 702 | 'de' => ['name' => 'Griechenland'], |
||
| 703 | ]); |
||
| 704 | |||
| 705 | // verify translated attributed is correctly returned when empty (non-existing fallback is ignored) |
||
| 706 | $this->app->setLocale('en'); |
||
| 707 | $this->assertEquals('', $country->getAttribute('name')); |
||
| 708 | |||
| 709 | $this->app->setLocale('de'); |
||
| 710 | $this->assertEquals('Griechenland', $country->getAttribute('name')); |
||
| 711 | } |
||
| 712 | |||
| 713 | public function test_translation_with_multiconnection() |
||
| 714 | { |
||
| 715 | $this->migrate('mysql2'); |
||
| 716 | |||
| 717 | // Add country & translation in second db |
||
| 718 | $country = new Country(); |
||
| 719 | $country->setConnection('mysql2'); |
||
| 720 | $country->code = 'sg'; |
||
| 721 | $country->{'name:sg'} = 'Singapore'; |
||
| 722 | $this->assertTrue($country->save()); |
||
| 723 | |||
| 724 | $countryId = $country->id; |
||
| 725 | |||
| 726 | // Verify added country & translation in second db |
||
| 727 | $country = new Country(); |
||
| 728 | $country->setConnection('mysql2'); |
||
| 729 | $sgCountry2 = $country->newQuery()->find($countryId); |
||
| 730 | $this->assertEquals('Singapore', $sgCountry2->translate('sg')->name); |
||
| 731 | |||
| 732 | // Verify added country not in default db |
||
| 733 | $country = new Country(); |
||
| 734 | $country->setConnection('mysql'); |
||
| 735 | $sgCountry1 = $country->newQuery()->where('code', 'sg')->get(); |
||
| 736 | $this->assertEmpty($sgCountry1); |
||
| 737 | |||
| 738 | // Verify added translation not in default db |
||
| 739 | $country = new Country(); |
||
| 740 | $country->setConnection('mysql'); |
||
| 741 | $sgCountry1 = $country->newQuery()->find($countryId); |
||
| 742 | $this->assertEmpty($sgCountry1->translate('sg')); |
||
| 743 | } |
||
| 744 | |||
| 745 | public function test_empty_translated_attribute() |
||
| 746 | { |
||
| 747 | $country = Country::whereCode('gr')->first(); |
||
| 748 | $this->app->setLocale('invalid'); |
||
| 749 | $this->assertSame(null, $country->name); |
||
| 750 | } |
||
| 751 | |||
| 752 | public function test_numeric_translated_attribute() |
||
| 753 | { |
||
| 754 | $this->app->config->set('translatable.fallback_locale', 'de'); |
||
| 755 | $this->app->config->set('translatable.use_fallback', true); |
||
| 756 | |||
| 757 | $city = new class extends \Dimsav\Translatable\Test\Model\City { |
||
| 758 | protected $fillable = [ |
||
| 759 | 'country_id', |
||
| 760 | ]; |
||
| 761 | protected $table = 'cities'; |
||
| 762 | public $translationModel = \Dimsav\Translatable\Test\Model\CityTranslation::class; |
||
| 763 | public $translationForeignKey = 'city_id'; |
||
| 764 | |||
| 765 | protected function isEmptyTranslatableAttribute(string $key, $value): bool |
||
| 766 | { |
||
| 767 | if ($key === 'name') { |
||
| 768 | return is_null($value); |
||
| 769 | } |
||
| 770 | |||
| 771 | return empty($value); |
||
| 772 | } |
||
| 773 | }; |
||
| 774 | $city->fill([ |
||
| 775 | 'country_id' => Country::first()->getKey(), |
||
| 776 | 'en' => ['name' => '0'], |
||
| 777 | 'de' => ['name' => '1'], |
||
| 778 | 'fr' => ['name' => null], |
||
| 779 | ]); |
||
| 780 | $city->save(); |
||
| 781 | |||
| 782 | $this->app->setLocale('en'); |
||
| 783 | $this->assertSame('0', $city->name); |
||
| 784 | |||
| 785 | $this->app->setLocale('fr'); |
||
| 786 | $this->assertSame('1', $city->name); |
||
| 787 | } |
||
| 788 | } |
||
| 789 |
It seems like the method you are trying to call exists only in some of the possible types.
Let’s take a look at an example:
Available Fixes
Add an additional type-check:
Only allow a single type to be passed if the variable comes from a parameter: