Fixing issue where defining a pattern for :action would create an optional route parameter that wouldn't respect its pattern.

Added the default action value only when a pattern isn't defined.
Added tests to CakeRoute and Router to cover this case.
Fixes #1197
This commit is contained in:
mark_story 2010-10-14 21:54:44 -04:00
parent 025090fbe9
commit 67874bd906
2 changed files with 68 additions and 1 deletions

View file

@ -269,7 +269,10 @@ class Router {
$self->__prefixes[] = $defaults['prefix']; $self->__prefixes[] = $defaults['prefix'];
$self->__prefixes = array_keys(array_flip($self->__prefixes)); $self->__prefixes = array_keys(array_flip($self->__prefixes));
} }
$defaults += array('action' => 'index', 'plugin' => null); $defaults += array('plugin' => null);
if (empty($options['action'])) {
$defaults += array('action' => 'index');
}
$routeClass = 'CakeRoute'; $routeClass = 'CakeRoute';
if (isset($options['routeClass'])) { if (isset($options['routeClass'])) {
$routeClass = $options['routeClass']; $routeClass = $options['routeClass'];

View file

@ -1694,6 +1694,45 @@ class RouterTest extends CakeTestCase {
$this->assertEqual($result['pass'][0], 'something. . .', 'Period was chopped off %s'); $this->assertEqual($result['pass'][0], 'something. . .', 'Period was chopped off %s');
} }
/**
* test that patterns work for :action
*
* @return void
*/
function testParsingWithPatternOnAction() {
Router::reload();
Router::connect(
'/blog/:action/*',
array('controller' => 'blog_posts'),
array('action' => 'other|actions')
);
$result = Router::parse('/blog/other');
$expected = array(
'plugin' => null,
'controller' => 'blog_posts',
'action' => 'other',
'pass' => array(),
'named' => array()
);
$this->assertEqual($expected, $result);
$result = Router::parse('/blog/foobar');
$expected = array(
'plugin' => null,
'controller' => 'blog',
'action' => 'foobar',
'pass' => array(),
'named' => array()
);
$this->assertEqual($expected, $result);
$result = Router::url(array('controller' => 'blog_posts', 'action' => 'foo'));
$this->assertEqual('/blog_posts/foo', $result);
$result = Router::url(array('controller' => 'blog_posts', 'action' => 'actions'));
$this->assertEqual('/blog/actions', $result);
}
/** /**
* testParsingWithPrefixes method * testParsingWithPrefixes method
* *
@ -2489,6 +2528,31 @@ class CakeRouteTestCase extends CakeTestCase {
$this->assertFalse($result); $this->assertFalse($result);
} }
/**
* test that patterns work for :action
*
* @return void
*/
function testPatternOnAction() {
$route =& new CakeRoute(
'/blog/:action/*',
array('controller' => 'blog_posts'),
array('action' => 'other|actions')
);
$result = $route->match(array('controller' => 'blog_posts', 'action' => 'foo'));
$this->assertFalse($result);
$result = $route->match(array('controller' => 'blog_posts', 'action' => 'actions'));
$this->assertTrue($result);
$result = $route->parse('/blog/other');
$expected = array('controller' => 'blog_posts', 'action' => 'other', 'pass' => array(), 'named' => array());
$this->assertEqual($expected, $result);
$result = $route->parse('/blog/foobar');
$this->assertFalse($result);
}
/** /**
* test persistParams ability to persist parameters from $params and remove params. * test persistParams ability to persist parameters from $params and remove params.
* *