diff --git a/core/lib/Drupal/Core/Form/ConfirmFormHelper.php b/core/lib/Drupal/Core/Form/ConfirmFormHelper.php index 0c1ff8b..1b1fa30 100644 --- a/core/lib/Drupal/Core/Form/ConfirmFormHelper.php +++ b/core/lib/Drupal/Core/Form/ConfirmFormHelper.php @@ -34,42 +34,34 @@ class ConfirmFormHelper { */ public static function buildCancelLink(ConfirmFormInterface $form, Request $request) { // Prepare cancel link. - $link = array(); $query = $request->query; // If a destination is specified, that serves as the cancel link. if ($query->has('destination')) { $options = UrlHelper::parse($query->get('destination')); $link = array( - '#type' => 'link', - '#title' => $form->getCancelText(), '#href' => $options['path'], '#options' => $options, ); } // Check for a route-based cancel link. elseif ($route = $form->getCancelRoute()) { - // @todo this is not officially supported. Change the interface and all - // implementations so they return a Url object. - if ($route instanceof Url) { - $route = $route->toArray(); + if (!($route instanceof Url)) { + if (empty($route['route_name'])) { + throw new \UnexpectedValueException(String::format('Missing route name in !class::getCancelRoute().', array('!class' => get_class($form)))); + } + // Ensure there is something to pass as the params and options. + $route += array( + 'route_parameters' => array(), + 'options' => array(), + ); + $route = new Url($route['route_name'], $route['route_parameters'], $route['options']); } - if (empty($route['route_name'])) { - throw new \UnexpectedValueException(String::format('Missing route name in !class::getCancelRoute().', array('!class' => get_class($form)))); - } - $route += array( - 'route_parameters' => array(), - 'options' => array(), - ); - // Ensure there is something to pass as the params and options. - $link = array( - '#type' => 'link', - '#title' => $form->getCancelText(), - '#route_name' => $route['route_name'], - '#route_parameters' => $route['route_parameters'], - '#options' => $route['options'], - ); + + $link = $route->toRenderArray(); } + $link['#type'] = 'link'; + $link['#title'] = $form->getCancelText(); return $link; } diff --git a/core/lib/Drupal/Core/Url.php b/core/lib/Drupal/Core/Url.php index 35665e5..8a4d756 100644 --- a/core/lib/Drupal/Core/Url.php +++ b/core/lib/Drupal/Core/Url.php @@ -354,19 +354,25 @@ public function toString() { * An associative array containing all the properties of the route. */ public function toArray() { - if ($this->isExternal()) { - return array( - 'path' => $this->getPath(), - 'options' => $this->getOptions(), - ); - } - else { - return array( - 'route_name' => $this->getRouteName(), - 'route_parameters' => $this->getRouteParameters(), - 'options' => $this->getOptions(), - ); - } + return array( + 'route_name' => $this->getRouteName(), + 'route_parameters' => $this->getRouteParameters(), + 'options' => $this->getOptions(), + ); + } + + /** + * Returns the route information for a render array. + * + * @return array + * An associative array suitable for a render array. + */ + public function toRenderArray() { + return array( + '#route_name' => $this->getRouteName(), + '#route_parameters' => $this->getRouteParameters(), + '#options' => $this->getOptions(), + ); } /** diff --git a/core/tests/Drupal/Tests/Core/Form/ConfirmFormHelperTest.php b/core/tests/Drupal/Tests/Core/Form/ConfirmFormHelperTest.php index 20bc343..5381bfb 100644 --- a/core/tests/Drupal/Tests/Core/Form/ConfirmFormHelperTest.php +++ b/core/tests/Drupal/Tests/Core/Form/ConfirmFormHelperTest.php @@ -39,28 +39,8 @@ public function testCancelLinkTitle() { ->method('getCancelText') ->will($this->returnValue($cancel_text)); - $cancel_route = array( - 'route_name' => '', - ); - $form->expects($this->any()) - ->method('getCancelRoute') - ->will($this->returnValue($cancel_route)); - $link = ConfirmFormHelper::buildCancelLink($form, new Request()); $this->assertSame($cancel_text, $link['#title']); - $this->assertSame($cancel_route['route_name'], $link['#route_name']); - $this->assertSame(array(), $link['#route_parameters']); - } - - /** - * Tests that an empty render array is returned if there is no cancel route. - */ - public function testNullCancelRoute() { - // This mock will return NULL for method getCancelRoute(). - $form = $this->getMock('Drupal\Core\Form\ConfirmFormInterface'); - - $link = ConfirmFormHelper::buildCancelLink($form, new Request()); - $this->assertSame(array(), $link); } /** diff --git a/core/tests/Drupal/Tests/Core/UrlTest.php b/core/tests/Drupal/Tests/Core/UrlTest.php index 668794f..a2a7ec0 100644 --- a/core/tests/Drupal/Tests/Core/UrlTest.php +++ b/core/tests/Drupal/Tests/Core/UrlTest.php @@ -7,6 +7,7 @@ namespace Drupal\Tests\Core; +use Drupal\Component\Utility\UrlHelper; use Drupal\Core\DependencyInjection\ContainerBuilder; use Drupal\Core\Url; use Drupal\Tests\UnitTestCase; @@ -217,6 +218,16 @@ public function testGetPathForInternalUrl($urls) { } /** + * Tests the getPath() method for external URLs. + * + * @covers ::getPath + */ + public function testGetPathForExternalUrl() { + $url = Url::createFromPath('http://example.com/test'); + $this->assertEquals('http://example.com/test', $url->getPath()); + } + + /** * Tests the toString() method. * * @param \Drupal\Core\Url[] $urls @@ -271,6 +282,17 @@ public function testGetRouteName($urls) { } /** + * Tests the getRouteName() with an external URL. + * + * @covers ::getRouteName + * @expectedException \UnexpectedValueException + */ + public function testGetRouteNameWithExternalUrl() { + $url = Url::createFromPath('http://example.com'); + $url->getRouteName(); + } + + /** * Tests the getRouteParameters() method. * * @param \Drupal\Core\Url[] $urls @@ -287,6 +309,17 @@ public function testGetRouteParameters($urls) { } /** + * Tests the getRouteParameter() with an external URL. + * + * @covers ::getRouteParameter + * @expectedException \UnexpectedValueException + */ + public function testGetRouteParametersWithExternalUrl() { + $url = Url::createFromPath('http://example.com'); + $url->getRouteParameters(); + } + + /** * Tests the getOptions() method. * * @param \Drupal\Core\Url[] $urls