diff --git a/lib/Cake/Routing/Dispatcher.php b/lib/Cake/Routing/Dispatcher.php index cab5675ac1e..74a065d04b6 100644 --- a/lib/Cake/Routing/Dispatcher.php +++ b/lib/Cake/Routing/Dispatcher.php @@ -27,6 +27,9 @@ App::uses('Scaffold', 'Controller'); App::uses('View', 'View'); App::uses('Debugger', 'Utility'); +App::uses('CakeEvent', 'Event'); +App::uses('CakeEventManager', 'Event'); +App::uses('CakeEventListener', 'Event'); /** * Dispatcher converts Requests into controller actions. It uses the dispatched Request @@ -35,7 +38,9 @@ * * @package Cake.Routing */ -class Dispatcher { +class Dispatcher implements CakeEventListener { + + protected $_eventManager; /** * Constructor. @@ -48,6 +53,25 @@ public function __construct($base = false) { } } + public function getEventManager() { + if (!$this->_eventManager) { + $this->_eventManager = new CakeEventManager(); + $this->_eventManager->attach($this); + } + return $this->_eventManager; + } + + + public function implementedEvents() { + return array( + 'Dispatcher.before' => array( + array('callable' => array($this, 'asset')), + array('callable' => array($this, 'cached')), + array('callable' => array($this, 'parseParams')), + ) + ); + } + /** * Dispatches and invokes given Request, handing over control to the involved controller. If the controller is set * to autoRender, via Controller::$autoRender, then Dispatcher will render the view. @@ -67,12 +91,16 @@ public function __construct($base = false) { * @throws MissingControllerException When the controller is missing. */ public function dispatch(CakeRequest $request, CakeResponse $response, $additionalParams = array()) { - if ($this->asset($request->url, $response) || $this->cached($request->here())) { + $beforeEvent = new CakeEvent('Dispatcher.before', $this, compact('request', 'response', 'additionalParams')); + $this->getEventManager()->dispatch($beforeEvent); + + $request = $beforeEvent->data['request']; + if ($beforeEvent->result instanceof CakeResponse) { + $beforeEvent->result->send(); return; } Router::setRequestInfo($request); - $request = $this->parseParams($request, $additionalParams); $controller = $this->_getController($request, $response); if (!($controller instanceof Controller)) { @@ -82,7 +110,14 @@ public function dispatch(CakeRequest $request, CakeResponse $response, $addition )); } - return $this->_invoke($controller, $request, $response); + $response = $this->_invoke($controller, $request, $response); + if (isset($request->params['return'])) { + return $response->body(); + } + + $afterEvent = new CakeEvent('Dispatcher.after', $this, compact('request', 'response')); + $this->getEventManager()->dispatch($afterEvent); + $afterEvent->data['response']->send(); } /** @@ -93,7 +128,7 @@ public function dispatch(CakeRequest $request, CakeResponse $response, $addition * @param Controller $controller Controller to invoke * @param CakeRequest $request The request object to invoke the controller for. * @param CakeResponse $response The response object to receive the output - * @return void + * @return CakeResponse te resulting response object */ protected function _invoke(Controller $controller, CakeRequest $request, CakeResponse $response) { $controller->constructClasses(); @@ -113,22 +148,18 @@ protected function _invoke(Controller $controller, CakeRequest $request, CakeRes } $controller->shutdownProcess(); - if (isset($request->params['return'])) { - return $response->body(); - } - $response->send(); + return $response; } /** * Applies Routing and additionalParameters to the request to be dispatched. * If Routes have not been loaded they will be loaded, and app/Config/routes.php will be run. * - * @param CakeRequest $request CakeRequest object to mine for parameter information. - * @param array $additionalParams An array of additional parameters to set to the request. - * Useful when Object::requestAction() is involved - * @return CakeRequest The request object with routing params set. + * @param CakeEvent $event containing the request, response and additional params + * @return void */ - public function parseParams(CakeRequest $request, $additionalParams = array()) { + public function parseParams($event) { + $request = $event->data['request']; if (count(Router::$routes) == 0) { $namedExpressions = Router::getNamedExpressions(); extract($namedExpressions); @@ -138,10 +169,9 @@ public function parseParams(CakeRequest $request, $additionalParams = array()) { $params = Router::parse($request->url); $request->addParams($params); - if (!empty($additionalParams)) { - $request->addParams($additionalParams); + if (!empty($event->data['additionalParams'])) { + $request->addParams($event->data['additionalParams']); } - return $request; } /** @@ -200,12 +230,13 @@ protected function _loadRoutes() { } /** - * Outputs cached dispatch view cache + * Checks whether the response was cached and set the body accordingly. * - * @param string $path Requested URL path with any query string parameters - * @return string|boolean False if is not cached or output + * @param CakeEvent $event containing the request and response object + * @return CakeResponse with cached content if found, null otherwise */ - public function cached($path) { + public function cached($event) { + $path = $event->data['request']->here(); if (Configure::read('Cache.check') === true) { if ($path == '/') { $path = 'home'; @@ -220,23 +251,29 @@ public function cached($path) { if (file_exists($filename)) { $controller = null; $view = new View($controller); - return $view->renderCache($filename, microtime(true)); + $result = $view->renderCache($filename, microtime(true)); + if ($result !== false) { + $event->data['response']->body($result); + return $event->data['response']; + } } } - return false; } /** * Checks if a requested asset exists and sends it to the browser * - * @param string $url Requested URL - * @param CakeResponse $response The response object to put the file contents in. - * @return boolean True on success if the asset file was found and sent + * @param CakeEvent $event containing the request and response object + * @return CakeResponse if the client is requesting a recognized asset, null otherwise */ - public function asset($url, CakeResponse $response) { + public function asset($event) { + $url = $event->data['request']->url; + $response = $event->data['response']; + if (strpos($url, '..') !== false || strpos($url, '.') === false) { - return false; + return; } + $filters = Configure::read('Asset.filter'); $isCss = ( strpos($url, 'ccss/') === 0 || @@ -246,17 +283,21 @@ public function asset($url, CakeResponse $response) { strpos($url, 'cjs/') === 0 || preg_match('#^/((theme/[^/]+)/cjs/)|(([^/]+)(?statusCode(404); - $response->send(); - return true; + $event->stopPropagation(); + return $response; } elseif ($isCss) { include WWW_ROOT . DS . $filters['css']; - return true; + $event->stopPropagation(); + return $response; } elseif ($isJs) { include WWW_ROOT . DS . $filters['js']; - return true; + $event->stopPropagation(); + return $response; } + $pathSegments = explode('.', $url); $ext = array_pop($pathSegments); $parts = explode('/', $url); @@ -283,10 +324,10 @@ public function asset($url, CakeResponse $response) { } if ($assetFile !== null) { + $event->stopPropagation(); $this->_deliverAsset($response, $assetFile, $ext); - return true; + return $response; } - return false; } /** diff --git a/lib/Cake/Test/Case/Routing/DispatcherTest.php b/lib/Cake/Test/Case/Routing/DispatcherTest.php index 8b277c70627..7530fed2213 100644 --- a/lib/Cake/Test/Case/Routing/DispatcherTest.php +++ b/lib/Cake/Test/Case/Routing/DispatcherTest.php @@ -44,6 +44,13 @@ protected function _sendHeader($name, $value = null) { */ class TestDispatcher extends Dispatcher { +/** + * Controller instance, made publicly available for testing + * + * @var Controller + **/ + public $controller; + /** * invoke method * @@ -52,8 +59,8 @@ class TestDispatcher extends Dispatcher { * @return void */ protected function _invoke(Controller $controller, CakeRequest $request, CakeResponse $response) { - $result = parent::_invoke($controller, $request, $response); - return $controller; + $this->controller = $controller; + return parent::_invoke($controller, $request, $response); } } @@ -565,14 +572,15 @@ public function tearDown() { */ public function testParseParamsWithoutZerosAndEmptyPost() { $Dispatcher = new Dispatcher(); - - $test = $Dispatcher->parseParams(new CakeRequest("/testcontroller/testaction/params1/params2/params3")); - $this->assertSame($test['controller'], 'testcontroller'); - $this->assertSame($test['action'], 'testaction'); - $this->assertSame($test['pass'][0], 'params1'); - $this->assertSame($test['pass'][1], 'params2'); - $this->assertSame($test['pass'][2], 'params3'); - $this->assertFalse(!empty($test['form'])); + $request = new CakeRequest("/testcontroller/testaction/params1/params2/params3"); + $event = new CakeEvent('DispatcherTest', $Dispatcher, array('request' => $request)); + $Dispatcher->parseParams($event); + $this->assertSame($request['controller'], 'testcontroller'); + $this->assertSame($request['action'], 'testaction'); + $this->assertSame($request['pass'][0], 'params1'); + $this->assertSame($request['pass'][1], 'params2'); + $this->assertSame($request['pass'][2], 'params3'); + $this->assertFalse(!empty($request['form'])); } /** @@ -583,9 +591,11 @@ public function testParseParamsWithoutZerosAndEmptyPost() { public function testParseParamsReturnsPostedData() { $_POST['testdata'] = "My Posted Content"; $Dispatcher = new Dispatcher(); - - $test = $Dispatcher->parseParams(new CakeRequest("/")); - $this->assertEquals("My Posted Content", $test['data']['testdata']); + $request = new CakeRequest("/"); + $event = new CakeEvent('DispatcherTest', $Dispatcher, array('request' => $request)); + $Dispatcher->parseParams($event); + $test = $Dispatcher->parseParams($event); + $this->assertEquals("My Posted Content", $request['data']['testdata']); } /** @@ -595,7 +605,10 @@ public function testParseParamsReturnsPostedData() { */ public function testParseParamsWithSingleZero() { $Dispatcher = new Dispatcher(); - $test = $Dispatcher->parseParams(new CakeRequest("/testcontroller/testaction/1/0/23")); + $test = new CakeRequest("/testcontroller/testaction/1/0/23"); + $event = new CakeEvent('DispatcherTest', $Dispatcher, array('request' => $test)); + $Dispatcher->parseParams($event); + $this->assertSame($test['controller'], 'testcontroller'); $this->assertSame($test['action'], 'testaction'); $this->assertSame($test['pass'][0], '1'); @@ -610,7 +623,10 @@ public function testParseParamsWithSingleZero() { */ public function testParseParamsWithManySingleZeros() { $Dispatcher = new Dispatcher(); - $test = $Dispatcher->parseParams(new CakeRequest("/testcontroller/testaction/0/0/0/0/0/0")); + $test = new CakeRequest("/testcontroller/testaction/0/0/0/0/0/0"); + $event = new CakeEvent('DispatcherTest', $Dispatcher, array('request' => $test)); + $Dispatcher->parseParams($event); + $this->assertRegExp('/\\A(?:0)\\z/', $test['pass'][0]); $this->assertRegExp('/\\A(?:0)\\z/', $test['pass'][1]); $this->assertRegExp('/\\A(?:0)\\z/', $test['pass'][2]); @@ -626,8 +642,10 @@ public function testParseParamsWithManySingleZeros() { */ public function testParseParamsWithManyZerosInEachSectionOfUrl() { $Dispatcher = new Dispatcher(); - $request = new CakeRequest("/testcontroller/testaction/000/0000/00000/000000/000000/0000000"); - $test = $Dispatcher->parseParams($request); + $test = new CakeRequest("/testcontroller/testaction/000/0000/00000/000000/000000/0000000"); + $event = new CakeEvent('DispatcherTest', $Dispatcher, array('request' => $test)); + $Dispatcher->parseParams($event); + $this->assertRegExp('/\\A(?:000)\\z/', $test['pass'][0]); $this->assertRegExp('/\\A(?:0000)\\z/', $test['pass'][1]); $this->assertRegExp('/\\A(?:00000)\\z/', $test['pass'][2]); @@ -643,9 +661,10 @@ public function testParseParamsWithManyZerosInEachSectionOfUrl() { */ public function testParseParamsWithMixedOneToManyZerosInEachSectionOfUrl() { $Dispatcher = new Dispatcher(); + $test = new CakeRequest("/testcontroller/testaction/01/0403/04010/000002/000030/0000400"); + $event = new CakeEvent('DispatcherTest', $Dispatcher, array('request' => $test)); + $Dispatcher->parseParams($event); - $request = new CakeRequest("/testcontroller/testaction/01/0403/04010/000002/000030/0000400"); - $test = $Dispatcher->parseParams($request); $this->assertRegExp('/\\A(?:01)\\z/', $test['pass'][0]); $this->assertRegExp('/\\A(?:0403)\\z/', $test['pass'][1]); $this->assertRegExp('/\\A(?:04010)\\z/', $test['pass'][2]); @@ -667,22 +686,25 @@ public function testQueryStringOnRoot() { $_GET = array('coffee' => 'life', 'sleep' => 'sissies'); $Dispatcher = new Dispatcher(); - $uri = new CakeRequest('posts/home/?coffee=life&sleep=sissies'); - $result = $Dispatcher->parseParams($uri); - $this->assertRegExp('/posts/', $result['controller']); - $this->assertRegExp('/home/', $result['action']); - $this->assertTrue(isset($result['url']['sleep'])); - $this->assertTrue(isset($result['url']['coffee'])); + $request = new CakeRequest('posts/home/?coffee=life&sleep=sissies'); + $event = new CakeEvent('DispatcherTest', $Dispatcher, array('request' => $request)); + $Dispatcher->parseParams($event); + + $this->assertRegExp('/posts/', $request['controller']); + $this->assertRegExp('/home/', $request['action']); + $this->assertTrue(isset($request['url']['sleep'])); + $this->assertTrue(isset($request['url']['coffee'])); $Dispatcher = new Dispatcher(); - $uri = new CakeRequest('/?coffee=life&sleep=sissy'); - - $result = $Dispatcher->parseParams($uri); - $this->assertRegExp('/pages/', $result['controller']); - $this->assertRegExp('/display/', $result['action']); - $this->assertTrue(isset($result['url']['sleep'])); - $this->assertTrue(isset($result['url']['coffee'])); - $this->assertEquals('life', $result['url']['coffee']); + $request = new CakeRequest('/?coffee=life&sleep=sissy'); + + $event = new CakeEvent('DispatcherTest', $Dispatcher, array('request' => $request)); + $Dispatcher->parseParams($event); + $this->assertRegExp('/pages/', $request['controller']); + $this->assertRegExp('/display/', $request['action']); + $this->assertTrue(isset($request['url']['sleep'])); + $this->assertTrue(isset($request['url']['coffee'])); + $this->assertEquals('life', $request['url']['coffee']); } /** @@ -700,7 +722,7 @@ public function testMissingController() { $url = new CakeRequest('some_controller/home/param:value/param2:value2'); $response = $this->getMock('CakeResponse'); - $controller = $Dispatcher->dispatch($url, $response, array('return' => 1)); + $Dispatcher->dispatch($url, $response, array('return' => 1)); } /** @@ -718,7 +740,7 @@ public function testMissingControllerInterface() { $url = new CakeRequest('dispatcher_test_interface/index'); $response = $this->getMock('CakeResponse'); - $controller = $Dispatcher->dispatch($url, $response, array('return' => 1)); + $Dispatcher->dispatch($url, $response, array('return' => 1)); } /** @@ -736,7 +758,7 @@ public function testMissingControllerAbstract() { $url = new CakeRequest('dispatcher_test_abstract/index'); $response = $this->getMock('CakeResponse'); - $controller = $Dispatcher->dispatch($url, $response, array('return' => 1)); + $Dispatcher->dispatch($url, $response, array('return' => 1)); } /** @@ -753,27 +775,27 @@ public function testDispatchBasic() { $url = new CakeRequest('pages/home/param:value/param2:value2'); $response = $this->getMock('CakeResponse'); - $controller = $Dispatcher->dispatch($url, $response, array('return' => 1)); + $Dispatcher->dispatch($url, $response, array('return' => 1)); $expected = 'Pages'; - $this->assertEquals($expected, $controller->name); + $this->assertEquals($expected, $Dispatcher->controller->name); $expected = array('0' => 'home', 'param' => 'value', 'param2' => 'value2'); - $this->assertSame($expected, $controller->passedArgs); + $this->assertSame($expected, $Dispatcher->controller->passedArgs); - Configure::write('App.baseUrl','/pages/index.php'); + Configure::write('App.baseUrl', '/pages/index.php'); $url = new CakeRequest('pages/home'); - $controller = $Dispatcher->dispatch($url, $response, array('return' => 1)); + $Dispatcher->dispatch($url, $response, array('return' => 1)); $expected = 'Pages'; - $this->assertEquals($expected, $controller->name); + $this->assertEquals($expected, $Dispatcher->controller->name); $url = new CakeRequest('pages/home/'); - $controller = $Dispatcher->dispatch($url, $response, array('return' => 1)); - $this->assertNull($controller->plugin); + $Dispatcher->dispatch($url, $response, array('return' => 1)); + $this->assertNull($Dispatcher->controller->plugin); $expected = 'Pages'; - $this->assertEquals($expected, $controller->name); + $this->assertEquals($expected, $Dispatcher->controller->name); unset($Dispatcher); @@ -782,24 +804,24 @@ public function testDispatchBasic() { Configure::write('App.baseUrl', '/timesheets/index.php'); $url = new CakeRequest('timesheets'); - $controller = $Dispatcher->dispatch($url, $response, array('return' => 1)); + $Dispatcher->dispatch($url, $response, array('return' => 1)); $expected = 'Timesheets'; - $this->assertEquals($expected, $controller->name); + $this->assertEquals($expected, $Dispatcher->controller->name); $url = new CakeRequest('timesheets/'); - $controller = $Dispatcher->dispatch($url, $response, array('return' => 1)); + $Dispatcher->dispatch($url, $response, array('return' => 1)); - $this->assertEquals('Timesheets', $controller->name); + $this->assertEquals('Timesheets', $Dispatcher->controller->name); $this->assertEquals('/timesheets/index.php', $url->base); $url = new CakeRequest('test_dispatch_pages/camelCased'); - $controller = $Dispatcher->dispatch($url, $response, array('return' => 1)); - $this->assertEquals('TestDispatchPages', $controller->name); + $Dispatcher->dispatch($url, $response, array('return' => 1)); + $this->assertEquals('TestDispatchPages', $Dispatcher->controller->name); $url = new CakeRequest('test_dispatch_pages/camelCased/something. .'); - $controller = $Dispatcher->dispatch($url, $response, array('return' => 1)); - $this->assertEquals('something. .', $controller->params['pass'][0], 'Period was chopped off. %s'); + $Dispatcher->dispatch($url, $response, array('return' => 1)); + $this->assertEquals('something. .', $Dispatcher->controller->params['pass'][0], 'Period was chopped off. %s'); } /** @@ -834,18 +856,18 @@ public function testAdminDispatch() { $response = $this->getMock('CakeResponse'); Router::reload(); - $controller = $Dispatcher->dispatch($url, $response, array('return' => 1)); + $Dispatcher->dispatch($url, $response, array('return' => 1)); - $this->assertEquals('TestDispatchPages', $controller->name); + $this->assertEquals('TestDispatchPages', $Dispatcher->controller->name); - $this->assertSame($controller->passedArgs, array('param' => 'value', 'param2' => 'value2')); - $this->assertTrue($controller->params['admin']); + $this->assertSame($Dispatcher->controller->passedArgs, array('param' => 'value', 'param2' => 'value2')); + $this->assertTrue($Dispatcher->controller->params['admin']); $expected = '/cake/repo/branches/1.2.x.x/index.php/admin/test_dispatch_pages/index/param:value/param2:value2'; - $this->assertSame($expected, $controller->here); + $this->assertSame($expected, $Dispatcher->controller->here); $expected = '/cake/repo/branches/1.2.x.x/index.php'; - $this->assertSame($expected, $controller->base); + $this->assertSame($expected, $Dispatcher->controller->base); } /** @@ -865,22 +887,23 @@ public function testPluginDispatch() { $url = new CakeRequest('my_plugin/some_pages/home/param:value/param2:value2'); $response = $this->getMock('CakeResponse'); - $controller = $Dispatcher->dispatch($url, $response, array('return' => 1)); + $Dispatcher->dispatch($url, $response, array('return' => 1)); - $result = $Dispatcher->parseParams($url); + $event = new CakeEvent('DispatcherTest', $Dispatcher, array('request' => $url)); + $Dispatcher->parseParams($event); $expected = array( 'pass' => array('home'), 'named' => array('param' => 'value', 'param2' => 'value2'), 'plugin' => 'my_plugin', 'controller' => 'some_pages', 'action' => 'display' ); foreach ($expected as $key => $value) { - $this->assertEquals($value, $result[$key], 'Value mismatch ' . $key . ' %'); + $this->assertEquals($value, $url[$key], 'Value mismatch ' . $key . ' %'); } - $this->assertSame($controller->plugin, 'MyPlugin'); - $this->assertSame($controller->name, 'SomePages'); - $this->assertSame($controller->params['controller'], 'some_pages'); - $this->assertSame($controller->passedArgs, array('0' => 'home', 'param' => 'value', 'param2' => 'value2')); + $this->assertSame($Dispatcher->controller->plugin, 'MyPlugin'); + $this->assertSame($Dispatcher->controller->name, 'SomePages'); + $this->assertSame($Dispatcher->controller->params['controller'], 'some_pages'); + $this->assertSame($Dispatcher->controller->passedArgs, array('0' => 'home', 'param' => 'value', 'param2' => 'value2')); } /** @@ -903,12 +926,12 @@ public function testAutomaticPluginDispatch() { $url = new CakeRequest('my_plugin/other_pages/index/param:value/param2:value2'); $response = $this->getMock('CakeResponse'); - $controller = $Dispatcher->dispatch($url, $response, array('return' => 1)); + $Dispatcher->dispatch($url, $response, array('return' => 1)); - $this->assertSame($controller->plugin, 'MyPlugin'); - $this->assertSame($controller->name, 'OtherPages'); - $this->assertSame($controller->action, 'index'); - $this->assertSame($controller->passedArgs, array('param' => 'value', 'param2' => 'value2')); + $this->assertSame($Dispatcher->controller->plugin, 'MyPlugin'); + $this->assertSame($Dispatcher->controller->name, 'OtherPages'); + $this->assertSame($Dispatcher->controller->action, 'index'); + $this->assertSame($Dispatcher->controller->passedArgs, array('param' => 'value', 'param2' => 'value2')); $expected = '/cake/repo/branches/1.2.x.x/my_plugin/other_pages/index/param:value/param2:value2'; $this->assertSame($expected, $url->here); @@ -936,12 +959,12 @@ public function testAutomaticPluginControllerDispatch() { $url = new CakeRequest('my_plugin/my_plugin/add/param:value/param2:value2'); $response = $this->getMock('CakeResponse'); - $controller = $Dispatcher->dispatch($url, $response, array('return' => 1)); + $Dispatcher->dispatch($url, $response, array('return' => 1)); - $this->assertSame($controller->plugin, 'MyPlugin'); - $this->assertSame($controller->name, 'MyPlugin'); - $this->assertSame($controller->action, 'add'); - $this->assertEquals(array('param' => 'value', 'param2' => 'value2'), $controller->params['named']); + $this->assertSame($Dispatcher->controller->plugin, 'MyPlugin'); + $this->assertSame($Dispatcher->controller->name, 'MyPlugin'); + $this->assertSame($Dispatcher->controller->action, 'add'); + $this->assertEquals(array('param' => 'value', 'param2' => 'value2'), $Dispatcher->controller->params['named']); Router::reload(); require CAKE . 'Config' . DS . 'routes.php'; @@ -955,13 +978,13 @@ public function testAutomaticPluginControllerDispatch() { $pluginUrl = Inflector::underscore($plugin); $url = new CakeRequest($pluginUrl); - $controller = $Dispatcher->dispatch($url, $response, array('return' => 1)); - $this->assertSame($controller->plugin, 'MyPlugin'); - $this->assertSame($controller->name, 'MyPlugin'); - $this->assertSame($controller->action, 'index'); + $Dispatcher->dispatch($url, $response, array('return' => 1)); + $this->assertSame($Dispatcher->controller->plugin, 'MyPlugin'); + $this->assertSame($Dispatcher->controller->name, 'MyPlugin'); + $this->assertSame($Dispatcher->controller->action, 'index'); $expected = $pluginUrl; - $this->assertEquals($expected, $controller->params['controller']); + $this->assertEquals($expected, $Dispatcher->controller->params['controller']); Configure::write('Routing.prefixes', array('admin')); @@ -972,19 +995,19 @@ public function testAutomaticPluginControllerDispatch() { $url = new CakeRequest('admin/my_plugin/my_plugin/add/5/param:value/param2:value2'); $response = $this->getMock('CakeResponse'); - $controller = $Dispatcher->dispatch($url, $response, array('return' => 1)); + $Dispatcher->dispatch($url, $response, array('return' => 1)); - $this->assertEquals('my_plugin', $controller->params['plugin']); - $this->assertEquals('my_plugin', $controller->params['controller']); - $this->assertEquals('admin_add', $controller->params['action']); - $this->assertEquals(array(5), $controller->params['pass']); - $this->assertEquals(array('param' => 'value', 'param2' => 'value2'), $controller->params['named']); - $this->assertSame($controller->plugin, 'MyPlugin'); - $this->assertSame($controller->name, 'MyPlugin'); - $this->assertSame($controller->action, 'admin_add'); + $this->assertEquals('my_plugin', $Dispatcher->controller->params['plugin']); + $this->assertEquals('my_plugin', $Dispatcher->controller->params['controller']); + $this->assertEquals('admin_add', $Dispatcher->controller->params['action']); + $this->assertEquals(array(5), $Dispatcher->controller->params['pass']); + $this->assertEquals(array('param' => 'value', 'param2' => 'value2'), $Dispatcher->controller->params['named']); + $this->assertSame($Dispatcher->controller->plugin, 'MyPlugin'); + $this->assertSame($Dispatcher->controller->name, 'MyPlugin'); + $this->assertSame($Dispatcher->controller->action, 'admin_add'); $expected = array(0 => 5, 'param' => 'value', 'param2' => 'value2'); - $this->assertEquals($expected, $controller->passedArgs); + $this->assertEquals($expected, $Dispatcher->controller->passedArgs); Configure::write('Routing.prefixes', array('admin')); CakePlugin::load('ArticlesTest', array('path' => '/fake/path')); @@ -993,10 +1016,10 @@ public function testAutomaticPluginControllerDispatch() { $Dispatcher = new TestDispatcher(); - $controller = $Dispatcher->dispatch(new CakeRequest('admin/articles_test'), $response, array('return' => 1)); - $this->assertSame($controller->plugin, 'ArticlesTest'); - $this->assertSame($controller->name, 'ArticlesTest'); - $this->assertSame($controller->action, 'admin_index'); + $Dispatcher->dispatch(new CakeRequest('admin/articles_test'), $response, array('return' => 1)); + $this->assertSame($Dispatcher->controller->plugin, 'ArticlesTest'); + $this->assertSame($Dispatcher->controller->name, 'ArticlesTest'); + $this->assertSame($Dispatcher->controller->action, 'admin_index'); $expected = array( 'pass' => array(), @@ -1009,7 +1032,7 @@ public function testAutomaticPluginControllerDispatch() { 'return' => 1 ); foreach ($expected as $key => $value) { - $this->assertEquals($expected[$key], $controller->request[$key], 'Value mismatch ' . $key); + $this->assertEquals($expected[$key], $Dispatcher->controller->request[$key], 'Value mismatch ' . $key); } } @@ -1029,11 +1052,11 @@ public function testAutomaticPluginDispatchWithShortAccess() { $url = new CakeRequest('my_plugin/'); $response = $this->getMock('CakeResponse'); - $controller = $Dispatcher->dispatch($url, $response, array('return' => 1)); - $this->assertEquals('my_plugin', $controller->params['controller']); - $this->assertEquals('my_plugin', $controller->params['plugin']); - $this->assertEquals('index', $controller->params['action']); - $this->assertFalse(isset($controller->params['pass'][0])); + $Dispatcher->dispatch($url, $response, array('return' => 1)); + $this->assertEquals('my_plugin', $Dispatcher->controller->params['controller']); + $this->assertEquals('my_plugin', $Dispatcher->controller->params['plugin']); + $this->assertEquals('index', $Dispatcher->controller->params['action']); + $this->assertFalse(isset($Dispatcher->controller->params['pass'][0])); } /** @@ -1058,25 +1081,25 @@ public function testPluginShortCutUrlsWithControllerThatNeedsToBeLoaded() { $url = new CakeRequest('test_plugin/'); $response = $this->getMock('CakeResponse'); - $controller = $Dispatcher->dispatch($url, $response, array('return' => 1)); - $this->assertEquals('test_plugin', $controller->params['controller']); - $this->assertEquals('test_plugin', $controller->params['plugin']); - $this->assertEquals('index', $controller->params['action']); - $this->assertFalse(isset($controller->params['pass'][0])); + $Dispatcher->dispatch($url, $response, array('return' => 1)); + $this->assertEquals('test_plugin', $Dispatcher->controller->params['controller']); + $this->assertEquals('test_plugin', $Dispatcher->controller->params['plugin']); + $this->assertEquals('index', $Dispatcher->controller->params['action']); + $this->assertFalse(isset($Dispatcher->controller->params['pass'][0])); $url = new CakeRequest('/test_plugin/tests/index'); - $controller = $Dispatcher->dispatch($url, $response, array('return' => 1)); - $this->assertEquals('tests', $controller->params['controller']); - $this->assertEquals('test_plugin', $controller->params['plugin']); - $this->assertEquals('index', $controller->params['action']); - $this->assertFalse(isset($controller->params['pass'][0])); + $Dispatcher->dispatch($url, $response, array('return' => 1)); + $this->assertEquals('tests', $Dispatcher->controller->params['controller']); + $this->assertEquals('test_plugin', $Dispatcher->controller->params['plugin']); + $this->assertEquals('index', $Dispatcher->controller->params['action']); + $this->assertFalse(isset($Dispatcher->controller->params['pass'][0])); $url = new CakeRequest('/test_plugin/tests/index/some_param'); - $controller = $Dispatcher->dispatch($url, $response, array('return' => 1)); - $this->assertEquals('tests', $controller->params['controller']); - $this->assertEquals('test_plugin', $controller->params['plugin']); - $this->assertEquals('index', $controller->params['action']); - $this->assertEquals('some_param', $controller->params['pass'][0]); + $Dispatcher->dispatch($url, $response, array('return' => 1)); + $this->assertEquals('tests', $Dispatcher->controller->params['controller']); + $this->assertEquals('test_plugin', $Dispatcher->controller->params['plugin']); + $this->assertEquals('index', $Dispatcher->controller->params['action']); + $this->assertEquals('some_param', $Dispatcher->controller->params['pass'][0]); App::build(); } @@ -1095,7 +1118,7 @@ public function testAutomaticPluginControllerMissingActionDispatch() { $url = new CakeRequest('my_plugin/not_here/param:value/param2:value2'); $response = $this->getMock('CakeResponse'); - $controller = $Dispatcher->dispatch($url, $response, array('return' => 1)); + $Dispatcher->dispatch($url, $response, array('return' => 1)); } /** @@ -1113,7 +1136,7 @@ public function testAutomaticPluginControllerIndexMissingAction() { $url = new CakeRequest('my_plugin/param:value/param2:value2'); $response = $this->getMock('CakeResponse'); - $controller = $Dispatcher->dispatch($url, $response, array('return' => 1)); + $Dispatcher->dispatch($url, $response, array('return' => 1)); } /** @@ -1132,14 +1155,14 @@ public function testTestPluginDispatch() { $url = new CakeRequest('/test_plugin/tests/index'); $response = $this->getMock('CakeResponse'); - $result = $Dispatcher->dispatch($url, $response, array('return' => 1)); + $Dispatcher->dispatch($url, $response, array('return' => 1)); $this->assertTrue(class_exists('TestsController')); $this->assertTrue(class_exists('TestPluginAppController')); $this->assertTrue(class_exists('PluginsComponent')); - $this->assertEquals('tests', $result->params['controller']); - $this->assertEquals('test_plugin', $result->params['plugin']); - $this->assertEquals('index', $result->params['action']); + $this->assertEquals('tests', $Dispatcher->controller->params['controller']); + $this->assertEquals('test_plugin', $Dispatcher->controller->params['plugin']); + $this->assertEquals('index', $Dispatcher->controller->params['action']); App::build(); } @@ -1155,23 +1178,23 @@ public function testChangingParamsFromBeforeFilter() { $url = new CakeRequest('some_posts/index/param:value/param2:value2'); try { - $controller = $Dispatcher->dispatch($url, $response, array('return' => 1)); + $Dispatcher->dispatch($url, $response, array('return' => 1)); $this->fail('No exception.'); } catch (MissingActionException $e) { $this->assertEquals('Action SomePostsController::view() could not be found.', $e->getMessage()); } $url = new CakeRequest('some_posts/something_else/param:value/param2:value2'); - $controller = $Dispatcher->dispatch($url, $response, array('return' => 1)); + $Dispatcher->dispatch($url, $response, array('return' => 1)); $expected = 'SomePosts'; - $this->assertEquals($expected, $controller->name); + $this->assertEquals($expected, $Dispatcher->controller->name); $expected = 'change'; - $this->assertEquals($expected, $controller->action); + $this->assertEquals($expected, $Dispatcher->controller->action); $expected = array('changed'); - $this->assertSame($expected, $controller->params['pass']); + $this->assertSame($expected, $Dispatcher->controller->params['pass']); } /** @@ -1328,16 +1351,18 @@ public function testAsset($url, $file) { * @return void */ public function testMissingAssetProcessor404() { - $response = $this->getMock('CakeResponse', array('_sendHeader')); + $response = $this->getMock('CakeResponse', array('send')); $Dispatcher = new TestDispatcher(); Configure::write('Asset.filter', array( 'js' => '', 'css' => null )); - ob_start(); - $this->assertTrue($Dispatcher->asset('ccss/cake.generic.css', $response)); - $result = ob_get_clean(); + $request = new CakeRequest('ccss/cake.generic.css'); + $event = new CakeEvent('DispatcherTest', $Dispatcher, compact('request', 'response')); + $this->assertSame($response, $Dispatcher->asset($event)); + $this->assertEquals('404', $response->statusCode()); + $this->assertTrue($event->isStopped()); } /** @@ -1352,17 +1377,36 @@ public function testAssetFilterForThemeAndPlugins() { 'js' => '', 'css' => '' )); - $this->assertTrue($Dispatcher->asset('theme/test_theme/ccss/cake.generic.css', $response)); - - $this->assertTrue($Dispatcher->asset('theme/test_theme/cjs/debug_kit.js', $response)); - $this->assertTrue($Dispatcher->asset('test_plugin/ccss/cake.generic.css', $response)); - - $this->assertTrue($Dispatcher->asset('test_plugin/cjs/debug_kit.js', $response)); - - $this->assertFalse($Dispatcher->asset('css/ccss/debug_kit.css', $response)); - - $this->assertFalse($Dispatcher->asset('js/cjs/debug_kit.js', $response)); + $request = new CakeRequest('theme/test_theme/ccss/cake.generic.css'); + $event = new CakeEvent('DispatcherTest', $Dispatcher, compact('request', 'response')); + $this->assertSame($response, $Dispatcher->asset($event)); + $this->assertTrue($event->isStopped()); + + $request = new CakeRequest('theme/test_theme/cjs/debug_kit.js'); + $event = new CakeEvent('DispatcherTest', $Dispatcher, compact('request', 'response')); + $this->assertSame($response, $Dispatcher->asset($event)); + $this->assertTrue($event->isStopped()); + + $request = new CakeRequest('test_plugin/ccss/cake.generic.css'); + $event = new CakeEvent('DispatcherTest', $Dispatcher, compact('request', 'response')); + $this->assertSame($response, $Dispatcher->asset($event)); + $this->assertTrue($event->isStopped()); + + $request = new CakeRequest('test_plugin/cjs/debug_kit.js'); + $event = new CakeEvent('DispatcherTest', $Dispatcher, compact('request', 'response')); + $this->assertSame($response, $Dispatcher->asset($event)); + $this->assertTrue($event->isStopped()); + + $request = new CakeRequest('css/ccss/debug_kit.css'); + $event = new CakeEvent('DispatcherTest', $Dispatcher, compact('request', 'response')); + $this->assertNull($Dispatcher->asset($event)); + $this->assertFalse($event->isStopped()); + + $request = new CakeRequest('js/cjs/debug_kit.js'); + $event = new CakeEvent('DispatcherTest', $Dispatcher, compact('request', 'response')); + $this->assertNull($Dispatcher->asset($event)); + $this->assertFalse($event->isStopped()); } /** @@ -1411,15 +1455,14 @@ public function testFullPageCachingDispatch($url) { $dispatcher = new TestDispatcher(); $request = new CakeRequest($url); - $response = new CakeResponse(); + $response = $this->getMock('CakeResponse', array('send')); - ob_start(); $dispatcher->dispatch($request, $response); - $out = ob_get_clean(); + $out = $response->body(); - ob_start(); - $dispatcher->cached($request->here()); - $cached = ob_get_clean(); + $event = new CakeEvent('DispatcherTest', $dispatcher, array('request' => $request, 'response' => $response)); + $response = $dispatcher->cached($event); + $cached = $response->body(); $cached = preg_replace('//', '', $cached); @@ -1441,16 +1484,20 @@ public function testHttpMethodOverrides() { $_SERVER['REQUEST_METHOD'] = 'POST'; $dispatcher = new Dispatcher(); - $result = $dispatcher->parseParams(new CakeRequest('/posts')); + $request = new CakeRequest('/posts'); + $event = new CakeEvent('DispatcherTest', $dispatcher, array('request' => $request)); + $dispatcher->parseParams($event); $expected = array('pass' => array(), 'named' => array(), 'plugin' => null, 'controller' => 'posts', 'action' => 'add', '[method]' => 'POST'); foreach ($expected as $key => $value) { - $this->assertEquals($value, $result[$key], 'Value mismatch for ' . $key . ' %s'); + $this->assertEquals($value, $request[$key], 'Value mismatch for ' . $key . ' %s'); } $_SERVER['REQUEST_METHOD'] = 'GET'; $_SERVER['HTTP_X_HTTP_METHOD_OVERRIDE'] = 'PUT'; - $result = $dispatcher->parseParams(new CakeRequest('/posts/5')); + $request = new CakeRequest('/posts/5'); + $event = new CakeEvent('DispatcherTest', $dispatcher, array('request' => $request)); + $dispatcher->parseParams($event); $expected = array( 'pass' => array('5'), 'named' => array(), @@ -1461,24 +1508,28 @@ public function testHttpMethodOverrides() { '[method]' => 'PUT' ); foreach ($expected as $key => $value) { - $this->assertEquals($value, $result[$key], 'Value mismatch for ' . $key . ' %s'); + $this->assertEquals($value, $request[$key], 'Value mismatch for ' . $key . ' %s'); } unset($_SERVER['HTTP_X_HTTP_METHOD_OVERRIDE']); $_SERVER['REQUEST_METHOD'] = 'GET'; - $result = $dispatcher->parseParams(new CakeRequest('/posts/5')); + $request = new CakeRequest('/posts/5'); + $event = new CakeEvent('DispatcherTest', $dispatcher, array('request' => $request)); + $dispatcher->parseParams($event); $expected = array('pass' => array('5'), 'named' => array(), 'id' => '5', 'plugin' => null, 'controller' => 'posts', 'action' => 'view', '[method]' => 'GET'); foreach ($expected as $key => $value) { - $this->assertEquals($value, $result[$key], 'Value mismatch for ' . $key . ' %s'); + $this->assertEquals($value, $request[$key], 'Value mismatch for ' . $key . ' %s'); } $_POST['_method'] = 'PUT'; - $result = $dispatcher->parseParams(new CakeRequest('/posts/5')); + $request = new CakeRequest('/posts/5'); + $event = new CakeEvent('DispatcherTest', $dispatcher, array('request' => $request)); + $dispatcher->parseParams($event); $expected = array('pass' => array('5'), 'named' => array(), 'id' => '5', 'plugin' => null, 'controller' => 'posts', 'action' => 'edit', '[method]' => 'PUT'); foreach ($expected as $key => $value) { - $this->assertEquals($value, $result[$key], 'Value mismatch for ' . $key . ' %s'); + $this->assertEquals($value, $request[$key], 'Value mismatch for ' . $key . ' %s'); } $_POST['_method'] = 'POST'; @@ -1486,13 +1537,15 @@ public function testHttpMethodOverrides() { $_POST['extra'] = 'data'; $_SERVER = array(); - $result = $dispatcher->parseParams(new CakeRequest('/posts')); + $request = new CakeRequest('/posts'); + $event = new CakeEvent('DispatcherTest', $dispatcher, array('request' => $request)); + $dispatcher->parseParams($event); $expected = array( 'pass' => array(), 'named' => array(), 'plugin' => null, 'controller' => 'posts', 'action' => 'add', '[method]' => 'POST', 'data' => array('extra' => 'data', 'Post' => array('title' => 'New Post')), ); foreach ($expected as $key => $value) { - $this->assertEquals($value, $result[$key], 'Value mismatch for ' . $key . ' %s'); + $this->assertEquals($value, $request[$key], 'Value mismatch for ' . $key . ' %s'); } unset($_POST['_method']); diff --git a/lib/Cake/View/View.php b/lib/Cake/View/View.php index 9e0269ae8ae..8a4d129d09e 100644 --- a/lib/Cake/View/View.php +++ b/lib/Cake/View/View.php @@ -557,8 +557,7 @@ public function renderCache($filename, $timeStart) { header('Content-type: text/xml'); } $commentLength = strlen(''); - echo substr($out, $commentLength); - return true; + return substr($out, $commentLength); } } }