1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace TractorCow\Fluent\Tests\Middleware; |
4
|
|
|
|
5
|
|
|
use SilverStripe\Control\Controller; |
6
|
|
|
use SilverStripe\Control\HTTPRequest; |
7
|
|
|
use SilverStripe\Core\Config\Config; |
8
|
|
|
use SilverStripe\Dev\SapphireTest; |
9
|
|
|
use TractorCow\Fluent\Extension\FluentDirectorExtension; |
10
|
|
|
use TractorCow\Fluent\Model\Domain; |
11
|
|
|
use TractorCow\Fluent\Model\Locale; |
12
|
|
|
use TractorCow\Fluent\State\FluentState; |
13
|
|
|
|
14
|
|
|
class DetectLocaleMiddlewareTest extends SapphireTest |
15
|
|
|
{ |
16
|
|
|
protected static $fixture_file = 'DetectLocaleMiddlewareTest.yml'; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* @var DetectLocaleMiddleware |
20
|
|
|
*/ |
21
|
|
|
protected $middleware; |
22
|
|
|
|
23
|
|
|
protected function setUp() |
24
|
|
|
{ |
25
|
|
|
parent::setUp(); |
26
|
|
|
$this->middleware = new Stub\DetectLocaleMiddlewareSpy; |
|
|
|
|
27
|
|
|
|
28
|
|
|
Config::modify()->set(FluentDirectorExtension::class, 'query_param', 'l'); |
29
|
|
|
|
30
|
|
|
// Enable localedetection |
31
|
|
|
FluentDirectorExtension::config()->set('detect_locale', true); |
32
|
|
|
|
33
|
|
|
// Clear cache |
34
|
|
|
Locale::clearCached(); |
35
|
|
|
Domain::clearCached(); |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
public function testGetPersistKey() |
39
|
|
|
{ |
40
|
|
|
$state = FluentState::singleton(); |
41
|
|
|
$state->setIsFrontend(true); |
42
|
|
|
$this->assertSame('FluentLocale', $this->middleware->getPersistKey()); |
43
|
|
|
|
44
|
|
|
$state->setIsFrontend(false); |
45
|
|
|
$this->assertSame('FluentLocale_CMS', $this->middleware->getPersistKey()); |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* @dataProvider localePriorityProvider |
50
|
|
|
*/ |
51
|
|
|
public function testGetLocalePriority($url, $routeParams, $queryParams, $persisted, $header, $expected) |
52
|
|
|
{ |
53
|
|
|
$request = new HTTPRequest('GET', $url, $queryParams); |
54
|
|
|
$request->setRouteParams($routeParams); |
55
|
|
|
$request->setSession(Controller::curr()->getRequest()->getSession()); |
56
|
|
|
$this->middleware->setPersistLocale($request, null); |
57
|
|
|
|
58
|
|
|
if ($persisted) { |
59
|
|
|
$this->middleware->setPersistLocale($request, $persisted); |
60
|
|
|
} |
61
|
|
|
if ($header) { |
62
|
|
|
$request->addHeader('Accept-Language', $header); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
$this->assertSame($expected, $this->middleware->getLocale($request)); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* @return array[] List of tests with arguments: $url, $routeParams, $queryParams, $persisted, $header, $expected |
70
|
|
|
*/ |
71
|
|
|
public function localePriorityProvider() |
72
|
|
|
{ |
73
|
|
|
return [/* |
|
|
|
|
74
|
|
|
// First priority: controller routing params |
75
|
|
|
['/nz/foo', ['l' => 'en_NZ'], ['l' => 'en_AU'], 'fr_FR', 'es-US', 'en_NZ'], |
76
|
|
|
// Second priority: request params |
77
|
|
|
['/foo', [], ['l' => 'en_AU'], 'fr_FR', 'es-US', 'en_AU'], |
78
|
|
|
// Third priority: persisted locale |
79
|
|
|
['/foo', [], [], 'fr_FR', 'es-US', 'fr_FR'], |
80
|
|
|
// Default to the default locale when not on the homepage |
81
|
|
|
['/foo', [], [], null, 'es-US', 'es_ES'],*/ |
82
|
|
|
// Home page only - fourth priority is request header |
83
|
|
|
['/', [], [], null, 'es-US', 'es_US'], |
84
|
|
|
]; |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
public function testLocaleIsAlwaysPersistedEvenIfNotSetByTheMiddleware() |
88
|
|
|
{ |
89
|
|
|
$request = new HTTPRequest('GET', '/'); |
90
|
|
|
FluentState::singleton()->setLocale('dummy'); |
91
|
|
|
|
92
|
|
|
$middleware = $this->getMockBuilder(Stub\DetectLocaleMiddlewareSpy::class) |
93
|
|
|
->setMethods(['getLocale', 'setPersistLocale']) |
94
|
|
|
->getMock(); |
95
|
|
|
|
96
|
|
|
$middleware->expects($this->never())->method('getLocale'); |
97
|
|
|
$middleware->expects($this->once())->method('setPersistLocale')->with($request, 'dummy'); |
|
|
|
|
98
|
|
|
|
99
|
|
|
$middleware->process($request, function () { |
100
|
|
|
// no-op |
101
|
|
|
}); |
102
|
|
|
} |
103
|
|
|
} |
104
|
|
|
|
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..