Skip to content

Commit

Permalink
Updating ProjectTask::getAdmin(). Renamed to ProjectTask::getPrefix()…
Browse files Browse the repository at this point in the history
…. Now returns the user selected prefix to use for baking.

Test cases updated.  Use of method updated.
  • Loading branch information
markstory committed Oct 7, 2009
1 parent d38e508 commit 651a3f1
Show file tree
Hide file tree
Showing 6 changed files with 73 additions and 26 deletions.
6 changes: 3 additions & 3 deletions cake/console/libs/tasks/controller.php
Expand Up @@ -86,15 +86,15 @@ function execute() {
$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);
}
}

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);
Expand Down Expand Up @@ -194,7 +194,7 @@ function __interactive() {
$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');
}

Expand Down
33 changes: 21 additions & 12 deletions cake/console/libs/tasks/project.php
Expand Up @@ -258,10 +258,10 @@ function cakeAdmin($name) {
$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;
Expand All @@ -277,22 +277,31 @@ function cakeAdmin($name) {
* @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 . '_';
Expand Down
2 changes: 1 addition & 1 deletion cake/console/libs/tasks/view.php
Expand Up @@ -233,7 +233,7 @@ function __interactive() {
$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) {
Expand Down
6 changes: 3 additions & 3 deletions cake/tests/cases/console/libs/tasks/controller.test.php
Expand Up @@ -55,7 +55,7 @@

Mock::generatePartial(
'ProjectTask', 'ControllerMockProjectTask',
array('in', 'out', 'err', 'createFile', '_stop', '_checkUnitTest', 'getAdmin')
array('in', 'out', 'err', 'createFile', '_stop', '_checkUnitTest', 'getPrefix')
);

Mock::generate('TestTask', 'ControllerMockTestTask');
Expand Down Expand Up @@ -485,7 +485,7 @@ function testExecuteWithControllerAndBoth() {
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');
Expand All @@ -509,7 +509,7 @@ function testExecuteWithControllerAndAdmin() {
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');
Expand Down
50 changes: 44 additions & 6 deletions cake/tests/cases/console/libs/tasks/project.test.php
Expand Up @@ -132,22 +132,60 @@ function testSecuritySaltGeneration() {
}

/**
* 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_');
}

/**
Expand Down
2 changes: 1 addition & 1 deletion cake/tests/cases/console/libs/tasks/view.test.php
Expand Up @@ -428,7 +428,7 @@ function testExecuteInteractiveWithAdmin() {
$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');
Expand Down

0 comments on commit 651a3f1

Please sign in to comment.