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