Newer
Older
Angie Byron
committed
<?php
namespace Drupal\Tests\Core\Path;
use Drupal\Core\ParamConverter\ParamNotConvertedException;
use Drupal\Core\Path\PathValidator;
use Drupal\Tests\UnitTestCase;
use Symfony\Cmf\Component\Routing\RouteObjectInterface;
use Symfony\Component\HttpFoundation\ParameterBag;
use Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException;
use Symfony\Component\Routing\Exception\MethodNotAllowedException;
Angie Byron
committed
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
use Symfony\Component\Routing\Exception\ResourceNotFoundException;
/**
* @coversDefaultClass \Drupal\Core\Path\PathValidator
* @group Routing
*/
class PathValidatorTest extends UnitTestCase {
/**
* The mocked access aware router.
*
* @var \Drupal\Core\Routing\AccessAwareRouterInterface|\PHPUnit_Framework_MockObject_MockObject
*/
protected $accessAwareRouter;
/**
* The mocked access unaware router.
* @var \Symfony\Component\Routing\Matcher\UrlMatcherInterface|\PHPUnit_Framework_MockObject_MockObject
*/
protected $accessUnawareRouter;
/**
* The mocked account.
*
* @var \Drupal\Core\Session\AccountInterface|\PHPUnit_Framework_MockObject_MockObject
*/
protected $account;
/**
* The path processor.
*
* @var \Drupal\Core\PathProcessor\InboundPathProcessorInterface|\PHPUnit_Framework_MockObject_MockObject
*/
protected $pathProcessor;
/**
* The tested path validator.
*
* @var \Drupal\Core\Path\PathValidator
*/
protected $pathValidator;
/**
* {@inheritdoc}
*/
protected function setUp() {
parent::setUp();
$this->accessAwareRouter = $this->getMock('Drupal\Core\Routing\AccessAwareRouterInterface');
$this->accessUnawareRouter = $this->getMock('Symfony\Component\Routing\Matcher\UrlMatcherInterface');
$this->account = $this->getMock('Drupal\Core\Session\AccountInterface');
$this->pathProcessor = $this->getMock('Drupal\Core\PathProcessor\InboundPathProcessorInterface');
$this->pathValidator = new PathValidator($this->accessAwareRouter, $this->accessUnawareRouter, $this->account, $this->pathProcessor);
}
/**
* Tests the isValid() method for the frontpage.
*
* @covers ::isValid
*/
public function testIsValidWithFrontpage() {
$this->accessAwareRouter->expects($this->never())
->method('match');
$this->assertTrue($this->pathValidator->isValid('<front>'));
}
Angie Byron
committed
/**
* Tests the isValid() method for <none> (used for jumplinks).
*
* @covers ::isValid
*/
public function testIsValidWithNone() {
$this->accessAwareRouter->expects($this->never())
->method('match');
$this->assertTrue($this->pathValidator->isValid('<none>'));
}
Angie Byron
committed
/**
* Tests the isValid() method for an external URL.
*
* @covers ::isValid
*/
public function testIsValidWithExternalUrl() {
$this->accessAwareRouter->expects($this->never())
->method('match');
$this->assertTrue($this->pathValidator->isValid('https://www.drupal.org'));
Angie Byron
committed
}
/**
* Tests the isValid() method with an invalid external URL.
Angie Byron
committed
*/
public function testIsValidWithInvalidExternalUrl() {
$this->accessAwareRouter->expects($this->never())
->method('match');
$this->assertFalse($this->pathValidator->isValid('http://'));
}
/**
* Tests the isValid() method with a 'link to any page' permission.
*
* @covers ::isValid
Angie Byron
committed
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
*/
public function testIsValidWithLinkToAnyPageAccount() {
$this->account->expects($this->once())
->method('hasPermission')
->with('link to any page')
->willReturn(TRUE);
$this->accessAwareRouter->expects($this->never())
->method('match');
$this->accessUnawareRouter->expects($this->once())
->method('match')
->with('/test-path')
->willReturn([RouteObjectInterface::ROUTE_NAME => 'test_route', '_raw_variables' => new ParameterBag(['key' => 'value'])]);
$this->pathProcessor->expects($this->once())
->method('processInbound')
->willReturnArgument(0);
$this->assertTrue($this->pathValidator->isValid('test-path'));
}
/**
* Tests the isValid() method without the 'link to any page' permission.
*
* @covers ::isValid
*/
public function testIsValidWithoutLinkToAnyPageAccount() {
$this->account->expects($this->once())
->method('hasPermission')
->with('link to any page')
->willReturn(FALSE);
$this->accessUnawareRouter->expects($this->never())
->method('match');
$this->accessAwareRouter->expects($this->once())
->method('match')
->with('/test-path')
->willReturn([RouteObjectInterface::ROUTE_NAME => 'test_route', '_raw_variables' => new ParameterBag(['key' => 'value'])]);
$this->pathProcessor->expects($this->once())
->method('processInbound')
->willReturnArgument(0);
$this->assertTrue($this->pathValidator->isValid('test-path'));
}
/**
* Tests the isValid() method with a path alias.
*
* @covers ::isValid
*/
public function testIsValidWithPathAlias() {
$this->account->expects($this->once())
->method('hasPermission')
->with('link to any page')
->willReturn(FALSE);
$this->accessUnawareRouter->expects($this->never())
->method('match');
$this->accessAwareRouter->expects($this->once())
->method('match')
->with('/test-path')
->willReturn([RouteObjectInterface::ROUTE_NAME => 'test_route', '_raw_variables' => new ParameterBag(['key' => 'value'])]);
$this->pathProcessor->expects($this->once())
->method('processInbound')
->with('/path-alias', $this->anything())
->willReturn('/test-path');
Angie Byron
committed
$this->assertTrue($this->pathValidator->isValid('path-alias'));
}
/**
* Tests the isValid() method with a user without access to the path.
*
* @covers ::isValid
Angie Byron
committed
*/
public function testIsValidWithAccessDenied() {
$this->account->expects($this->once())
->method('hasPermission')
->with('link to any page')
->willReturn(FALSE);
$this->accessUnawareRouter->expects($this->never())
->method('match');
$this->accessAwareRouter->expects($this->once())
->method('match')
->with('/test-path')
->willThrowException(new AccessDeniedHttpException());
$this->pathProcessor->expects($this->once())
->method('processInbound')
->willReturnArgument(0);
$this->assertFalse($this->pathValidator->isValid('test-path'));
}
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
/**
* @covers ::isValid
* @covers ::getPathAttributes
*/
public function testIsValidWithResourceNotFound() {
$this->account->expects($this->once())
->method('hasPermission')
->with('link to any page')
->willReturn(FALSE);
$this->accessUnawareRouter->expects($this->never())
->method('match');
$this->accessAwareRouter->expects($this->once())
->method('match')
->with('/test-path')
->willThrowException(new ResourceNotFoundException());
$this->pathProcessor->expects($this->once())
->method('processInbound')
->willReturnArgument(0);
$this->assertFalse($this->pathValidator->isValid('test-path'));
}
/**
* @covers ::isValid
* @covers ::getPathAttributes
*/
public function testIsValidWithParamNotConverted() {
$this->account->expects($this->once())
->method('hasPermission')
->with('link to any page')
->willReturn(FALSE);
$this->accessUnawareRouter->expects($this->never())
->method('match');
$this->accessAwareRouter->expects($this->once())
->method('match')
->with('/test-path')
->willThrowException(new ParamNotConvertedException());
$this->pathProcessor->expects($this->once())
->method('processInbound')
->willReturnArgument(0);
$this->assertFalse($this->pathValidator->isValid('test-path'));
}
/**
* @covers ::isValid
* @covers ::getPathAttributes
*/
public function testIsValidWithMethodNotAllowed() {
$this->account->expects($this->once())
->method('hasPermission')
->with('link to any page')
->willReturn(FALSE);
$this->accessUnawareRouter->expects($this->never())
->method('match');
$this->accessAwareRouter->expects($this->once())
->method('match')
->with('/test-path')
->willThrowException(new MethodNotAllowedException([]));
$this->pathProcessor->expects($this->once())
->method('processInbound')
->willReturnArgument(0);
$this->assertFalse($this->pathValidator->isValid('test-path'));
}
Angie Byron
committed
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
/**
* Tests the isValid() method with a not working param converting.
*
* @covers ::isValid
*/
public function testIsValidWithFailingParameterConverting() {
$this->account->expects($this->once())
->method('hasPermission')
->with('link to any page')
->willReturn(FALSE);
$this->accessUnawareRouter->expects($this->never())
->method('match');
$this->accessAwareRouter->expects($this->once())
->method('match')
->with('/entity-test/1')
->willThrowException(new ParamNotConvertedException());
$this->pathProcessor->expects($this->once())
->method('processInbound')
->willReturnArgument(0);
$this->assertFalse($this->pathValidator->isValid('entity-test/1'));
}
/**
* Tests the isValid() method with a not existing path.
*
* @covers ::isValid
*/
public function testIsValidWithNotExistingPath() {
$this->account->expects($this->once())
->method('hasPermission')
->with('link to any page')
->willReturn(FALSE);
$this->accessUnawareRouter->expects($this->never())
->method('match');
$this->accessAwareRouter->expects($this->once())
->method('match')
->with('/not-existing-path')
->willThrowException(new ResourceNotFoundException());
$this->pathProcessor->expects($this->once())
->method('processInbound')
->willReturnArgument(0);
$this->assertFalse($this->pathValidator->isValid('not-existing-path'));
}
/**
* Tests the getUrlIfValid() method when there is access.
*
* @covers ::getUrlIfValid
* @covers ::getPathAttributes
Angie Byron
committed
*/
public function testGetUrlIfValidWithAccess() {
Alex Pott
committed
$this->account->expects($this->exactly(2))
Angie Byron
committed
->method('hasPermission')
->with('link to any page')
->willReturn(FALSE);
Alex Pott
committed
$this->accessAwareRouter->expects($this->exactly(2))
Angie Byron
committed
->method('match')
->with('/test-path')
->willReturn([RouteObjectInterface::ROUTE_NAME => 'test_route', '_raw_variables' => new ParameterBag(['key' => 'value'])]);
Alex Pott
committed
$this->pathProcessor->expects($this->exactly(2))
Angie Byron
committed
->method('processInbound')
->willReturnArgument(0);
$url = $this->pathValidator->getUrlIfValid('test-path');
$this->assertInstanceOf('Drupal\Core\Url', $url);
$this->assertEquals('test_route', $url->getRouteName());
Alex Pott
committed
$this->assertEquals(['key' => 'value'], $url->getRouteParameters());
// Test with leading /.
$url = $this->pathValidator->getUrlIfValid('/test-path');
$this->assertInstanceOf('Drupal\Core\Url', $url);
$this->assertEquals('test_route', $url->getRouteName());
Angie Byron
committed
$this->assertEquals(['key' => 'value'], $url->getRouteParameters());
}
/**
* Tests the getUrlIfValid() method with a query in the path.
Angie Byron
committed
*/
public function testGetUrlIfValidWithQuery() {
$this->account->expects($this->once())
->method('hasPermission')
->with('link to any page')
->willReturn(FALSE);
$this->accessAwareRouter->expects($this->once())
->method('match')
->with('/test-path?k=bar')
->willReturn([RouteObjectInterface::ROUTE_NAME => 'test_route', '_raw_variables' => new ParameterBag()]);
$this->pathProcessor->expects($this->once())
->method('processInbound')
->willReturnArgument(0);
$url = $this->pathValidator->getUrlIfValid('test-path?k=bar');
$this->assertInstanceOf('Drupal\Core\Url', $url);
$this->assertEquals('test_route', $url->getRouteName());
$this->assertEquals(['k' => 'bar'], $url->getOptions()['query']);
}
/**
* Tests the getUrlIfValid() method where there is no access.
Angie Byron
committed
*/
public function testGetUrlIfValidWithoutAccess() {
$this->account->expects($this->once())
->method('hasPermission')
->with('link to any page')
->willReturn(FALSE);
$this->accessAwareRouter->expects($this->once())
->method('match')
->with('/test-path')
->willThrowException(new AccessDeniedHttpException());
$this->pathProcessor->expects($this->once())
->method('processInbound')
->willReturnArgument(0);
$url = $this->pathValidator->getUrlIfValid('test-path');
$this->assertFalse($url);
}
/**
* Tests the getUrlIfValid() method with a front page + query + fragments.
Angie Byron
committed
*/
public function testGetUrlIfValidWithFrontPageAndQueryAndFragments() {
$url = $this->pathValidator->getUrlIfValid('<front>?hei=sen#berg');
$this->assertEquals('<front>', $url->getRouteName());
$this->assertEquals(['hei' => 'sen'], $url->getOptions()['query']);
$this->assertEquals('berg', $url->getOptions()['fragment']);
}
/**
* Tests the getUrlIfValidWithoutAccessCheck() method.
*
* @covers ::getUrlIfValidWithoutAccessCheck
*/
public function testGetUrlIfValidWithoutAccessCheck() {
$this->account->expects($this->never())
->method('hasPermission')
->with('link to any page');
$this->accessAwareRouter->expects($this->never())
->method('match');
$this->accessUnawareRouter->expects($this->once())
->method('match')
->with('/test-path')
->willReturn([RouteObjectInterface::ROUTE_NAME => 'test_route', '_raw_variables' => new ParameterBag(['key' => 'value'])]);
$this->pathProcessor->expects($this->once())
->method('processInbound')
->willReturnArgument(0);
$url = $this->pathValidator->getUrlIfValidWithoutAccessCheck('test-path');
$this->assertInstanceOf('Drupal\Core\Url', $url);
$this->assertEquals('test_route', $url->getRouteName());
$this->assertEquals(['key' => 'value'], $url->getRouteParameters());
}