1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace TractorCow\Fluent\Tests\Model; |
4
|
|
|
|
5
|
|
|
use SilverStripe\Dev\SapphireTest; |
6
|
|
|
use SilverStripe\Forms\CheckboxField; |
7
|
|
|
use TractorCow\Fluent\Extension\FluentExtension; |
8
|
|
|
use TractorCow\Fluent\Extension\FluentDirectorExtension; |
9
|
|
|
use TractorCow\Fluent\Model\Domain; |
10
|
|
|
use TractorCow\Fluent\Model\Locale; |
11
|
|
|
use TractorCow\Fluent\State\FluentState; |
12
|
|
|
use SilverStripe\Core\Config\Config; |
13
|
|
|
|
14
|
|
|
class LocaleTest extends SapphireTest |
15
|
|
|
{ |
16
|
|
|
protected static $fixture_file = 'LocaleTest.yml'; |
17
|
|
|
|
18
|
|
|
public function setUp() |
19
|
|
|
{ |
20
|
|
|
parent::setUp(); |
21
|
|
|
|
22
|
|
|
// Clear cache |
23
|
|
|
Locale::clearCached(); |
24
|
|
|
Domain::clearCached(); |
25
|
|
|
FluentState::singleton() |
26
|
|
|
->setLocale('es_US') |
27
|
|
|
->setDomain('fluent.co.nz') |
28
|
|
|
->setIsDomainMode(true); |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
public function testGetDefaultWithoutArguments() |
32
|
|
|
{ |
33
|
|
|
$result = Locale::getDefault(); |
34
|
|
|
|
35
|
|
|
$this->assertInstanceOf(Locale::class, $result); |
36
|
|
|
// Note: default_sort order is included here |
37
|
|
|
$this->assertSame('en_AU', $result->Locale, 'First Locale with IsDefault true is returned'); |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
public function testGetDefaultWithDomainArgument() |
41
|
|
|
{ |
42
|
|
|
// spanish has_one default locale |
43
|
|
|
/** @var Domain $domain */ |
44
|
|
|
$domain = $this->objFromFixture(Domain::class, 'spanish'); |
45
|
|
|
$result = Locale::getDefault($domain->Domain); |
46
|
|
|
|
47
|
|
|
$this->assertInstanceOf(Locale::class, $result); |
48
|
|
|
$this->assertSame('es_US', $result->Locale, 'Domain respects has_one to DefaultLocale'); |
49
|
|
|
|
50
|
|
|
// kiwi doesn't has_one to any default, but the IsDefault is a child |
51
|
|
|
/** @var Domain $domain2 */ |
52
|
|
|
$domain2 = $this->objFromFixture(Domain::class, 'kiwi'); |
53
|
|
|
$result2 = Locale::getDefault($domain2->Domain); |
54
|
|
|
|
55
|
|
|
$this->assertInstanceOf(Locale::class, $result2); |
56
|
|
|
$this->assertSame('en_AU', $result2->Locale, 'First Locale in Domain with IsDefault true is returned'); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
public function testGetDefaultWithCurrentDomainArgument() |
60
|
|
|
{ |
61
|
|
|
// Get current default |
62
|
|
|
$result = Locale::getDefault(true); // Should use fluent.co.nz current domain |
63
|
|
|
$this->assertInstanceOf(Locale::class, $result); |
64
|
|
|
$this->assertSame('en_AU', $result->Locale, 'First Locale in Domain with IsDefault true is returned'); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* @dataProvider isLocaleProvider |
69
|
|
|
* @param string $locale |
70
|
|
|
* @param string $input |
71
|
|
|
* @param bool $expected |
72
|
|
|
*/ |
73
|
|
|
public function testIsLocale($locale, $input, $expected) |
74
|
|
|
{ |
75
|
|
|
$localeObj = Locale::create()->setField('Locale', $locale); |
76
|
|
|
$this->assertSame($expected, $localeObj->isLocale($input)); |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* @return array[] |
81
|
|
|
*/ |
82
|
|
|
public function isLocaleProvider() |
83
|
|
|
{ |
84
|
|
|
return [ |
85
|
|
|
['en_NZ', 'en_NZ', true], |
86
|
|
|
['en_nz', 'en-NZ', true], |
87
|
|
|
['en-NZ', 'en_nz', true], |
88
|
|
|
['en-nz', 'en-nz', true], |
89
|
|
|
['en_NZ', 'en-NZ-1990', true], |
90
|
|
|
['en_NZ', 'en_AU', false], |
91
|
|
|
['en_NZ', 'fr-fr-1990', false], |
92
|
|
|
]; |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
public function testGetNativeName() |
96
|
|
|
{ |
97
|
|
|
$this->assertSame('Spanish', Locale::getByLocale('es_US')->getNativeName()); |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
public function testGetBaseURLContainsDomainAndURLSegmentForNonDefaultLocale() |
101
|
|
|
{ |
102
|
|
|
// es_ES has a domain but is not the default locale for that domain |
103
|
|
|
$result = Locale::getByLocale('es_ES')->getBaseURL(); |
104
|
|
|
$this->assertContains('fluent.es', $result, "Locale's domain is in the URL"); |
105
|
|
|
$this->assertContains('/es/', $result, 'URL segment for non-default locale is in the URL'); |
106
|
|
|
|
107
|
|
|
// Turning off domain mode removes domain but not prefix |
108
|
|
|
FluentState::singleton()->setIsDomainMode(false); |
109
|
|
|
$result = Locale::getByLocale('es_ES')->getBaseURL(); |
110
|
|
|
$this->assertNotContains('fluent.es', $result, "Locale's domain is in the URL"); |
111
|
|
|
$this->assertContains('/es/', $result, 'URL segment for non-default locale is in the URL'); |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
public function testBaseURLPrefixDisabled() |
115
|
|
|
{ |
116
|
|
|
// Default base url includes the default url segment |
117
|
|
|
$result = Locale::getDefault()->getBaseURL(); |
118
|
|
|
$this->assertContains('/au/', $result); |
119
|
|
|
|
120
|
|
|
// Default base url shortens the default locale url base by excluding the locale's url segment |
121
|
|
|
Config::inst()->set(FluentDirectorExtension::class, 'disable_default_prefix', true); |
|
|
|
|
122
|
|
|
$result = Locale::getDefault()->getBaseURL(); |
123
|
|
|
$this->assertNotContains('/au/', $result); |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
public function testGetBaseURLOnlyContainsDomainForPrefixDisabledDefaultLocale() |
127
|
|
|
{ |
128
|
|
|
Config::inst()->set(FluentDirectorExtension::class, 'disable_default_prefix', true); |
129
|
|
|
|
130
|
|
|
// es_US has a domain and is the default |
131
|
|
|
$result = Locale::getByLocale('es_US')->getBaseURL(); |
132
|
|
|
$this->assertContains('fluent.es', $result, "Locale's domain is in the URL"); |
133
|
|
|
$this->assertNotContains('/es-usa/', $result, 'URL segment is not in the URL for default locales'); |
134
|
|
|
|
135
|
|
|
// When domain mode is turned off, prefix is now necessary |
136
|
|
|
FluentState::singleton()->setIsDomainMode(false); |
137
|
|
|
$result = Locale::getByLocale('es_US')->getBaseURL(); |
138
|
|
|
$this->assertNotContains('fluent.es', $result, "Domain not used"); |
139
|
|
|
$this->assertContains('/es-usa/', $result, 'URL Segment necessary for non-global default'); |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
public function testGetSiblings() |
143
|
|
|
{ |
144
|
|
|
$esUS = Locale::getByLocale('es_US'); |
145
|
|
|
$this->assertListEquals([ |
146
|
|
|
[ 'Locale' => 'es_US' ], |
147
|
|
|
[ 'Locale' => 'es_ES' ], |
148
|
|
|
], $esUS->getSiblingLocales()); |
149
|
|
|
|
150
|
|
|
// Test without domain mode |
151
|
|
|
FluentState::singleton()->setIsDomainMode(false); |
152
|
|
|
|
153
|
|
|
$esUS = Locale::getByLocale('es_US'); |
154
|
|
|
$this->assertListEquals([ |
155
|
|
|
[ 'Locale' => 'es_US' ], |
156
|
|
|
[ 'Locale' => 'es_ES' ], |
157
|
|
|
[ 'Locale' => 'en_NZ' ], |
158
|
|
|
[ 'Locale' => 'en_AU' ], |
159
|
|
|
], $esUS->getSiblingLocales()); |
160
|
|
|
} |
161
|
|
|
|
162
|
|
|
public function testGetIsDefault() |
163
|
|
|
{ |
164
|
|
|
$esUS = Locale::getByLocale('es_US'); |
165
|
|
|
$esES = Locale::getByLocale('es_ES'); |
166
|
|
|
$enNZ = Locale::getByLocale('en_NZ'); |
167
|
|
|
$enAU = Locale::getByLocale('en_AU'); |
168
|
|
|
|
169
|
|
|
// In domain mode, two are default |
170
|
|
|
$this->assertTrue($esUS->getIsDefault()); // Locale.DefaultLocale = this |
171
|
|
|
$this->assertTrue($enAU->getIsDefault()); // IsGlobalDefault = 1 |
172
|
|
|
$this->assertFalse($enNZ->getIsDefault()); |
173
|
|
|
$this->assertFalse($esES->getIsDefault()); |
174
|
|
|
|
175
|
|
|
// In non-domain mode, only one default |
176
|
|
|
FluentState::singleton()->setIsDomainMode(false); |
177
|
|
|
$this->assertFalse($esUS->getIsDefault()); |
178
|
|
|
$this->assertTrue($enAU->getIsDefault()); // IsGlobalDefault = 1 |
179
|
|
|
$this->assertFalse($enNZ->getIsDefault()); |
180
|
|
|
$this->assertFalse($esES->getIsDefault()); |
181
|
|
|
} |
182
|
|
|
|
183
|
|
|
public function testGetIsOnlyLocale() |
184
|
|
|
{ |
185
|
|
|
$esUS = Locale::getByLocale('es_US'); |
186
|
|
|
$esES = Locale::getByLocale('es_ES'); |
187
|
|
|
|
188
|
|
|
$this->assertFalse($esUS->getIsOnlyLocale()); |
189
|
|
|
$this->assertFalse($esUS->getIsOnlyLocale()); |
190
|
|
|
|
191
|
|
|
// Delete esES will affect this |
192
|
|
|
$esES->delete(); |
193
|
|
|
Locale::clearCached(); |
194
|
|
|
Domain::clearCached(); |
195
|
|
|
|
196
|
|
|
$this->assertTrue($esUS->getIsOnlyLocale()); |
197
|
|
|
|
198
|
|
|
// Turning off domain mode means this locale is joined with all the other domain locales |
199
|
|
|
FluentState::singleton()->setIsDomainMode(false); |
200
|
|
|
$this->assertFalse($esUS->getIsOnlyLocale()); |
201
|
|
|
} |
202
|
|
|
|
203
|
|
|
public function testGlobalDefaultCheckedOnFirstLocale() |
204
|
|
|
{ |
205
|
|
|
Locale::get()->removeAll(); |
206
|
|
|
Locale::clearCached(); |
207
|
|
|
|
208
|
|
|
$firstLocale = new Locale; |
209
|
|
|
|
210
|
|
|
$fields = $firstLocale->getCMSFields(); |
211
|
|
|
|
212
|
|
|
/** @var CheckboxField $checkbox */ |
213
|
|
|
$checkbox = $fields->fieldByName('Root.Main.IsGlobalDefault'); |
214
|
|
|
$this->assertTrue((bool) $checkbox->Value()); |
215
|
|
|
} |
216
|
|
|
|
217
|
|
|
public function testGetLocaleSuffix() |
218
|
|
|
{ |
219
|
|
|
$locale = Locale::getByLocale('es_US'); |
220
|
|
|
|
221
|
|
|
$this->assertSame('US', $locale->getLocaleSuffix()); |
222
|
|
|
} |
223
|
|
|
} |
224
|
|
|
|