1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
use Dimsav\Translatable\Test\Model\Country; |
4
|
|
|
|
5
|
|
|
class ScopesTest extends TestsBase |
|
|
|
|
6
|
|
|
{ |
7
|
|
|
public function test_translated_in_scope_returns_only_translated_records_for_this_locale() |
8
|
|
|
{ |
9
|
|
|
$translatedCountries = Country::translatedIn('fr')->get(); |
10
|
|
|
$this->assertEquals($translatedCountries->count(), 1); |
11
|
|
|
} |
12
|
|
|
|
13
|
|
|
public function test_translated_in_scope_works_with_default_locale() |
14
|
|
|
{ |
15
|
|
|
App::setLocale('de'); |
16
|
|
|
$translatedCountries = Country::translatedIn()->get(); |
17
|
|
|
|
18
|
|
|
$this->assertSame($translatedCountries->count(), 1); |
19
|
|
|
$this->assertSame('Griechenland', $translatedCountries->first()->name); |
20
|
|
|
} |
21
|
|
|
|
22
|
|
View Code Duplication |
public function test_not_translated_in_scope_returns_only_not_translated_records_for_this_locale() |
|
|
|
|
23
|
|
|
{ |
24
|
|
|
$notTranslatedCountries = Country::notTranslatedIn('en')->get(); |
|
|
|
|
25
|
|
|
$this->assertCount(2, $notTranslatedCountries); |
26
|
|
|
|
27
|
|
|
foreach ($notTranslatedCountries as $notTranslatedCountry) { |
28
|
|
|
$this->assertFalse($notTranslatedCountry->hasTranslation('en')); |
29
|
|
|
} |
30
|
|
|
} |
31
|
|
|
|
32
|
|
View Code Duplication |
public function test_not_translated_in_scope_works_with_default_locale() |
|
|
|
|
33
|
|
|
{ |
34
|
|
|
App::setLocale('en'); |
35
|
|
|
$notTranslatedCountries = Country::notTranslatedIn()->get(); |
|
|
|
|
36
|
|
|
$this->assertCount(2, $notTranslatedCountries); |
37
|
|
|
|
38
|
|
|
foreach ($notTranslatedCountries as $notTranslatedCountry) { |
39
|
|
|
$this->assertFalse($notTranslatedCountry->hasTranslation('en')); |
40
|
|
|
} |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
public function test_translated_scope_returns_records_with_at_least_one_translation() |
44
|
|
|
{ |
45
|
|
|
$translatedCountries = Country::translated()->get(); |
46
|
|
|
$this->assertEquals($translatedCountries->count(), 2); |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
public function test_lists_of_translated_fields() |
50
|
|
|
{ |
51
|
|
|
App::setLocale('de'); |
52
|
|
|
$list = [[ |
53
|
|
|
'id' => '1', |
54
|
|
|
'name' => 'Griechenland', |
55
|
|
|
]]; |
56
|
|
|
$this->assertEquals($list, Country::listsTranslations('name')->get()->toArray()); |
|
|
|
|
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
public function test_lists_of_translated_fields_with_fallback() |
60
|
|
|
{ |
61
|
|
|
App::make('config')->set('translatable.fallback_locale', 'en'); |
62
|
|
|
App::setLocale('de'); |
63
|
|
|
$country = new Country(); |
64
|
|
|
$country->useTranslationFallback = true; |
|
|
|
|
65
|
|
|
$list = [[ |
66
|
|
|
'id' => '1', |
67
|
|
|
'name' => 'Griechenland', |
68
|
|
|
], [ |
69
|
|
|
'id' => '2', |
70
|
|
|
'name' => 'France', |
71
|
|
|
]]; |
72
|
|
|
$this->assertEquals($list, $country->listsTranslations('name')->get()->toArray()); |
|
|
|
|
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
public function test_scope_withTranslation_without_fallback() |
76
|
|
|
{ |
77
|
|
|
$result = Country::withTranslation()->first(); |
|
|
|
|
78
|
|
|
$loadedTranslations = $result->toArray()['translations']; |
79
|
|
|
$this->assertCount(1, $loadedTranslations); |
80
|
|
|
$this->assertSame('Greece', $loadedTranslations[0]['name']); |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
public function test_scope_withTranslation_with_fallback() |
84
|
|
|
{ |
85
|
|
|
App::make('config')->set('translatable.fallback_locale', 'de'); |
86
|
|
|
App::make('config')->set('translatable.use_fallback', true); |
87
|
|
|
|
88
|
|
|
$result = Country::withTranslation()->first(); |
|
|
|
|
89
|
|
|
$loadedTranslations = $result->toArray()['translations']; |
90
|
|
|
$this->assertCount(2, $loadedTranslations); |
91
|
|
|
$this->assertSame('Greece', $loadedTranslations[0]['name']); |
92
|
|
|
$this->assertSame('Griechenland', $loadedTranslations[1]['name']); |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
public function test_whereTranslation_filters_by_translation() |
96
|
|
|
{ |
97
|
|
|
/** @var Country $country */ |
98
|
|
|
$country = Country::whereTranslation('name', 'Greece')->first(); |
|
|
|
|
99
|
|
|
$this->assertSame('gr', $country->code); |
|
|
|
|
100
|
|
|
} |
101
|
|
|
|
102
|
|
View Code Duplication |
public function test_whereTranslation_filters_by_translation_and_locale() |
|
|
|
|
103
|
|
|
{ |
104
|
|
|
Country::create(['code' => 'some-code', 'name' => 'Griechenland']); |
105
|
|
|
|
106
|
|
|
$this->assertSame(2, Country::whereTranslation('name', 'Griechenland')->count()); |
|
|
|
|
107
|
|
|
|
108
|
|
|
$result = Country::whereTranslation('name', 'Griechenland', 'de')->get(); |
|
|
|
|
109
|
|
|
$this->assertSame(1, $result->count()); |
110
|
|
|
$this->assertSame('gr', $result->first()->code); |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
public function test_whereTranslationLike_filters_by_translation() |
114
|
|
|
{ |
115
|
|
|
/** @var Country $country */ |
116
|
|
|
$country = Country::whereTranslationLike('name', '%Greec%')->first(); |
|
|
|
|
117
|
|
|
$this->assertSame('gr', $country->code); |
|
|
|
|
118
|
|
|
} |
119
|
|
|
|
120
|
|
View Code Duplication |
public function test_whereTranslationLike_filters_by_translation_and_locale() |
|
|
|
|
121
|
|
|
{ |
122
|
|
|
Country::create(['code' => 'some-code', 'name' => 'Griechenland']); |
123
|
|
|
|
124
|
|
|
$this->assertSame(2, Country::whereTranslationLike('name', 'Griechen%')->count()); |
|
|
|
|
125
|
|
|
|
126
|
|
|
$result = Country::whereTranslationLike('name', '%riechenlan%', 'de')->get(); |
|
|
|
|
127
|
|
|
$this->assertSame(1, $result->count()); |
128
|
|
|
$this->assertSame('gr', $result->first()->code); |
129
|
|
|
} |
130
|
|
|
} |
131
|
|
|
|
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.