Skip to content

Commit

Permalink
Fix handling of base paths.
Browse files Browse the repository at this point in the history
When the implementation was switched to use getRequestTarget(), the
tests incorrectly handled the base paths for empty urls.

Refs #11958
  • Loading branch information
markstory committed Apr 18, 2018
1 parent 9bf1f49 commit 7696f5b
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 8 deletions.
2 changes: 1 addition & 1 deletion src/Routing/Router.php
Expand Up @@ -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;
}
Expand Down
13 changes: 8 additions & 5 deletions tests/TestCase/Routing/RouterTest.php
Expand Up @@ -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));
Expand All @@ -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));
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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);

Expand Down
31 changes: 29 additions & 2 deletions tests/TestCase/View/Helper/UrlHelperTest.php
Expand Up @@ -70,7 +70,7 @@ public function tearDown()
*
* @return void
*/
public function testUrlConversion()
public function testBuildUrlConversion()
{
Router::connect('/:controller/:action/*');

Expand Down Expand Up @@ -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);
Expand Down

0 comments on commit 7696f5b

Please sign in to comment.