From 7696f5b0fc10e247d26ecb6aa2eabca43876d6de Mon Sep 17 00:00:00 2001 From: Mark Story Date: Tue, 17 Apr 2018 21:06:30 -0400 Subject: [PATCH] Fix handling of base paths. When the implementation was switched to use getRequestTarget(), the tests incorrectly handled the base paths for empty urls. Refs #11958 --- src/Routing/Router.php | 2 +- tests/TestCase/Routing/RouterTest.php | 13 ++++---- tests/TestCase/View/Helper/UrlHelperTest.php | 31 ++++++++++++++++++-- 3 files changed, 38 insertions(+), 8 deletions(-) diff --git a/src/Routing/Router.php b/src/Routing/Router.php index de6a95161c3..cab3db2f875 100644 --- a/src/Routing/Router.php +++ b/src/Routing/Router.php @@ -633,7 +633,7 @@ public static function url($url = null, $full = false) } if (empty($url)) { - $output = isset($here) ? $here : $base . '/'; + $output = $base . (isset($here) ? $here : '/'); if ($full) { $output = static::fullBaseUrl() . $output; } diff --git a/tests/TestCase/Routing/RouterTest.php b/tests/TestCase/Routing/RouterTest.php index d4590d0fe14..a01b06b6287 100644 --- a/tests/TestCase/Routing/RouterTest.php +++ b/tests/TestCase/Routing/RouterTest.php @@ -61,7 +61,7 @@ public function tearDown() * * @return void */ - public function testbaseUrl() + public function testBaseUrl() { $this->assertRegExp('/^http(s)?:\/\//', Router::url('/', true)); $this->assertRegExp('/^http(s)?:\/\//', Router::url(null, true)); @@ -73,7 +73,7 @@ public function testbaseUrl() * * @return void */ - public function testfullBaseURL() + public function testFullBaseURL() { Router::fullBaseUrl('http://example.com'); $this->assertEquals('http://example.com/', Router::url('/', true)); @@ -571,14 +571,17 @@ public function testUrlGenerationWithBasePath() 'plugin' => null, 'controller' => 'subscribe', ], - 'url' => '/magazine/', + 'url' => '/subscribe', 'base' => '/magazine', 'webroot' => '/magazine/' ]); Router::pushRequest($request); $result = Router::url(); - $this->assertEquals('/magazine/', $result); + $this->assertEquals('/magazine/subscribe', $result); + + $result = Router::url([]); + $this->assertEquals('/magazine/subscribe', $result); $result = Router::url('/'); $this->assertEquals('/magazine/', $result); @@ -933,7 +936,7 @@ public function testUrlGenerationWithPrefix() ], 'webroot' => '/magazine/', 'base' => '/magazine', - 'url' => '/magazine/admin/subscriptions/edit/1', + 'url' => '/admin/subscriptions/edit/1', ]); Router::setRequestInfo($request); diff --git a/tests/TestCase/View/Helper/UrlHelperTest.php b/tests/TestCase/View/Helper/UrlHelperTest.php index 2a34614fbb5..eeff77ff879 100644 --- a/tests/TestCase/View/Helper/UrlHelperTest.php +++ b/tests/TestCase/View/Helper/UrlHelperTest.php @@ -70,7 +70,7 @@ public function tearDown() * * @return void */ - public function testUrlConversion() + public function testBuildUrlConversion() { Router::connect('/:controller/:action/*'); @@ -104,10 +104,37 @@ public function testUrlConversion() $this->assertEquals('/posts/index?one=value&two=value&three=purple&page=1', $result); } + /** + * ensure that build factors in base paths. + * + * @return void + */ + public function testBuildBasePath() + { + Router::connect('/:controller/:action/*'); + $request = new ServerRequest([ + 'params' => [ + 'action' => 'index', + 'plugin' => null, + 'controller' => 'subscribe', + ], + 'url' => '/subscribe', + 'base' => '/magazine', + 'webroot' => '/magazine/' + ]); + Router::pushRequest($request); + + $this->assertEquals('/magazine/subscribe', $this->Helper->build()); + $this->assertEquals( + '/magazine/articles/add', + $this->Helper->build(['controller' => 'articles', 'action' => 'add']) + ); + } + /** * @return void */ - public function testUrlConversionUnescaped() + public function testBuildUrlConversionUnescaped() { $result = $this->Helper->build('/controller/action/1?one=1&two=2', ['escape' => false]); $this->assertEquals('/controller/action/1?one=1&two=2', $result);