Upgraded drupal core with security updates
[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   /**
78    * Data provider for testFromUri().
79    */
80   public function providerFromUri() {
81     return [
82       // [$uri, $is_external]
83       // An external URI.
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],
97     ];
98   }
99
100   /**
101    * Tests the fromUri() method.
102    *
103    * @covers ::fromUri
104    * @dataProvider providerFromInvalidUri
105    */
106   public function testFromInvalidUri($uri) {
107     $this->setExpectedException(\InvalidArgumentException::class);
108     $url = Url::fromUri($uri);
109   }
110
111   /**
112    * Data provider for testFromInvalidUri().
113    */
114   public function providerFromInvalidUri() {
115     return [
116       // Schemeless paths.
117       ['test'],
118       ['/test'],
119       // Schemeless path with a query string.
120       ['foo?bar'],
121       // Only a query string.
122       ['?bar'],
123       // Only a fragment.
124       ['#foo'],
125       // Disallowed characters in the authority (host name) that are valid
126       // elsewhere in the path.
127       ['base://(:;2&+h^'],
128       ['base://AKI@&hO@'],
129     ];
130   }
131
132   /**
133    * Tests the createFromRequest method.
134    *
135    * @covers ::createFromRequest
136    */
137   public function testCreateFromRequest() {
138     $request = Request::create('/test-path');
139
140     $this->router->expects($this->once())
141       ->method('matchRequest')
142       ->with($request)
143       ->will($this->throwException(new ResourceNotFoundException()));
144
145     $this->setExpectedException(ResourceNotFoundException::class);
146     Url::createFromRequest($request);
147   }
148
149   /**
150    * Tests the isExternal() method.
151    *
152    * @depends testFromUri
153    * @dataProvider providerFromUri
154    *
155    * @covers ::isExternal
156    */
157   public function testIsExternal($uri, $is_external) {
158     $url = Url::fromUri($uri);
159     $this->assertSame($url->isExternal(), $is_external);
160   }
161
162   /**
163    * Tests the toString() method.
164    *
165    * @depends testFromUri
166    * @dataProvider providerFromUri
167    *
168    * @covers ::toString
169    */
170   public function testToString($uri) {
171     $url = Url::fromUri($uri);
172     $this->assertSame($uri, $url->toString());
173   }
174
175   /**
176    * Tests the getRouteName() method.
177    *
178    * @depends testFromUri
179    * @dataProvider providerFromUri
180    *
181    * @covers ::getRouteName
182    */
183   public function testGetRouteName($uri) {
184     $url = Url::fromUri($uri);
185     $this->setExpectedException(\UnexpectedValueException::class);
186     $url->getRouteName();
187   }
188
189   /**
190    * Tests the getRouteParameters() method.
191    *
192    * @depends testFromUri
193    * @dataProvider providerFromUri
194    *
195    * @covers ::getRouteParameters
196    */
197   public function testGetRouteParameters($uri) {
198     $url = Url::fromUri($uri);
199     $this->setExpectedException(\UnexpectedValueException::class);
200     $url->getRouteParameters();
201   }
202
203   /**
204    * Tests the getInternalPath() method.
205    *
206    * @depends testFromUri
207    * @dataProvider providerFromUri
208    *
209    * @covers ::getInternalPath
210    */
211   public function testGetInternalPath($uri) {
212     $url = Url::fromUri($uri);
213     $this->setExpectedException(\Exception::class);
214     $url->getInternalPath();
215   }
216
217   /**
218    * Tests the getPath() method.
219    *
220    * @depends testFromUri
221    * @dataProvider providerFromUri
222    *
223    * @covers ::getUri
224    */
225   public function testGetUri($uri) {
226     $url = Url::fromUri($uri);
227     $this->assertNotNull($url->getUri());
228   }
229
230   /**
231    * Tests the getOptions() method.
232    *
233    * @depends testFromUri
234    * @dataProvider providerFromUri
235    *
236    * @covers ::getOptions
237    */
238   public function testGetOptions($uri) {
239     $url = Url::fromUri($uri);
240     $this->assertInternalType('array', $url->getOptions());
241   }
242
243 }