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