Skip to content

Commit

Permalink
Fix InflectedRoute calling parent() incorrectly.
Browse files Browse the repository at this point in the history
The parent call in InflectedRoute was missing the `$method` parameter.
This caused routes to not match correctly when routes have method
conditions.

Refs #10220
  • Loading branch information
markstory committed Feb 15, 2017
1 parent df61bce commit e8c75f1
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 2 deletions.
2 changes: 1 addition & 1 deletion src/Routing/Route/InflectedRoute.php
Expand Up @@ -43,7 +43,7 @@ class InflectedRoute extends Route
*/
public function parse($url, $method = '')
{
$params = parent::parse($url);
$params = parent::parse($url, $method);
if (!$params) {
return false;
}
Expand Down
2 changes: 1 addition & 1 deletion src/Routing/Route/Route.php
Expand Up @@ -327,7 +327,7 @@ public function parse($url, $method = '')
if (empty($method)) {
// Deprecated reading the global state is deprecated and will be removed in 4.x
$request = Router::getRequest(true) ?: ServerRequest::createFromGlobals();
$method = $request->env('REQUEST_METHOD');
$method = $request->getMethod();
}
if (!in_array($method, (array)$this->defaults['_method'], true)) {
return false;
Expand Down
15 changes: 15 additions & 0 deletions tests/TestCase/Routing/Route/InflectedRouteTest.php
Expand Up @@ -192,6 +192,21 @@ public function testParse()
$this->assertEquals(['tv_shows'], $result['pass']);
}

/**
* Test that parse() checks methods.
*
* @return void
*/
public function testParseMethodMatch()
{
$route = new InflectedRoute('/:controller/:action', ['_method' => 'POST']);
$this->assertFalse($route->parse('/blog_posts/add_new', 'GET'));

$result = $route->parse('/blog_posts/add_new', 'POST');
$this->assertEquals('BlogPosts', $result['controller']);
$this->assertEquals('add_new', $result['action']);
}

/**
* @return void
*/
Expand Down
18 changes: 18 additions & 0 deletions tests/TestCase/TestSuite/IntegrationTestCaseTest.php
Expand Up @@ -39,6 +39,7 @@ public function setUp()
parent::setUp();
Configure::write('App.namespace', 'TestApp');

Router::connect('/get/:controller/:action', ['_method' => 'GET'], ['routeClass' => 'InflectedRoute']);
Router::connect('/:controller/:action/*', [], ['routeClass' => 'InflectedRoute']);
DispatcherFactory::clear();
DispatcherFactory::add('Routing');
Expand Down Expand Up @@ -183,6 +184,23 @@ public function testGet()
$this->assertNotEmpty($this->_response);
$this->assertInstanceOf('Cake\Network\Response', $this->_response);
$this->assertEquals('This is a test', $this->_response->body());

$this->_response = null;
$this->get('/get/request_action/test_request_action');
$this->assertEquals('This is a test', $this->_response->body());
}

/**
* Test sending get requests sets the request method
*
* @return void
*/
public function testGetSpecificRouteHttpServer()
{
$this->useHttpServer(true);
$this->get('/get/request_action/test_request_action');
$this->assertResponseOk();
$this->assertEquals('This is a test', $this->_response->body());
}

/**
Expand Down

0 comments on commit e8c75f1

Please sign in to comment.