Further Drupal 8.6.4 changes. Some core files were not committed before a commit...
[yaffs-website] / web / core / tests / Drupal / Tests / Core / UnroutedUrlTest.php
1 <?php
2
3 namespace Drupal\Tests\Core;
4
5 use Drupal\Core\DependencyInjection\ContainerBuilder;
6 use Drupal\Core\Url;
7 use Drupal\Tests\UnitTestCase;
8 use Symfony\Component\HttpFoundation\Request;
9 use Symfony\Component\Routing\Exception\ResourceNotFoundException;
10
11 /**
12  * @coversDefaultClass \Drupal\Core\Url
13  * @group UrlTest
14  */
15 class UnroutedUrlTest extends UnitTestCase {
16
17   /**
18    * The URL assembler
19    *
20    * @var \Drupal\Core\Utility\UnroutedUrlAssemblerInterface|\PHPUnit_Framework_MockObject_MockObject
21    */
22   protected $urlAssembler;
23
24   /**
25    * The router.
26    *
27    * @var \Drupal\Tests\Core\Routing\TestRouterInterface|\PHPUnit_Framework_MockObject_MockObject
28    */
29   protected $router;
30
31   /**
32    * An unrouted, external URL to test.
33    *
34    * @var string
35    */
36   protected $unroutedExternal = 'https://www.drupal.org';
37
38   /**
39    * An unrouted, internal URL to test.
40    *
41    * @var string
42    */
43   protected $unroutedInternal = 'base:robots.txt';
44
45   /**
46    * {@inheritdoc}
47    */
48   protected function setUp() {
49     parent::setUp();
50
51     $this->urlAssembler = $this->getMock('Drupal\Core\Utility\UnroutedUrlAssemblerInterface');
52     $this->urlAssembler->expects($this->any())
53       ->method('assemble')
54       ->will($this->returnArgument(0));
55
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);
61   }
62
63   /**
64    * Tests the fromUri() method.
65    *
66    * @covers ::fromUri
67    *
68    * @dataProvider providerFromUri
69    */
70   public function testFromUri($uri, $is_external) {
71     $url = Url::fromUri($uri);
72
73     $this->assertInstanceOf('Drupal\Core\Url', $url);
74   }
75
76   /**
77    * Data provider for testFromUri().
78    */
79   public function providerFromUri() {
80     return [
81       // [$uri, $is_external]
82       // An external URI.
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],
96     ];
97   }
98
99   /**
100    * Tests the fromUri() method.
101    *
102    * @covers ::fromUri
103    * @dataProvider providerFromInvalidUri
104    */
105   public function testFromInvalidUri($uri) {
106     $this->setExpectedException(\InvalidArgumentException::class);
107     $url = Url::fromUri($uri);
108   }
109
110   /**
111    * Data provider for testFromInvalidUri().
112    */
113   public function providerFromInvalidUri() {
114     return [
115       // Schemeless paths.
116       ['test'],
117       ['/test'],
118       // Schemeless path with a query string.
119       ['foo?bar'],
120       // Only a query string.
121       ['?bar'],
122       // Only a fragment.
123       ['#foo'],
124       // Disallowed characters in the authority (host name) that are valid
125       // elsewhere in the path.
126       ['base://(:;2&+h^'],
127       ['base://AKI@&hO@'],
128     ];
129   }
130
131   /**
132    * Tests the createFromRequest method.
133    *
134    * @covers ::createFromRequest
135    */
136   public function testCreateFromRequest() {
137     $request = Request::create('/test-path');
138
139     $this->router->expects($this->once())
140       ->method('matchRequest')
141       ->with($request)
142       ->will($this->throwException(new ResourceNotFoundException()));
143
144     $this->setExpectedException(ResourceNotFoundException::class);
145     Url::createFromRequest($request);
146   }
147
148   /**
149    * Tests the isExternal() method.
150    *
151    * @depends testFromUri
152    * @dataProvider providerFromUri
153    *
154    * @covers ::isExternal
155    */
156   public function testIsExternal($uri, $is_external) {
157     $url = Url::fromUri($uri);
158     $this->assertSame($url->isExternal(), $is_external);
159   }
160
161   /**
162    * Tests the toString() method.
163    *
164    * @depends testFromUri
165    * @dataProvider providerFromUri
166    *
167    * @covers ::toString
168    */
169   public function testToString($uri) {
170     $url = Url::fromUri($uri);
171     $this->assertSame($uri, $url->toString());
172   }
173
174   /**
175    * Tests the getRouteName() method.
176    *
177    * @depends testFromUri
178    * @dataProvider providerFromUri
179    *
180    * @covers ::getRouteName
181    */
182   public function testGetRouteName($uri) {
183     $url = Url::fromUri($uri);
184     $this->setExpectedException(\UnexpectedValueException::class);
185     $url->getRouteName();
186   }
187
188   /**
189    * Tests the getRouteParameters() method.
190    *
191    * @depends testFromUri
192    * @dataProvider providerFromUri
193    *
194    * @covers ::getRouteParameters
195    */
196   public function testGetRouteParameters($uri) {
197     $url = Url::fromUri($uri);
198     $this->setExpectedException(\UnexpectedValueException::class);
199     $url->getRouteParameters();
200   }
201
202   /**
203    * Tests the getInternalPath() method.
204    *
205    * @depends testFromUri
206    * @dataProvider providerFromUri
207    *
208    * @covers ::getInternalPath
209    */
210   public function testGetInternalPath($uri) {
211     $url = Url::fromUri($uri);
212     $this->setExpectedException(\Exception::class);
213     $url->getInternalPath();
214   }
215
216   /**
217    * Tests the getPath() method.
218    *
219    * @depends testFromUri
220    * @dataProvider providerFromUri
221    *
222    * @covers ::getUri
223    */
224   public function testGetUri($uri) {
225     $url = Url::fromUri($uri);
226     $this->assertNotNull($url->getUri());
227   }
228
229   /**
230    * Tests the getOptions() method.
231    *
232    * @depends testFromUri
233    * @dataProvider providerFromUri
234    *
235    * @covers ::getOptions
236    */
237   public function testGetOptions($uri) {
238     $url = Url::fromUri($uri);
239     $this->assertInternalType('array', $url->getOptions());
240   }
241
242 }