3 namespace Drupal\Tests\Core;
5 use Drupal\Core\DependencyInjection\ContainerBuilder;
7 use Drupal\Tests\UnitTestCase;
8 use Symfony\Component\HttpFoundation\Request;
9 use Symfony\Component\Routing\Exception\ResourceNotFoundException;
12 * @coversDefaultClass \Drupal\Core\Url
15 class UnroutedUrlTest extends UnitTestCase {
20 * @var \Drupal\Core\Utility\UnroutedUrlAssemblerInterface|\PHPUnit_Framework_MockObject_MockObject
22 protected $urlAssembler;
27 * @var \Drupal\Tests\Core\Routing\TestRouterInterface|\PHPUnit_Framework_MockObject_MockObject
32 * An unrouted, external URL to test.
36 protected $unroutedExternal = 'https://www.drupal.org';
39 * An unrouted, internal URL to test.
43 protected $unroutedInternal = 'base:robots.txt';
48 protected function setUp() {
51 $this->urlAssembler = $this->getMock('Drupal\Core\Utility\UnroutedUrlAssemblerInterface');
52 $this->urlAssembler->expects($this->any())
54 ->will($this->returnArgument(0));
56 $this->router = $this->getMock('Drupal\Tests\Core\Routing\TestRouterInterface');
57 $container = new ContainerBuilder();
58 $container->set('router.no_access_checks', $this->router);
59 $container->set('unrouted_url_assembler', $this->urlAssembler);
60 \Drupal::setContainer($container);
64 * Tests the fromUri() method.
68 * @dataProvider providerFromUri
70 public function testFromUri($uri, $is_external) {
71 $url = Url::fromUri($uri);
73 $this->assertInstanceOf('Drupal\Core\Url', $url);
77 * Data provider for testFromUri().
79 public function providerFromUri() {
81 // [$uri, $is_external]
83 ['https://www.drupal.org', TRUE],
84 // A protocol-relative URL.
85 ['//www.drupal.org', TRUE],
86 // An internal, unrouted, base-relative URI.
87 ['base:robots.txt', FALSE],
88 // Base-relative URIs with special characters.
89 ['base:AKI@&hO@', FALSE],
90 ['base:(:;2&+h^', FALSE],
91 // Various token formats.
92 ['base:node/[token]', FALSE],
93 ['base:node/%', FALSE],
94 ['base:node/[token:token]', FALSE],
95 ['base:node/{{ token }}', FALSE],
100 * Tests the fromUri() method.
103 * @dataProvider providerFromInvalidUri
105 public function testFromInvalidUri($uri) {
106 $this->setExpectedException(\InvalidArgumentException::class);
107 $url = Url::fromUri($uri);
111 * Data provider for testFromInvalidUri().
113 public function providerFromInvalidUri() {
118 // Schemeless path with a query string.
120 // Only a query string.
124 // Disallowed characters in the authority (host name) that are valid
125 // elsewhere in the path.
132 * Tests the createFromRequest method.
134 * @covers ::createFromRequest
136 public function testCreateFromRequest() {
137 $request = Request::create('/test-path');
139 $this->router->expects($this->once())
140 ->method('matchRequest')
142 ->will($this->throwException(new ResourceNotFoundException()));
144 $this->setExpectedException(ResourceNotFoundException::class);
145 Url::createFromRequest($request);
149 * Tests the isExternal() method.
151 * @depends testFromUri
152 * @dataProvider providerFromUri
154 * @covers ::isExternal
156 public function testIsExternal($uri, $is_external) {
157 $url = Url::fromUri($uri);
158 $this->assertSame($url->isExternal(), $is_external);
162 * Tests the toString() method.
164 * @depends testFromUri
165 * @dataProvider providerFromUri
169 public function testToString($uri) {
170 $url = Url::fromUri($uri);
171 $this->assertSame($uri, $url->toString());
175 * Tests the getRouteName() method.
177 * @depends testFromUri
178 * @dataProvider providerFromUri
180 * @covers ::getRouteName
182 public function testGetRouteName($uri) {
183 $url = Url::fromUri($uri);
184 $this->setExpectedException(\UnexpectedValueException::class);
185 $url->getRouteName();
189 * Tests the getRouteParameters() method.
191 * @depends testFromUri
192 * @dataProvider providerFromUri
194 * @covers ::getRouteParameters
196 public function testGetRouteParameters($uri) {
197 $url = Url::fromUri($uri);
198 $this->setExpectedException(\UnexpectedValueException::class);
199 $url->getRouteParameters();
203 * Tests the getInternalPath() method.
205 * @depends testFromUri
206 * @dataProvider providerFromUri
208 * @covers ::getInternalPath
210 public function testGetInternalPath($uri) {
211 $url = Url::fromUri($uri);
212 $this->setExpectedException(\Exception::class);
213 $url->getInternalPath();
217 * Tests the getPath() method.
219 * @depends testFromUri
220 * @dataProvider providerFromUri
224 public function testGetUri($uri) {
225 $url = Url::fromUri($uri);
226 $this->assertNotNull($url->getUri());
230 * Tests the getOptions() method.
232 * @depends testFromUri
233 * @dataProvider providerFromUri
235 * @covers ::getOptions
237 public function testGetOptions($uri) {
238 $url = Url::fromUri($uri);
239 $this->assertInternalType('array', $url->getOptions());