UnroutedUrlTest.php 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  1. <?php
  2. namespace Drupal\Tests\Core;
  3. use Drupal\Core\DependencyInjection\ContainerBuilder;
  4. use Drupal\Core\Url;
  5. use Drupal\Tests\UnitTestCase;
  6. use Symfony\Component\HttpFoundation\Request;
  7. use Symfony\Component\Routing\Exception\ResourceNotFoundException;
  8. /**
  9. * @coversDefaultClass \Drupal\Core\Url
  10. * @group UrlTest
  11. */
  12. class UnroutedUrlTest extends UnitTestCase {
  13. /**
  14. * The URL assembler
  15. *
  16. * @var \Drupal\Core\Utility\UnroutedUrlAssemblerInterface|\PHPUnit\Framework\MockObject\MockObject
  17. */
  18. protected $urlAssembler;
  19. /**
  20. * The router.
  21. *
  22. * @var \Drupal\Tests\Core\Routing\TestRouterInterface|\PHPUnit\Framework\MockObject\MockObject
  23. */
  24. protected $router;
  25. /**
  26. * An unrouted, external URL to test.
  27. *
  28. * @var string
  29. */
  30. protected $unroutedExternal = 'https://www.drupal.org';
  31. /**
  32. * An unrouted, internal URL to test.
  33. *
  34. * @var string
  35. */
  36. protected $unroutedInternal = 'base:robots.txt';
  37. /**
  38. * {@inheritdoc}
  39. */
  40. protected function setUp() {
  41. parent::setUp();
  42. $this->urlAssembler = $this->createMock('Drupal\Core\Utility\UnroutedUrlAssemblerInterface');
  43. $this->urlAssembler->expects($this->any())
  44. ->method('assemble')
  45. ->will($this->returnArgument(0));
  46. $this->router = $this->createMock('Drupal\Tests\Core\Routing\TestRouterInterface');
  47. $container = new ContainerBuilder();
  48. $container->set('router.no_access_checks', $this->router);
  49. $container->set('unrouted_url_assembler', $this->urlAssembler);
  50. \Drupal::setContainer($container);
  51. }
  52. /**
  53. * Tests the fromUri() method.
  54. *
  55. * @covers ::fromUri
  56. *
  57. * @dataProvider providerFromUri
  58. */
  59. public function testFromUri($uri, $is_external) {
  60. $url = Url::fromUri($uri);
  61. $this->assertInstanceOf('Drupal\Core\Url', $url);
  62. }
  63. /**
  64. * Data provider for testFromUri().
  65. */
  66. public function providerFromUri() {
  67. return [
  68. // [$uri, $is_external]
  69. // An external URI.
  70. ['https://www.drupal.org', TRUE],
  71. // A protocol-relative URL.
  72. ['//www.drupal.org', TRUE],
  73. // An internal, unrouted, base-relative URI.
  74. ['base:robots.txt', FALSE],
  75. // Base-relative URIs with special characters.
  76. ['base:AKI@&hO@', FALSE],
  77. ['base:(:;2&+h^', FALSE],
  78. // Various token formats.
  79. ['base:node/[token]', FALSE],
  80. ['base:node/%', FALSE],
  81. ['base:node/[token:token]', FALSE],
  82. ['base:node/{{ token }}', FALSE],
  83. ];
  84. }
  85. /**
  86. * Tests the fromUri() method.
  87. *
  88. * @covers ::fromUri
  89. * @dataProvider providerFromInvalidUri
  90. */
  91. public function testFromInvalidUri($uri) {
  92. $this->expectException(\InvalidArgumentException::class);
  93. $url = Url::fromUri($uri);
  94. }
  95. /**
  96. * Data provider for testFromInvalidUri().
  97. */
  98. public function providerFromInvalidUri() {
  99. return [
  100. // Schemeless paths.
  101. ['test'],
  102. ['/test'],
  103. // Schemeless path with a query string.
  104. ['foo?bar'],
  105. // Only a query string.
  106. ['?bar'],
  107. // Only a fragment.
  108. ['#foo'],
  109. // Disallowed characters in the authority (host name) that are valid
  110. // elsewhere in the path.
  111. ['base://(:;2&+h^'],
  112. ['base://AKI@&hO@'],
  113. ];
  114. }
  115. /**
  116. * Tests the createFromRequest method.
  117. *
  118. * @covers ::createFromRequest
  119. */
  120. public function testCreateFromRequest() {
  121. $request = Request::create('/test-path');
  122. $this->router->expects($this->once())
  123. ->method('matchRequest')
  124. ->with($request)
  125. ->will($this->throwException(new ResourceNotFoundException()));
  126. $this->expectException(ResourceNotFoundException::class);
  127. Url::createFromRequest($request);
  128. }
  129. /**
  130. * Tests the isExternal() method.
  131. *
  132. * @depends testFromUri
  133. * @dataProvider providerFromUri
  134. *
  135. * @covers ::isExternal
  136. */
  137. public function testIsExternal($uri, $is_external) {
  138. $url = Url::fromUri($uri);
  139. $this->assertSame($url->isExternal(), $is_external);
  140. }
  141. /**
  142. * Tests the toString() method.
  143. *
  144. * @depends testFromUri
  145. * @dataProvider providerFromUri
  146. *
  147. * @covers ::toString
  148. */
  149. public function testToString($uri) {
  150. $url = Url::fromUri($uri);
  151. $this->assertSame($uri, $url->toString());
  152. }
  153. /**
  154. * Tests the getRouteName() method.
  155. *
  156. * @depends testFromUri
  157. * @dataProvider providerFromUri
  158. *
  159. * @covers ::getRouteName
  160. */
  161. public function testGetRouteName($uri) {
  162. $url = Url::fromUri($uri);
  163. $this->expectException(\UnexpectedValueException::class);
  164. $url->getRouteName();
  165. }
  166. /**
  167. * Tests the getRouteParameters() method.
  168. *
  169. * @depends testFromUri
  170. * @dataProvider providerFromUri
  171. *
  172. * @covers ::getRouteParameters
  173. */
  174. public function testGetRouteParameters($uri) {
  175. $url = Url::fromUri($uri);
  176. $this->expectException(\UnexpectedValueException::class);
  177. $url->getRouteParameters();
  178. }
  179. /**
  180. * Tests the getInternalPath() method.
  181. *
  182. * @depends testFromUri
  183. * @dataProvider providerFromUri
  184. *
  185. * @covers ::getInternalPath
  186. */
  187. public function testGetInternalPath($uri) {
  188. $url = Url::fromUri($uri);
  189. $this->expectException(\Exception::class);
  190. $url->getInternalPath();
  191. }
  192. /**
  193. * Tests the getPath() method.
  194. *
  195. * @depends testFromUri
  196. * @dataProvider providerFromUri
  197. *
  198. * @covers ::getUri
  199. */
  200. public function testGetUri($uri) {
  201. $url = Url::fromUri($uri);
  202. $this->assertNotNull($url->getUri());
  203. }
  204. /**
  205. * Tests the getOptions() method.
  206. *
  207. * @depends testFromUri
  208. * @dataProvider providerFromUri
  209. *
  210. * @covers ::getOptions
  211. */
  212. public function testGetOptions($uri) {
  213. $url = Url::fromUri($uri);
  214. $this->assertIsArray($url->getOptions());
  215. }
  216. }