From 2eec6dc943b281bc063746efff617ccdda2be550 Mon Sep 17 00:00:00 2001 From: gwoo Date: Sat, 20 Oct 2007 23:59:46 +0000 Subject: [PATCH] allowing beforeFilter or Component::startup() to change params. closes #1643. Removed Controller::$namedArgs, passedArgs provides the same functionality, updated dispatcher test. git-svn-id: https://svn.cakephp.org/repo/branches/1.2.x.x@5824 3807eeeb-6ff5-0310-8944-8be069107fe0 --- cake/dispatcher.php | 19 ++++---- cake/tests/cases/dispatcher.test.php | 68 ++++++++++++++++++++++------ 2 files changed, 63 insertions(+), 24 deletions(-) diff --git a/cake/dispatcher.php b/cake/dispatcher.php index 520383928..075f9539d 100644 --- a/cake/dispatcher.php +++ b/cake/dispatcher.php @@ -184,16 +184,14 @@ class Dispatcher extends Object { $controller->base = $this->base; $controller->here = $this->here; $controller->webroot = $this->webroot; - $controller->params = $this->params; $controller->plugin = $this->plugin; - $controller->action = $this->params['action']; - $controller->webservices = $this->params['webservices']; + $controller->params =& $this->params; + $controller->action =& $this->params['action']; + $controller->webservices =& $this->params['webservices']; + $controller->passedArgs =& $this->params['pass']; - $controller->passedArgs = $this->params['pass']; - $controller->namedArgs = Set::diff(Set::extract($this->params['pass'], '{n}'), $this->params['pass']); - - if (!empty($controller->params['data'])) { - $controller->data =& $controller->params['data']; + if (!empty($this->params['data'])) { + $controller->data =& $this->params['data']; } else { $controller->data = null; } @@ -235,8 +233,9 @@ class Dispatcher extends Object { $controller->constructClasses(); + $this->start($controller); + if ($privateAction) { - $this->start($controller); return $this->cakeError('privateAction', array( array( 'className' => Inflector::camelize($this->params['controller']."Controller"), @@ -261,9 +260,7 @@ class Dispatcher extends Object { * @access protected */ function _invoke (&$controller, $params, $missingAction = false) { - $this->start($controller); $classVars = get_object_vars($controller); - if ($missingAction && in_array('scaffold', array_keys($classVars))) { uses('controller'. DS . 'scaffold'); return new Scaffold($controller, $params); diff --git a/cake/tests/cases/dispatcher.test.php b/cake/tests/cases/dispatcher.test.php index 44b23d8dd..fb691abb3 100644 --- a/cake/tests/cases/dispatcher.test.php +++ b/cake/tests/cases/dispatcher.test.php @@ -32,11 +32,10 @@ require_once CAKE.'app_controller.php'; class TestDispatcher extends Dispatcher { function _invoke(&$controller, $params, $missingAction) { - $this->start($controller); + $controller->params =& $params; $classVars = get_object_vars($controller); if ($missingAction && in_array('scaffold', array_keys($classVars))) { uses('controller'. DS . 'scaffold'); - return new Scaffold($controller, $params); } elseif ($missingAction && !in_array('scaffold', array_keys($classVars))) { return $this->cakeError('missingAction', array( @@ -52,10 +51,6 @@ class TestDispatcher extends Dispatcher { return $controller; } - function start(&$controller) { - return; - } - function cakeError($filename) { return $filename; } @@ -131,6 +126,22 @@ class ArticlesTestController extends ArticlesTestAppController { return true; } } + +class SomePostsController extends AppController { + + var $name = 'SomePosts'; + var $uses = array(); + var $autoRender = false; + + function beforeFilter() { + $this->params['action'] = 'view'; + $this->params['pass'] = array('changed'); + } + + function index() { + return true; + } +} /** * Short description for class. * @@ -370,6 +381,15 @@ class DispatcherTest extends UnitTestCase { $expectedWebroot = '/CakeBB/app/webroot/'; $this->assertEqual($expectedWebroot, $dispatcher->webroot); + Configure::write('App.baseUrl', '/dbhauser/index.php'); + $_SERVER['DOCUMENT_ROOT'] = '/kunden/homepages/4/d181710652/htdocs/joomla'; + $_SERVER['SCRIPT_FILENAME'] = '/kunden/homepages/4/d181710652/htdocs/joomla/dbhauser/index.php'; + $result = $dispatcher->baseUrl(); + $expected = '/dbhauser/index.php'; + $this->assertEqual($expected, $result); + $expectedWebroot = '/dbhauser/app/webroot/'; + $this->assertEqual($expectedWebroot, $dispatcher->webroot); + } function testBaseUrlAndWebrootWithBase() { @@ -447,8 +467,8 @@ class DispatcherTest extends UnitTestCase { $expected = 'Pages'; $this->assertEqual($expected, $controller->name); - $expected = array('param' => 'value', 'param2' => 'value2'); - $this->assertIdentical($expected, $controller->namedArgs); + $expected = array('0' => 'home', 'param' => 'value', 'param2' => 'value2'); + $this->assertIdentical($expected, $controller->passedArgs); } function testAdminDispatch() { @@ -469,7 +489,7 @@ class DispatcherTest extends UnitTestCase { $this->assertEqual($expected, $controller->name); $expected = array('param' => 'value', 'param2' => 'value2'); - $this->assertIdentical($expected, $controller->namedArgs); + $this->assertIdentical($expected, $controller->passedArgs); $this->assertTrue($controller->params['admin']); $expected = '/cake/repo/branches/1.2.x.x/index.php/admin/test_dispatch_pages/index/param:value/param2:value2'; @@ -513,8 +533,8 @@ class DispatcherTest extends UnitTestCase { $expected = 'SomePages'; $this->assertIdentical($expected, $controller->name); - $expected = array('param'=>'value', 'param2'=>'value2'); - $this->assertIdentical($expected, $controller->namedArgs); + $expected = array('0' => 'home', 'param'=>'value', 'param2'=>'value2'); + $this->assertIdentical($expected, $controller->passedArgs); $expected = '/cake/repo/branches/1.2.x.x/my_plugin/some_pages/home/param:value/param2:value2'; $this->assertIdentical($expected, $controller->here); @@ -547,7 +567,7 @@ class DispatcherTest extends UnitTestCase { $this->assertIdentical($expected, $controller->action); $expected = array('param'=>'value', 'param2'=>'value2'); - $this->assertIdentical($expected, $controller->namedArgs); + $this->assertIdentical($expected, $controller->passedArgs); $expected = '/cake/repo/branches/1.2.x.x/my_plugin/other_pages/index/param:value/param2:value2'; $this->assertIdentical($expected, $controller->here); @@ -582,6 +602,8 @@ class DispatcherTest extends UnitTestCase { $this->assertEqual($controller->params['pass'], $expected); Router::reload(); + Router::connect('/admin/:controller/:action/*', array('controller' => 'pages', 'action' => 'index', 'admin' => true, 'prefix' => 'admin')); + $dispatcher =& new TestDispatcher(); $dispatcher->base = false; $url = 'admin/articles_test'; @@ -602,7 +624,7 @@ class DispatcherTest extends UnitTestCase { 'prefix' => 'admin', 'admin' => true, 'form' => array(), 'url' => array('url' => 'admin/articles_test'), 'bare' => 0, 'webservices' => null, 'return' => 1 ); - $this->assertIdentical($controller->params, $expected); + $this->assertEqual($controller->params, $expected); } function testAutomaticPluginControllerMissingActionDispatch() { @@ -641,6 +663,26 @@ class DispatcherTest extends UnitTestCase { $this->assertIdentical($expected, $controller); } + function testChangingParamsFromBeforeFilter() { + $dispatcher =& new TestDispatcher(); + $url = 'some_posts/index/param:value/param2:value2'; + + restore_error_handler(); + $controller = $dispatcher->dispatch($url, array('return' => 1)); + set_error_handler('simpleTestErrorHandler'); + + $expected = 'SomePosts'; + $this->assertEqual($expected, $controller->name); + + $expected = 'view'; + $this->assertEqual($expected, $controller->action); + + + $expected = array('changed'); + $this->assertIdentical($expected, $controller->params['pass']); + } + + function tearDown() { $_GET = $this->_get; }