1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
use Dimsav\Translatable\Test\Model\Country; |
4
|
|
|
use Dimsav\Translatable\Test\Model\Vegetable; |
5
|
|
|
|
6
|
|
|
class ScopesTest extends TestsBase |
7
|
|
|
{ |
8
|
|
|
public function test_translated_in_scope_returns_only_translated_records_for_this_locale() |
9
|
|
|
{ |
10
|
|
|
$translatedCountries = Country::translatedIn('fr')->get(); |
11
|
|
|
$this->assertEquals($translatedCountries->count(), 1); |
12
|
|
|
} |
13
|
|
|
|
14
|
|
|
public function test_translated_in_scope_works_with_default_locale() |
15
|
|
|
{ |
16
|
|
|
App::setLocale('de'); |
17
|
|
|
$translatedCountries = Country::translatedIn()->get(); |
18
|
|
|
|
19
|
|
|
$this->assertSame($translatedCountries->count(), 1); |
20
|
|
|
$this->assertSame('Griechenland', $translatedCountries->first()->name); |
21
|
|
|
} |
22
|
|
|
|
23
|
|
View Code Duplication |
public function test_not_translated_in_scope_returns_only_not_translated_records_for_this_locale() |
|
|
|
|
24
|
|
|
{ |
25
|
|
|
$notTranslatedCountries = Country::notTranslatedIn('en')->get(); |
26
|
|
|
$this->assertCount(2, $notTranslatedCountries); |
27
|
|
|
|
28
|
|
|
foreach ($notTranslatedCountries as $notTranslatedCountry) { |
29
|
|
|
$this->assertFalse($notTranslatedCountry->hasTranslation('en')); |
30
|
|
|
} |
31
|
|
|
} |
32
|
|
|
|
33
|
|
View Code Duplication |
public function test_not_translated_in_scope_works_with_default_locale() |
|
|
|
|
34
|
|
|
{ |
35
|
|
|
App::setLocale('en'); |
36
|
|
|
$notTranslatedCountries = Country::notTranslatedIn()->get(); |
37
|
|
|
$this->assertCount(2, $notTranslatedCountries); |
38
|
|
|
|
39
|
|
|
foreach ($notTranslatedCountries as $notTranslatedCountry) { |
40
|
|
|
$this->assertFalse($notTranslatedCountry->hasTranslation('en')); |
41
|
|
|
} |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
public function test_translated_scope_returns_records_with_at_least_one_translation() |
45
|
|
|
{ |
46
|
|
|
$translatedCountries = Country::translated()->get(); |
47
|
|
|
$this->assertEquals($translatedCountries->count(), 2); |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
public function test_lists_of_translated_fields() |
51
|
|
|
{ |
52
|
|
|
App::setLocale('de'); |
53
|
|
|
App::make('config')->set('translatable.to_array_always_loads_translations', false); |
54
|
|
|
|
55
|
|
|
$arr = Country::listsTranslations('name')->get()->toArray(); |
56
|
|
|
|
57
|
|
|
$this->assertEquals(1, count($arr)); |
58
|
|
|
$this->assertEquals('1', $arr[0]['id']); |
59
|
|
|
$this->assertEquals('Griechenland', $arr[0]['name']); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
public function test_lists_of_translated_fields_with_fallback() |
63
|
|
|
{ |
64
|
|
|
App::make('config')->set('translatable.fallback_locale', 'en'); |
65
|
|
|
App::make('config')->set('translatable.to_array_always_loads_translations', false); |
66
|
|
|
App::setLocale('de'); |
67
|
|
|
$country = new Country(); |
68
|
|
|
$country->useTranslationFallback = true; |
69
|
|
|
$list = [[ |
|
|
|
|
70
|
|
|
'id' => 1, |
71
|
|
|
'name' => 'Griechenland', |
72
|
|
|
], [ |
73
|
|
|
'id' => 2, |
74
|
|
|
'name' => 'France', |
75
|
|
|
]]; |
76
|
|
|
|
77
|
|
|
$arr = $country->listsTranslations('name')->get()->toArray(); |
78
|
|
|
|
79
|
|
|
$this->assertEquals(2, count($arr)); |
80
|
|
|
|
81
|
|
|
$this->assertEquals(1, $arr[0]['id']); |
82
|
|
|
$this->assertEquals('Griechenland', $arr[0]['name']); |
83
|
|
|
|
84
|
|
|
$this->assertEquals(2, $arr[1]['id']); |
85
|
|
|
$this->assertEquals('France', $arr[1]['name']); |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
public function test_lists_of_translated_fields_disable_autoload_translations() |
89
|
|
|
{ |
90
|
|
|
App::setLocale('de'); |
91
|
|
|
App::make('config')->set('translatable.to_array_always_loads_translations', true); |
92
|
|
|
|
93
|
|
|
$list = [[ |
94
|
|
|
'id' => 1, |
95
|
|
|
'name' => 'Griechenland', |
96
|
|
|
]]; |
97
|
|
|
Country::disableAutoloadTranslations(); |
98
|
|
|
$this->assertEquals($list, Country::listsTranslations('name')->get()->toArray()); |
99
|
|
|
Country::defaultAutoloadTranslations(); |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
public function test_scope_withTranslation_without_fallback() |
103
|
|
|
{ |
104
|
|
|
$result = Country::withTranslation()->first(); |
105
|
|
|
$loadedTranslations = $result->toArray()['translations']; |
106
|
|
|
$this->assertCount(1, $loadedTranslations); |
107
|
|
|
$this->assertSame('Greece', $loadedTranslations[0]['name']); |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
public function test_scope_withTranslation_with_fallback() |
111
|
|
|
{ |
112
|
|
|
App::make('config')->set('translatable.fallback_locale', 'de'); |
113
|
|
|
App::make('config')->set('translatable.use_fallback', true); |
114
|
|
|
|
115
|
|
|
$result = Country::withTranslation()->first(); |
116
|
|
|
$loadedTranslations = $result->toArray()['translations']; |
117
|
|
|
$this->assertCount(2, $loadedTranslations); |
118
|
|
|
$this->assertSame('Greece', $loadedTranslations[0]['name']); |
119
|
|
|
$this->assertSame('Griechenland', $loadedTranslations[1]['name']); |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
public function test_scope_withTranslation_with_country_based_fallback() |
123
|
|
|
{ |
124
|
|
|
App::make('config')->set('translatable.fallback_locale', 'en'); |
125
|
|
|
App::make('config')->set('translatable.use_fallback', true); |
126
|
|
|
App::setLocale('en-GB'); |
127
|
|
|
$result = Vegetable::withTranslation()->find(1)->toArray(); |
128
|
|
|
$this->assertSame('courgette', $result['name']); |
129
|
|
|
|
130
|
|
|
App::setLocale('de-CH'); |
131
|
|
|
$result = Vegetable::withTranslation()->find(1)->toArray(); |
132
|
|
|
$expectedTranslations = [ |
|
|
|
|
133
|
|
|
['name' => 'zucchini', 'locale' => 'en'], |
134
|
|
|
['name' => 'Zucchini', 'locale' => 'de'], |
135
|
|
|
['name' => 'Zucchetti', 'locale' => 'de-CH'], |
136
|
|
|
]; |
137
|
|
|
$translations = $result['translations']; |
138
|
|
|
|
139
|
|
|
$this->assertEquals(3, count($translations)); |
140
|
|
|
|
141
|
|
|
$this->assertEquals('en', $translations[0]['locale']); |
142
|
|
|
$this->assertEquals('zucchini', $translations[0]['name']); |
143
|
|
|
|
144
|
|
|
$this->assertEquals('de', $translations[1]['locale']); |
145
|
|
|
$this->assertEquals('Zucchini', $translations[1]['name']); |
146
|
|
|
|
147
|
|
|
$this->assertEquals('de-CH', $translations[2]['locale']); |
148
|
|
|
$this->assertEquals('Zucchetti', $translations[2]['name']); |
149
|
|
|
} |
150
|
|
|
|
151
|
|
|
public function test_whereTranslation_filters_by_translation() |
152
|
|
|
{ |
153
|
|
|
/** @var Country $country */ |
154
|
|
|
$country = Country::whereTranslation('name', 'Greece')->first(); |
155
|
|
|
$this->assertSame('gr', $country->code); |
156
|
|
|
} |
157
|
|
|
|
158
|
|
View Code Duplication |
public function test_orWhereTranslation_filters_by_translation() |
|
|
|
|
159
|
|
|
{ |
160
|
|
|
$result = Country::whereTranslation('name', 'Greece')->orWhereTranslation('name', 'France')->get(); |
161
|
|
|
$this->assertCount(2, $result); |
162
|
|
|
$this->assertSame('Greece', $result[0]->name); |
163
|
|
|
$this->assertSame('France', $result[1]->name); |
164
|
|
|
} |
165
|
|
|
|
166
|
|
View Code Duplication |
public function test_whereTranslation_filters_by_translation_and_locale() |
|
|
|
|
167
|
|
|
{ |
168
|
|
|
Country::create(['code' => 'some-code', 'name' => 'Griechenland']); |
169
|
|
|
|
170
|
|
|
$this->assertSame(2, Country::whereTranslation('name', 'Griechenland')->count()); |
171
|
|
|
|
172
|
|
|
$result = Country::whereTranslation('name', 'Griechenland', 'de')->get(); |
173
|
|
|
$this->assertSame(1, $result->count()); |
174
|
|
|
$this->assertSame('gr', $result->first()->code); |
175
|
|
|
} |
176
|
|
|
|
177
|
|
|
public function test_whereTranslationLike_filters_by_translation() |
178
|
|
|
{ |
179
|
|
|
/** @var Country $country */ |
180
|
|
|
$country = Country::whereTranslationLike('name', '%Greec%')->first(); |
181
|
|
|
$this->assertSame('gr', $country->code); |
182
|
|
|
} |
183
|
|
|
|
184
|
|
View Code Duplication |
public function test_orWhereTranslationLike_filters_by_translation() |
|
|
|
|
185
|
|
|
{ |
186
|
|
|
$result = Country::whereTranslationLike('name', '%eece%')->orWhereTranslationLike('name', '%ance%')->get(); |
187
|
|
|
$this->assertCount(2, $result); |
188
|
|
|
$this->assertSame('Greece', $result[0]->name); |
189
|
|
|
$this->assertSame('France', $result[1]->name); |
190
|
|
|
} |
191
|
|
|
|
192
|
|
View Code Duplication |
public function test_whereTranslationLike_filters_by_translation_and_locale() |
|
|
|
|
193
|
|
|
{ |
194
|
|
|
Country::create(['code' => 'some-code', 'name' => 'Griechenland']); |
195
|
|
|
|
196
|
|
|
$this->assertSame(2, Country::whereTranslationLike('name', 'Griechen%')->count()); |
197
|
|
|
|
198
|
|
|
$result = Country::whereTranslationLike('name', '%riechenlan%', 'de')->get(); |
199
|
|
|
$this->assertSame(1, $result->count()); |
200
|
|
|
$this->assertSame('gr', $result->first()->code); |
201
|
|
|
} |
202
|
|
|
|
203
|
|
|
public function test_orderByTranslation_sorts_by_key_asc() |
204
|
|
|
{ |
205
|
|
|
$result = Country::orderByTranslation('name')->get(); |
206
|
|
|
$this->assertSame(2, $result->first()->id); |
207
|
|
|
} |
208
|
|
|
|
209
|
|
|
public function test_orderByTranslation_sorts_by_key_desc() |
210
|
|
|
{ |
211
|
|
|
$result = Country::orderByTranslation('name', 'desc')->get(); |
212
|
|
|
$this->assertSame(1, $result->first()->id); |
213
|
|
|
} |
214
|
|
|
} |
215
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.