Permalink
Browse files

Convert all camelCase plugin and controller entries to underscore. Cl…

…oses #13
  • Loading branch information...
1 parent a15e58c commit 0a4404c05c9d948631aa4170a5d4275017453587 @josegonzalez committed Jun 18, 2011
Showing with 127 additions and 24 deletions.
  1. +54 −11 controllers/components/permit.php
  2. +73 −13 tests/cases/controllers/components/permit.test.php
View
65 controllers/components/permit.php
@@ -54,6 +54,13 @@ class PermitComponent extends Object {
var $_user = null;
/**
+ * Current request parameters
+ *
+ * @var array
+ */
+ var $_requestParams = array();
+
+/**
* Initializes SanctionComponent for use in the controller
*
* @param object $controller A reference to the instantiating controller object
@@ -70,6 +77,13 @@ function initialize(&$controller, $config = array()) {
$Permit =& PermitComponent::getInstance();
$this->routes = $Permit->routes;
+
+ $this->_requestParams = $controller->params;
+ foreach (array('controller', 'plugin') as $inflected) {
+ if (isset($this->_requestParams[$inflected])) {
+ $this->_requestParams[$inflected] = strtolower(Inflector::underscore($this->_requestParams[$inflected]));
+ }
+ }
}
@@ -85,17 +99,17 @@ function startup(&$controller) {
if ($this->settings['isTest']) return;
foreach ($this->routes as $route) {
- if ($this->_parse($controller, $route['route'])) {
+ if ($this->_parse($route['route'])) {
if ($this->_execute($route)) {
- if (isset($controller->params['url']['url'])) {
- $url = $controller->params['url']['url'];
+ if (isset($this->_requestParams['url']['url'])) {
+ $url = $this->_requestParams['url']['url'];
} else {
- $url = $controller->params;
+ $url = $this->_requestParams;
}
$url = Router::normalize($url);
- if (!empty($controller->params['url']) && count($controller->params['url']) >= 2) {
- $query = $controller->params['url'];
+ if (!empty($this->_requestParams['url']) && count($this->_requestParams['url']) >= 2) {
+ $query = $this->_requestParams['url'];
unset($query['url'], $query['ext']);
$url .= Router::queryString($query, array());
}
@@ -115,24 +129,30 @@ function startup(&$controller) {
* @return boolean true if current request matches Permit route, false otherwise
* @access protected
*/
- function _parse(&$controller, $route) {
+ function _parse($route) {
$count = count($route);
if ($count == 0) {
return false;
}
foreach ($route as $key => $value) {
- if (isset($controller->params[$key])) {
+ if (isset($this->_requestParams[$key])) {
$values = (array) $value;
- $check = (array) $controller->params[$key];
+ $check = (array) $this->_requestParams[$key];
if (!count($values) && in_array(null, $check)) {
$count--;
continue;
}
- foreach ($check as $k => $_check) {
- $check[$k] = strtolower($_check);
+ if (in_array($key, array('controller', 'plugin'))) {
+ foreach ($check as $k => $_check) {
+ $check[$k] = Inflector::underscore(strtolower($_check));
+ }
+ } else {
+ foreach ($check as $k => $_check) {
+ $check[$k] = strtolower($_check);
+ }
}
foreach ($values as $k => $v) {
@@ -257,6 +277,29 @@ function access($route, $rules = array(), $redirect = array()) {
$redirect
);
+ //convert underscored names to camelCase as additional way of accessing a controller
+ foreach (array('controller', 'plugin') as $inflected) {
+ if (isset($route[$inflected])) {
+ if (is_array($route[$inflected])) {
+ foreach ($route[$inflected] as $i => $controllerName) {
+ $route[$inflected][$i] = Inflector::underscore($controllerName);
+ }
+ } else {
+ $route[$inflected] = Inflector::underscore($route[$inflected]);
+ }
+ }
+ }
+
+ foreach ($route as $k => $value) {
+ if (is_array($value)) {
+ foreach ($value as $i => $_value) {
+ $route[$k][$i] = strtolower($_value);
+ }
+ } else {
+ $route[$k] = strtolower($value);
+ }
+ }
+
$newRoute = array(
'route' => $route,
'rules' => $rules,
View
86 tests/cases/controllers/components/permit.test.php
@@ -239,7 +239,7 @@ function startTest() {
restore_error_handler();
$this->Controller->constructClasses();
- $this->Controller->params = array(
+ $this->Controller->params = $this->Controller->Permit->_requestParams = array(
'pass' => array(), 'named' => array(),
'plugin' => '', 'controller' => 'posts',
'action' => 'index'
@@ -300,41 +300,85 @@ function endTest() {
unset($this->Controller);
}
+ function setup() {
+ $Permit = PermitComponent::getInstance();
+ $this->Controller->Permit->_user = $Permit->_user = array();
+ $this->Controller->params = array(
+ 'pass' => array(), 'named' => array(),
+ 'plugin' => '', 'controller' => 'posts',
+ 'action' => 'index'
+ );
+ $this->Controller->Permit->_requestParams = $Permit->_requestParams = array(
+ 'pass' => array(), 'named' => array(),
+ 'plugin' => '', 'controller' => 'Posts',
+ 'action' => 'index'
+ );
+
+ $this->Controller->Permit->routes = $Permit->routes = array();
+ $this->Controller->Permit->executed = $Permit->executed = null;
+ $this->Controller->Permit->settings = $Permit->settings = array(
+ 'path' => 'MockAuthTest',
+ 'check' => 'id',
+ 'isTest' => true,
+ );
+
+ $this->Controller->testUrl = null;
+ }
+
function testSingleParse() {
$testRoute = array('controller' => 'posts');
- $this->assertTrue($this->Controller->Permit->_parse($this->Controller, $testRoute));
+ $this->assertTrue($this->Controller->Permit->_parse($testRoute));
$testRoute = array('controller' => 'posts', 'action' => 'index');
- $this->assertTrue($this->Controller->Permit->_parse($this->Controller, $testRoute));
+ $this->assertTrue($this->Controller->Permit->_parse($testRoute));
$testRoute = array('plugin' => null, 'controller' => 'posts', 'action' => 'index');
- $this->assertTrue($this->Controller->Permit->_parse($this->Controller, $testRoute));
+ $this->assertTrue($this->Controller->Permit->_parse($testRoute));
$testRoute = array('controller' => 'posts', 'action' => 'add');
- $this->assertFalse($this->Controller->Permit->_parse($this->Controller, $testRoute));
+ $this->assertFalse($this->Controller->Permit->_parse($testRoute));
$testRoute = array('controller' => 'users', 'action' => 'index');
- $this->assertFalse($this->Controller->Permit->_parse($this->Controller, $testRoute));
+ $this->assertFalse($this->Controller->Permit->_parse($testRoute));
}
function testMultipleParse() {
$testRoute = array('controller' => 'posts', 'action' => array('index'));
- $this->assertTrue($this->Controller->Permit->_parse($this->Controller, $testRoute));
+ $this->assertTrue($this->Controller->Permit->_parse($testRoute));
$testRoute = array('controller' => 'posts', 'action' => array('index', 'add'));
- $this->assertTrue($this->Controller->Permit->_parse($this->Controller, $testRoute));
+ $this->assertTrue($this->Controller->Permit->_parse($testRoute));
$testRoute = array('controller' => array('posts', 'users'), 'action' => array('index', 'add'));
- $this->assertTrue($this->Controller->Permit->_parse($this->Controller, $testRoute));
+ $this->assertTrue($this->Controller->Permit->_parse($testRoute));
$testRoute = array('plugin' => array(null, 'blog'),
'controller' => array('posts', 'users'),
'action' => array('index', 'add')
);
- $this->assertTrue($this->Controller->Permit->_parse($this->Controller, $testRoute));
+ $this->assertTrue($this->Controller->Permit->_parse($testRoute));
$testRoute = array('controller' => 'posts', 'action' => array('add', 'edit', 'delete'));
- $this->assertFalse($this->Controller->Permit->_parse($this->Controller, $testRoute));
+ $this->assertFalse($this->Controller->Permit->_parse($testRoute));
+ }
+
+ function testCaseAndInflectionParse() {
+ $testRoute = array('controller' => 'POSTS');
+ $this->assertTrue($this->Controller->Permit->_parse($testRoute));
+
+ $this->Controller->params = $this->Controller->Permit->_requestParams = array(
+ 'pass' => array(), 'named' => array(),
+ 'plugin' => '', 'controller' => 'blog_posts',
+ 'action' => 'INDEX'
+ );
+ $testRoute = array('controller' => 'POSTS');
+ $this->assertFalse($this->Controller->Permit->_parse($testRoute));
+
+ $testRoute = array('controller' => 'Blog_POSTS');
+ $this->assertTrue($this->Controller->Permit->_parse($testRoute));
+
+ $testRoute = array('action' => 'inDex');
+ $this->assertTrue($this->Controller->Permit->_parse($testRoute));
}
function testDenyAccess() {
@@ -468,6 +512,7 @@ function testAuthMultidimensionalExecute() {
function testStartup() {
$this->Controller->Permit->settings['isTest'] = false;
+
$this->Controller->Permit->access(
array('controller' => 'posts', 'action' => array('add', 'edit', 'delete')),
array('auth' => true),
@@ -494,6 +539,21 @@ function testStartup() {
);
$this->Controller->Permit->startup($this->Controller);
$this->assertEqual($this->Controller->testUrl, '/users/login');
+ $this->Controller->testUrl = null;
+
+ $this->Controller->params = $this->Controller->Permit->_requestParams = array(
+ 'pass' => array(), 'named' => array(),
+ 'plugin' => '', 'controller' => 'blog_posts',
+ 'action' => 'index'
+ );
+ $this->Controller->Permit->startup($this->Controller);
+ $this->Controller->Permit->access(
+ array('controller' => 'blogPosts', 'action' => 'index'),
+ array('auth' => true),
+ array('redirect' => array('controller' => 'users', 'action' => 'login'))
+ );
+ $this->Controller->Permit->startup($this->Controller);
+ $this->assertEqual($this->Controller->testUrl, '/users/login');
}
function testAccess() {
@@ -614,5 +674,5 @@ function testPermitObject() {
$this->assertEqual(end($Permit->routes), $expected);
reset($Permit->routes);
}
-}
-?>
+
+}

0 comments on commit 0a4404c

Please sign in to comment.