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