Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

3.0 request action #939

Merged
merged 7 commits into from

2 participants

Mark Story Juan Basso
Mark Story
Owner

Extract requestAction() into a trait. This lets us limit the number of places that its exposed by default, and give people the option to add it in where they need it later on.

Juan Basso
Collaborator

I guess you should add the trait in the Cake\View\View as well, to use in the ctp files. Leaving the current functionality.

...Cake/Controller/Component/RequestHandlerComponent.php
@@ -43,6 +44,8 @@
*/
class RequestHandlerComponent extends Component {
+ use RequestActionTrait;
Juan Basso Collaborator
jrbasso added a note

Can't you use thru controller?

Mark Story Owner

True, I'll fix that and the omission in View\View.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Mark Story markstory Remove requestAction from RequestHandlerComponent.
The method is available on the controller objects. Use that instead.
0930645
Mark Story markstory commented on the diff
lib/Cake/View/View.php
@@ -64,6 +65,8 @@
*/
class View extends Object {
+ use RequestActionTrait;
Mark Story Owner

Added here @jrbasso

Juan Basso Collaborator
jrbasso added a note

:+1:

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Mark Story
Owner

If there aren't any other comments on this, I'll merge it in tomorrow and update the documentation for 3.0

Juan Basso
Collaborator
Mark Story markstory merged commit c401b81 into from
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Nov 3, 2012
  1. Mark Story

    Extract requestAction() into a trait.

    markstory authored
    Having a separate trait allows us to reduce the number of places that
    requestAction is exposed, and also allow people to use requestAction
    where they need without inheritance.
  2. Mark Story
  3. Mark Story

    Update array usage.

    markstory authored
  4. Mark Story
  5. Mark Story
  6. Mark Story

    Expand documentation.

    markstory authored
Commits on Nov 5, 2012
  1. Mark Story

    Remove requestAction from RequestHandlerComponent.

    markstory authored
    The method is available on the controller objects. Use that instead.
This page is out of date. Refresh to see the latest.
5 lib/Cake/Controller/Component.php
View
@@ -1,8 +1,5 @@
<?php
/**
- *
- * PHP 5
- *
* CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
* Copyright 2005-2012, Cake Software Foundation, Inc. (http://cakefoundation.org)
*
@@ -11,11 +8,11 @@
*
* @copyright Copyright 2005-2012, Cake Software Foundation, Inc. (http://cakefoundation.org)
* @link http://cakephp.org CakePHP(tm) Project
- * @package Cake.Controller
* @since CakePHP(tm) v 1.2
* @license MIT License (http://www.opensource.org/licenses/mit-license.php)
*/
namespace Cake\Controller;
+
use Cake\Core\Object;
/**
2  lib/Cake/Controller/Component/RequestHandlerComponent.php
View
@@ -262,7 +262,7 @@ public function beforeRedirect(Controller $controller, $url, $status = null, $ex
$code = key($statusCode);
$this->response->statusCode($code);
}
- $this->response->body($this->requestAction($url, array('return', 'bare' => false)));
+ $this->response->body($controller->requestAction($url, array('return', 'bare' => false)));
$this->response->send();
$this->_stop();
}
2  lib/Cake/Controller/Controller.php
View
@@ -25,6 +25,7 @@
use Cake\Network\Request;
use Cake\Network\Response;
use Cake\Routing\Router;
+use Cake\Routing\RequestActionTrait;
use Cake\Utility\ClassRegistry;
use Cake\Utility\Inflector;
use Cake\Utility\MergeVariablesTrait;
@@ -65,6 +66,7 @@
class Controller extends Object implements EventListener {
use MergeVariablesTrait;
+ use RequestActionTrait;
/**
* The name of this controller. Controller names are plural, named after the model they manipulate.
73 lib/Cake/Core/Object.php
View
@@ -15,10 +15,6 @@
namespace Cake\Core;
use Cake\Log\LogTrait;
-use Cake\Network\Request;
-use Cake\Network\Response;
-use Cake\Routing\Dispatcher;
-use Cake\Routing\Router;
use Cake\Utility\Hash;
/**
@@ -50,75 +46,6 @@ public function toString() {
}
/**
- * Calls a controller's method from any location. Can be used to connect controllers together
- * or tie plugins into a main application. requestAction can be used to return rendered views
- * or fetch the return value from controller actions.
- *
- * Under the hood this method uses Router::reverse() to convert the $url parameter into a string
- * URL. You should use URL formats that are compatible with Router::reverse()
- *
- * #### Passing POST and GET data
- *
- * POST and GET data can be simulated in requestAction. Use `$extra['query']` for
- * GET data. The `$extra['post']` parameter allows POST data simulation.
- *
- * @param string|array $url String or array-based url. Unlike other url arrays in CakePHP, this
- * url will not automatically handle passed arguments in the $url parameter.
- * @param array $extra if array includes the key "return" it sets the AutoRender to true. Can
- * also be used to submit GET/POST data, and passed arguments.
- * @return mixed Boolean true or false on success/failure, or contents
- * of rendered action if 'return' is set in $extra.
- */
- public function requestAction($url, $extra = array()) {
- if (empty($url)) {
- return false;
- }
- if (($index = array_search('return', $extra)) !== false) {
- $extra['return'] = 0;
- $extra['autoRender'] = 1;
- unset($extra[$index]);
- }
- $extra = array_merge(
- ['autoRender' => 0, 'return' => 1, 'bare' => 1, 'requested' => 1],
- $extra
- );
- $post = $query = [];
- if (isset($extra['post'])) {
- $post = $extra['post'];
- }
- if (isset($extra['query'])) {
- $query = $extra['query'];
- }
- unset($extra['post'], $extra['query']);
-
- if (is_string($url) && strpos($url, FULL_BASE_URL) === 0) {
- $url = Router::normalize(str_replace(FULL_BASE_URL, '', $url));
- }
- if (is_string($url)) {
- $params = array(
- 'url' => $url
- );
- } elseif (is_array($url)) {
- $params = array_merge($url, [
- 'pass' => [],
- 'base' => false,
- 'url' => Router::reverse($url)
- ]);
- }
- if (!empty($post)) {
- $params['post'] = $post;
- }
- if (!empty($query)) {
- $params['query'] = $query;
- }
- $request = new Request($params);
- $dispatcher = new Dispatcher();
- $result = $dispatcher->dispatch($request, new Response(), $extra);
- Router::popRequest();
- return $result;
- }
-
-/**
* Calls a method on this object with the given parameters. Provides an OO wrapper
* for `call_user_func_array`
*
13 lib/Cake/Network/Request.php
View
@@ -39,7 +39,7 @@ class Request implements \ArrayAccess {
'plugin' => null,
'controller' => null,
'action' => null,
- 'pass' => array(),
+ 'pass' => [],
);
/**
@@ -49,14 +49,21 @@ class Request implements \ArrayAccess {
*
* @var array
*/
- public $data = array();
+ public $data = [];
/**
* Array of querystring arguments
*
* @var array
*/
- public $query = array();
+ public $query = [];
+
+/**
+ * Array of cookie data.
+ *
+ * @var array
+ */
+ public $cookies = [];
/**
* The url string used for the request.
125 lib/Cake/Routing/RequestActionTrait.php
View
@@ -0,0 +1,125 @@
+<?php
+/**
+ * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
+ * Copyright 2005-2012, Cake Software Foundation, Inc. (http://cakefoundation.org)
+ *
+ * Licensed under The MIT License
+ * Redistributions of files must retain the above copyright notice.
+ *
+ * @copyright Copyright 2005-2012, Cake Software Foundation, Inc. (http://cakefoundation.org)
+ * @link http://cakephp.org CakePHP(tm) Project
+ * @since CakePHP(tm) v 3.0.0
+ * @license MIT License (http://www.opensource.org/licenses/mit-license.php)
+ */
+namespace Cake\Routing;
+
+use Cake\Network\Request;
+use Cake\Network\Response;
+use Cake\Routing\Dispatcher;
+use Cake\Routing\Router;
+
+/**
+ * Provides the requestAction() method for doing sub-requests
+ *
+ * @package Cake.Routing
+ */
+trait RequestActionTrait {
+
+/**
+ * Calls a controller's method from any location. Can be used to connect controllers together
+ * or tie plugins into a main application. requestAction can be used to return rendered views
+ * or fetch the return value from controller actions.
+ *
+ * Under the hood this method uses Router::reverse() to convert the $url parameter into a string
+ * URL. You should use URL formats that are compatible with Router::reverse()
+ *
+ * ### Examples
+ *
+ * A basic example getting the return value of the controller action:
+ *
+ * {{{
+ * $variables = $this->requestAction('/articles/popular');
+ * }}}
+ *
+ * A basic example of request action to fetch a rendered page without the layout.
+ *
+ * {{{
+ * $viewHtml = $this->requestAction('/articles/popular', ['return']);
+ * }}}
+ *
+ * You can also pass the URL as an array:
+ *
+ * {{{
+ * $vars = $this->requestAction(['controller' => 'articles', 'action' => 'popular']);
+ * }}}
+ *
+ * ### Passing other request data
+ *
+ * You can pass POST, GET, COOKIE and other data into the request using the apporpriate keys.
+ * Cookies can be passed using the `cookies` key. Get parameters can be set with `query` and post
+ * data can be sent using the `post` key.
+ *
+ * {{{
+ * $vars = $this->requestAction('/articles/popular', [
+ * 'query' => ['page' = > 1],
+ * 'cookies' => ['remember_me' => 1],
+ * ]);
+ * }}}
+ *
+ * @param string|array $url String or array-based url. Unlike other url arrays in CakePHP, this
+ * url will not automatically handle passed arguments in the $url parameter.
+ * @param array $extra if array includes the key "return" it sets the autoRender to true. Can
+ * also be used to submit GET/POST data, and passed arguments.
+ * @return mixed Boolean true or false on success/failure, or contents
+ * of rendered action if 'return' is set in $extra.
+ */
+ public function requestAction($url, $extra = array()) {
+ if (empty($url)) {
+ return false;
+ }
+ if (($index = array_search('return', $extra)) !== false) {
+ $extra['return'] = 0;
+ $extra['autoRender'] = 1;
+ unset($extra[$index]);
+ }
+ $extra = array_merge(
+ ['autoRender' => 0, 'return' => 1, 'bare' => 1, 'requested' => 1],
+ $extra
+ );
+ $post = $query = [];
+ if (isset($extra['post'])) {
+ $post = $extra['post'];
+ }
+ if (isset($extra['query'])) {
+ $query = $extra['query'];
+ }
+ unset($extra['post'], $extra['query']);
+
+ if (is_string($url) && strpos($url, FULL_BASE_URL) === 0) {
+ $url = Router::normalize(str_replace(FULL_BASE_URL, '', $url));
+ }
+ if (is_string($url)) {
+ $params = [
+ 'url' => $url
+ ];
+ } elseif (is_array($url)) {
+ $params = array_merge($url, [
+ 'pass' => [],
+ 'base' => false,
+ 'url' => Router::reverse($url)
+ ]);
+ }
+ if (!empty($post)) {
+ $params['post'] = $post;
+ }
+ if (!empty($query)) {
+ $params['query'] = $query;
+ }
+ $request = new Request($params);
+ $dispatcher = new Dispatcher();
+ $result = $dispatcher->dispatch($request, new Response(), $extra);
+ Router::popRequest();
+ return $result;
+ }
+
+}
6 lib/Cake/Test/TestCase/Controller/Component/SessionComponentTest.php
View
@@ -94,14 +94,14 @@ public function testSessionIdConsistentAcrossRequestAction() {
$Session->check('Test');
$this->assertTrue(isset($_SESSION));
- $Object = new Object();
+ $Controller = new Controller();
$Session = new SessionComponent($this->ComponentCollection);
$expected = $Session->id();
- $result = $Object->requestAction('/session_test/session_id');
+ $result = $Controller->requestAction('/session_test/session_id');
$this->assertEquals($expected, $result);
- $result = $Object->requestAction('/orange_session_test/session_id');
+ $result = $Controller->requestAction('/orange_session_test/session_id');
$this->assertEquals($expected, $result);
}
255 lib/Cake/Test/TestCase/Core/ObjectTest.php
View
@@ -337,259 +337,4 @@ public function testMethodDispatching() {
$this->assertSame($this->object->methodCalls, $expected);
}
-/**
- * testRequestAction method
- *
- * @return void
- */
- public function testRequestAction() {
- App::build(array(
- 'Model' => array(CAKE . 'Test/TestApp/Model/'),
- 'View' => array(CAKE . 'Test/TestApp/View/'),
- 'Controller' => array(CAKE . 'Test/TestApp/Controller/')
- ), App::RESET);
- $this->assertNull(Router::getRequest(), 'request stack should be empty.');
-
- $result = $this->object->requestAction('');
- $this->assertFalse($result);
-
- $result = $this->object->requestAction('/request_action/test_request_action');
- $expected = 'This is a test';
- $this->assertEquals($expected, $result);
-
- $result = $this->object->requestAction(FULL_BASE_URL . '/request_action/test_request_action');
- $expected = 'This is a test';
- $this->assertEquals($expected, $result);
-
- $result = $this->object->requestAction('/request_action/another_ra_test/2/5');
- $expected = 7;
- $this->assertEquals($expected, $result);
-
- $result = $this->object->requestAction('/tests_apps/index', array('return'));
- $expected = 'This is the TestsAppsController index view ';
- $this->assertEquals($expected, $result);
-
- $result = $this->object->requestAction('/tests_apps/some_method');
- $expected = 5;
- $this->assertEquals($expected, $result);
-
- $result = $this->object->requestAction('/request_action/paginate_request_action');
- $this->assertTrue($result);
-
- $result = $this->object->requestAction('/request_action/normal_request_action');
- $expected = 'Hello World';
- $this->assertEquals($expected, $result);
-
- $this->assertNull(Router::getRequest(), 'requests were not popped off the stack, this will break url generation');
- }
-
-/**
- * test requestAction() and plugins.
- *
- * @return void
- */
- public function testRequestActionPlugins() {
- App::build(array(
- 'Plugin' => array(CAKE . 'Test/TestApp/Plugin/'),
- ), App::RESET);
- Plugin::load('TestPlugin');
- Router::reload();
-
- $result = $this->object->requestAction('/test_plugin/tests/index', array('return'));
- $expected = 'test plugin index';
- $this->assertEquals($expected, $result);
-
- $result = $this->object->requestAction('/test_plugin/tests/index/some_param', array('return'));
- $expected = 'test plugin index';
- $this->assertEquals($expected, $result);
-
- $result = $this->object->requestAction(
- array('controller' => 'tests', 'action' => 'index', 'plugin' => 'test_plugin'), array('return')
- );
- $expected = 'test plugin index';
- $this->assertEquals($expected, $result);
-
- $result = $this->object->requestAction('/test_plugin/tests/some_method');
- $expected = 25;
- $this->assertEquals($expected, $result);
-
- $result = $this->object->requestAction(
- array('controller' => 'tests', 'action' => 'some_method', 'plugin' => 'test_plugin')
- );
- $expected = 25;
- $this->assertEquals($expected, $result);
- }
-
-/**
- * test requestAction() with arrays.
- *
- * @return void
- */
- public function testRequestActionArray() {
- App::build(array(
- 'Model' => array(CAKE . 'Test/TestApp/Model/'),
- 'View' => array(CAKE . 'Test/TestApp/View/'),
- 'Controller' => array(CAKE . 'Test/TestApp/Controller/'),
- 'Plugin' => array(CAKE . 'Test/TestApp/Plugin/')
- ), App::RESET);
- Plugin::load(array('TestPlugin'));
- Router::reload();
- require CAKE . 'Config/routes.php';
-
- $result = $this->object->requestAction(
- array('controller' => 'request_action', 'action' => 'test_request_action')
- );
- $expected = 'This is a test';
- $this->assertEquals($expected, $result);
-
- $result = $this->object->requestAction(
- array('controller' => 'request_action', 'action' => 'another_ra_test'),
- array('pass' => array('5', '7'))
- );
- $expected = 12;
- $this->assertEquals($expected, $result);
-
- $result = $this->object->requestAction(
- array('controller' => 'tests_apps', 'action' => 'index'), array('return')
- );
- $expected = 'This is the TestsAppsController index view ';
- $this->assertEquals($expected, $result);
-
- $result = $this->object->requestAction(array('controller' => 'tests_apps', 'action' => 'some_method'));
- $expected = 5;
- $this->assertEquals($expected, $result);
-
- $result = $this->object->requestAction(
- array('controller' => 'request_action', 'action' => 'normal_request_action')
- );
- $expected = 'Hello World';
- $this->assertEquals($expected, $result);
-
- $result = $this->object->requestAction(
- array('controller' => 'request_action', 'action' => 'paginate_request_action')
- );
- $this->assertTrue($result);
-
- $result = $this->object->requestAction(
- array('controller' => 'request_action', 'action' => 'paginate_request_action'),
- array('pass' => array(5))
- );
- $this->assertTrue($result);
- }
-
-/**
- * Test that requestAction() does not forward the 0 => return value.
- *
- * @return void
- */
- public function testRequestActionRemoveReturnParam() {
- $result = $this->object->requestAction(
- '/request_action/param_check', array('return')
- );
- $this->assertEquals('', $result, 'Return key was found');
- }
-
-/**
- * Test that requestAction() is populating $this->params properly
- *
- * @return void
- */
- public function testRequestActionParamParseAndPass() {
- $result = $this->object->requestAction('/request_action/params_pass');
- $this->assertEquals('request_action/params_pass', $result->url);
- $this->assertEquals('request_action', $result['controller']);
- $this->assertEquals('params_pass', $result['action']);
- $this->assertEquals(null, $result['plugin']);
- }
-
-/**
- * Test that requestAction handles get parameters correctly.
- *
- * @return void
- */
- public function testRequestActionGetParameters() {
- $result = $this->object->requestAction(
- '/request_action/params_pass?get=value&limit=5'
- );
- $this->assertEquals('value', $result->query['get']);
-
- $result = $this->object->requestAction(
- array('controller' => 'request_action', 'action' => 'params_pass'),
- array('query' => array('get' => 'value', 'limit' => 5))
- );
- $this->assertEquals('value', $result->query['get']);
- }
-
-/**
- * test that requestAction does not fish data out of the POST
- * superglobal.
- *
- * @return void
- */
- public function testRequestActionNoPostPassing() {
- Router::reload();
- require CAKE . 'Config/routes.php';
-
- $_POST = array(
- 'item' => 'value'
- );
- $result = $this->object->requestAction(array('controller' => 'request_action', 'action' => 'post_pass'));
- $this->assertEmpty($result);
-
- $result = $this->object->requestAction(
- array('controller' => 'request_action', 'action' => 'post_pass'),
- array('post' => $_POST)
- );
- $expected = $_POST;
- $this->assertEquals($expected, $result);
- }
-
-/**
- * test that requestAction() can get query data from the query string and
- * query option.
- *
- * @return void
- */
- public function testRequestActionWithQueryString() {
- Router::reload();
- require CAKE . 'Config/routes.php';
- $query = ['page' => 1, 'sort' => 'title'];
- $result = $this->object->requestAction(
- ['controller' => 'request_action', 'action' => 'query_pass'],
- ['query' => $query]
- );
- $this->assertEquals($query, $result);
-
- $result = $this->object->requestAction(
- '/request_action/query_pass?page=3&sort=body'
- );
- $expected = ['page' => 3, 'sort' => 'body'];
- $this->assertEquals($expected, $result);
- }
-
-/**
- * Test requestAction with post data.
- *
- * @return void
- */
- public function testRequestActionPostWithData() {
- Router::reload();
- require CAKE . 'Config/routes.php';
-
- $data = array(
- 'Post' => array('id' => 2)
- );
- $result = $this->object->requestAction(
- array('controller' => 'request_action', 'action' => 'post_pass'),
- array('post' => $data)
- );
- $this->assertEquals($data, $result);
-
- $result = $this->object->requestAction(
- '/request_action/post_pass',
- array('post' => $data)
- );
- $this->assertEquals($data, $result);
- }
-
}
295 lib/Cake/Test/TestCase/Routing/RequestActionTraitTest.php
View
@@ -0,0 +1,295 @@
+<?php
+/**
+ * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
+ * Copyright 2005-2012, Cake Software Foundation, Inc. (http://cakefoundation.org)
+ *
+ * Licensed under The MIT License
+ * Redistributions of files must retain the above copyright notice.
+ *
+ * @copyright Copyright 2005-2012, Cake Software Foundation, Inc. (http://cakefoundation.org)
+ * @link http://cakephp.org CakePHP(tm) Project
+ * @since CakePHP(tm) v 3.0.0
+ * @license MIT License (http://www.opensource.org/licenses/mit-license.php)
+ */
+namespace Cake\Test\TestCase\Routing;
+
+use Cake\Core\App;
+use Cake\Core\Configure;
+use Cake\Core\Plugin;
+use Cake\Routing\RequestActionTrait;
+use Cake\Routing\Router;
+use Cake\TestSuite\TestCase;
+
+/**
+ * @package Cake.Test.TestCase.Routing
+ */
+class RequestActionTraitTest extends TestCase {
+
+/**
+ * fixtures
+ *
+ * @var string
+ */
+ public $fixtures = array('core.post', 'core.test_plugin_comment', 'core.comment');
+
+/**
+ * Setup
+ *
+ * @return void
+ */
+ public function setUp() {
+ parent::setUp();
+ Configure::write('App.namespace', 'TestApp');
+ Configure::write('Security.salt', 'not-the-default');
+ $this->object = $this->getObjectForTrait('Cake\Routing\RequestActionTrait');
+ }
+
+/**
+ * testRequestAction method
+ *
+ * @return void
+ */
+ public function testRequestAction() {
+ App::build(array(
+ 'Model' => array(CAKE . 'Test/TestApp/Model/'),
+ 'View' => array(CAKE . 'Test/TestApp/View/'),
+ 'Controller' => array(CAKE . 'Test/TestApp/Controller/')
+ ), App::RESET);
+ $this->assertNull(Router::getRequest(), 'request stack should be empty.');
+
+ $result = $this->object->requestAction('');
+ $this->assertFalse($result);
+
+ $result = $this->object->requestAction('/request_action/test_request_action');
+ $expected = 'This is a test';
+ $this->assertEquals($expected, $result);
+
+ $result = $this->object->requestAction(FULL_BASE_URL . '/request_action/test_request_action');
+ $expected = 'This is a test';
+ $this->assertEquals($expected, $result);
+
+ $result = $this->object->requestAction('/request_action/another_ra_test/2/5');
+ $expected = 7;
+ $this->assertEquals($expected, $result);
+
+ $result = $this->object->requestAction('/tests_apps/index', array('return'));
+ $expected = 'This is the TestsAppsController index view ';
+ $this->assertEquals($expected, $result);
+
+ $result = $this->object->requestAction('/tests_apps/some_method');
+ $expected = 5;
+ $this->assertEquals($expected, $result);
+
+ $result = $this->object->requestAction('/request_action/paginate_request_action');
+ $this->assertTrue($result);
+
+ $result = $this->object->requestAction('/request_action/normal_request_action');
+ $expected = 'Hello World';
+ $this->assertEquals($expected, $result);
+
+ $this->assertNull(Router::getRequest(), 'requests were not popped off the stack, this will break url generation');
+ }
+
+/**
+ * test requestAction() and plugins.
+ *
+ * @return void
+ */
+ public function testRequestActionPlugins() {
+ App::build(array(
+ 'Plugin' => array(CAKE . 'Test/TestApp/Plugin/'),
+ ), App::RESET);
+ Plugin::load('TestPlugin');
+ Router::reload();
+
+ $result = $this->object->requestAction('/test_plugin/tests/index', array('return'));
+ $expected = 'test plugin index';
+ $this->assertEquals($expected, $result);
+
+ $result = $this->object->requestAction('/test_plugin/tests/index/some_param', array('return'));
+ $expected = 'test plugin index';
+ $this->assertEquals($expected, $result);
+
+ $result = $this->object->requestAction(
+ array('controller' => 'tests', 'action' => 'index', 'plugin' => 'test_plugin'), array('return')
+ );
+ $expected = 'test plugin index';
+ $this->assertEquals($expected, $result);
+
+ $result = $this->object->requestAction('/test_plugin/tests/some_method');
+ $expected = 25;
+ $this->assertEquals($expected, $result);
+
+ $result = $this->object->requestAction(
+ array('controller' => 'tests', 'action' => 'some_method', 'plugin' => 'test_plugin')
+ );
+ $expected = 25;
+ $this->assertEquals($expected, $result);
+ }
+
+/**
+ * test requestAction() with arrays.
+ *
+ * @return void
+ */
+ public function testRequestActionArray() {
+ App::build(array(
+ 'Model' => array(CAKE . 'Test/TestApp/Model/'),
+ 'View' => array(CAKE . 'Test/TestApp/View/'),
+ 'Controller' => array(CAKE . 'Test/TestApp/Controller/'),
+ 'Plugin' => array(CAKE . 'Test/TestApp/Plugin/')
+ ), App::RESET);
+ Plugin::load(array('TestPlugin'));
+
+ $result = $this->object->requestAction(
+ array('controller' => 'request_action', 'action' => 'test_request_action')
+ );
+ $expected = 'This is a test';
+ $this->assertEquals($expected, $result);
+
+ $result = $this->object->requestAction(
+ array('controller' => 'request_action', 'action' => 'another_ra_test'),
+ array('pass' => array('5', '7'))
+ );
+ $expected = 12;
+ $this->assertEquals($expected, $result);
+
+ $result = $this->object->requestAction(
+ array('controller' => 'tests_apps', 'action' => 'index'), array('return')
+ );
+ $expected = 'This is the TestsAppsController index view ';
+ $this->assertEquals($expected, $result);
+
+ $result = $this->object->requestAction(array('controller' => 'tests_apps', 'action' => 'some_method'));
+ $expected = 5;
+ $this->assertEquals($expected, $result);
+
+ $result = $this->object->requestAction(
+ array('controller' => 'request_action', 'action' => 'normal_request_action')
+ );
+ $expected = 'Hello World';
+ $this->assertEquals($expected, $result);
+
+ $result = $this->object->requestAction(
+ array('controller' => 'request_action', 'action' => 'paginate_request_action')
+ );
+ $this->assertTrue($result);
+
+ $result = $this->object->requestAction(
+ array('controller' => 'request_action', 'action' => 'paginate_request_action'),
+ array('pass' => array(5))
+ );
+ $this->assertTrue($result);
+ }
+
+/**
+ * Test that requestAction() does not forward the 0 => return value.
+ *
+ * @return void
+ */
+ public function testRequestActionRemoveReturnParam() {
+ $result = $this->object->requestAction(
+ '/request_action/param_check', array('return')
+ );
+ $this->assertEquals('', $result, 'Return key was found');
+ }
+
+/**
+ * Test that requestAction() is populating $this->params properly
+ *
+ * @return void
+ */
+ public function testRequestActionParamParseAndPass() {
+ $result = $this->object->requestAction('/request_action/params_pass');
+ $this->assertEquals('request_action/params_pass', $result->url);
+ $this->assertEquals('request_action', $result['controller']);
+ $this->assertEquals('params_pass', $result['action']);
+ $this->assertEquals(null, $result['plugin']);
+ }
+
+/**
+ * test that requestAction does not fish data out of the POST
+ * superglobal.
+ *
+ * @return void
+ */
+ public function testRequestActionNoPostPassing() {
+ $_POST = array(
+ 'item' => 'value'
+ );
+ $result = $this->object->requestAction(array('controller' => 'request_action', 'action' => 'post_pass'));
+ $expected = null;
+ $this->assertEmpty($result);
+
+ $result = $this->object->requestAction(
+ array('controller' => 'request_action', 'action' => 'post_pass'),
+ array('post' => $_POST)
+ );
+ $expected = $_POST;
+ $this->assertEquals($expected, $result);
+ }
+
+/**
+ * test that requestAction() can get query data from the query string and
+ * query option.
+ *
+ * @return void
+ */
+ public function testRequestActionWithQueryString() {
+ Router::reload();
+ require CAKE . 'Config/routes.php';
+ $query = ['page' => 1, 'sort' => 'title'];
+ $result = $this->object->requestAction(
+ ['controller' => 'request_action', 'action' => 'query_pass'],
+ ['query' => $query]
+ );
+ $this->assertEquals($query, $result);
+
+ $result = $this->object->requestAction(
+ '/request_action/query_pass?page=3&sort=body'
+ );
+ $expected = ['page' => 3, 'sort' => 'body'];
+ $this->assertEquals($expected, $result);
+ }
+
+/**
+ * Test requestAction with post data.
+ *
+ * @return void
+ */
+ public function testRequestActionPostWithData() {
+ $data = array(
+ 'Post' => array('id' => 2)
+ );
+ $result = $this->object->requestAction(
+ array('controller' => 'request_action', 'action' => 'post_pass'),
+ array('post' => $data)
+ );
+ $this->assertEquals($data, $result);
+
+ $result = $this->object->requestAction(
+ '/request_action/post_pass',
+ array('post' => $data)
+ );
+ $this->assertEquals($data, $result);
+ }
+
+/**
+ * Test that requestAction handles get parameters correctly.
+ *
+ * @return void
+ */
+ public function testRequestActionGetParameters() {
+ $result = $this->object->requestAction(
+ '/request_action/params_pass?get=value&limit=5'
+ );
+ $this->assertEquals('value', $result->query['get']);
+
+ $result = $this->object->requestAction(
+ array('controller' => 'request_action', 'action' => 'params_pass'),
+ array('query' => array('get' => 'value', 'limit' => 5))
+ );
+ $this->assertEquals('value', $result->query['get']);
+ }
+
+}
3  lib/Cake/View/View.php
View
@@ -29,6 +29,7 @@
use Cake\Event\EventManager;
use Cake\Network\Request;
use Cake\Network\Response;
+use Cake\Routing\RequestActionTrait;
use Cake\Routing\Router;
use Cake\Utility\Inflector;
use Cake\Utility\ObjectCollection;
@@ -64,6 +65,8 @@
*/
class View extends Object {
+ use RequestActionTrait;
Mark Story Owner

Added here @jrbasso

Juan Basso Collaborator
jrbasso added a note

:+1:

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
+
/**
* Helpers collection
*
Something went wrong with that request. Please try again.