1
2
3
4
5
6
7
8
9
10
11
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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
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
|
<?php
declare(strict_types=1);
namespace Drupal\Tests\Core;
use Drupal\Core\DependencyInjection\ContainerBuilder;
use Drupal\Core\Url;
use Drupal\Tests\UnitTestCase;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Routing\Exception\ResourceNotFoundException;
/**
* @coversDefaultClass \Drupal\Core\Url
* @group UrlTest
*/
class UnroutedUrlTest extends UnitTestCase {
/**
* The URL assembler.
*
* @var \Drupal\Core\Utility\UnroutedUrlAssemblerInterface|\PHPUnit\Framework\MockObject\MockObject
*/
protected $urlAssembler;
/**
* The router.
*
* @var \Drupal\Tests\Core\Routing\TestRouterInterface|\PHPUnit\Framework\MockObject\MockObject
*/
protected $router;
/**
* An unrouted, external URL to test.
*
* @var string
*/
protected $unroutedExternal = 'https://www.drupal.org';
/**
* An unrouted, internal URL to test.
*
* @var string
*/
protected $unroutedInternal = 'base:robots.txt';
/**
* {@inheritdoc}
*/
protected function setUp(): void {
parent::setUp();
$this->urlAssembler = $this->createMock('Drupal\Core\Utility\UnroutedUrlAssemblerInterface');
$this->urlAssembler->expects($this->any())
->method('assemble')
->willReturnArgument(0);
$this->router = $this->createMock('Drupal\Tests\Core\Routing\TestRouterInterface');
$container = new ContainerBuilder();
$container->set('router.no_access_checks', $this->router);
$container->set('unrouted_url_assembler', $this->urlAssembler);
\Drupal::setContainer($container);
}
/**
* Tests the fromUri() method.
*
* @covers ::fromUri
*
* @dataProvider providerFromUri
*/
public function testFromUri($uri, $is_external): void {
$url = Url::fromUri($uri);
$this->assertInstanceOf('Drupal\Core\Url', $url);
}
/**
* Data provider for testFromUri().
*/
public static function providerFromUri() {
return [
// [$uri, $is_external]
// An external URI.
['https://www.drupal.org', TRUE],
// A protocol-relative URL.
['//www.drupal.org', TRUE],
// An internal, unrouted, base-relative URI.
['base:robots.txt', FALSE],
// Base-relative URIs with special characters.
['base:AKI@&hO@', FALSE],
['base:(:;2&+h^', FALSE],
// Various token formats.
['base:node/[token]', FALSE],
['base:node/%', FALSE],
['base:node/[token:token]', FALSE],
['base:node/{{ token }}', FALSE],
];
}
/**
* Tests the fromUri() method.
*
* @covers ::fromUri
* @dataProvider providerFromInvalidUri
*/
public function testFromInvalidUri($uri): void {
$this->expectException(\InvalidArgumentException::class);
Url::fromUri($uri);
}
/**
* Data provider for testFromInvalidUri().
*/
public static function providerFromInvalidUri() {
return [
// Schemeless paths.
['test'],
['/test'],
// Schemeless path with a query string.
['foo?bar'],
// Only a query string.
['?bar'],
// Only a fragment.
['#foo'],
// Disallowed characters in the authority (host name) that are valid
// elsewhere in the path.
['base://(:;2&+h^'],
];
}
/**
* Tests the createFromRequest method.
*
* @covers ::createFromRequest
*/
public function testCreateFromRequest(): void {
$request = Request::create('/test-path');
$this->router->expects($this->once())
->method('matchRequest')
->with($request)
->will($this->throwException(new ResourceNotFoundException()));
$this->expectException(ResourceNotFoundException::class);
Url::createFromRequest($request);
}
/**
* Tests the isExternal() method.
*
* @depends testFromUri
* @dataProvider providerFromUri
*
* @covers ::isExternal
*/
public function testIsExternal($uri, $is_external): void {
$url = Url::fromUri($uri);
$this->assertSame($url->isExternal(), $is_external);
}
/**
* Tests the toString() method.
*
* @depends testFromUri
* @dataProvider providerFromUri
*
* @covers ::toString
*/
public function testToString($uri): void {
$url = Url::fromUri($uri);
$this->assertSame($uri, $url->toString());
}
/**
* Tests the getRouteName() method.
*
* @depends testFromUri
* @dataProvider providerFromUri
*
* @covers ::getRouteName
*/
public function testGetRouteName($uri): void {
$url = Url::fromUri($uri);
$this->expectException(\UnexpectedValueException::class);
$url->getRouteName();
}
/**
* Tests the getRouteParameters() method.
*
* @depends testFromUri
* @dataProvider providerFromUri
*
* @covers ::getRouteParameters
*/
public function testGetRouteParameters($uri): void {
$url = Url::fromUri($uri);
$this->expectException(\UnexpectedValueException::class);
$url->getRouteParameters();
}
/**
* Tests the getInternalPath() method.
*
* @depends testFromUri
* @dataProvider providerFromUri
*
* @covers ::getInternalPath
*/
public function testGetInternalPath($uri): void {
$url = Url::fromUri($uri);
$this->expectException(\Exception::class);
$url->getInternalPath();
}
/**
* Tests the getPath() method.
*
* @depends testFromUri
* @dataProvider providerFromUri
*
* @covers ::getUri
*/
public function testGetUri($uri): void {
$url = Url::fromUri($uri);
$this->assertNotNull($url->getUri());
}
/**
* Tests the getOptions() method.
*
* @depends testFromUri
* @dataProvider providerFromUri
*
* @covers ::getOptions
*/
public function testGetOptions($uri): void {
$url = Url::fromUri($uri);
$this->assertIsArray($url->getOptions());
}
}
|