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);
78 * Data provider for testFromUri().
80 public function providerFromUri() {
82 // [$uri, $is_external]
84 ['https://www.drupal.org', TRUE],
85 // A protocol-relative URL.
86 ['//www.drupal.org', TRUE],
87 // An internal, unrouted, base-relative URI.
88 ['base:robots.txt', FALSE],
89 // Base-relative URIs with special characters.
90 ['base:AKI@&hO@', FALSE],
91 ['base:(:;2&+h^', FALSE],
92 // Various token formats.
93 ['base:node/[token]', FALSE],
94 ['base:node/%', FALSE],
95 ['base:node/[token:token]', FALSE],
96 ['base:node/{{ token }}', FALSE],
101 * Tests the fromUri() method.
104 * @dataProvider providerFromInvalidUri
106 public function testFromInvalidUri($uri) {
107 $this->setExpectedException(\InvalidArgumentException::class);
108 $url = Url::fromUri($uri);
112 * Data provider for testFromInvalidUri().
114 public function providerFromInvalidUri() {
119 // Schemeless path with a query string.
121 // Only a query string.
125 // Disallowed characters in the authority (host name) that are valid
126 // elsewhere in the path.
133 * Tests the createFromRequest method.
135 * @covers ::createFromRequest
137 public function testCreateFromRequest() {
138 $request = Request::create('/test-path');
140 $this->router->expects($this->once())
141 ->method('matchRequest')
143 ->will($this->throwException(new ResourceNotFoundException()));
145 $this->setExpectedException(ResourceNotFoundException::class);
146 Url::createFromRequest($request);
150 * Tests the isExternal() method.
152 * @depends testFromUri
153 * @dataProvider providerFromUri
155 * @covers ::isExternal
157 public function testIsExternal($uri, $is_external) {
158 $url = Url::fromUri($uri);
159 $this->assertSame($url->isExternal(), $is_external);
163 * Tests the toString() method.
165 * @depends testFromUri
166 * @dataProvider providerFromUri
170 public function testToString($uri) {
171 $url = Url::fromUri($uri);
172 $this->assertSame($uri, $url->toString());
176 * Tests the getRouteName() method.
178 * @depends testFromUri
179 * @dataProvider providerFromUri
181 * @covers ::getRouteName
183 public function testGetRouteName($uri) {
184 $url = Url::fromUri($uri);
185 $this->setExpectedException(\UnexpectedValueException::class);
186 $url->getRouteName();
190 * Tests the getRouteParameters() method.
192 * @depends testFromUri
193 * @dataProvider providerFromUri
195 * @covers ::getRouteParameters
197 public function testGetRouteParameters($uri) {
198 $url = Url::fromUri($uri);
199 $this->setExpectedException(\UnexpectedValueException::class);
200 $url->getRouteParameters();
204 * Tests the getInternalPath() method.
206 * @depends testFromUri
207 * @dataProvider providerFromUri
209 * @covers ::getInternalPath
211 public function testGetInternalPath($uri) {
212 $url = Url::fromUri($uri);
213 $this->setExpectedException(\Exception::class);
214 $url->getInternalPath();
218 * Tests the getPath() method.
220 * @depends testFromUri
221 * @dataProvider providerFromUri
225 public function testGetUri($uri) {
226 $url = Url::fromUri($uri);
227 $this->assertNotNull($url->getUri());
231 * Tests the getOptions() method.
233 * @depends testFromUri
234 * @dataProvider providerFromUri
236 * @covers ::getOptions
238 public function testGetOptions($uri) {
239 $url = Url::fromUri($uri);
240 $this->assertInternalType('array', $url->getOptions());