1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* @copyright Copyright (C) eZ Systems AS. All rights reserved. |
5
|
|
|
* @license For full copyright and license information view LICENSE file distributed with this source code. |
6
|
|
|
*/ |
7
|
|
|
namespace eZ\Publish\Core\MVC\Symfony\SiteAccess\Tests; |
8
|
|
|
|
9
|
|
|
use eZ\Publish\Core\MVC\Symfony\SiteAccess; |
10
|
|
|
use eZ\Publish\Core\MVC\Symfony\SiteAccess\Matcher\URIElement; |
11
|
|
|
use eZ\Publish\Core\MVC\Symfony\SiteAccess\Router; |
12
|
|
|
use eZ\Publish\Core\MVC\Symfony\SiteAccess\Matcher\URIElement as URIElementMatcher; |
13
|
|
|
use eZ\Publish\Core\MVC\Symfony\Routing\SimplifiedRequest; |
14
|
|
|
use Psr\Log\LoggerInterface; |
15
|
|
|
|
16
|
|
|
class RouterURIElement2Test extends RouterBaseTest |
17
|
|
|
{ |
18
|
|
View Code Duplication |
public function matchProvider(): array |
19
|
|
|
{ |
20
|
|
|
return [ |
21
|
|
|
[SimplifiedRequest::fromUrl('http://example.com'), 'default_sa'], |
22
|
|
|
[SimplifiedRequest::fromUrl('https://example.com'), 'default_sa'], |
23
|
|
|
[SimplifiedRequest::fromUrl('http://example.com/'), 'default_sa'], |
24
|
|
|
[SimplifiedRequest::fromUrl('https://example.com/'), 'default_sa'], |
25
|
|
|
[SimplifiedRequest::fromUrl('http://example.com//'), 'default_sa'], |
26
|
|
|
[SimplifiedRequest::fromUrl('https://example.com//'), 'default_sa'], |
27
|
|
|
[SimplifiedRequest::fromUrl('http://example.com:8080/'), 'default_sa'], |
28
|
|
|
[SimplifiedRequest::fromUrl('http://example.com/first_siteaccess/'), 'default_sa'], |
29
|
|
|
[SimplifiedRequest::fromUrl('http://example.com/?first_siteaccess'), 'default_sa'], |
30
|
|
|
[SimplifiedRequest::fromUrl('http://example.com/?first_sa'), 'default_sa'], |
31
|
|
|
[SimplifiedRequest::fromUrl('http://example.com/first_salt'), 'default_sa'], |
32
|
|
|
[SimplifiedRequest::fromUrl('http://example.com/first_sa.foo'), 'default_sa'], |
33
|
|
|
[SimplifiedRequest::fromUrl('http://example.com/test'), 'default_sa'], |
34
|
|
|
[SimplifiedRequest::fromUrl('http://example.com/test/foo/'), 'test_foo'], |
35
|
|
|
[SimplifiedRequest::fromUrl('http://example.com/test/foo/bar/'), 'test_foo'], |
36
|
|
|
[SimplifiedRequest::fromUrl('http://example.com/test/foo/bar/first_sa'), 'test_foo'], |
37
|
|
|
[SimplifiedRequest::fromUrl('http://example.com/default_sa'), 'default_sa'], |
38
|
|
|
|
39
|
|
|
[SimplifiedRequest::fromUrl('http://example.com/first_sa'), 'first_sa'], |
40
|
|
|
[SimplifiedRequest::fromUrl('http://example.com/first_sa/'), 'first_sa'], |
41
|
|
|
// Double slashes shouldn't be considered as one |
42
|
|
|
[SimplifiedRequest::fromUrl('http://example.com//first_sa//'), 'default_sa'], |
43
|
|
|
[SimplifiedRequest::fromUrl('http://example.com///first_sa///test'), 'default_sa'], |
44
|
|
|
[SimplifiedRequest::fromUrl('http://example.com//first_sa//foo/bar'), 'default_sa'], |
45
|
|
|
[SimplifiedRequest::fromUrl('http://first_siteaccess:82/foo//bar/'), 'first_sa'], |
46
|
|
|
[SimplifiedRequest::fromUrl('http://example.com/first_sa/foo'), 'first_sa_foo'], |
47
|
|
|
[SimplifiedRequest::fromUrl('http://example.com:82/first_sa/'), 'first_sa'], |
48
|
|
|
[SimplifiedRequest::fromUrl('http://third_siteaccess/first_sa/'), 'first_sa'], |
49
|
|
|
[SimplifiedRequest::fromUrl('http://first_sa/'), 'first_sa'], |
50
|
|
|
[SimplifiedRequest::fromUrl('https://first_sa/'), 'first_sa'], |
51
|
|
|
[SimplifiedRequest::fromUrl('http://first_sa:81/'), 'first_sa'], |
52
|
|
|
[SimplifiedRequest::fromUrl('http://first_siteaccess/'), 'first_sa'], |
53
|
|
|
[SimplifiedRequest::fromUrl('http://first_siteaccess:82/'), 'first_sa'], |
54
|
|
|
[SimplifiedRequest::fromUrl('http://first_siteaccess:83/'), 'first_sa'], |
55
|
|
|
[SimplifiedRequest::fromUrl('http://first_siteaccess/foo/'), 'first_sa'], |
56
|
|
|
[SimplifiedRequest::fromUrl('http://first_siteaccess:83/foo/baz/'), 'foo_baz'], |
57
|
|
|
|
58
|
|
|
[SimplifiedRequest::fromUrl('http://example.com/second_sa'), 'second_sa'], |
59
|
|
|
[SimplifiedRequest::fromUrl('http://example.com/second_sa/'), 'second_sa'], |
60
|
|
|
[SimplifiedRequest::fromUrl('http://example.com/second_sa?param1=foo'), 'second_sa'], |
61
|
|
|
[SimplifiedRequest::fromUrl('http://example.com/second_sa/foo/'), 'second_sa_foo'], |
62
|
|
|
[SimplifiedRequest::fromUrl('http://example.com:82/second_sa/'), 'second_sa'], |
63
|
|
|
[SimplifiedRequest::fromUrl('http://example.com:83/second_sa/'), 'second_sa'], |
64
|
|
|
[SimplifiedRequest::fromUrl('http://first_siteaccess:82/second_sa/'), 'second_sa'], |
65
|
|
|
[SimplifiedRequest::fromUrl('http://first_siteaccess:83/second_sa/'), 'second_sa'], |
66
|
|
|
]; |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* @param int $level |
71
|
|
|
* @param string $uri |
72
|
|
|
* @param string $expectedFixedUpURI |
73
|
|
|
* |
74
|
|
|
* @dataProvider analyseProvider |
75
|
|
|
*/ |
76
|
|
View Code Duplication |
public function testAnalyseURI($level, $uri, $expectedFixedUpURI) |
77
|
|
|
{ |
78
|
|
|
$matcher = new URIElementMatcher([$level]); |
79
|
|
|
$matcher->setRequest( |
80
|
|
|
new SimplifiedRequest(['pathinfo' => $uri]) |
81
|
|
|
); |
82
|
|
|
$this->assertSame($expectedFixedUpURI, $matcher->analyseURI($uri)); |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* @param int $level |
87
|
|
|
* @param string $uri |
88
|
|
|
* @param string $expectedFixedUpURI |
89
|
|
|
* |
90
|
|
|
* @dataProvider analyseProvider |
91
|
|
|
*/ |
92
|
|
View Code Duplication |
public function testAnalyseURILevelAsInt($level, $uri, $expectedFixedUpURI) |
93
|
|
|
{ |
94
|
|
|
$matcher = new URIElementMatcher($level); |
95
|
|
|
$matcher->setRequest( |
96
|
|
|
new SimplifiedRequest(['pathinfo' => $uri]) |
97
|
|
|
); |
98
|
|
|
$this->assertSame($expectedFixedUpURI, $matcher->analyseURI($uri)); |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* @param int $level |
103
|
|
|
* @param string $fullUri |
104
|
|
|
* @param string $linkUri |
105
|
|
|
* |
106
|
|
|
* @dataProvider analyseProvider |
107
|
|
|
*/ |
108
|
|
View Code Duplication |
public function testAnalyseLink($level, $fullUri, $linkUri) |
109
|
|
|
{ |
110
|
|
|
$matcher = new URIElementMatcher([$level]); |
111
|
|
|
$matcher->setRequest( |
112
|
|
|
new SimplifiedRequest(['pathinfo' => $fullUri]) |
113
|
|
|
); |
114
|
|
|
$this->assertSame($fullUri, $matcher->analyseLink($linkUri)); |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
public function analyseProvider() |
118
|
|
|
{ |
119
|
|
|
return [ |
120
|
|
|
[2, '/my/siteaccess/foo/bar', '/foo/bar'], |
121
|
|
|
[2, '/vive/le/sucre/en-poudre', '/sucre/en-poudre'], |
122
|
|
|
// Issue https://jira.ez.no/browse/EZP-20125 |
123
|
|
|
[1, '/fre/content/edit/104/1/fre-FR', '/content/edit/104/1/fre-FR'], |
124
|
|
|
[1, '/fre/utf8-with-accent/é/fre/à/à/fre/é', '/utf8-with-accent/é/fre/à/à/fre/é'], |
125
|
|
|
[2, '/é/fre/utf8-with-accent/é/fre/à/à/fre/é', '/utf8-with-accent/é/fre/à/à/fre/é'], |
126
|
|
|
[2, '/prefix/fre/url/alias/prefix/fre/prefix/fre/url', '/url/alias/prefix/fre/prefix/fre/url'], |
127
|
|
|
// regression after the first fix of EZP-20125 |
128
|
|
|
[1, '/sitaccess', ''], |
129
|
|
|
[1, '/sitaccess/', '/'], |
130
|
|
|
[2, '/prefix/siteaccess', ''], |
131
|
|
|
[2, '/prefix/siteaccess/', '/'], |
132
|
|
|
]; |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
/** |
136
|
|
|
* @dataProvider reverseMatchProvider |
137
|
|
|
*/ |
138
|
|
|
public function testReverseMatch($siteAccessName, $originalPathinfo) |
139
|
|
|
{ |
140
|
|
|
$expectedSiteAccessPath = str_replace('_', '/', $siteAccessName); |
141
|
|
|
$matcher = new URIElementMatcher([2]); |
142
|
|
|
$matcher->setRequest(new SimplifiedRequest(['pathinfo' => $originalPathinfo])); |
143
|
|
|
|
144
|
|
|
$result = $matcher->reverseMatch($siteAccessName); |
145
|
|
|
$this->assertInstanceOf(URIElement::class, $result); |
146
|
|
|
$this->assertSame("/{$expectedSiteAccessPath}{$originalPathinfo}", $result->getRequest()->pathinfo); |
147
|
|
|
$this->assertSame("/$expectedSiteAccessPath/some/linked/uri", $result->analyseLink('/some/linked/uri')); |
148
|
|
|
$this->assertSame('/foo/bar/baz', $result->analyseURI("/$expectedSiteAccessPath/foo/bar/baz")); |
149
|
|
|
} |
150
|
|
|
|
151
|
|
|
public function reverseMatchProvider() |
152
|
|
|
{ |
153
|
|
|
return [ |
154
|
|
|
['some_thing', '/foo/bar'], |
155
|
|
|
['another_siteaccess', '/foo/bar'], |
156
|
|
|
]; |
157
|
|
|
} |
158
|
|
|
|
159
|
|
|
public function testReverseMatchFail() |
160
|
|
|
{ |
161
|
|
|
$matcher = new URIElementMatcher([2]); |
162
|
|
|
$matcher->setRequest(new SimplifiedRequest(['pathinfo' => '/my/siteaccess/foo/bar'])); |
163
|
|
|
$this->assertNull($matcher->reverseMatch('another_siteaccess_again_dont_tell_me')); |
164
|
|
|
} |
165
|
|
|
|
166
|
|
View Code Duplication |
public function testSerialize() |
167
|
|
|
{ |
168
|
|
|
$matcher = new URIElementMatcher([2]); |
169
|
|
|
$matcher->setRequest(new SimplifiedRequest(['pathinfo' => '/foo/bar'])); |
170
|
|
|
$sa = new SiteAccess('test', 'test', $matcher); |
171
|
|
|
$serializedSA1 = serialize($sa); |
172
|
|
|
|
173
|
|
|
$matcher->setRequest(new SimplifiedRequest(['pathinfo' => '/foo/bar/baz'])); |
174
|
|
|
$serializedSA2 = serialize($sa); |
175
|
|
|
|
176
|
|
|
$this->assertSame($serializedSA1, $serializedSA2); |
177
|
|
|
} |
178
|
|
|
|
179
|
|
View Code Duplication |
protected function createRouter(): Router |
180
|
|
|
{ |
181
|
|
|
return new Router( |
182
|
|
|
$this->matcherBuilder, |
183
|
|
|
$this->createMock(LoggerInterface::class), |
184
|
|
|
'default_sa', |
185
|
|
|
[ |
186
|
|
|
'URIElement' => [ |
187
|
|
|
'value' => 2, |
188
|
|
|
], |
189
|
|
|
'Map\\URI' => [ |
190
|
|
|
'first_sa' => 'first_sa', |
191
|
|
|
'second_sa' => 'second_sa', |
192
|
|
|
], |
193
|
|
|
'Map\\Host' => [ |
194
|
|
|
'first_sa' => 'first_sa', |
195
|
|
|
'first_siteaccess' => 'first_sa', |
196
|
|
|
], |
197
|
|
|
], |
198
|
|
|
$this->siteAccessProvider |
199
|
|
|
); |
200
|
|
|
} |
201
|
|
|
|
202
|
|
|
/** |
203
|
|
|
* @return \eZ\Publish\Core\MVC\Symfony\SiteAccess\Tests\SiteAccessSetting[] |
204
|
|
|
*/ |
205
|
|
|
public function getSiteAccessProviderSettings(): array |
206
|
|
|
{ |
207
|
|
|
return [ |
208
|
|
|
new SiteAccessSetting('first_sa', true), |
209
|
|
|
new SiteAccessSetting('second_sa', true), |
210
|
|
|
new SiteAccessSetting('third_sa', true), |
211
|
|
|
new SiteAccessSetting('fourth_sa', true), |
212
|
|
|
new SiteAccessSetting('fifth_sa', true), |
213
|
|
|
new SiteAccessSetting('test_foo', true), |
214
|
|
|
new SiteAccessSetting('first_sa_foo', true), |
215
|
|
|
new SiteAccessSetting('second_sa_foo', true), |
216
|
|
|
new SiteAccessSetting('foo_baz', true), |
217
|
|
|
]; |
218
|
|
|
} |
219
|
|
|
} |
220
|
|
|
|