|
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
|
|
|
$list = [[ |
|
56
|
|
|
'id' => '1', |
|
57
|
|
|
'name' => 'Griechenland', |
|
58
|
|
|
]]; |
|
59
|
|
|
$this->assertArraySubset($list, Country::listsTranslations('name')->get()->toArray()); |
|
|
|
|
|
|
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
|
|
|
$this->assertArraySubset($list, $country->listsTranslations('name')->get()->toArray()); |
|
|
|
|
|
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
public function test_scope_withTranslation_without_fallback() |
|
80
|
|
|
{ |
|
81
|
|
|
$result = Country::withTranslation()->first(); |
|
|
|
|
|
|
82
|
|
|
$loadedTranslations = $result->toArray()['translations']; |
|
83
|
|
|
$this->assertCount(1, $loadedTranslations); |
|
84
|
|
|
$this->assertSame('Greece', $loadedTranslations[0]['name']); |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
|
public function test_scope_withTranslation_with_fallback() |
|
88
|
|
|
{ |
|
89
|
|
|
App::make('config')->set('translatable.fallback_locale', 'de'); |
|
90
|
|
|
App::make('config')->set('translatable.use_fallback', true); |
|
91
|
|
|
|
|
92
|
|
|
$result = Country::withTranslation()->first(); |
|
|
|
|
|
|
93
|
|
|
$loadedTranslations = $result->toArray()['translations']; |
|
94
|
|
|
$this->assertCount(2, $loadedTranslations); |
|
95
|
|
|
$this->assertSame('Greece', $loadedTranslations[0]['name']); |
|
96
|
|
|
$this->assertSame('Griechenland', $loadedTranslations[1]['name']); |
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
|
|
public function test_scope_withTranslation_with_country_based_fallback() |
|
100
|
|
|
{ |
|
101
|
|
|
App::make('config')->set('translatable.fallback_locale', 'en'); |
|
102
|
|
|
App::make('config')->set('translatable.use_fallback', true); |
|
103
|
|
|
App::setLocale('en-GB'); |
|
104
|
|
|
$result = Vegetable::withTranslation()->find(1)->toArray(); |
|
|
|
|
|
|
105
|
|
|
$this->assertSame('courgette', $result['name']); |
|
106
|
|
|
|
|
107
|
|
|
App::setLocale('de-CH'); |
|
108
|
|
|
$result = Vegetable::withTranslation()->find(1)->toArray(); |
|
|
|
|
|
|
109
|
|
|
$expectedTranslations = [ |
|
110
|
|
|
['name' => 'zucchini', 'locale' => 'en'], |
|
111
|
|
|
['name' => 'Zucchini', 'locale' => 'de'], |
|
112
|
|
|
['name' => 'Zucchetti', 'locale' => 'de-CH'], |
|
113
|
|
|
]; |
|
114
|
|
|
$this->assertArraySubset($expectedTranslations, $result['translations']); |
|
115
|
|
|
} |
|
116
|
|
|
|
|
117
|
|
|
public function test_whereTranslation_filters_by_translation() |
|
118
|
|
|
{ |
|
119
|
|
|
/** @var Country $country */ |
|
120
|
|
|
$country = Country::whereTranslation('name', 'Greece')->first(); |
|
|
|
|
|
|
121
|
|
|
$this->assertSame('gr', $country->code); |
|
|
|
|
|
|
122
|
|
|
} |
|
123
|
|
|
|
|
124
|
|
View Code Duplication |
public function test_orWhereTranslation_filters_by_translation() |
|
|
|
|
|
|
125
|
|
|
{ |
|
126
|
|
|
$result = Country::whereTranslation('name', 'Greece')->orWhereTranslation('name', 'France')->get(); |
|
|
|
|
|
|
127
|
|
|
$this->assertCount(2, $result); |
|
128
|
|
|
$this->assertSame('Greece', $result[0]->name); |
|
129
|
|
|
$this->assertSame('France', $result[1]->name); |
|
130
|
|
|
} |
|
131
|
|
|
|
|
132
|
|
View Code Duplication |
public function test_whereTranslation_filters_by_translation_and_locale() |
|
|
|
|
|
|
133
|
|
|
{ |
|
134
|
|
|
Country::create(['code' => 'some-code', 'name' => 'Griechenland']); |
|
|
|
|
|
|
135
|
|
|
|
|
136
|
|
|
$this->assertSame(2, Country::whereTranslation('name', 'Griechenland')->count()); |
|
|
|
|
|
|
137
|
|
|
|
|
138
|
|
|
$result = Country::whereTranslation('name', 'Griechenland', 'de')->get(); |
|
|
|
|
|
|
139
|
|
|
$this->assertSame(1, $result->count()); |
|
140
|
|
|
$this->assertSame('gr', $result->first()->code); |
|
141
|
|
|
} |
|
142
|
|
|
|
|
143
|
|
|
public function test_whereTranslationLike_filters_by_translation() |
|
144
|
|
|
{ |
|
145
|
|
|
/** @var Country $country */ |
|
146
|
|
|
$country = Country::whereTranslationLike('name', '%Greec%')->first(); |
|
|
|
|
|
|
147
|
|
|
$this->assertSame('gr', $country->code); |
|
|
|
|
|
|
148
|
|
|
} |
|
149
|
|
|
|
|
150
|
|
View Code Duplication |
public function test_orWhereTranslationLike_filters_by_translation() |
|
|
|
|
|
|
151
|
|
|
{ |
|
152
|
|
|
$result = Country::whereTranslationLike('name', '%eece%')->orWhereTranslationLike('name', '%ance%')->get(); |
|
|
|
|
|
|
153
|
|
|
$this->assertCount(2, $result); |
|
154
|
|
|
$this->assertSame('Greece', $result[0]->name); |
|
155
|
|
|
$this->assertSame('France', $result[1]->name); |
|
156
|
|
|
} |
|
157
|
|
|
|
|
158
|
|
View Code Duplication |
public function test_whereTranslationLike_filters_by_translation_and_locale() |
|
|
|
|
|
|
159
|
|
|
{ |
|
160
|
|
|
Country::create(['code' => 'some-code', 'name' => 'Griechenland']); |
|
|
|
|
|
|
161
|
|
|
|
|
162
|
|
|
$this->assertSame(2, Country::whereTranslationLike('name', 'Griechen%')->count()); |
|
|
|
|
|
|
163
|
|
|
|
|
164
|
|
|
$result = Country::whereTranslationLike('name', '%riechenlan%', 'de')->get(); |
|
|
|
|
|
|
165
|
|
|
$this->assertSame(1, $result->count()); |
|
166
|
|
|
$this->assertSame('gr', $result->first()->code); |
|
167
|
|
|
} |
|
168
|
|
|
} |
|
169
|
|
|
|