diff --git a/core/lib/Drupal/Core/Url.php b/core/lib/Drupal/Core/Url.php index 3f0637c..8fc8a09 100644 --- a/core/lib/Drupal/Core/Url.php +++ b/core/lib/Drupal/Core/Url.php @@ -85,15 +85,16 @@ public function __construct(UrlGeneratorInterface $url_generator, $route_name, $ * * @return static * An Url object. + * + * @throws \Symfony\Component\Routing\Exception\ResourceNotFoundException */ public static function createFromPath($path) { // Look up the route name and parameters used for the given path. $request = Request::create('/' . $path); $request->attributes->set('_system_path', $path); - if ($result = \Drupal::service('router')->matchRequest($request)) { - return new static(\Drupal::urlGenerator(), $result['_route'], $result['_raw_variables']->all()); - } + $result = \Drupal::service('router')->matchRequest($request); + return new static(\Drupal::urlGenerator(), $result['_route'], $result['_raw_variables']->all()); } /** @@ -140,7 +141,7 @@ public function toArray() { return array( 'route_name' => $this->routeName, 'route_parameters' => $this->routeParameters, - 'route_options' => $this->routeOptions, + 'options' => $this->routeOptions, ); } diff --git a/core/modules/menu_link/lib/Drupal/menu_link/Entity/MenuLink.php b/core/modules/menu_link/lib/Drupal/menu_link/Entity/MenuLink.php index 9440c77..ffb5786 100644 --- a/core/modules/menu_link/lib/Drupal/menu_link/Entity/MenuLink.php +++ b/core/modules/menu_link/lib/Drupal/menu_link/Entity/MenuLink.php @@ -13,6 +13,7 @@ use Drupal\Core\Routing\UrlMatcher; use Drupal\Core\Url; use Drupal\menu_link\MenuLinkInterface; +use Symfony\Component\Routing\Exception\ResourceNotFoundException; use Symfony\Component\Routing\Route; /** @@ -547,11 +548,12 @@ public function preSave(EntityStorageControllerInterface $storage_controller) { } // Find the route_name. if (!isset($this->route_name)) { - if ($url = Url::createFromPath($this->link_path)) { + try { + $url = Url::createFromPath($this->link_path); $this->route_name = $url->getRouteName(); $this->route_parameters = $url->getRouteParameters(); } - else { + catch (ResourceNotFoundException $e) { $this->route_name = ''; $this->route_parameters = array(); } diff --git a/core/modules/shortcut/lib/Drupal/shortcut/Entity/Shortcut.php b/core/modules/shortcut/lib/Drupal/shortcut/Entity/Shortcut.php index 27419e9..6784bd0 100644 --- a/core/modules/shortcut/lib/Drupal/shortcut/Entity/Shortcut.php +++ b/core/modules/shortcut/lib/Drupal/shortcut/Entity/Shortcut.php @@ -13,6 +13,7 @@ use Drupal\Core\Routing\UrlMatcher; use Drupal\Core\Url; use Drupal\shortcut\ShortcutInterface; +use Symfony\Component\Routing\Exception\ResourceNotFoundException; /** * Defines the shortcut entity class. @@ -125,10 +126,13 @@ public static function preCreate(EntityStorageControllerInterface $storage_contr public function preSave(EntityStorageControllerInterface $storage_controller) { parent::preSave($storage_controller); - if ($url = Url::createFromPath($this->path->value)) { + try { + $url = Url::createFromPath($this->path->value); $this->setRouteName($url->getRouteName()); $this->setRouteParams($url->getRouteParameters()); } + catch (ResourceNotFoundException $e) { + } } /** diff --git a/core/tests/Drupal/Tests/Core/Routing/UrlTest.php b/core/tests/Drupal/Tests/Core/Routing/UrlTest.php index 6da4f4a..65eb1af 100644 --- a/core/tests/Drupal/Tests/Core/Routing/UrlTest.php +++ b/core/tests/Drupal/Tests/Core/Routing/UrlTest.php @@ -20,17 +20,27 @@ * @group Drupal * @group Routing * - * @see \Drupal\Core\Url + * @coversDefaultClass \Drupal\Core\Url */ class UrlTest extends UnitTestCase { /** - * The url generator + * The URL generator * - * @var \PHPUnit_Framework_MockObject_MockObject + * @var \Drupal\Core\Routing\UrlGeneratorInterface|\PHPUnit_Framework_MockObject_MockObject */ protected $urlGenerator; + /** + * The router. + * + * @var \Symfony\Component\Routing\Matcher\RequestMatcherInterface|\PHPUnit_Framework_MockObject_MockObject + */ + protected $router; + + /** + * {@inheritdoc} + */ public static function getInfo() { return array( 'name' => 'Url object', @@ -54,47 +64,74 @@ protected function setUp() { $this->urlGenerator->expects($this->any()) ->method('generate') ->will($this->returnValueMap($map)); - } - /** - * Tests the createFromPath method. - * - * @see \Drupal\Core\Url::createFromPath() - */ - public function testCreateFromPath() { - $router = $this->getMock('Symfony\Component\Routing\Matcher\RequestMatcherInterface'); + $this->router = $this->getMock('Symfony\Component\Routing\Matcher\RequestMatcherInterface'); $container = new ContainerBuilder(); - $container->set('router', $router); + $container->set('router', $this->router); $container->set('url_generator', $this->urlGenerator); \Drupal::setContainer($container); + } - $router->expects($this->at(0)) + /** + * Tests the createFromPath method. + * + * @covers ::createFromPath() + */ + public function testCreateFromPath() { + + $this->router->expects($this->at(0)) ->method('matchRequest') + ->with($this->isInstanceOf('Symfony\Component\HttpFoundation\Request')) ->will($this->returnValue(array( RouteObjectInterface::ROUTE_NAME => 'view.frontpage.page_1', '_raw_variables' => new ParameterBag(), ))); - $router->expects($this->at(1)) + $this->router->expects($this->at(1)) ->method('matchRequest') + ->with($this->isInstanceOf('Symfony\Component\HttpFoundation\Request')) ->will($this->returnValue(array( RouteObjectInterface::ROUTE_NAME => 'node_view', '_raw_variables' => new ParameterBag(array('node' => '1')), ))); - $router->expects($this->at(2)) + $this->router->expects($this->at(2)) ->method('matchRequest') + ->with($this->isInstanceOf('Symfony\Component\HttpFoundation\Request')) ->will($this->returnValue(array( RouteObjectInterface::ROUTE_NAME => 'node_edit', '_raw_variables' => new ParameterBag(array('node' => '2')), ))); - $router->expects($this->at(3)) + + $this->assertCreateFromPath(array('view.frontpage.page_1', array(), array()), 'node'); + $this->assertCreateFromPath(array('node_view', array('node' => '1'), array()), 'node/1'); + $this->assertCreateFromPath(array('node_edit', array('node' => '2'), array()), 'node/2/edit'); + } + + /** + * Asserts that creating a URL from a path returns the correct results. + * + * @param array $expected + * The expected URL info. + * @param string $path + * The path to check. + */ + protected function assertCreateFromPath(array $expected, $path) { + $this->assertSame($expected, array_values(Url::createFromPath($path)->toArray())); + } + + /** + * Tests that an invalid path will thrown an exception. + * + * @covers ::createFromPath() + * + * @expectedException \Symfony\Component\Routing\Exception\ResourceNotFoundException + */ + public function testCreateFromPathInvalid() { + $this->router->expects($this->once()) ->method('matchRequest') + ->with($this->isInstanceOf('Symfony\Component\HttpFoundation\Request')) ->will($this->throwException(new ResourceNotFoundException())); - $this->assertEquals(array('view.frontpage.page_1', array()), Url::createFromPath('node')); - $this->assertEquals(array('node_view', array('node' => '1')), Url::createFromPath('node/1')); - $this->assertEquals(array('node_edit', array('node' => '2')), Url::createFromPath('node/2/edit')); - - $this->assertEquals(NULL, Url::createFromPath('non-existing')); + Url::createFromPath('non-existing'); } }