1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
use Dimsav\Translatable\Test\Model\Country; |
4
|
|
|
use Dimsav\Translatable\Test\Model\CountryStrict; |
5
|
|
|
use Dimsav\Translatable\Test\Model\CountryWithCustomLocaleKey; |
6
|
|
|
use Dimsav\Translatable\Test\Model\Food; |
7
|
|
|
use Dimsav\Translatable\Test\Model\Person; |
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_suffix_set() |
20
|
|
|
{ |
21
|
|
|
App::make('config')->set('translatable.translation_suffix', 'Trans'); |
22
|
|
|
$country = new Country(); |
23
|
|
|
$this->assertEquals( |
24
|
|
|
'Dimsav\Translatable\Test\Model\CountryTrans', |
25
|
|
|
$country->getTranslationModelName()); |
26
|
|
|
} |
27
|
|
|
|
28
|
|
|
public function test_it_returns_custom_TranslationModelName() |
29
|
|
|
{ |
30
|
|
|
$country = new Country(); |
31
|
|
|
|
32
|
|
|
$this->assertEquals( |
33
|
|
|
$country->getTranslationModelNameDefault(), |
34
|
|
|
$country->getTranslationModelName() |
35
|
|
|
); |
36
|
|
|
|
37
|
|
|
$country->translationModel = 'MyAwesomeCountryTranslation'; |
38
|
|
|
$this->assertEquals( |
39
|
|
|
'MyAwesomeCountryTranslation', |
40
|
|
|
$country->getTranslationModelName() |
41
|
|
|
); |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
public function test_it_returns_relation_key() |
45
|
|
|
{ |
46
|
|
|
$country = new Country(); |
47
|
|
|
$this->assertEquals('country_id', $country->getRelationKey()); |
48
|
|
|
|
49
|
|
|
$country->translationForeignKey = 'my_awesome_key'; |
50
|
|
|
$this->assertEquals('my_awesome_key', $country->getRelationKey()); |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
public function test_it_returns_the_translation() |
54
|
|
|
{ |
55
|
|
|
/** @var Country $country */ |
56
|
|
|
$country = Country::whereCode('gr')->first(); |
57
|
|
|
|
58
|
|
|
$englishTranslation = $country->translate('el'); |
59
|
|
|
$this->assertEquals('Ελλάδα', $englishTranslation->name); |
60
|
|
|
|
61
|
|
|
$englishTranslation = $country->translate('en'); |
62
|
|
|
$this->assertEquals('Greece', $englishTranslation->name); |
63
|
|
|
|
64
|
|
|
$this->app->setLocale('el'); |
65
|
|
|
$englishTranslation = $country->translate(); |
66
|
|
|
$this->assertEquals('Ελλάδα', $englishTranslation->name); |
67
|
|
|
|
68
|
|
|
$this->app->setLocale('en'); |
69
|
|
|
$englishTranslation = $country->translate(); |
70
|
|
|
$this->assertEquals('Greece', $englishTranslation->name); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
public function test_it_returns_the_translation_with_accessor() |
74
|
|
|
{ |
75
|
|
|
/** @var Country $country */ |
76
|
|
|
$country = Country::whereCode('gr')->first(); |
77
|
|
|
|
78
|
|
|
$this->assertEquals('Ελλάδα', $country->{'name:el'}); |
79
|
|
|
$this->assertEquals('Greece', $country->{'name:en'}); |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
public function test_it_returns_null_when_the_locale_doesnt_exist() |
83
|
|
|
{ |
84
|
|
|
/** @var Country $country */ |
85
|
|
|
$country = Country::whereCode('gr')->first(); |
86
|
|
|
|
87
|
|
|
$this->assertSame(null, $country->{'name:unknown-locale'}); |
88
|
|
|
} |
89
|
|
|
|
90
|
|
View Code Duplication |
public function test_it_saves_translations() |
|
|
|
|
91
|
|
|
{ |
92
|
|
|
$country = Country::whereCode('gr')->first(); |
93
|
|
|
|
94
|
|
|
$country->name = '1234'; |
95
|
|
|
$country->save(); |
96
|
|
|
|
97
|
|
|
$country = Country::whereCode('gr')->first(); |
98
|
|
|
$this->assertEquals('1234', $country->name); |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
public function test_it_saves_translations_with_mutator() |
102
|
|
|
{ |
103
|
|
|
$country = Country::whereCode('gr')->first(); |
104
|
|
|
|
105
|
|
|
$country->{'name:en'} = '1234'; |
106
|
|
|
$country->{'name:el'} = '5678'; |
107
|
|
|
$country->save(); |
108
|
|
|
|
109
|
|
|
$country = Country::whereCode('gr')->first(); |
110
|
|
|
|
111
|
|
|
$this->app->setLocale('en'); |
112
|
|
|
$translation = $country->translate(); |
113
|
|
|
$this->assertEquals('1234', $translation->name); |
114
|
|
|
|
115
|
|
|
$this->app->setLocale('el'); |
116
|
|
|
$translation = $country->translate(); |
117
|
|
|
$this->assertEquals('5678', $translation->name); |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
public function test_it_uses_default_locale_to_return_translations() |
121
|
|
|
{ |
122
|
|
|
$country = Country::whereCode('gr')->first(); |
123
|
|
|
|
124
|
|
|
$country->translate('el')->name = 'abcd'; |
125
|
|
|
|
126
|
|
|
$this->app->setLocale('el'); |
127
|
|
|
$this->assertEquals('abcd', $country->name); |
128
|
|
|
$country->save(); |
129
|
|
|
|
130
|
|
|
$country = Country::whereCode('gr')->first(); |
131
|
|
|
$this->assertEquals('abcd', $country->translate('el')->name); |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
public function test_it_creates_translations() |
135
|
|
|
{ |
136
|
|
|
$country = new Country(); |
137
|
|
|
$country->code = 'be'; |
|
|
|
|
138
|
|
|
$country->save(); |
139
|
|
|
|
140
|
|
|
$country = Country::whereCode('be')->first(); |
141
|
|
|
$country->name = 'Belgium'; |
142
|
|
|
$country->save(); |
143
|
|
|
|
144
|
|
|
$country = Country::whereCode('be')->first(); |
145
|
|
|
$this->assertEquals('Belgium', $country->name); |
146
|
|
|
} |
147
|
|
|
|
148
|
|
View Code Duplication |
public function test_it_creates_translations_using_the_shortcut() |
|
|
|
|
149
|
|
|
{ |
150
|
|
|
$country = new Country(); |
151
|
|
|
$country->code = 'be'; |
|
|
|
|
152
|
|
|
$country->name = 'Belgium'; |
|
|
|
|
153
|
|
|
$country->save(); |
154
|
|
|
|
155
|
|
|
$country = Country::whereCode('be')->first(); |
156
|
|
|
$this->assertEquals('Belgium', $country->name); |
157
|
|
|
} |
158
|
|
|
|
159
|
|
|
public function test_it_creates_translations_using_mass_assignment() |
160
|
|
|
{ |
161
|
|
|
$data = [ |
162
|
|
|
'code' => 'be', |
163
|
|
|
'name' => 'Belgium', |
164
|
|
|
]; |
165
|
|
|
$country = Country::create($data); |
166
|
|
|
$this->assertEquals('be', $country->code); |
|
|
|
|
167
|
|
|
$this->assertEquals('Belgium', $country->name); |
|
|
|
|
168
|
|
|
} |
169
|
|
|
|
170
|
|
|
public function test_it_creates_translations_using_mass_assignment_and_locales() |
171
|
|
|
{ |
172
|
|
|
$data = [ |
173
|
|
|
'code' => 'be', |
174
|
|
|
'en' => ['name' => 'Belgium'], |
175
|
|
|
'fr' => ['name' => 'Belgique'], |
176
|
|
|
]; |
177
|
|
|
$country = Country::create($data); |
178
|
|
|
$this->assertEquals('be', $country->code); |
|
|
|
|
179
|
|
|
$this->assertEquals('Belgium', $country->translate('en')->name); |
180
|
|
|
$this->assertEquals('Belgique', $country->translate('fr')->name); |
181
|
|
|
|
182
|
|
|
$country = Country::whereCode('be')->first(); |
183
|
|
|
$this->assertEquals('Belgium', $country->translate('en')->name); |
184
|
|
|
$this->assertEquals('Belgique', $country->translate('fr')->name); |
185
|
|
|
} |
186
|
|
|
|
187
|
|
|
public function test_it_skips_mass_assignment_if_attributes_non_fillable() |
188
|
|
|
{ |
189
|
|
|
$data = [ |
190
|
|
|
'code' => 'be', |
191
|
|
|
'en' => ['name' => 'Belgium'], |
192
|
|
|
'fr' => ['name' => 'Belgique'], |
193
|
|
|
]; |
194
|
|
|
$country = CountryStrict::create($data); |
195
|
|
|
$this->assertEquals('be', $country->code); |
|
|
|
|
196
|
|
|
$this->assertNull($country->translate('en')); |
197
|
|
|
$this->assertNull($country->translate('fr')); |
198
|
|
|
} |
199
|
|
|
|
200
|
|
|
public function test_it_returns_if_object_has_translation() |
201
|
|
|
{ |
202
|
|
|
$country = Country::find(1); |
203
|
|
|
$this->assertTrue($country->hasTranslation('en')); |
204
|
|
|
$this->assertFalse($country->hasTranslation('abc')); |
205
|
|
|
} |
206
|
|
|
|
207
|
|
|
public function test_it_returns_default_translation() |
208
|
|
|
{ |
209
|
|
|
App::make('config')->set('translatable.fallback_locale', 'de'); |
210
|
|
|
|
211
|
|
|
$country = Country::find(1); |
212
|
|
|
$this->assertSame($country->getTranslation('ch', true)->name, 'Griechenland'); |
213
|
|
|
$this->assertSame($country->translateOrDefault('ch')->name, 'Griechenland'); |
214
|
|
|
$this->assertSame($country->getTranslation('ch', false), null); |
215
|
|
|
} |
216
|
|
|
|
217
|
|
|
public function test_fallback_option_in_config_overrides_models_fallback_option() |
218
|
|
|
{ |
219
|
|
|
App::make('config')->set('translatable.fallback_locale', 'de'); |
220
|
|
|
|
221
|
|
|
$country = Country::find(1); |
222
|
|
|
$this->assertEquals($country->getTranslation('ch', true)->locale, 'de'); |
223
|
|
|
|
224
|
|
|
$country->useTranslationFallback = false; |
225
|
|
|
$this->assertEquals($country->getTranslation('ch', true)->locale, 'de'); |
226
|
|
|
|
227
|
|
|
$country->useTranslationFallback = true; |
228
|
|
|
$this->assertEquals($country->getTranslation('ch')->locale, 'de'); |
229
|
|
|
|
230
|
|
|
$country->useTranslationFallback = false; |
231
|
|
|
$this->assertSame($country->getTranslation('ch'), null); |
232
|
|
|
} |
233
|
|
|
|
234
|
|
|
public function test_configuration_defines_if_fallback_is_used() |
235
|
|
|
{ |
236
|
|
|
App::make('config')->set('translatable.fallback_locale', 'de'); |
237
|
|
|
App::make('config')->set('translatable.use_fallback', true); |
238
|
|
|
|
239
|
|
|
$country = Country::find(1); |
240
|
|
|
$this->assertEquals($country->getTranslation('ch')->locale, 'de'); |
241
|
|
|
} |
242
|
|
|
|
243
|
|
|
public function test_useTranslationFallback_overrides_configuration() |
244
|
|
|
{ |
245
|
|
|
App::make('config')->set('translatable.fallback_locale', 'de'); |
246
|
|
|
App::make('config')->set('translatable.use_fallback', true); |
247
|
|
|
$country = Country::find(1); |
248
|
|
|
$country->useTranslationFallback = false; |
249
|
|
|
$this->assertSame($country->getTranslation('ch'), null); |
250
|
|
|
} |
251
|
|
|
|
252
|
|
|
public function test_it_returns_null_if_fallback_is_not_defined() |
253
|
|
|
{ |
254
|
|
|
App::make('config')->set('translatable.fallback_locale', 'ch'); |
255
|
|
|
|
256
|
|
|
$country = Country::find(1); |
257
|
|
|
$this->assertSame($country->getTranslation('pl', true), null); |
258
|
|
|
} |
259
|
|
|
|
260
|
|
|
public function test_it_fills_a_non_default_language_with_fallback_set() |
261
|
|
|
{ |
262
|
|
|
App::make('config')->set('translatable.fallback_locale', 'en'); |
263
|
|
|
|
264
|
|
|
$country = new Country(); |
265
|
|
|
$country->fill([ |
266
|
|
|
'code' => 'gr', |
267
|
|
|
'en' => ['name' => 'Greece'], |
268
|
|
|
'de' => ['name' => 'Griechenland'], |
269
|
|
|
]); |
270
|
|
|
|
271
|
|
|
$this->assertEquals($country->translate('en')->name, 'Greece'); |
272
|
|
|
} |
273
|
|
|
|
274
|
|
|
public function test_it_creates_a_new_translation() |
275
|
|
|
{ |
276
|
|
|
App::make('config')->set('translatable.fallback_locale', 'en'); |
277
|
|
|
|
278
|
|
|
$country = Country::create(['code' => 'gr']); |
279
|
|
|
$country->getNewTranslation('en')->name = 'Greece'; |
280
|
|
|
$country->save(); |
281
|
|
|
|
282
|
|
|
$this->assertEquals($country->translate('en')->name, 'Greece'); |
283
|
|
|
} |
284
|
|
|
|
285
|
|
|
public function test_the_locale_key_is_locale_by_default() |
286
|
|
|
{ |
287
|
|
|
$country = Country::find(1); |
288
|
|
|
$this->assertEquals($country->getLocaleKey(), 'locale'); |
289
|
|
|
} |
290
|
|
|
|
291
|
|
|
public function test_the_locale_key_can_be_overridden_in_configuration() |
292
|
|
|
{ |
293
|
|
|
App::make('config')->set('translatable.locale_key', 'language_id'); |
294
|
|
|
|
295
|
|
|
$country = Country::find(1); |
296
|
|
|
$this->assertEquals($country->getLocaleKey(), 'language_id'); |
297
|
|
|
} |
298
|
|
|
|
299
|
|
|
public function test_the_locale_key_can_be_customized_per_model() |
300
|
|
|
{ |
301
|
|
|
$country = CountryWithCustomLocaleKey::find(1); |
302
|
|
|
$this->assertEquals($country->getLocaleKey(), 'language_id'); |
303
|
|
|
} |
304
|
|
|
|
305
|
|
|
public function test_it_reads_the_configuration() |
306
|
|
|
{ |
307
|
|
|
$this->assertEquals(App::make('config')->get('translatable.translation_suffix'), 'Translation'); |
308
|
|
|
} |
309
|
|
|
|
310
|
|
|
public function test_getting_translation_does_not_create_translation() |
311
|
|
|
{ |
312
|
|
|
$country = Country::with('translations')->find(1); |
|
|
|
|
313
|
|
|
$translation = $country->getTranslation('abc', false); |
314
|
|
|
$this->assertSame($translation, null); |
315
|
|
|
} |
316
|
|
|
|
317
|
|
|
public function test_getting_translated_field_does_not_create_translation() |
318
|
|
|
{ |
319
|
|
|
$this->app->setLocale('en'); |
320
|
|
|
$country = new Country(['code' => 'pl']); |
321
|
|
|
$country->save(); |
322
|
|
|
|
323
|
|
|
$country->name; |
|
|
|
|
324
|
|
|
|
325
|
|
|
$this->assertSame($country->getTranslation('en'), null); |
326
|
|
|
} |
327
|
|
|
|
328
|
|
|
/** |
329
|
|
|
* @expectedException Dimsav\Translatable\Exception\LocalesNotDefinedException |
330
|
|
|
*/ |
331
|
|
|
public function test_if_locales_are_not_defined_throw_exception() |
332
|
|
|
{ |
333
|
|
|
$this->app->config->set('translatable.locales', []); |
|
|
|
|
334
|
|
|
new Country(['code' => 'pl']); |
335
|
|
|
} |
336
|
|
|
|
337
|
|
|
public function test_it_has_methods_that_return_always_a_translation() |
338
|
|
|
{ |
339
|
|
|
$country = Country::find(1)->first(); |
340
|
|
|
$this->assertSame('abc', $country->translateOrNew('abc')->locale); |
341
|
|
|
} |
342
|
|
|
|
343
|
|
|
public function test_configuration_overrides_fillable() |
344
|
|
|
{ |
345
|
|
|
App::make('config')->set('translatable.always_fillable', true); |
346
|
|
|
|
347
|
|
|
$country = new CountryStrict([ |
348
|
|
|
'en' => ['name' => 'Not fillable'], |
349
|
|
|
'code' => 'te', |
350
|
|
|
]); |
351
|
|
|
|
352
|
|
|
$this->assertSame($country->getTranslation('en')->name, 'Not fillable'); |
353
|
|
|
} |
354
|
|
|
|
355
|
|
|
public function test_it_returns_if_attribute_is_translated() |
356
|
|
|
{ |
357
|
|
|
$country = new Country(); |
358
|
|
|
|
359
|
|
|
$this->assertTrue($country->isTranslationAttribute('name')); |
360
|
|
|
$this->assertFalse($country->isTranslationAttribute('some-field')); |
361
|
|
|
} |
362
|
|
|
|
363
|
|
|
public function test_config_overrides_apps_locale() |
364
|
|
|
{ |
365
|
|
|
$country = Country::find(1); |
366
|
|
|
App::make('config')->set('translatable.locale', 'de'); |
367
|
|
|
|
368
|
|
|
$this->assertSame('Griechenland', $country->name); |
369
|
|
|
} |
370
|
|
|
|
371
|
|
|
public function test_locales_as_array_keys_are_properly_detected() |
372
|
|
|
{ |
373
|
|
|
$this->app->config->set('translatable.locales', ['en' => ['US', 'GB']]); |
|
|
|
|
374
|
|
|
|
375
|
|
|
$data = [ |
376
|
|
|
'en' => ['name' => 'French fries'], |
377
|
|
|
'en-US' => ['name' => 'American french fries'], |
378
|
|
|
'en-GB' => ['name' => 'Chips'], |
379
|
|
|
]; |
380
|
|
|
$frenchFries = Food::create($data); |
381
|
|
|
|
382
|
|
|
$this->assertSame('French fries', $frenchFries->getTranslation('en')->name); |
383
|
|
|
$this->assertSame('Chips', $frenchFries->getTranslation('en-GB')->name); |
384
|
|
|
$this->assertSame('American french fries', $frenchFries->getTranslation('en-US')->name); |
385
|
|
|
} |
386
|
|
|
|
387
|
|
|
public function test_locale_separator_can_be_configured() |
388
|
|
|
{ |
389
|
|
|
$this->app->config->set('translatable.locales', ['en' => ['GB']]); |
|
|
|
|
390
|
|
|
$this->app->config->set('translatable.locale_separator', '_'); |
|
|
|
|
391
|
|
|
$data = [ |
392
|
|
|
'en_GB' => ['name' => 'Chips'], |
393
|
|
|
]; |
394
|
|
|
$frenchFries = Food::create($data); |
395
|
|
|
|
396
|
|
|
$this->assertSame('Chips', $frenchFries->getTranslation('en_GB')->name); |
397
|
|
|
} |
398
|
|
|
|
399
|
|
|
public function test_fallback_for_country_based_locales() |
400
|
|
|
{ |
401
|
|
|
$this->app->config->set('translatable.use_fallback', true); |
|
|
|
|
402
|
|
|
$this->app->config->set('translatable.fallback_locale', 'fr'); |
|
|
|
|
403
|
|
|
$this->app->config->set('translatable.locales', ['en' => ['US', 'GB'], 'fr']); |
|
|
|
|
404
|
|
|
$this->app->config->set('translatable.locale_separator', '-'); |
|
|
|
|
405
|
|
|
$data = [ |
406
|
|
|
'id' => 1, |
407
|
|
|
'fr' => ['name' => 'frites'], |
408
|
|
|
'en-GB' => ['name' => 'chips'], |
409
|
|
|
'en' => ['name' => 'french fries'], |
410
|
|
|
]; |
411
|
|
|
Food::create($data); |
412
|
|
|
$fries = Food::find(1); |
413
|
|
|
$this->assertSame('french fries', $fries->getTranslation('en-US')->name); |
414
|
|
|
} |
415
|
|
|
|
416
|
|
|
public function test_to_array_and_fallback_with_country_based_locales_enabled() |
417
|
|
|
{ |
418
|
|
|
$this->app->config->set('translatable.use_fallback', true); |
|
|
|
|
419
|
|
|
$this->app->config->set('translatable.fallback_locale', 'fr'); |
|
|
|
|
420
|
|
|
$this->app->config->set('translatable.locales', ['en' => ['GB'], 'fr']); |
|
|
|
|
421
|
|
|
$this->app->config->set('translatable.locale_separator', '-'); |
|
|
|
|
422
|
|
|
$data = [ |
423
|
|
|
'id' => 1, |
424
|
|
|
'fr' => ['name' => 'frites'], |
425
|
|
|
]; |
426
|
|
|
Food::create($data); |
427
|
|
|
$fritesArray = Food::find(1)->toArray(); |
428
|
|
|
$this->assertSame('frites', $fritesArray['name']); |
429
|
|
|
} |
430
|
|
|
|
431
|
|
|
public function test_it_should_mutate_the_translated_attribute_if_a_mutator_is_set_on_model() |
432
|
|
|
{ |
433
|
|
|
$person = new Person(['name' => 'john doe']); |
434
|
|
|
$person->save(); |
435
|
|
|
$person = Person::find(1); |
436
|
|
|
$this->assertEquals('John doe', $person->name); |
437
|
|
|
} |
438
|
|
|
|
439
|
|
|
public function test_it_deletes_all_translations() |
440
|
|
|
{ |
441
|
|
|
$country = Country::whereCode('gr')->first(); |
442
|
|
|
$this->assertSame(4, count($country->translations)); |
443
|
|
|
|
444
|
|
|
$country->deleteTranslations(); |
445
|
|
|
|
446
|
|
|
$this->assertSame(0, count($country->translations)); |
447
|
|
|
$country = Country::whereCode('gr')->first(); |
448
|
|
|
$this->assertSame(0, count($country->translations)); |
449
|
|
|
} |
450
|
|
|
|
451
|
|
|
public function test_it_deletes_translations_for_given_locales() |
452
|
|
|
{ |
453
|
|
|
$country = Country::whereCode('gr')->with('translations')->first(); |
454
|
|
|
$count = count($country->translations); |
455
|
|
|
|
456
|
|
|
$country->deleteTranslations('fr'); |
457
|
|
|
|
458
|
|
|
$this->assertSame($count - 1, count($country->translations)); |
459
|
|
|
$country = Country::whereCode('gr')->with('translations')->first(); |
460
|
|
|
$this->assertSame($count - 1, count($country->translations)); |
461
|
|
|
$this->assertSame(null, $country->translate('fr')); |
462
|
|
|
} |
463
|
|
|
|
464
|
|
|
public function test_passing_an_empty_array_should_not_delete_translations() |
465
|
|
|
{ |
466
|
|
|
$country = Country::whereCode('gr')->with('translations')->first(); |
467
|
|
|
$count = count($country->translations); |
468
|
|
|
|
469
|
|
|
$country->deleteTranslations([]); |
470
|
|
|
|
471
|
|
|
$country = Country::whereCode('gr')->with('translations')->first(); |
472
|
|
|
$this->assertSame($count, count($country->translations)); |
473
|
|
|
} |
474
|
|
|
} |
475
|
|
|
|
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.