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