Skip to content

Commit

Permalink
Merge pull request #9525 from cakephp/requestaction-fix
Browse files Browse the repository at this point in the history
Fix requestAction() not working with middleware.
  • Loading branch information
lorenzo committed Sep 28, 2016
2 parents 79e8757 + d7d0e22 commit bdbe2ba
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 1 deletion.
20 changes: 20 additions & 0 deletions src/Routing/RequestActionTrait.php
Expand Up @@ -17,6 +17,8 @@
use Cake\Network\Request;
use Cake\Network\Response;
use Cake\Network\Session;
use Cake\Routing\Filter\ControllerFactoryFilter;
use Cake\Routing\Filter\RoutingFilter;

/**
* Provides the requestAction() method for doing sub-requests
Expand Down Expand Up @@ -154,6 +156,24 @@ public function requestAction($url, array $extra = [])
$request = new Request($params);
$request->addParams($extra);
$dispatcher = DispatcherFactory::create();

// If an application is using PSR7 middleware,
// we need to 'fix' their missing dispatcher filters.
$needed = [
'routing' => RoutingFilter::class,
'controller' => ControllerFactoryFilter::class
];
foreach ($dispatcher->filters() as $filter) {
if ($filter instanceof RoutingFilter) {
unset($needed['routing']);
}
if ($filter instanceof ControllerFactoryFilter) {
unset($needed['controller']);
}
}
foreach ($needed as $class) {
$dispatcher->addFilter(new $class);
}
$result = $dispatcher->dispatch($request, new Response());
Router::popRequest();

Expand Down
16 changes: 15 additions & 1 deletion tests/TestCase/Routing/RequestActionTraitTest.php
Expand Up @@ -25,7 +25,6 @@
*/
class RequestActionTraitTest extends TestCase
{

/**
* fixtures
*
Expand Down Expand Up @@ -421,4 +420,19 @@ public function testRequestActionSession()
);
$this->assertEquals('bar', $result);
}

/**
* requestAction relies on both the RoutingFilter and ControllerFactory
* filters being connected. Ensure it can correct the missing state.
*
* @return void
*/
public function testRequestActionAddsRequiredFilters()
{
DispatcherFactory::clear();

$result = $this->object->requestAction('/request_action/test_request_action');
$expected = 'This is a test';
$this->assertEquals($expected, $result);
}
}

0 comments on commit bdbe2ba

Please sign in to comment.