Skip to content

Commit

Permalink
Clean up URL generation.
Browse files Browse the repository at this point in the history
Instead of detecting the controller based on the model names, only use
the current request. If you'd like a form to always point at a specific
URL you will need to set the action/url options, this reducess the magic
and makes FormHelper simpler to understand.

This change also makes the put/post conventions simpler. Any time the
context is not in create mode, an update will be done.
  • Loading branch information
markstory committed Feb 11, 2014
1 parent 17e3dbf commit 4ea06a8
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 50 deletions.
29 changes: 13 additions & 16 deletions src/View/Helper/FormHelper.php
Expand Up @@ -274,7 +274,6 @@ public function tagIsInvalid() {
* @link http://book.cakephp.org/2.0/en/core-libraries/helpers/form.html#options-for-create
*/
public function create($model = null, $options = []) {
$id = $key = false;
$append = '';

if (empty($options['context'])) {
Expand All @@ -287,7 +286,7 @@ public function create($model = null, $options = []) {
$isCreate = $this->_context->isCreate();

$options = array_merge([
'type' => (!$isCreate && empty($options['action'])) ? 'put' : 'post',
'type' => !$isCreate ? 'put' : 'post',
'action' => null,
'url' => null,
'default' => true,
Expand All @@ -297,29 +296,27 @@ public function create($model = null, $options = []) {
if ($options['action'] === null && $options['url'] === null) {
$options['action'] = $this->request->here(false);
} elseif (empty($options['url']) || is_array($options['url'])) {
// TODO restore convention around model->controller name.
if (empty($options['url']['controller'])) {
$options['url']['controller'] = Inflector::underscore($this->request->params['controller']);
}
if (empty($options['action'])) {
$options['action'] = $this->request->params['action'];
if (isset($options['action']) && empty($options['url']['action'])) {
$options['url']['action'] = $options['action'];
}

$plugin = null;
if ($this->plugin) {
$plugin = Inflector::underscore($this->plugin);
}
$plugin = $this->plugin ? Inflector::underscore($this->plugin) : null;
$actionDefaults = array(
'plugin' => $plugin,
'controller' => $this->_View->viewPath,
'action' => $options['action'],
'controller' => Inflector::underscore($this->request->params['controller']),
'action' => $this->request->params['action'],
);

$options['action'] = (array)$options['url'] + $actionDefaults;

// TODO add primary key handling
if (empty($options['action'][0]) && !empty($id)) {
$pk = $this->_context->primaryKey();
if (count($pk)) {
$id = $this->_context->val($pk[0]);
}
if (empty($options['action'][0]) && isset($id)) {
$options['action'][0] = $id;
}

} elseif (is_string($options['url'])) {
$options['action'] = $options['url'];
}
Expand Down
89 changes: 55 additions & 34 deletions tests/TestCase/View/Helper/FormHelperTest.php
Expand Up @@ -487,8 +487,9 @@ public function setUp() {

$this->Form = new FormHelper($this->View);
$this->Form->Html = new HtmlHelper($this->View);
$this->Form->request = new Request('contacts/add');
$this->Form->request->here = '/contacts/add';
$this->Form->request = new Request('articles/add');
$this->Form->request->here = '/articles/add';
$this->Form->request['controller'] = 'articles';
$this->Form->request['action'] = 'add';
$this->Form->request->webroot = '';
$this->Form->request->base = '';
Expand All @@ -502,6 +503,21 @@ public function setUp() {
'meridianRegex' => 'preg:/(?:<option value="(am|pm)">\\1<\/option>[\r\n]*)*/',
);

$this->article = [
'schema' => [
'id' => ['type' => 'integer'],
'author_id' => ['type' => 'integer', 'null' => true],
'title' => ['type' => 'string', 'null' => true],
'body' => 'text',
'published' => ['type' => 'string', 'length' => 1, 'default' => 'N'],
'_constraints' => ['primary' => ['type' => 'primary', 'columns' => ['id']]]
],
'required' => [
'author_id' => true,
'title' => true,
]
];

Configure::write('Security.salt', 'foo!');
Router::connect('/:controller', array('action' => 'index'));
Router::connect('/:controller/:action/*');
Expand Down Expand Up @@ -617,9 +633,8 @@ public function testCreateContextSelectionBuiltIn($data, $class) {
*
* @return void
*/
public function testCreate() {
$this->markTestIncomplete('Need to revisit once models work again.');
$result = $this->Form->create('Contact');
public function testCreateTypeOptions() {
$result = $this->Form->create(false);
$encoding = strtolower(Configure::read('App.encoding'));
$expected = array(
'form' => array(
Expand All @@ -632,21 +647,21 @@ public function testCreate() {
);
$this->assertTags($result, $expected);

$result = $this->Form->create('Contact', array('type' => 'GET'));
$result = $this->Form->create(false, array('type' => 'GET'));
$expected = array('form' => array(
'id' => 'ContactAddForm', 'method' => 'get', 'action' => '/contacts/add',
'accept-charset' => $encoding
));
$this->assertTags($result, $expected);

$result = $this->Form->create('Contact', array('type' => 'get'));
$result = $this->Form->create(false, array('type' => 'get'));
$expected = array('form' => array(
'id' => 'ContactAddForm', 'method' => 'get', 'action' => '/contacts/add',
'accept-charset' => $encoding
));
$this->assertTags($result, $expected);

$result = $this->Form->create('Contact', array('type' => 'put'));
$result = $this->Form->create(false, array('type' => 'put'));
$expected = array(
'form' => array(
'id' => 'ContactAddForm', 'method' => 'post', 'action' => '/contacts/add',
Expand All @@ -658,7 +673,19 @@ public function testCreate() {
);
$this->assertTags($result, $expected);

$result = $this->Form->create('Contact', array('type' => 'file'));
$result = $this->Form->create(false, array('type' => 'patch'));
$expected = array(
'form' => array(
'method' => 'post', 'action' => '/articles/add',
'accept-charset' => $encoding
),
'div' => array('style' => 'display:none;'),
'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'PATCH'),
'/div'
);
$this->assertTags($result, $expected);

$result = $this->Form->create(false, array('type' => 'file'));
$expected = array(
'form' => array(
'id' => 'ContactAddForm', 'method' => 'post', 'action' => '/contacts/add',
Expand All @@ -669,11 +696,17 @@ public function testCreate() {
'/div'
);
$this->assertTags($result, $expected);
}

$this->Form->request->data['Contact']['id'] = 1;
$this->Form->request->here = '/contacts/edit/1';
$this->Form->request['action'] = 'edit';
$result = $this->Form->create('Contact');
/**
* Test opening a form for an update operation.
*
* @return void
*/
public function testCreateUpdateForm() {
$encoding = strtolower(Configure::read('App.encoding'));

$result = $this->Form->create($this->article);
$expected = array(
'form' => array(
'id' => 'ContactEditForm', 'method' => 'post', 'action' => '/contacts/edit/1',
Expand All @@ -688,7 +721,7 @@ public function testCreate() {
$this->Form->request->data['Contact']['id'] = 1;
$this->Form->request->here = '/contacts/edit/1';
$this->Form->request['action'] = 'edit';
$result = $this->Form->create('Contact', array('type' => 'file'));
$result = $this->Form->create($this->article, array('type' => 'file'));
$expected = array(
'form' => array(
'id' => 'ContactEditForm', 'method' => 'post', 'action' => '/contacts/edit/1',
Expand Down Expand Up @@ -726,48 +759,36 @@ public function testCreate() {
$this->assertTags($result, $expected);

$this->Form->request['action'] = 'add';
$result = $this->Form->create('User', array('url' => array('action' => 'login')));
$expected = array(
'form' => array(
'id' => 'UserAddForm', 'method' => 'post', 'action' => '/users/login',
'accept-charset' => $encoding
),
'div' => array('style' => 'display:none;'),
'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
'/div'
);
$this->assertTags($result, $expected);

$result = $this->Form->create('User', array('action' => 'login'));
$result = $this->Form->create($this->article, array('url' => array('action' => 'publish')));
$expected = array(
'form' => array(
'id' => 'UserLoginForm', 'method' => 'post', 'action' => '/users/login',
'method' => 'post', 'action' => '/articles/publish/1',
'accept-charset' => $encoding
),
'div' => array('style' => 'display:none;'),
'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'PUT'),
'/div'
);
$this->assertTags($result, $expected);

$result = $this->Form->create('User', array('url' => '/users/login'));
$result = $this->Form->create($this->article, array('url' => '/articles/publish'));
$expected = array(
'form' => array('method' => 'post', 'action' => '/users/login', 'accept-charset' => $encoding, 'id' => 'UserAddForm'),
'div' => array('style' => 'display:none;'),
'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'PUT'),
'/div'
);
$this->assertTags($result, $expected);

$this->Form->request['controller'] = 'pages';
$result = $this->Form->create('User', array('action' => 'signup'));
$result = $this->Form->create($this->article, array('action' => 'signup'));
$expected = array(
'form' => array(
'id' => 'UserSignupForm', 'method' => 'post', 'action' => '/users/signup',
'method' => 'post', 'action' => '/pages/signup/1',
'accept-charset' => $encoding
),
'div' => array('style' => 'display:none;'),
'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'PUT'),
'/div'
);
$this->assertTags($result, $expected);
Expand Down

0 comments on commit 4ea06a8

Please sign in to comment.