Skip to content
Permalink
Browse files

Fix failing tests.

* Both mailto: and javascript: were not being handled correctly.
* Fix failing reverse() test, as there were two matching routes.
* Fix failing prefix test from 2.2
  • Loading branch information...
markstory committed May 27, 2012
1 parent 66b9130 commit 5aaa5c59401ac2fa2e99c24d779b8db7193201a8
Showing with 38 additions and 14 deletions.
  1. +5 −2 lib/Cake/Routing/Route/Route.php
  2. +16 −11 lib/Cake/Routing/Router.php
  3. +17 −1 lib/Cake/Test/TestCase/Routing/RouterTest.php
@@ -441,8 +441,11 @@ public function match($url, $context = array()) {
* @return string Composed route string.
*/
protected function _writeUrl($params, $pass = array(), $query = array()) {
if (isset($params['prefix'], $params['action'])) {
$params['action'] = str_replace($params['prefix'] . '_', '', $params['action']);
if (isset($params['prefix'])) {
$prefixed = $params['prefix'] . '_';
}
if (isset($prefixed, $params['action']) && strpos($params['action'], $prefixed) === 0) {
$params['action'] = substr($params['action'], strlen($prefixed) * -1);
unset($params['prefix']);
}
@@ -660,10 +660,21 @@ public static function url($url = null, $options = array()) {
$options = array();
}
$urlType = gettype($url);
$hasColonSlash = $hasLeadingSlash = $isJavascript = $isMailto = false;
if ($urlType === 'string') {
$isJavascript = strpos($url, 'javascript:') === 0;
$isMailto = strpos($url, 'mailto:') === 0;
$hasColonSlash = strpos($url, '://') !== false;
$hasLeadingSlash = isset($url[0]) ? $url[0] === '/' : false;
}
if (
$urlType === 'string' &&
strpos($url, ':') !== false &&
strpos($url, '/') === false
strpos($url, '/') === false &&
!$isMailto &&
!$isJavascript
) {
$url = self::_splitName($url, $options);
$urlType = 'array';
@@ -685,12 +696,6 @@ public static function url($url = null, $options = array()) {
}
$output = $frag = null;
$hasColonSlash = $hasLeadingSlash = false;
if ($urlType === 'string') {
$hasColonSlash = strpos($url, '://') !== false;
$hasLeadingSlash = isset($url[0]) ? $url[0] === '/' : false;
}
if (empty($url)) {
$output = isset($here) ? $here : '/';
@@ -752,7 +757,9 @@ public static function url($url = null, $options = array()) {
} elseif (
$urlType === 'string' &&
!$hasLeadingSlash &&
!$hasColonSlash
!$hasColonSlash &&
!$isMailto &&
!$isJavascript
) {
// named route.
$route = self::$_routes->get($url);
@@ -771,9 +778,7 @@ public static function url($url = null, $options = array()) {
} else {
// String urls.
if (
($hasColonSlash ||
(strpos($url, 'javascript:') === 0) ||
(strpos($url, 'mailto:') === 0)) ||
($hasColonSlash || $isJavascript || $isMailto) ||
(!strncmp($url, '#', 1))
) {
return $url;
@@ -859,6 +859,21 @@ public function testUrlGenerationWithUrlFilter() {
$this->assertEquals(2, $calledCount);
}
/**
* Test that strings starting with mailto: etc are handled correctly.
*
* @return void
*/
public function testUrlGenerationMailtoAndJavascript() {
$mailto = 'mailto:mark@example.com';
$result = Router::url($mailto);
$this->assertEquals($mailto, $result);
$js = 'javascript:alert("hi")';
$result = Router::url($js);
$this->assertEquals($js, $result);
}
/**
* test that you can leave active plugin routes with plugin = null
*
@@ -1900,7 +1915,7 @@ public function testCustomRouteException() {
*
* @return void
*/
public function testRouterReverse() {
public function testReverse() {
Router::connect('/:controller/:action/*');
$params = array(
'controller' => 'posts',
@@ -1916,6 +1931,7 @@ public function testRouterReverse() {
$result = Router::reverse($params);
$this->assertEquals('/posts/view/1', $result);
Router::reload();
Router::connect('/:lang/:controller/:action/*', array(), array('lang' => '[a-z]{3}'));
$params = array(
'lang' => 'eng',

0 comments on commit 5aaa5c5

Please sign in to comment.
You can’t perform that action at this time.