Browse Source

Re-adding session capability to RequestAction

Jose Lorenzo Rodriguez 12 years ago
parent
commit
497e3673ce

+ 17 - 0
src/Routing/RequestActionTrait.php

@@ -16,6 +16,7 @@ namespace Cake\Routing;
 use Cake\Core\Configure;
 use Cake\Network\Request;
 use Cake\Network\Response;
+use Cake\Network\Session;
 use Cake\Routing\DispatcherFactory;
 use Cake\Routing\Router;
 
@@ -66,6 +67,17 @@ trait RequestActionTrait {
  * ]);
  * }}}
  *
+ * ### Trasmitting the session
+ *
+ * By default action dispatched by this method will use a vanialla session object. If you want the
+ * a particular session instance to be used, you need to specify it.
+ *
+ * {{{
+ * $vars = $this->requestAction('/articles/popular', [
+ *   'session' => new Session($someSessionConfig)
+ * ]);
+ * }}}
+ *
  * @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
@@ -113,12 +125,17 @@ trait RequestActionTrait {
 				$params['params']['pass'] = [];
 			}
 		}
+
 		if (!empty($post)) {
 			$params['post'] = $post;
 		}
+
 		if (!empty($query)) {
 			$params['query'] = $query;
 		}
+
+		$params['session'] = isset($extra['session']) ? $extra['session'] : new Session();
+
 		$request = new Request($params);
 		$request->addParams($extra);
 		$dispatcher = DispatcherFactory::create();

+ 21 - 0
tests/TestCase/Routing/RequestActionTraitTest.php

@@ -306,4 +306,25 @@ class RequestActionTraitTest extends TestCase {
 		$this->assertEquals('value', $result['query']['get']);
 	}
 
+/**
+ * Tests that it is possible to transmit the session for the request
+ *
+ * @return void
+ */
+	public function testRequestActionSession() {
+		$result = $this->object->requestAction('/request_action/session_test');
+		$this->assertNull($result);
+
+		$session = $this->getMock('Cake\Network\Session');
+		$session->expects($this->once())
+			->method('read')
+			->with('foo')
+			->will($this->returnValue('bar'));
+		$result = $this->object->requestAction(
+			'/request_action/session_test',
+			['session' => $session]
+		);
+		$this->assertEquals('bar', $result);
+	}
+
 }

+ 10 - 0
tests/test_app/TestApp/Controller/RequestActionController.php

@@ -122,4 +122,14 @@ class RequestActionController extends AppController {
 		$this->response->body($content);
 	}
 
+/**
+ * Tests session transmission
+ *
+ * @return void
+ */
+	public function session_test() {
+		$this->response->body($this->request->session()->read('foo'));
+		return $this->response;
+	}
+
 }