mirror of
https://github.com/kamilwylegala/cakephp2-php8.git
synced 2024-11-15 03:18:26 +00:00
Merge remote-tracking branch 'origin/2.0' into 2.0-class-loading
This commit is contained in:
commit
9c0ab75a03
4 changed files with 436 additions and 231 deletions
|
@ -185,7 +185,7 @@ class CakeRoute {
|
|||
}
|
||||
if (!preg_match($this->_compiledRoute, $url, $route)) {
|
||||
return false;
|
||||
} else {
|
||||
}
|
||||
foreach ($this->defaults as $key => $val) {
|
||||
if ($key[0] === '[' && preg_match('/^\[(\w+)\]$/', $key, $header)) {
|
||||
if (isset($this->__headerMap[$header[1]])) {
|
||||
|
@ -214,17 +214,138 @@ class CakeRoute {
|
|||
unset($route[$i]);
|
||||
}
|
||||
$route['pass'] = $route['named'] = array();
|
||||
$route += $this->defaults;
|
||||
|
||||
//move numerically indexed elements from the defaults into pass.
|
||||
foreach ($route as $key => $value) {
|
||||
// Assign defaults, set passed args to pass
|
||||
foreach ($this->defaults as $key => $value) {
|
||||
if (isset($route[$key])) {
|
||||
continue;
|
||||
}
|
||||
if (is_integer($key)) {
|
||||
$route['pass'][] = $value;
|
||||
unset($route[$key]);
|
||||
continue;
|
||||
}
|
||||
$route[$key] = $value;
|
||||
}
|
||||
|
||||
if (isset($route['_args_'])) {
|
||||
list($pass, $named) = $this->_parseArgs($route['_args_'], $route);
|
||||
$route['pass'] = array_merge($route['pass'], $pass);
|
||||
$route['named'] = $named;
|
||||
unset($route['_args_']);
|
||||
}
|
||||
|
||||
// restructure 'pass' key route params
|
||||
if (isset($this->options['pass'])) {
|
||||
$j = count($this->options['pass']);
|
||||
while($j--) {
|
||||
if (isset($route[$this->options['pass'][$j]])) {
|
||||
array_unshift($route['pass'], $route[$this->options['pass'][$j]]);
|
||||
}
|
||||
}
|
||||
}
|
||||
return $route;
|
||||
}
|
||||
|
||||
/**
|
||||
* Parse passed and Named parameters into a list of passed args, and a hash of named parameters.
|
||||
* The local and global configuration for named parameters will be used.
|
||||
*
|
||||
* @param string $args A string with the passed & named params. eg. /1/page:2
|
||||
* @param string $context The current route context, which should contain controller/action keys.
|
||||
* @return array Array of ($pass, $named)
|
||||
*/
|
||||
protected function _parseArgs($args, $context) {
|
||||
$pass = $named = array();
|
||||
$args = explode('/', $args);
|
||||
|
||||
$namedConfig = Router::namedConfig();
|
||||
$greedy = $namedConfig['greedyNamed'];
|
||||
$rules = $namedConfig['rules'];
|
||||
if (!empty($this->options['named'])) {
|
||||
$greedy = isset($this->options['greedyNamed']) && $this->options['greedyNamed'] === true;
|
||||
foreach ((array)$this->options['named'] as $key => $val) {
|
||||
if (is_numeric($key)) {
|
||||
$rules[$val] = true;
|
||||
continue;
|
||||
}
|
||||
$rules[$key] = $val;
|
||||
}
|
||||
}
|
||||
|
||||
foreach ($args as $param) {
|
||||
if (empty($param) && $param !== '0' && $param !== 0) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$separatorIsPresent = strpos($param, $namedConfig['separator']) !== false;
|
||||
if ((!isset($this->options['named']) || !empty($this->options['named'])) && $separatorIsPresent) {
|
||||
list($key, $val) = explode($namedConfig['separator'], $param, 2);
|
||||
$hasRule = isset($rules[$key]);
|
||||
$passIt = (!$hasRule && !$greedy) || ($hasRule && !$this->_matchNamed($key, $val, $rules[$key], $context));
|
||||
if ($passIt) {
|
||||
$pass[] = $param;
|
||||
} else {
|
||||
if (preg_match_all('/\[([A-Za-z0-9_-]+)?\]/', $key, $matches, PREG_SET_ORDER)) {
|
||||
$matches = array_reverse($matches);
|
||||
$parts = explode('[', $key);
|
||||
$key = array_shift($parts);
|
||||
$arr = $val;
|
||||
foreach ($matches as $match) {
|
||||
if (empty($match[1])) {
|
||||
$arr = array($arr);
|
||||
} else {
|
||||
$arr = array(
|
||||
$match[1] => $arr
|
||||
);
|
||||
}
|
||||
}
|
||||
$val = $arr;
|
||||
}
|
||||
$named = array_merge_recursive($named, array($key => $val));
|
||||
}
|
||||
} else {
|
||||
$pass[] = $param;
|
||||
}
|
||||
}
|
||||
return array($pass, $named);
|
||||
}
|
||||
|
||||
/**
|
||||
* Return true if a given named $param's $val matches a given $rule depending on $context. Currently implemented
|
||||
* rule types are controller, action and match that can be combined with each other.
|
||||
*
|
||||
* @param string $param The name of the named parameter
|
||||
* @param string $val The value of the named parameter
|
||||
* @param array $rule The rule(s) to apply, can also be a match string
|
||||
* @param string $context An array with additional context information (controller / action)
|
||||
* @return boolean
|
||||
*/
|
||||
protected function _matchNamed($param, $val, $rule, $context) {
|
||||
if ($rule === true || $rule === false) {
|
||||
return $rule;
|
||||
}
|
||||
if (is_string($rule)) {
|
||||
$rule = array('match' => $rule);
|
||||
}
|
||||
if (!is_array($rule)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
$controllerMatches = (
|
||||
!isset($rule['controller'], $context['controller']) ||
|
||||
in_array($context['controller'], (array)$rule['controller'])
|
||||
);
|
||||
if (!$controllerMatches) {
|
||||
return false;
|
||||
}
|
||||
$actionMatches = (
|
||||
!isset($rule['action'], $context['action']) ||
|
||||
in_array($context['action'], (array)$rule['action'])
|
||||
);
|
||||
if (!$actionMatches) {
|
||||
return false;
|
||||
}
|
||||
return (!isset($rule['match']) || preg_match('/' . $rule['match'] . '/', $val));
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -274,8 +395,9 @@ class CakeRoute {
|
|||
return false;
|
||||
}
|
||||
|
||||
$greedyNamed = Router::$named['greedy'];
|
||||
$allowedNamedParams = Router::$named['rules'];
|
||||
$namedConfig = Router::namedConfig();
|
||||
$greedyNamed = $namedConfig['greedyNamed'];
|
||||
$allowedNamedParams = $namedConfig['rules'];
|
||||
|
||||
$named = $pass = $_query = array();
|
||||
|
||||
|
@ -332,7 +454,7 @@ class CakeRoute {
|
|||
}
|
||||
}
|
||||
}
|
||||
return $this->_writeUrl(array_merge($url, compact('pass', 'named', '_query')));
|
||||
return $this->_writeUrl(array_merge($url, compact('pass', 'named')));
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -352,7 +474,8 @@ class CakeRoute {
|
|||
$params['pass'] = implode('/', $params['pass']);
|
||||
}
|
||||
|
||||
$separator = Router::$named['separator'];
|
||||
$namedConfig = Router::namedConfig();
|
||||
$separator = $namedConfig['separator'];
|
||||
|
||||
if (!empty($params['named']) && is_array($params['named'])) {
|
||||
$named = array();
|
||||
|
@ -389,20 +512,4 @@ class CakeRoute {
|
|||
return $out;
|
||||
}
|
||||
|
||||
/**
|
||||
* Generates a well-formed querystring from $q
|
||||
*
|
||||
* Will compose an array or nested array into a proper querystring.
|
||||
*
|
||||
* @param mixed $q An array of parameters to compose into a query string.
|
||||
* @param bool $escape Whether or not to use escaped &
|
||||
* @return string
|
||||
*/
|
||||
public function queryString($q, $escape = false) {
|
||||
$join = '&';
|
||||
if ($escape === true) {
|
||||
$join = '&';
|
||||
}
|
||||
return '?' . http_build_query($q, null, $join);
|
||||
}
|
||||
}
|
|
@ -85,7 +85,7 @@ class Router {
|
|||
const ID = '[0-9]+';
|
||||
const UUID = '[A-Fa-f0-9]{8}-[A-Fa-f0-9]{4}-[A-Fa-f0-9]{4}-[A-Fa-f0-9]{4}-[A-Fa-f0-9]{12}';
|
||||
|
||||
private static $__named = array(
|
||||
private static $__namedExpressions = array(
|
||||
'Action' => Router::ACTION,
|
||||
'Year' => Router::YEAR,
|
||||
'Month' => Router::MONTH,
|
||||
|
@ -100,9 +100,9 @@ class Router {
|
|||
* @var string
|
||||
* @access public
|
||||
*/
|
||||
public static $named = array(
|
||||
protected static $_namedConfig = array(
|
||||
'default' => array('page', 'fields', 'order', 'limit', 'recursive', 'sort', 'direction', 'step'),
|
||||
'greedy' => true,
|
||||
'greedyNamed' => true,
|
||||
'separator' => ':',
|
||||
'rules' => false,
|
||||
);
|
||||
|
@ -186,10 +186,10 @@ class Router {
|
|||
* Gets the named route elements for use in app/config/routes.php
|
||||
*
|
||||
* @return array Named route elements
|
||||
* @see Router::$__named
|
||||
* @see Router::$__namedExpressions
|
||||
*/
|
||||
public static function getNamedExpressions() {
|
||||
return self::$__named;
|
||||
return self::$__namedExpressions;
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -369,7 +369,7 @@ class Router {
|
|||
*/
|
||||
public static function connectNamed($named, $options = array()) {
|
||||
if (isset($options['separator'])) {
|
||||
self::$named['separator'] = $options['separator'];
|
||||
self::$_namedConfig['separator'] = $options['separator'];
|
||||
unset($options['separator']);
|
||||
}
|
||||
|
||||
|
@ -380,23 +380,33 @@ class Router {
|
|||
$options = array_merge(array('default' => false, 'reset' => false, 'greedy' => true), $options);
|
||||
}
|
||||
|
||||
if ($options['reset'] == true || self::$named['rules'] === false) {
|
||||
self::$named['rules'] = array();
|
||||
if ($options['reset'] == true || self::$_namedConfig['rules'] === false) {
|
||||
self::$_namedConfig['rules'] = array();
|
||||
}
|
||||
|
||||
if ($options['default']) {
|
||||
$named = array_merge($named, self::$named['default']);
|
||||
$named = array_merge($named, self::$_namedConfig['default']);
|
||||
}
|
||||
|
||||
foreach ($named as $key => $val) {
|
||||
if (is_numeric($key)) {
|
||||
self::$named['rules'][$val] = true;
|
||||
self::$_namedConfig['rules'][$val] = true;
|
||||
} else {
|
||||
self::$named['rules'][$key] = $val;
|
||||
self::$_namedConfig['rules'][$key] = $val;
|
||||
}
|
||||
}
|
||||
self::$named['greedy'] = $options['greedy'];
|
||||
return self::$named;
|
||||
self::$_namedConfig['greedyNamed'] = $options['greedy'];
|
||||
return self::$_namedConfig;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the current named parameter configuration values.
|
||||
*
|
||||
* @return array
|
||||
* @see Router::$_namedConfig
|
||||
*/
|
||||
public static function namedConfig() {
|
||||
return self::$_namedConfig;
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -426,7 +436,10 @@ class Router {
|
|||
* @return array Array of mapped resources
|
||||
*/
|
||||
public static function mapResources($controller, $options = array()) {
|
||||
$options = array_merge(array('prefix' => '/', 'id' => self::$__named['ID'] . '|' . self::$__named['UUID']), $options);
|
||||
$options = array_merge(array(
|
||||
'prefix' => '/',
|
||||
'id' => self::ID . '|' . self::UUID
|
||||
), $options);
|
||||
$prefix = $options['prefix'];
|
||||
|
||||
foreach ((array)$controller as $ctlName) {
|
||||
|
@ -486,36 +499,7 @@ class Router {
|
|||
|
||||
if (($r = $route->parse($url)) !== false) {
|
||||
self::$_currentRoute[] =& $route;
|
||||
|
||||
$params = $route->options;
|
||||
$argOptions = array();
|
||||
|
||||
if (array_key_exists('named', $params)) {
|
||||
$argOptions['named'] = $params['named'];
|
||||
unset($params['named']);
|
||||
}
|
||||
if (array_key_exists('greedy', $params)) {
|
||||
$argOptions['greedy'] = $params['greedy'];
|
||||
unset($params['greedy']);
|
||||
}
|
||||
$out = $r;
|
||||
|
||||
if (isset($out['_args_'])) {
|
||||
$argOptions['context'] = array('action' => $out['action'], 'controller' => $out['controller']);
|
||||
$parsedArgs = self::getArgs($out['_args_'], $argOptions);
|
||||
$out['pass'] = array_merge($out['pass'], $parsedArgs['pass']);
|
||||
$out['named'] = $parsedArgs['named'];
|
||||
unset($out['_args_']);
|
||||
}
|
||||
|
||||
if (isset($params['pass'])) {
|
||||
$j = count($params['pass']);
|
||||
while($j--) {
|
||||
if (isset($out[$params['pass'][$j]])) {
|
||||
array_unshift($out['pass'], $out[$params['pass'][$j]]);
|
||||
}
|
||||
}
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
@ -621,7 +605,7 @@ class Router {
|
|||
self::connect('/:controller', array('action' => 'index'));
|
||||
self::connect('/:controller/:action/*');
|
||||
|
||||
if (self::$named['rules'] === false) {
|
||||
if (self::$_namedConfig['rules'] === false) {
|
||||
self::connectNamed(true);
|
||||
}
|
||||
self::$_defaultsMapped = true;
|
||||
|
@ -971,10 +955,10 @@ class Router {
|
|||
if (is_array($value)) {
|
||||
$flattend = Set::flatten($value, '][');
|
||||
foreach ($flattend as $namedKey => $namedValue) {
|
||||
$output .= '/' . $name . "[$namedKey]" . self::$named['separator'] . $namedValue;
|
||||
$output .= '/' . $name . "[$namedKey]" . self::$_namedConfig['separator'] . $namedValue;
|
||||
}
|
||||
} else {
|
||||
$output .= '/' . $name . self::$named['separator'] . $value;
|
||||
$output .= '/' . $name . self::$_namedConfig['separator'] . $value;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -984,61 +968,6 @@ class Router {
|
|||
return $output;
|
||||
}
|
||||
|
||||
/**
|
||||
* Takes an array of URL parameters and separates the ones that can be used as named arguments
|
||||
*
|
||||
* @param array $params Associative array of URL parameters.
|
||||
* @param string $controller Name of controller being routed. Used in scoping.
|
||||
* @param string $action Name of action being routed. Used in scoping.
|
||||
* @return array
|
||||
*/
|
||||
public static function getNamedElements($params, $controller = null, $action = null) {
|
||||
$named = array();
|
||||
|
||||
foreach ($params as $param => $val) {
|
||||
if (isset(self::$named['rules'][$param])) {
|
||||
$rule = self::$named['rules'][$param];
|
||||
if (Router::matchNamed($param, $val, $rule, compact('controller', 'action'))) {
|
||||
$named[substr($param, 1)] = $val;
|
||||
unset($params[$param]);
|
||||
}
|
||||
}
|
||||
}
|
||||
return array($named, $params);
|
||||
}
|
||||
|
||||
/**
|
||||
* Return true if a given named $param's $val matches a given $rule depending on $context. Currently implemented
|
||||
* rule types are controller, action and match that can be combined with each other.
|
||||
*
|
||||
* @param string $param The name of the named parameter
|
||||
* @param string $val The value of the named parameter
|
||||
* @param array $rule The rule(s) to apply, can also be a match string
|
||||
* @param string $context An array with additional context information (controller / action)
|
||||
* @return boolean
|
||||
*/
|
||||
public static function matchNamed($param, $val, $rule, $context = array()) {
|
||||
if ($rule === true || $rule === false) {
|
||||
return $rule;
|
||||
}
|
||||
if (is_string($rule)) {
|
||||
$rule = array('match' => $rule);
|
||||
}
|
||||
if (!is_array($rule)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
$controllerMatches = !isset($rule['controller'], $context['controller']) || in_array($context['controller'], (array)$rule['controller']);
|
||||
if (!$controllerMatches) {
|
||||
return false;
|
||||
}
|
||||
$actionMatches = !isset($rule['action'], $context['action']) || in_array($context['action'], (array)$rule['action']);
|
||||
if (!$actionMatches) {
|
||||
return false;
|
||||
}
|
||||
return (!isset($rule['match']) || preg_match('/' . $rule['match'] . '/', $val));
|
||||
}
|
||||
|
||||
/**
|
||||
* Generates a well-formed querystring from $q
|
||||
*
|
||||
|
@ -1205,70 +1134,6 @@ class Router {
|
|||
return self::$_validExtensions;
|
||||
}
|
||||
|
||||
/**
|
||||
* Takes a passed params and converts it to args
|
||||
*
|
||||
* @param array $params
|
||||
* @return array Array containing passed and named parameters
|
||||
*/
|
||||
public static function getArgs($args, $options = array()) {
|
||||
$pass = $named = array();
|
||||
$args = explode('/', $args);
|
||||
|
||||
$greedy = isset($options['greedy']) ? $options['greedy'] : self::$named['greedy'];
|
||||
$context = array();
|
||||
if (isset($options['context'])) {
|
||||
$context = $options['context'];
|
||||
}
|
||||
$rules = self::$named['rules'];
|
||||
if (isset($options['named'])) {
|
||||
$greedy = isset($options['greedy']) && $options['greedy'] === true;
|
||||
foreach ((array)$options['named'] as $key => $val) {
|
||||
if (is_numeric($key)) {
|
||||
$rules[$val] = true;
|
||||
continue;
|
||||
}
|
||||
$rules[$key] = $val;
|
||||
}
|
||||
}
|
||||
|
||||
foreach ($args as $param) {
|
||||
if (empty($param) && $param !== '0' && $param !== 0) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$separatorIsPresent = strpos($param, self::$named['separator']) !== false;
|
||||
if ((!isset($options['named']) || !empty($options['named'])) && $separatorIsPresent) {
|
||||
list($key, $val) = explode(self::$named['separator'], $param, 2);
|
||||
$hasRule = isset($rules[$key]);
|
||||
$passIt = (!$hasRule && !$greedy) || ($hasRule && !self::matchNamed($key, $val, $rules[$key], $context));
|
||||
if ($passIt) {
|
||||
$pass[] = $param;
|
||||
} else {
|
||||
if (preg_match_all('/\[([A-Za-z0-9_-]+)?\]/', $key, $matches, PREG_SET_ORDER)) {
|
||||
$matches = array_reverse($matches);
|
||||
$parts = explode('[', $key);
|
||||
$key = array_shift($parts);
|
||||
$arr = $val;
|
||||
foreach ($matches as $match) {
|
||||
if (empty($match[1])) {
|
||||
$arr = array($arr);
|
||||
} else {
|
||||
$arr = array(
|
||||
$match[1] => $arr
|
||||
);
|
||||
}
|
||||
}
|
||||
$val = $arr;
|
||||
}
|
||||
$named = array_merge_recursive($named, array($key => $val));
|
||||
}
|
||||
} else {
|
||||
$pass[] = $param;
|
||||
}
|
||||
}
|
||||
return compact('pass', 'named');
|
||||
}
|
||||
}
|
||||
|
||||
//Save the initial state
|
||||
|
|
|
@ -171,12 +171,14 @@ class CakeRouteTestCase extends CakeTestCase {
|
|||
$this->assertPattern($result, '/posts/08/01/2007/title-of-post');
|
||||
$result = $route->parse('/posts/08/01/2007/title-of-post');
|
||||
|
||||
$this->assertEqual(count($result), 8);
|
||||
$this->assertEqual(count($result), 7);
|
||||
$this->assertEqual($result['controller'], 'posts');
|
||||
$this->assertEqual($result['action'], 'view');
|
||||
$this->assertEqual($result['year'], '2007');
|
||||
$this->assertEqual($result['month'], '08');
|
||||
$this->assertEqual($result['day'], '01');
|
||||
$this->assertEquals($result['pass'][0], 'title-of-post');
|
||||
|
||||
|
||||
$route = new CakeRoute(
|
||||
"/:extra/page/:slug/*",
|
||||
|
@ -448,6 +450,35 @@ class CakeRouteTestCase extends CakeTestCase {
|
|||
$this->assertEqual($result['action'], 'index');
|
||||
}
|
||||
|
||||
/**
|
||||
* test numerically indexed defaults, get appeneded to pass
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
function testParseWithPassDefaults() {
|
||||
$route = new Cakeroute('/:controller', array('action' => 'display', 'home'));
|
||||
$result = $route->parse('/posts');
|
||||
$expected = array(
|
||||
'controller' => 'posts',
|
||||
'action' => 'display',
|
||||
'pass' => array('home'),
|
||||
'named' => array()
|
||||
);
|
||||
$this->assertEquals($expected, $result);
|
||||
}
|
||||
|
||||
/**
|
||||
* test that http header conditions can cause route failures.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
function testParseWithHttpHeaderConditions() {
|
||||
$_SERVER['REQUEST_METHOD'] = 'GET';
|
||||
$route = new CakeRoute('/sample', array('controller' => 'posts', 'action' => 'index', '[method]' => 'POST'));
|
||||
|
||||
$this->assertFalse($route->parse('/sample'));
|
||||
}
|
||||
|
||||
/**
|
||||
* test that patterns work for :action
|
||||
*
|
||||
|
@ -473,4 +504,202 @@ class CakeRouteTestCase extends CakeTestCase {
|
|||
$this->assertFalse($result);
|
||||
}
|
||||
|
||||
/**
|
||||
* test the parseArgs method
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
function testParsePassedArgument() {
|
||||
$route = new CakeRoute('/:controller/:action/*');
|
||||
$result = $route->parse('/posts/edit/1/2/0');
|
||||
$expected = array(
|
||||
'controller' => 'posts',
|
||||
'action' => 'edit',
|
||||
'pass' => array('1', '2', '0'),
|
||||
'named' => array()
|
||||
);
|
||||
$this->assertEquals($expected, $result);
|
||||
|
||||
$result = $route->parse('/posts/edit/a-string/page:1/sort:value');
|
||||
$expected = array(
|
||||
'controller' => 'posts',
|
||||
'action' => 'edit',
|
||||
'pass' => array('a-string'),
|
||||
'named' => array(
|
||||
'page' => 1,
|
||||
'sort' => 'value'
|
||||
)
|
||||
);
|
||||
$this->assertEquals($expected, $result);
|
||||
}
|
||||
|
||||
/**
|
||||
* test that only named parameter rules are followed.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
function testParseNamedParametersWithRules() {
|
||||
$route = new CakeRoute('/:controller/:action/*', array(), array(
|
||||
'named' => array(
|
||||
'wibble',
|
||||
'fish' => array('action' => 'index'),
|
||||
'fizz' => array('controller' => array('comments', 'other')),
|
||||
'pattern' => 'val-[\d]+'
|
||||
)
|
||||
));
|
||||
$result = $route->parse('/posts/display/wibble:spin/fish:trout/fizz:buzz/unknown:value');
|
||||
$expected = array(
|
||||
'controller' => 'posts',
|
||||
'action' => 'display',
|
||||
'pass' => array('fish:trout', 'fizz:buzz', 'unknown:value'),
|
||||
'named' => array(
|
||||
'wibble' => 'spin'
|
||||
)
|
||||
);
|
||||
$this->assertEquals($expected, $result, 'Fish should not be parsed, as action != index');
|
||||
|
||||
$result = $route->parse('/posts/index/wibble:spin/fish:trout/fizz:buzz');
|
||||
$expected = array(
|
||||
'controller' => 'posts',
|
||||
'action' => 'index',
|
||||
'pass' => array('fizz:buzz'),
|
||||
'named' => array(
|
||||
'wibble' => 'spin',
|
||||
'fish' => 'trout'
|
||||
)
|
||||
);
|
||||
$this->assertEquals($expected, $result, 'Fish should be parsed, as action == index');
|
||||
|
||||
$result = $route->parse('/comments/index/wibble:spin/fish:trout/fizz:buzz');
|
||||
$expected = array(
|
||||
'controller' => 'comments',
|
||||
'action' => 'index',
|
||||
'pass' => array(),
|
||||
'named' => array(
|
||||
'wibble' => 'spin',
|
||||
'fish' => 'trout',
|
||||
'fizz' => 'buzz'
|
||||
)
|
||||
);
|
||||
$this->assertEquals($expected, $result, 'All params should be parsed as conditions were met.');
|
||||
|
||||
$result = $route->parse('/comments/index/pattern:val--');
|
||||
$expected = array(
|
||||
'controller' => 'comments',
|
||||
'action' => 'index',
|
||||
'pass' => array('pattern:val--'),
|
||||
'named' => array()
|
||||
);
|
||||
$this->assertEquals($expected, $result, 'Named parameter pattern unmet.');
|
||||
|
||||
$result = $route->parse('/comments/index/pattern:val-2');
|
||||
$expected = array(
|
||||
'controller' => 'comments',
|
||||
'action' => 'index',
|
||||
'pass' => array(),
|
||||
'named' => array('pattern' => 'val-2')
|
||||
);
|
||||
$this->assertEquals($expected, $result, 'Named parameter pattern met.');
|
||||
}
|
||||
|
||||
/**
|
||||
* test that greedyNamed ignores rules.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
function testParseGreedyNamed() {
|
||||
$route = new CakeRoute('/:controller/:action/*', array(), array(
|
||||
'named' => array(
|
||||
'fizz' => array('controller' => 'comments'),
|
||||
'pattern' => 'val-[\d]+',
|
||||
),
|
||||
'greedyNamed' => true
|
||||
));
|
||||
$result = $route->parse('/posts/display/wibble:spin/fizz:buzz/pattern:ignored');
|
||||
$expected = array(
|
||||
'controller' => 'posts',
|
||||
'action' => 'display',
|
||||
'pass' => array('fizz:buzz', 'pattern:ignored'),
|
||||
'named' => array(
|
||||
'wibble' => 'spin',
|
||||
)
|
||||
);
|
||||
$this->assertEquals($expected, $result, 'Greedy named grabs everything, rules are followed');
|
||||
}
|
||||
|
||||
/**
|
||||
* test that parsing array format named parameters works
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
function testParseArrayNamedParameters() {
|
||||
$route = new CakeRoute('/:controller/:action/*');
|
||||
$result = $route->parse('/tests/action/var[]:val1/var[]:val2');
|
||||
$expected = array(
|
||||
'controller' => 'tests',
|
||||
'action' => 'action',
|
||||
'named' => array(
|
||||
'var' => array(
|
||||
'val1',
|
||||
'val2'
|
||||
)
|
||||
),
|
||||
'pass' => array(),
|
||||
);
|
||||
$this->assertEqual($result, $expected);
|
||||
|
||||
$result = $route->parse('/tests/action/theanswer[is]:42/var[]:val2/var[]:val3');
|
||||
$expected = array(
|
||||
'controller' => 'tests',
|
||||
'action' => 'action',
|
||||
'named' => array(
|
||||
'theanswer' => array(
|
||||
'is' => 42
|
||||
),
|
||||
'var' => array(
|
||||
'val2',
|
||||
'val3'
|
||||
)
|
||||
),
|
||||
'pass' => array(),
|
||||
);
|
||||
$this->assertEqual($result, $expected);
|
||||
|
||||
$result = $route->parse('/tests/action/theanswer[is][not]:42/theanswer[]:5/theanswer[is]:6');
|
||||
$expected = array(
|
||||
'controller' => 'tests',
|
||||
'action' => 'action',
|
||||
'named' => array(
|
||||
'theanswer' => array(
|
||||
5,
|
||||
'is' => array(
|
||||
6,
|
||||
'not' => 42
|
||||
)
|
||||
),
|
||||
),
|
||||
'pass' => array(),
|
||||
);
|
||||
$this->assertEqual($result, $expected);
|
||||
}
|
||||
|
||||
/**
|
||||
* test restructuring args with pass key
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
function testPassArgRestructure() {
|
||||
$route = new CakeRoute('/:controller/:action/:slug', array(), array(
|
||||
'pass' => array('slug')
|
||||
));
|
||||
$result = $route->parse('/posts/view/my-title');
|
||||
$expected = array(
|
||||
'controller' => 'posts',
|
||||
'action' => 'view',
|
||||
'slug' => 'my-title',
|
||||
'pass' => array('my-title'),
|
||||
'named' => array()
|
||||
);
|
||||
$this->assertEquals($expected, $result, 'Slug should have moved');
|
||||
}
|
||||
}
|
|
@ -437,6 +437,7 @@ class RouterTest extends CakeTestCase {
|
|||
$expected = '/tests/index/namedParam[keyed]:is an array/namedParam[0]:test';
|
||||
$this->assertEqual($result, $expected);
|
||||
|
||||
//@todo Delete from here down, tests are in CakeRoute now.
|
||||
$result = Router::parse('/tests/action/var[]:val1/var[]:val2');
|
||||
$expected = array(
|
||||
'controller' => 'tests',
|
||||
|
@ -1018,7 +1019,7 @@ class RouterTest extends CakeTestCase {
|
|||
$this->assertEqual($result, $expected);
|
||||
|
||||
Router::reload();
|
||||
Router::connect('/posts/view/*', array('controller' => 'posts', 'action' => 'view'), array('named' => array('foo', 'answer'), 'greedy' => true));
|
||||
Router::connect('/posts/view/*', array('controller' => 'posts', 'action' => 'view'), array('named' => array('foo', 'answer'), 'greedyNamed' => true));
|
||||
$result = Router::parse('/posts/view/foo:bar/routing:fun/answer:42');
|
||||
$expected = array('pass' => array(), 'named' => array('foo' => 'bar', 'routing' => 'fun', 'answer' => '42'), 'plugin' => null, 'controller' => 'posts', 'action' => 'view');
|
||||
$this->assertEqual($result, $expected);
|
||||
|
@ -1329,7 +1330,7 @@ class RouterTest extends CakeTestCase {
|
|||
*/
|
||||
function testConnectNamed() {
|
||||
$named = Router::connectNamed(false, array('default' => true));
|
||||
$this->assertFalse($named['greedy']);
|
||||
$this->assertFalse($named['greedyNamed']);
|
||||
$this->assertEqual(array_keys($named['rules']), $named['default']);
|
||||
|
||||
Router::reload();
|
||||
|
@ -1430,23 +1431,25 @@ class RouterTest extends CakeTestCase {
|
|||
Router::reload();
|
||||
$result = Router::connectNamed(false);
|
||||
$this->assertEqual(array_keys($result['rules']), array());
|
||||
$this->assertFalse($result['greedy']);
|
||||
$this->assertFalse($result['greedyNamed']);
|
||||
$result = Router::parse('/controller/action/param1:value1:1/param2:value2:3/param:value');
|
||||
$expected = array('pass' => array('param1:value1:1', 'param2:value2:3', 'param:value'), 'named' => array(), 'controller' => 'controller', 'action' => 'action', 'plugin' => null);
|
||||
$this->assertEqual($result, $expected);
|
||||
|
||||
Router::reload();
|
||||
$result = Router::connectNamed(true);
|
||||
$this->assertEqual(array_keys($result['rules']), Router::$named['default']);
|
||||
$this->assertTrue($result['greedy']);
|
||||
$named = Router::namedConfig();
|
||||
$this->assertEqual(array_keys($result['rules']), $named['default']);
|
||||
$this->assertTrue($result['greedyNamed']);
|
||||
Router::reload();
|
||||
Router::connectNamed(array('param1' => 'not-matching'));
|
||||
$result = Router::parse('/controller/action/param1:value1:1/param2:value2:3/param:value');
|
||||
$expected = array('pass' => array('param1:value1:1'), 'named' => array('param2' => 'value2:3', 'param' => 'value'), 'controller' => 'controller', 'action' => 'action', 'plugin' => null);
|
||||
$this->assertEqual($result, $expected);
|
||||
|
||||
//@todo delete this test.
|
||||
Router::reload();
|
||||
Router::connect('/foo/:action/*', array('controller' => 'bar'), array('named' => array('param1' => array('action' => 'index')), 'greedy' => true));
|
||||
Router::connect('/foo/:action/*', array('controller' => 'bar'), array('named' => array('param1' => array('action' => 'index')), 'greedyNamed' => true));
|
||||
$result = Router::parse('/foo/index/param1:value1:1/param2:value2:3/param:value');
|
||||
$expected = array('pass' => array(), 'named' => array('param1' => 'value1:1', 'param2' => 'value2:3', 'param' => 'value'), 'controller' => 'bar', 'action' => 'index', 'plugin' => null);
|
||||
$this->assertEqual($result, $expected);
|
||||
|
@ -1473,6 +1476,7 @@ class RouterTest extends CakeTestCase {
|
|||
$expected = array('pass' => array('param2:value2:3', 'param3:value'), 'named' => array('param1' => 'value1:1'), 'controller' => 'controller', 'action' => 'action', 'plugin' => null);
|
||||
$this->assertEqual($result, $expected);
|
||||
|
||||
//@todo delete this test.
|
||||
Router::reload();
|
||||
Router::connect('/foo/*', array('controller' => 'bar', 'action' => 'fubar'), array('named' => array('param1' => 'value[\d]:[\d]')));
|
||||
Router::connectNamed(array(), array('greedy' => false));
|
||||
|
|
Loading…
Reference in a new issue