|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* File containing the eZ\Publish\Core\MVC\Symfony\SiteAccess\Tests\RouterTest class. |
|
5
|
|
|
* |
|
6
|
|
|
* @copyright Copyright (C) eZ Systems AS. All rights reserved. |
|
7
|
|
|
* @license For full copyright and license information view LICENSE file distributed with this source code. |
|
8
|
|
|
*/ |
|
9
|
|
|
namespace eZ\Publish\Core\MVC\Symfony\SiteAccess\Tests; |
|
10
|
|
|
|
|
11
|
|
|
use eZ\Publish\Core\MVC\Symfony\SiteAccess; |
|
12
|
|
|
use PHPUnit\Framework\TestCase; |
|
13
|
|
|
use eZ\Publish\Core\MVC\Symfony\SiteAccess\Router; |
|
14
|
|
|
use eZ\Publish\Core\MVC\Symfony\Routing\SimplifiedRequest; |
|
15
|
|
|
use eZ\Publish\Core\MVC\Symfony\SiteAccess\MatcherBuilder; |
|
16
|
|
|
use Symfony\Component\HttpFoundation\Request; |
|
17
|
|
|
|
|
18
|
|
|
class RouterTest extends TestCase |
|
19
|
|
|
{ |
|
20
|
|
|
/** |
|
21
|
|
|
* @var \eZ\Publish\Core\MVC\Symfony\SiteAccess\MatcherBuilder |
|
22
|
|
|
*/ |
|
23
|
|
|
private $matcherBuilder; |
|
24
|
|
|
|
|
25
|
|
|
protected function setUp() |
|
26
|
|
|
{ |
|
27
|
|
|
parent::setUp(); |
|
28
|
|
|
$this->matcherBuilder = new MatcherBuilder(); |
|
29
|
|
|
} |
|
30
|
|
|
|
|
31
|
|
|
protected function tearDown() |
|
32
|
|
|
{ |
|
33
|
|
|
putenv('EZPUBLISH_SITEACCESS'); |
|
34
|
|
|
parent::tearDown(); |
|
35
|
|
|
} |
|
36
|
|
|
|
|
37
|
|
|
public function testConstructDebug() |
|
38
|
|
|
{ |
|
39
|
|
|
return $this->testConstruct(true); |
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
|
|
public function testConstruct($debug = false) |
|
43
|
|
|
{ |
|
44
|
|
|
return new Router( |
|
45
|
|
|
$this->matcherBuilder, |
|
46
|
|
|
$this->getMock('Psr\\Log\\LoggerInterface'), |
|
47
|
|
|
'default_sa', |
|
48
|
|
|
array( |
|
49
|
|
|
'Map\\URI' => array( |
|
50
|
|
|
'first_sa' => 'first_sa', |
|
51
|
|
|
'second_sa' => 'second_sa', |
|
52
|
|
|
), |
|
53
|
|
|
'Map\\Host' => array( |
|
54
|
|
|
'first_sa' => 'first_sa', |
|
55
|
|
|
'first_siteaccess' => 'first_sa', |
|
56
|
|
|
'third_siteaccess' => 'third_sa', |
|
57
|
|
|
), |
|
58
|
|
|
'Map\\Port' => array( |
|
59
|
|
|
81 => 'third_sa', |
|
60
|
|
|
82 => 'fourth_sa', |
|
61
|
|
|
83 => 'first_sa', |
|
62
|
|
|
85 => 'first_sa', |
|
63
|
|
|
), |
|
64
|
|
|
'Compound\\LogicalAnd' => array( |
|
65
|
|
|
array( |
|
66
|
|
|
'matchers' => array( |
|
67
|
|
|
'Map\\URI' => array('eng' => true), |
|
68
|
|
|
'Map\\Host' => array('fr.ezpublish.dev' => true), |
|
69
|
|
|
), |
|
70
|
|
|
'match' => 'fr_eng', |
|
71
|
|
|
), |
|
72
|
|
|
array( |
|
73
|
|
|
'matchers' => array( |
|
74
|
|
|
'Map\\URI' => array('fre' => true), |
|
75
|
|
|
'Map\\Host' => array('us.ezpublish.dev' => true), |
|
76
|
|
|
), |
|
77
|
|
|
'match' => 'fr_us', |
|
78
|
|
|
), |
|
79
|
|
|
), |
|
80
|
|
|
), |
|
81
|
|
|
array('first_sa', 'second_sa', 'third_sa', 'fourth_sa', 'headerbased_sa', 'fr_eng', 'fr_us'), |
|
82
|
|
|
null, |
|
83
|
|
|
$debug |
|
84
|
|
|
); |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
|
/** |
|
88
|
|
|
* @depends testConstruct |
|
89
|
|
|
* @dataProvider matchProvider |
|
90
|
|
|
*/ |
|
91
|
|
View Code Duplication |
public function testMatch(SimplifiedRequest $request, $siteAccess, Router $router) |
|
92
|
|
|
{ |
|
93
|
|
|
$sa = $router->match($request); |
|
94
|
|
|
$this->assertInstanceOf('eZ\\Publish\\Core\\MVC\\Symfony\\SiteAccess', $sa); |
|
95
|
|
|
$this->assertSame($siteAccess, $sa->name); |
|
96
|
|
|
// SiteAccess must be serializable as a whole |
|
97
|
|
|
// See https://jira.ez.no/browse/EZP-21613 |
|
98
|
|
|
$this->assertInternalType('string', serialize($sa)); |
|
99
|
|
|
$router->setSiteAccess(); |
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
|
|
/** |
|
103
|
|
|
* @depends testConstructDebug |
|
104
|
|
|
* @expectedException \eZ\Publish\Core\MVC\Exception\InvalidSiteAccessException |
|
105
|
|
|
* @expectedExceptionMessageRegExp /^Invalid siteaccess 'foobar_sa', matched by .+\. Valid siteaccesses are/ |
|
106
|
|
|
*/ |
|
107
|
|
|
public function testMatchWithDevEnvFail(Router $router) |
|
108
|
|
|
{ |
|
109
|
|
|
$saName = 'foobar_sa'; |
|
110
|
|
|
putenv("EZPUBLISH_SITEACCESS=$saName"); |
|
111
|
|
|
$router->match(new SimplifiedRequest()); |
|
112
|
|
|
} |
|
113
|
|
|
|
|
114
|
|
|
/** |
|
115
|
|
|
* @depends testConstruct |
|
116
|
|
|
* @expectedException \eZ\Publish\Core\MVC\Exception\InvalidSiteAccessException |
|
117
|
|
|
* @expectedExceptionMessageRegExp /^Invalid siteaccess 'foobar_sa', matched by .+\.$/ |
|
118
|
|
|
*/ |
|
119
|
|
|
public function testMatchWithProdEnvFail(Router $router) |
|
120
|
|
|
{ |
|
121
|
|
|
$saName = 'foobar_sa'; |
|
122
|
|
|
putenv("EZPUBLISH_SITEACCESS=$saName"); |
|
123
|
|
|
$router->match(new SimplifiedRequest()); |
|
124
|
|
|
} |
|
125
|
|
|
|
|
126
|
|
|
/** |
|
127
|
|
|
* @depends testConstruct |
|
128
|
|
|
*/ |
|
129
|
|
View Code Duplication |
public function testMatchWithEnv(Router $router) |
|
130
|
|
|
{ |
|
131
|
|
|
$saName = 'first_sa'; |
|
132
|
|
|
putenv("EZPUBLISH_SITEACCESS=$saName"); |
|
133
|
|
|
$sa = $router->match(new SimplifiedRequest()); |
|
134
|
|
|
$this->assertInstanceOf('eZ\\Publish\\Core\\MVC\\Symfony\\SiteAccess', $sa); |
|
135
|
|
|
$this->assertSame($saName, $sa->name); |
|
136
|
|
|
$this->assertSame('env', $sa->matchingType); |
|
137
|
|
|
$router->setSiteAccess(); |
|
138
|
|
|
} |
|
139
|
|
|
|
|
140
|
|
|
/** |
|
141
|
|
|
* @param \eZ\Publish\Core\MVC\Symfony\SiteAccess\Router $router |
|
142
|
|
|
* |
|
143
|
|
|
* @depends testConstruct |
|
144
|
|
|
*/ |
|
145
|
|
|
public function testMatchWithRequestHeader(Router $router) |
|
146
|
|
|
{ |
|
147
|
|
|
$saName = 'headerbased_sa'; |
|
148
|
|
|
$request = Request::create('/foo/bar'); |
|
149
|
|
|
$request->headers->set('X-Siteaccess', $saName); |
|
150
|
|
|
$sa = $router->match( |
|
151
|
|
|
new SimplifiedRequest( |
|
152
|
|
|
array( |
|
153
|
|
|
'headers' => $request->headers->all(), |
|
154
|
|
|
) |
|
155
|
|
|
) |
|
156
|
|
|
); |
|
157
|
|
|
$this->assertInstanceOf('eZ\\Publish\\Core\\MVC\\Symfony\\SiteAccess', $sa); |
|
158
|
|
|
$this->assertSame($saName, $sa->name); |
|
159
|
|
|
$this->assertSame('header', $sa->matchingType); |
|
160
|
|
|
$router->setSiteAccess(); |
|
161
|
|
|
} |
|
162
|
|
|
|
|
163
|
|
|
public function matchProvider() |
|
164
|
|
|
{ |
|
165
|
|
|
return array( |
|
166
|
|
|
array(SimplifiedRequest::fromUrl('http://example.com'), 'default_sa'), |
|
167
|
|
|
array(SimplifiedRequest::fromUrl('https://example.com'), 'default_sa'), |
|
168
|
|
|
array(SimplifiedRequest::fromUrl('http://example.com/'), 'default_sa'), |
|
169
|
|
|
array(SimplifiedRequest::fromUrl('https://example.com/'), 'default_sa'), |
|
170
|
|
|
array(SimplifiedRequest::fromUrl('http://example.com//'), 'default_sa'), |
|
171
|
|
|
array(SimplifiedRequest::fromUrl('https://example.com//'), 'default_sa'), |
|
172
|
|
|
array(SimplifiedRequest::fromUrl('http://example.com:8080/'), 'default_sa'), |
|
173
|
|
|
array(SimplifiedRequest::fromUrl('http://example.com/first_siteaccess/'), 'default_sa'), |
|
174
|
|
|
array(SimplifiedRequest::fromUrl('http://example.com/?first_siteaccess'), 'default_sa'), |
|
175
|
|
|
array(SimplifiedRequest::fromUrl('http://example.com/?first_sa'), 'default_sa'), |
|
176
|
|
|
array(SimplifiedRequest::fromUrl('http://example.com/first_salt'), 'default_sa'), |
|
177
|
|
|
array(SimplifiedRequest::fromUrl('http://example.com/first_sa.foo'), 'default_sa'), |
|
178
|
|
|
array(SimplifiedRequest::fromUrl('http://example.com/test'), 'default_sa'), |
|
179
|
|
|
array(SimplifiedRequest::fromUrl('http://example.com/test/foo/'), 'default_sa'), |
|
180
|
|
|
array(SimplifiedRequest::fromUrl('http://example.com/test/foo/bar/'), 'default_sa'), |
|
181
|
|
|
array(SimplifiedRequest::fromUrl('http://example.com/test/foo/bar/first_sa'), 'default_sa'), |
|
182
|
|
|
array(SimplifiedRequest::fromUrl('http://example.com/default_sa'), 'default_sa'), |
|
183
|
|
|
|
|
184
|
|
|
array(SimplifiedRequest::fromUrl('http://example.com/first_sa'), 'first_sa'), |
|
185
|
|
|
array(SimplifiedRequest::fromUrl('http://example.com/first_sa/'), 'first_sa'), |
|
186
|
|
|
array(SimplifiedRequest::fromUrl('http://example.com/first_sa//'), 'first_sa'), |
|
187
|
|
|
// Double slashes shouldn't be considered as one |
|
188
|
|
|
array(SimplifiedRequest::fromUrl('http://example.com//first_sa//'), 'default_sa'), |
|
189
|
|
|
array(SimplifiedRequest::fromUrl('http://example.com/first_sa///test'), 'first_sa'), |
|
190
|
|
|
array(SimplifiedRequest::fromUrl('http://example.com/first_sa/foo'), 'first_sa'), |
|
191
|
|
|
array(SimplifiedRequest::fromUrl('http://example.com/first_sa/foo/bar'), 'first_sa'), |
|
192
|
|
|
array(SimplifiedRequest::fromUrl('http://example.com:82/first_sa/'), 'first_sa'), |
|
193
|
|
|
array(SimplifiedRequest::fromUrl('http://third_siteaccess/first_sa/'), 'first_sa'), |
|
194
|
|
|
array(SimplifiedRequest::fromUrl('http://first_sa/'), 'first_sa'), |
|
195
|
|
|
array(SimplifiedRequest::fromUrl('https://first_sa/'), 'first_sa'), |
|
196
|
|
|
array(SimplifiedRequest::fromUrl('http://first_sa:81/'), 'first_sa'), |
|
197
|
|
|
array(SimplifiedRequest::fromUrl('http://first_siteaccess/'), 'first_sa'), |
|
198
|
|
|
array(SimplifiedRequest::fromUrl('http://first_siteaccess:82/'), 'first_sa'), |
|
199
|
|
|
array(SimplifiedRequest::fromUrl('http://first_siteaccess:83/'), 'first_sa'), |
|
200
|
|
|
array(SimplifiedRequest::fromUrl('http://first_siteaccess/foo/'), 'first_sa'), |
|
201
|
|
|
array(SimplifiedRequest::fromUrl('http://first_siteaccess:82/foo/'), 'first_sa'), |
|
202
|
|
|
array(SimplifiedRequest::fromUrl('http://first_siteaccess:83/foo/'), 'first_sa'), |
|
203
|
|
|
|
|
204
|
|
|
array(SimplifiedRequest::fromUrl('http://example.com/second_sa'), 'second_sa'), |
|
205
|
|
|
array(SimplifiedRequest::fromUrl('http://example.com/second_sa/'), 'second_sa'), |
|
206
|
|
|
array(SimplifiedRequest::fromUrl('http://example.com/second_sa?param1=foo'), 'second_sa'), |
|
207
|
|
|
array(SimplifiedRequest::fromUrl('http://example.com/second_sa/foo/'), 'second_sa'), |
|
208
|
|
|
array(SimplifiedRequest::fromUrl('http://example.com:82/second_sa/'), 'second_sa'), |
|
209
|
|
|
array(SimplifiedRequest::fromUrl('http://example.com:83/second_sa/'), 'second_sa'), |
|
210
|
|
|
array(SimplifiedRequest::fromUrl('http://first_siteaccess:82/second_sa/'), 'second_sa'), |
|
211
|
|
|
array(SimplifiedRequest::fromUrl('http://first_siteaccess:83/second_sa/'), 'second_sa'), |
|
212
|
|
|
|
|
213
|
|
|
array(SimplifiedRequest::fromUrl('http://example.com:81/'), 'third_sa'), |
|
214
|
|
|
array(SimplifiedRequest::fromUrl('https://example.com:81/'), 'third_sa'), |
|
215
|
|
|
array(SimplifiedRequest::fromUrl('http://example.com:81/foo'), 'third_sa'), |
|
216
|
|
|
array(SimplifiedRequest::fromUrl('http://example.com:81/foo/bar'), 'third_sa'), |
|
217
|
|
|
|
|
218
|
|
|
array(SimplifiedRequest::fromUrl('http://example.com:82/'), 'fourth_sa'), |
|
219
|
|
|
array(SimplifiedRequest::fromUrl('https://example.com:82/'), 'fourth_sa'), |
|
220
|
|
|
array(SimplifiedRequest::fromUrl('https://example.com:82/foo'), 'fourth_sa'), |
|
221
|
|
|
|
|
222
|
|
|
array(SimplifiedRequest::fromUrl('http://fr.ezpublish.dev/eng'), 'fr_eng'), |
|
223
|
|
|
array(SimplifiedRequest::fromUrl('http://us.ezpublish.dev/fre'), 'fr_us'), |
|
224
|
|
|
); |
|
225
|
|
|
} |
|
226
|
|
|
|
|
227
|
|
|
/** |
|
228
|
|
|
* @expectedException \InvalidArgumentException |
|
229
|
|
|
*/ |
|
230
|
|
|
public function testMatchByNameInvalidSiteAccess() |
|
231
|
|
|
{ |
|
232
|
|
|
$matcherBuilder = $this->getMock('eZ\Publish\Core\MVC\Symfony\SiteAccess\MatcherBuilderInterface'); |
|
233
|
|
|
$logger = $this->getMock('Psr\Log\LoggerInterface'); |
|
234
|
|
|
$router = new Router($matcherBuilder, $logger, 'default_sa', array(), array('foo', 'default_sa')); |
|
235
|
|
|
$router->matchByName('bar'); |
|
236
|
|
|
} |
|
237
|
|
|
|
|
238
|
|
|
public function testMatchByName() |
|
239
|
|
|
{ |
|
240
|
|
|
$matcherBuilder = $this->getMock('eZ\Publish\Core\MVC\Symfony\SiteAccess\MatcherBuilderInterface'); |
|
241
|
|
|
$logger = $this->getMock('Psr\Log\LoggerInterface'); |
|
242
|
|
|
$matcherClass = 'Map\Host'; |
|
243
|
|
|
$matchedSiteAccess = 'foo'; |
|
244
|
|
|
$matcherConfig = array( |
|
245
|
|
|
'phoenix-rises.fm' => $matchedSiteAccess, |
|
246
|
|
|
); |
|
247
|
|
|
$config = array( |
|
248
|
|
|
'Map\URI' => array('default' => 'default_sa'), |
|
249
|
|
|
$matcherClass => $matcherConfig, |
|
250
|
|
|
); |
|
251
|
|
|
|
|
252
|
|
|
$router = new Router($matcherBuilder, $logger, 'default_sa', $config, array($matchedSiteAccess, 'default_sa')); |
|
253
|
|
|
$matcherInitialSA = $this->getMock('eZ\Publish\Core\MVC\Symfony\SiteAccess\URILexer'); |
|
254
|
|
|
$router->setSiteAccess(new SiteAccess('test', 'test', $matcherInitialSA)); |
|
255
|
|
|
$matcherInitialSA |
|
256
|
|
|
->expects($this->once()) |
|
257
|
|
|
->method('analyseURI'); |
|
258
|
|
|
|
|
259
|
|
|
$matcher = $this->getMock('eZ\Publish\Core\MVC\Symfony\SiteAccess\VersatileMatcher'); |
|
260
|
|
|
$matcherBuilder |
|
261
|
|
|
->expects($this->exactly(2)) |
|
262
|
|
|
->method('buildMatcher') |
|
263
|
|
|
->will( |
|
264
|
|
|
$this->onConsecutiveCalls( |
|
265
|
|
|
$this->getMock('eZ\Publish\Core\MVC\Symfony\SiteAccess\Matcher'), |
|
266
|
|
|
$matcher |
|
267
|
|
|
) |
|
268
|
|
|
); |
|
269
|
|
|
|
|
270
|
|
|
$reverseMatchedMatcher = $this->getMock('eZ\Publish\Core\MVC\Symfony\SiteAccess\VersatileMatcher'); |
|
271
|
|
|
$matcher |
|
272
|
|
|
->expects($this->once()) |
|
273
|
|
|
->method('reverseMatch') |
|
274
|
|
|
->with($matchedSiteAccess) |
|
275
|
|
|
->will($this->returnValue($reverseMatchedMatcher)); |
|
276
|
|
|
|
|
277
|
|
|
$siteAccess = $router->matchByName($matchedSiteAccess); |
|
278
|
|
|
$this->assertInstanceOf('eZ\Publish\Core\MVC\Symfony\SiteAccess', $siteAccess); |
|
279
|
|
|
$this->assertSame($reverseMatchedMatcher, $siteAccess->matcher); |
|
280
|
|
|
$this->assertSame($matchedSiteAccess, $siteAccess->name); |
|
281
|
|
|
} |
|
282
|
|
|
|
|
283
|
|
|
public function testMatchByNameNoVersatileMatcher() |
|
284
|
|
|
{ |
|
285
|
|
|
$matcherBuilder = $this->getMock('eZ\Publish\Core\MVC\Symfony\SiteAccess\MatcherBuilderInterface'); |
|
286
|
|
|
$logger = $this->getMock('Psr\Log\LoggerInterface'); |
|
287
|
|
|
$matcherClass = 'Map\Host'; |
|
288
|
|
|
$defaultSiteAccess = 'default_sa'; |
|
289
|
|
|
$matcherConfig = array( |
|
290
|
|
|
'phoenix-rises.fm' => 'foo', |
|
291
|
|
|
); |
|
292
|
|
|
$config = array($matcherClass => $matcherConfig); |
|
293
|
|
|
|
|
294
|
|
|
$router = new Router($matcherBuilder, $logger, $defaultSiteAccess, $config, array($defaultSiteAccess, 'foo')); |
|
295
|
|
|
$router->setSiteAccess(new SiteAccess('test', 'test')); |
|
296
|
|
|
$request = $router->getRequest(); |
|
297
|
|
|
$matcherBuilder |
|
298
|
|
|
->expects($this->once()) |
|
299
|
|
|
->method('buildMatcher') |
|
300
|
|
|
->with($matcherClass, $matcherConfig, $request) |
|
301
|
|
|
->will($this->returnValue($this->getMock('eZ\Publish\Core\MVC\Symfony\SiteAccess\Matcher'))); |
|
302
|
|
|
|
|
303
|
|
|
$logger |
|
304
|
|
|
->expects($this->once()) |
|
305
|
|
|
->method('notice'); |
|
306
|
|
|
$this->assertEquals(new SiteAccess($defaultSiteAccess, 'default'), $router->matchByName($defaultSiteAccess)); |
|
307
|
|
|
} |
|
308
|
|
|
} |
|
309
|
|
|
|