Skip to content

Commit

Permalink
Merge pull request #2385 from markstory/3.0-controller-cleanup
Browse files Browse the repository at this point in the history
3.0 controller cleanup
  • Loading branch information
lorenzo committed Nov 23, 2013
2 parents 602a4d2 + 236760a commit 6a4dad2
Show file tree
Hide file tree
Showing 8 changed files with 4 additions and 331 deletions.
14 changes: 4 additions & 10 deletions Cake/Console/Command/Task/ControllerTask.php
Expand Up @@ -150,7 +150,6 @@ protected function _interactive() {

$helpers = $components = [];
$actions = '';
$wannaUseSession = 'y';
$wannaBakeAdminCrud = 'n';
$useDynamicScaffold = 'n';
$wannaBakeCrud = 'y';
Expand All @@ -175,21 +174,17 @@ protected function _interactive() {

$helpers = $this->doHelpers();
$components = $this->doComponents();

$wannaUseSession = $this->in(
__d('cake_console', "Would you like to use Session flash messages?"), ['y', 'n'], 'y'
);
}
} else {
list($wannaBakeCrud, $wannaBakeAdminCrud) = $this->_askAboutMethods();
}

if (strtolower($wannaBakeCrud) === 'y') {
$actions = $this->bakeActions($controllerName, null, strtolower($wannaUseSession) === 'y');
$actions = $this->bakeActions($controllerName, null);
}
if (strtolower($wannaBakeAdminCrud) === 'y') {
$admin = $this->Project->getPrefix();
$actions .= $this->bakeActions($controllerName, $admin, strtolower($wannaUseSession) === 'y');
$actions .= $this->bakeActions($controllerName, $admin);
}

$baked = false;
Expand Down Expand Up @@ -276,10 +271,9 @@ protected function _askAboutMethods() {
*
* @param string $controllerName Controller name
* @param string $admin Admin route to use
* @param boolean $wannaUseSession Set to true to use sessions, false otherwise
* @return string Baked actions
*/
public function bakeActions($controllerName, $admin = null, $wannaUseSession = true) {
public function bakeActions($controllerName, $admin = null) {
$currentModelName = $modelImport = $this->_modelName($controllerName);
$plugin = $this->plugin;
if ($plugin) {
Expand All @@ -302,7 +296,7 @@ public function bakeActions($controllerName, $admin = null, $wannaUseSession = t

$this->Template->set(compact(
'plugin', 'admin', 'controllerPath', 'pluralName', 'singularName',
'singularHumanName', 'pluralHumanName', 'modelObj', 'wannaUseSession', 'currentModelName',
'singularHumanName', 'pluralHumanName', 'modelObj', 'currentModelName',
'displayField', 'primaryKey'
));
$actions = $this->Template->generate('actions', 'controller_actions');
Expand Down
15 changes: 0 additions & 15 deletions Cake/Console/Templates/default/actions/controller_actions.ctp
Expand Up @@ -51,14 +51,10 @@
if ($this->request->is('post')) {
$this-><?php echo $currentModelName; ?>->create();
if ($this-><?php echo $currentModelName; ?>->save($this->request->data)) {
<?php if ($wannaUseSession): ?>
$this->Session->setFlash(__('The <?php echo strtolower($singularHumanName); ?> has been saved.'));
return $this->redirect(['action' => 'index']);
} else {
$this->Session->setFlash(__('The <?php echo strtolower($singularHumanName); ?> could not be saved. Please, try again.'));
<?php else: ?>
return $this->flash(__('The <?php echo strtolower($singularHumanName); ?> has been saved.'), ['action' => 'index']);
<?php endif; ?>
}
}
<?php
Expand Down Expand Up @@ -92,14 +88,10 @@
}
if ($this->request->is(['post', 'put'])) {
if ($this-><?php echo $currentModelName; ?>->save($this->request->data)) {
<?php if ($wannaUseSession): ?>
$this->Session->setFlash(__('The <?php echo strtolower($singularHumanName); ?> has been saved.'));
return $this->redirect(['action' => 'index']);
} else {
$this->Session->setFlash(__('The <?php echo strtolower($singularHumanName); ?> could not be saved. Please, try again.'));
<?php else: ?>
return $this->flash(__('The <?php echo strtolower($singularHumanName); ?> has been saved.'), ['action' => 'index']);
<?php endif; ?>
}
} else {
$options = ['conditions' => ['<?php echo $currentModelName; ?>.' . $this-><?php echo $currentModelName; ?>->primaryKey => $id]];
Expand Down Expand Up @@ -136,16 +128,9 @@
}
$this->request->onlyAllow('post', 'delete');
if ($this-><?php echo $currentModelName; ?>->delete()) {
<?php if ($wannaUseSession): ?>
$this->Session->setFlash(__('The <?php echo strtolower($singularHumanName); ?> has been deleted.'));
} else {
$this->Session->setFlash(__('The <?php echo strtolower($singularHumanName); ?> could not be deleted. Please, try again.'));
}
return $this->redirect(['action' => 'index']);
<?php else: ?>
return $this->flash(__('The <?php echo strtolower($singularHumanName); ?> has been deleted.'), ['action' => 'index']);
} else {
return $this->flash(__('The <?php echo strtolower($singularHumanName); ?> could not be deleted. Please, try again.'), ['action' => 'index']);
}
<?php endif; ?>
}
88 changes: 0 additions & 88 deletions Cake/Controller/Controller.php
Expand Up @@ -718,30 +718,6 @@ public function redirect($url, $status = null, $exit = true) {
}
}

/**
* Parse beforeRedirect Response
*
* @param mixed $response Response from beforeRedirect callback
* @param string|array $url The same value of beforeRedirect
* @param integer $status The same value of beforeRedirect
* @param boolean $exit The same value of beforeRedirect
* @return array Array with keys url, status and exit
*/
protected function _parseBeforeRedirect($response, $url, $status, $exit) {
if (is_array($response) && array_key_exists(0, $response)) {
foreach ($response as $resp) {
if (is_array($resp) && isset($resp['url'])) {
extract($resp, EXTR_OVERWRITE);
} elseif ($resp !== null) {
$url = $resp;
}
}
} elseif (is_array($response)) {
extract($response, EXTR_OVERWRITE);
}
return compact('url', 'status', 'exit');
}

/**
* Internally redirects one action to another. Does not perform another HTTP request unlike Controller::redirect()
*
Expand All @@ -764,48 +740,6 @@ public function setAction($action) {
return call_user_func_array(array(&$this, $action), $args);
}

/**
* Returns number of errors in a submitted FORM.
*
* @return integer Number of errors
*/
public function validate() {
$args = func_get_args();
$errors = call_user_func_array(array(&$this, 'validateErrors'), $args);

if ($errors === false) {
return 0;
}
return count($errors);
}

/**
* Validates models passed by parameters. Example:
*
* `$errors = $this->validateErrors($this->Article, $this->User);`
*
* @param mixed A list of models as a variable argument
* @return array Validation errors, or false if none
*/
public function validateErrors() {
$objects = func_get_args();

if (empty($objects)) {
return false;
}

$errors = array();
foreach ($objects as $object) {
if (isset($this->{$object->alias})) {
$object = $this->{$object->alias};
}
$object->set($object->data);
$errors = array_merge($errors, $object->invalidFields());
}

return $this->validationErrors = (!empty($errors) ? $errors : false);
}

/**
* Instantiates the correct view class, hands it its data, and uses it to render the view output.
*
Expand Down Expand Up @@ -867,28 +801,6 @@ public function referer($default = null, $local = false) {
return $referer;
}

/**
* Shows a message to the user for $pause seconds, then redirects to $url.
* Uses flash.ctp as the default layout for the message.
* Does not work if the current debug level is higher than 0.
*
* @param string $message Message to display to the user
* @param string|array $url Relative string or array-based URL to redirect to after the time expires
* @param integer $pause Time to show the message
* @param string $layout Layout you want to use, defaults to 'flash'
* @return void
* @link http://book.cakephp.org/2.0/en/controllers.html#Controller::flash
* @deprecated Will be removed in 3.0. Use Session::setFlash().
*/
public function flash($message, $url, $pause = 1, $layout = 'flash') {
$this->autoRender = false;
$this->set('url', Router::url($url));
$this->set('message', $message);
$this->set('pause', $pause);
$this->set('page_title', $message);
$this->render(false, $layout);
}

/**
* Handles pagination of records in Table objects.
*
Expand Down
22 changes: 0 additions & 22 deletions Cake/Test/TestApp/View/Layout/flash.ctp

This file was deleted.

13 changes: 0 additions & 13 deletions Cake/Test/TestCase/Console/Command/Task/ControllerTaskTest.php
@@ -1,7 +1,5 @@
<?php
/**
* ControllerTask Test Case
*
* CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
* Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
*
Expand Down Expand Up @@ -331,17 +329,6 @@ public function testBakeActionsUsingSessions() {
$this->assertContains('function admin_delete($id = null)', $result);
}

/**
* Test baking with Controller::flash() or no sessions.
*
* @return void
*/
public function testBakeActionsWithNoSessions() {
$result = $this->Task->bakeActions('BakeArticles', null, false);
$expected = file_get_contents(CAKE . 'Test' . DS . 'bake_compare' . DS . 'Controller' . DS . 'ActionsWithNoSessions.ctp');
$this->assertTextEquals($expected, $result);
}

/**
* test baking a test
*
Expand Down
85 changes: 0 additions & 85 deletions Cake/Test/TestCase/Controller/ControllerTest.php
Expand Up @@ -363,46 +363,6 @@ public function testAliasName() {
unset($Controller);
}

/**
* testFlash method
*
* @return void
*/
public function testFlash() {
$request = new Request('controller_posts/index');
$request->webroot = '/';
$request->base = '/';

$Controller = new Controller($request, $this->getMock('Cake\Network\Response', array('_sendHeader')));
$Controller->flash('this should work', '/flash');
$result = $Controller->response->body();

$expected = '<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>this should work</title>
<style><!--
P { text-align:center; font:bold 1.1em sans-serif }
A { color:#444; text-decoration:none }
A:HOVER { text-decoration: underline; color:#44E }
--></style>
</head>
<body>
<p><a href="/flash">this should work</a></p>
</body>
</html>';
$result = str_replace(array("\t", "\r\n", "\n"), "", $result);
$expected = str_replace(array("\t", "\r\n", "\n"), "", $expected);
$this->assertEquals($expected, $result);

$Controller = new Controller($request);
$Controller->response = $this->getMock('Cake\Network\Response', array('_sendHeader'));
$Controller->flash('this should work', '/flash', 1, 'ajax2');
$result = $Controller->response->body();
$this->assertRegExp('/Ajax!/', $result);
}

/**
* testRender method
*
Expand Down Expand Up @@ -747,51 +707,6 @@ public function testSetAction() {
$this->assertSame('view', $TestController->view);
}

/**
* testValidateErrors method
*
* @return void
*/
public function testValidateErrors() {
$this->markTestIncomplete('Need to revisit once models work again.');
ClassRegistry::flush();
$request = new Request('controller_posts/index');

$TestController = new TestController($request);
$TestController->constructClasses();
$this->assertFalse($TestController->validateErrors());
$this->assertEquals(0, $TestController->validate());

$TestController->Comment->invalidate('some_field', 'error_message');
$TestController->Comment->invalidate('some_field2', 'error_message2');

$comment = new \TestApp\Model\Comment($request);
$comment->set('someVar', 'data');
$result = $TestController->validateErrors($comment);
$expected = array('some_field' => array('error_message'), 'some_field2' => array('error_message2'));
$this->assertSame($expected, $result);
$this->assertEquals(2, $TestController->validate($comment));
}

/**
* test that validateErrors works with any old model.
*
* @return void
*/
public function testValidateErrorsOnArbitraryModels() {
$this->markTestIncomplete('Need to revisit once models work again.');
Configure::write('Config.language', 'eng');
$TestController = new TestController();

$Post = new \TestApp\Model\Post();
$Post->validate = array('title' => 'notEmpty');
$Post->set('title', '');
$result = $TestController->validateErrors($Post);

$expected = array('title' => array('The provided value is invalid'));
$this->assertEquals($expected, $result);
}

/**
* Tests that the startup process calls the correct functions
*
Expand Down
14 changes: 0 additions & 14 deletions Cake/Test/TestCase/View/ViewTest.php
Expand Up @@ -1003,19 +1003,6 @@ public function testRender() {
$this->assertTrue(isset($View->viewVars['content_for_layout']), 'content_for_layout should be a view var');
$this->assertTrue(isset($View->viewVars['scripts_for_layout']), 'scripts_for_layout should be a view var');

$this->PostsController->set('url', 'flash');
$this->PostsController->set('message', 'yo what up');
$this->PostsController->set('pause', 3);
$this->PostsController->set('page_title', 'yo what up');

$View = new TestView($this->PostsController);
$result = $View->render(false, 'flash');

$this->assertRegExp("/<title>yo what up<\/title>/", $result);
$this->assertRegExp("/<p><a href=\"flash\">yo what up<\/a><\/p>/", $result);

$this->assertTrue($View->render(false, 'flash'));

$this->PostsController->helpers = array('Session', 'Cache', 'Html');
$this->PostsController->constructClasses();
$this->PostsController->cacheAction = array('index' => 3600);
Expand All @@ -1027,7 +1014,6 @@ public function testRender() {

$this->assertRegExp("/<meta http-equiv=\"Content-Type\" content=\"text\/html; charset=utf-8\" \/>\s*<title>/", $result);
$this->assertRegExp("/<div id=\"content\">\s*posts index\s*<\/div>/", $result);
$this->assertRegExp("/<div id=\"content\">\s*posts index\s*<\/div>/", $result);
}

/**
Expand Down

0 comments on commit 6a4dad2

Please sign in to comment.