Skip to content

Commit 7696f5b

Browse files
committed
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
1 parent 9bf1f49 commit 7696f5b

File tree

3 files changed

+38
-8
lines changed

3 files changed

+38
-8
lines changed

src/Routing/Router.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -633,7 +633,7 @@ public static function url($url = null, $full = false)
633633
}
634634

635635
if (empty($url)) {
636-
$output = isset($here) ? $here : $base . '/';
636+
$output = $base . (isset($here) ? $here : '/');
637637
if ($full) {
638638
$output = static::fullBaseUrl() . $output;
639639
}

tests/TestCase/Routing/RouterTest.php

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ public function tearDown()
6161
*
6262
* @return void
6363
*/
64-
public function testbaseUrl()
64+
public function testBaseUrl()
6565
{
6666
$this->assertRegExp('/^http(s)?:\/\//', Router::url('/', true));
6767
$this->assertRegExp('/^http(s)?:\/\//', Router::url(null, true));
@@ -73,7 +73,7 @@ public function testbaseUrl()
7373
*
7474
* @return void
7575
*/
76-
public function testfullBaseURL()
76+
public function testFullBaseURL()
7777
{
7878
Router::fullBaseUrl('http://example.com');
7979
$this->assertEquals('http://example.com/', Router::url('/', true));
@@ -571,14 +571,17 @@ public function testUrlGenerationWithBasePath()
571571
'plugin' => null,
572572
'controller' => 'subscribe',
573573
],
574-
'url' => '/magazine/',
574+
'url' => '/subscribe',
575575
'base' => '/magazine',
576576
'webroot' => '/magazine/'
577577
]);
578578
Router::pushRequest($request);
579579

580580
$result = Router::url();
581-
$this->assertEquals('/magazine/', $result);
581+
$this->assertEquals('/magazine/subscribe', $result);
582+
583+
$result = Router::url([]);
584+
$this->assertEquals('/magazine/subscribe', $result);
582585

583586
$result = Router::url('/');
584587
$this->assertEquals('/magazine/', $result);
@@ -933,7 +936,7 @@ public function testUrlGenerationWithPrefix()
933936
],
934937
'webroot' => '/magazine/',
935938
'base' => '/magazine',
936-
'url' => '/magazine/admin/subscriptions/edit/1',
939+
'url' => '/admin/subscriptions/edit/1',
937940
]);
938941
Router::setRequestInfo($request);
939942

tests/TestCase/View/Helper/UrlHelperTest.php

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ public function tearDown()
7070
*
7171
* @return void
7272
*/
73-
public function testUrlConversion()
73+
public function testBuildUrlConversion()
7474
{
7575
Router::connect('/:controller/:action/*');
7676

@@ -104,10 +104,37 @@ public function testUrlConversion()
104104
$this->assertEquals('/posts/index?one=value&two=value&three=purple&page=1', $result);
105105
}
106106

107+
/**
108+
* ensure that build factors in base paths.
109+
*
110+
* @return void
111+
*/
112+
public function testBuildBasePath()
113+
{
114+
Router::connect('/:controller/:action/*');
115+
$request = new ServerRequest([
116+
'params' => [
117+
'action' => 'index',
118+
'plugin' => null,
119+
'controller' => 'subscribe',
120+
],
121+
'url' => '/subscribe',
122+
'base' => '/magazine',
123+
'webroot' => '/magazine/'
124+
]);
125+
Router::pushRequest($request);
126+
127+
$this->assertEquals('/magazine/subscribe', $this->Helper->build());
128+
$this->assertEquals(
129+
'/magazine/articles/add',
130+
$this->Helper->build(['controller' => 'articles', 'action' => 'add'])
131+
);
132+
}
133+
107134
/**
108135
* @return void
109136
*/
110-
public function testUrlConversionUnescaped()
137+
public function testBuildUrlConversionUnescaped()
111138
{
112139
$result = $this->Helper->build('/controller/action/1?one=1&two=2', ['escape' => false]);
113140
$this->assertEquals('/controller/action/1?one=1&two=2', $result);

0 commit comments

Comments
 (0)