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