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