Updating ProjectTask::getAdmin(). Renamed to ProjectTask::getPrefix(). Now returns the user selected prefix to use for baking.

Test cases updated.  Use of method updated.
This commit is contained in:
mark_story 2009-10-07 00:46:13 -04:00
parent d38e50805d
commit 651a3f16b2
6 changed files with 73 additions and 26 deletions

View file

@ -86,7 +86,7 @@ class ControllerTask extends Shell {
$this->out(__('Baking basic crud methods for ', true) . $controller);
$actions = $this->bakeActions($controller);
} elseif (!empty($this->args[1]) && $this->args[1] == 'admin') {
$admin = $this->Project->getAdmin();
$admin = $this->Project->getPrefix();
if ($admin) {
$this->out('Adding ' . Configure::read('Routing.admin') .' methods');
$actions= $this->bakeActions($controller, $admin);
@ -94,7 +94,7 @@ class ControllerTask extends Shell {
}
if (!empty($this->args[2]) && $this->args[2] == 'admin') {
$admin = $this->Project->getAdmin();
$admin = $this->Project->getPrefix();
if ($admin) {
$this->out('Adding ' . Configure::read('Routing.admin') .' methods');
$actions .= "\n" . $this->bakeActions($controller, $admin);
@ -194,7 +194,7 @@ class ControllerTask extends Shell {
$actions = $this->bakeActions($controllerName, null, strtolower($wannaUseSession) == 'y');
}
if (strtolower($wannaBakeAdminCrud) == 'y') {
$admin = $this->Project->getAdmin();
$admin = $this->Project->getPrefix();
$actions .= $this->bakeActions($controllerName, $admin, strtolower($wannaUseSession) == 'y');
}

View file

@ -258,10 +258,10 @@ class ProjectTask extends Shell {
$path = (empty($this->configPath)) ? CONFIGS : $this->configPath;
$File =& new File($path . 'core.php');
$contents = $File->read();
if (preg_match('%([/\\t\\x20]*Configure::write\(\'Routing.admin\',[\\t\\x20\'a-z]*\\);)%', $contents, $match)) {
$result = str_replace($match[0], "\t" . 'Configure::write(\'Routing.admin\', \''.$name.'\');', $contents);
if (preg_match('%([/\\t\\x20]*Configure::write\(\'Routing.prefixes\',[\\t\\x20\'a-z,\)\(]*\\);)%', $contents, $match)) {
$result = str_replace($match[0], "\t" . 'Configure::write(\'Routing.prefixes\', array(\''.$name.'\'));', $contents);
if ($File->write($result)) {
Configure::write('Routing.admin', $name);
Configure::write('Routing.prefixes', array($name));
return true;
} else {
return false;
@ -277,22 +277,31 @@ class ProjectTask extends Shell {
* @return string Admin route to use
* @access public
*/
function getAdmin() {
function getPrefix() {
$admin = '';
$cakeAdmin = null;
$adminRoute = Configure::read('Routing.admin');
if (!empty($adminRoute)) {
return $adminRoute . '_';
$prefixes = Configure::read('Routing.prefixes');
if (!empty($prefixes)) {
if (count($prefixes) == 1) {
return $prefixes[0] . '_';
}
$options = array();
foreach ($prefixes as $i => $prefix) {
$options[] = $i + 1;
$this->out($i + 1 . '. ' . $prefix);
}
$selection = $this->in(__('Please choose a prefix to bake with.', true), $options, 1);
return $prefixes[$selection - 1] . '_';
}
$this->out('You need to enable Configure::write(\'Routing.admin\',\'admin\') in /app/config/core.php to use admin routing.');
$this->out(__('What would you like the admin route to be?', true));
$this->out('You need to enable Configure::write(\'Routing.prefixes\',array(\'admin\')) in /app/config/core.php to use prefix routing.');
$this->out(__('What would you like the prefix route to be?', true));
$this->out(__('Example: www.example.com/admin/controller', true));
while ($admin == '') {
$admin = $this->in(__("What would you like the admin route to be?", true), null, 'admin');
$admin = $this->in(__("What would you like the prefix route to be?", true), null, 'admin');
}
if ($this->cakeAdmin($admin) !== true) {
$this->out(__('Unable to write to /app/config/core.php.', true));
$this->out('You need to enable Configure::write(\'Routing.admin\',\'admin\') in /app/config/core.php to use admin routing.');
$this->out('You need to enable Configure::write(\'Routing.prefixes\',array(\'admin\')) in /app/config/core.php to use prefix routing.');
$this->_stop();
}
return $admin . '_';

View file

@ -233,7 +233,7 @@ class ViewTask extends Shell {
$this->bakeActions($actions, $vars);
}
if (strtolower($wannaDoAdmin) == 'y') {
$admin = $this->Project->getAdmin();
$admin = $this->Project->getPrefix();
$regularActions = $this->scaffoldActions;
$adminActions = array();
foreach ($regularActions as $action) {

View file

@ -55,7 +55,7 @@ Mock::generatePartial(
Mock::generatePartial(
'ProjectTask', 'ControllerMockProjectTask',
array('in', 'out', 'err', 'createFile', '_stop', '_checkUnitTest', 'getAdmin')
array('in', 'out', 'err', 'createFile', '_stop', '_checkUnitTest', 'getPrefix')
);
Mock::generate('TestTask', 'ControllerMockTestTask');
@ -485,7 +485,7 @@ class ControllerTaskTest extends CakeTestCase {
if ($skip) {
return;
}
$this->Task->Project->setReturnValue('getAdmin', 'admin_');
$this->Task->Project->setReturnValue('getPrefix', 'admin_');
$this->Task->connection = 'test_suite';
$this->Task->path = '/my/path/';
$this->Task->args = array('Articles', 'public', 'admin');
@ -509,7 +509,7 @@ class ControllerTaskTest extends CakeTestCase {
if ($skip) {
return;
}
$this->Task->Project->setReturnValue('getAdmin', 'admin_');
$this->Task->Project->setReturnValue('getPrefix', 'admin_');
$this->Task->connection = 'test_suite';
$this->Task->path = '/my/path/';
$this->Task->args = array('Articles', 'admin');

View file

@ -132,22 +132,60 @@ class ProjectTaskTest extends CakeTestCase {
}
/**
* test getAdmin method, and that it returns Routing.admin or writes to config file.
* test getPrefix method, and that it returns Routing.prefix or writes to config file.
*
* @return void
**/
function testGetAdmin() {
Configure::write('Routing.admin', 'admin');
$result = $this->Task->getAdmin();
function testGetPrefix() {
Configure::write('Routing.prefixes', array('admin'));
$result = $this->Task->getPrefix();
$this->assertEqual($result, 'admin_');
Configure::write('Routing.admin', null);
Configure::write('Routing.prefixes', null);
$this->_setupTestProject();
$this->Task->configPath = $this->Task->path . 'bake_test_app' . DS . 'config' . DS;
$this->Task->setReturnValue('in', 'super_duper_admin');
$result = $this->Task->getAdmin();
$result = $this->Task->getPrefix();
$this->assertEqual($result, 'super_duper_admin_');
$file =& new File($this->Task->configPath . 'core.php');
$file->delete();
}
/**
* test cakeAdmin() writing core.php
*
* @return void
**/
function testCakeAdmin() {
$file =& new File(CONFIGS . 'core.php');
$contents = $file->read();;
$file =& new File(TMP . 'tests' . DS . 'core.php');
$file->write($contents);
Configure::write('Routing.prefixes', null);
$this->Task->configPath = TMP . 'tests' . DS;
$result = $this->Task->cakeAdmin('my_prefix');
$this->assertTrue($result);
$this->assertEqual(Configure::read('Routing.prefixes'), array('my_prefix'));
$file->delete();
}
/**
* test getting the prefix with more than one prefix setup
*
* @return void
**/
function testGetPrefixWithMultiplePrefixes() {
Configure::write('Routing.prefixes', array('admin', 'ninja', 'shinobi'));
$this->_setupTestProject();
$this->Task->configPath = $this->Task->path . 'bake_test_app' . DS . 'config' . DS;
$this->Task->setReturnValue('in', 2);
$result = $this->Task->getPrefix();
$this->assertEqual($result, 'ninja_');
}
/**

View file

@ -428,7 +428,7 @@ class ViewTaskTest extends CakeTestCase {
$this->Task->args = array();
$this->Task->Controller->setReturnValue('getName', 'ViewTaskComments');
$this->Task->Project->setReturnValue('getAdmin', 'admin_');
$this->Task->Project->setReturnValue('getPrefix', 'admin_');
$this->Task->setReturnValueAt(0, 'in', 'y');
$this->Task->setReturnValueAt(1, 'in', 'n');
$this->Task->setReturnValueAt(2, 'in', 'y');