Permalink
Browse files

Minor refactoring.

Adding tests for form->create() with file type forms.
  • Loading branch information...
1 parent 15da4a7 commit ac302bb6c2da6f25820827fbb0fc3b7c9f5c21a9 @markstory markstory committed Oct 31, 2009
Showing with 34 additions and 7 deletions.
  1. +4 −3 cake/libs/view/helpers/form.php
  2. +30 −4 cake/tests/cases/libs/view/helpers/form.test.php
View
7 cake/libs/view/helpers/form.php
@@ -181,7 +181,6 @@ function create($model = null, $options = array()) {
$data = $this->fieldset[$this->model()];
$recordExists = (
isset($this->data[$model]) &&
- isset($this->data[$model][$data['key']]) &&
!empty($this->data[$model][$data['key']])
);
@@ -190,6 +189,7 @@ function create($model = null, $options = array()) {
$id = $this->data[$model][$data['key']];
}
}
+
$options = array_merge(array(
'type' => ($created && empty($options['action'])) ? 'put' : 'post',
'action' => null,
@@ -970,14 +970,15 @@ function radio($fieldName, $options = array(), $attributes = array()) {
}
$out = array();
+ $hiddenField = isset($attributes['hiddenField']) ? $attributes['hiddenField'] : true;
+ unset($attributes['hiddenField']);
+
foreach ($options as $optValue => $optTitle) {
$optionsHere = array('value' => $optValue);
if (isset($value) && $optValue == $value) {
$optionsHere['checked'] = 'checked';
}
- $hiddenField = isset($attributes['hiddenField']) ? $attributes['hiddenField'] : true;
- unset($attributes['hiddenField']);
$parsedOptions = $this->_parseAttributes(
array_merge($attributes, $optionsHere),
array('name', 'type', 'id'), '', ' '
View
34 cake/tests/cases/libs/view/helpers/form.test.php
@@ -721,7 +721,7 @@ function tearDown() {
* @access public
* @return void
*/
- function testFormCreateWithSecurity() {
+ function testCreateWithSecurity() {
$this->Form->params['_Token'] = array('key' => 'testKey');
$encoding = strtolower(Configure::read('App.encoding'));
$result = $this->Form->create('Contact', array('url' => '/contacts/add'));
@@ -4691,12 +4691,12 @@ function testSubmitImage() {
}
/**
- * testFormCreate method
+ * test the create() method
*
* @access public
* @return void
*/
- function testFormCreate() {
+ function testCreate() {
$result = $this->Form->create('Contact');
$encoding = strtolower(Configure::read('App.encoding'));
$expected = array(
@@ -4736,6 +4736,18 @@ function testFormCreate() {
);
$this->assertTags($result, $expected);
+ $result = $this->Form->create('Contact', array('type' => 'file'));
+ $expected = array(
+ 'form' => array(
+ 'id' => 'ContactAddForm', 'method' => 'post', 'action' => '/contacts/add/',
+ 'accept-charset' => $encoding, 'enctype' => 'multipart/form-data'
+ ),
+ 'fieldset' => array('style' => 'preg:/display\s*\:\s*none;\s*/'),
+ 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
+ '/fieldset'
+ );
+ $this->assertTags($result, $expected);
+
$this->Form->data['Contact']['id'] = 1;
$this->Form->params['action'] = 'edit';
$result = $this->Form->create('Contact');
@@ -4750,6 +4762,20 @@ function testFormCreate() {
);
$this->assertTags($result, $expected);
+ $this->Form->data['Contact']['id'] = 1;
+ $this->Form->params['action'] = 'edit';
+ $result = $this->Form->create('Contact', array('type' => 'file'));
+ $expected = array(
+ 'form' => array(
+ 'id' => 'ContactEditForm', 'method' => 'post', 'action' => '/contacts/edit/1',
+ 'accept-charset' => $encoding, 'enctype' => 'multipart/form-data'
+ ),
+ 'fieldset' => array('style' => 'preg:/display\s*\:\s*none;\s*/'),
+ 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'PUT'),
+ '/fieldset'
+ );
+ $this->assertTags($result, $expected);
+
$this->Form->data['ContactNonStandardPk']['pk'] = 1;
$result = $this->Form->create('ContactNonStandardPk');
$expected = array(
@@ -4875,7 +4901,7 @@ function testCreateWithAcceptCharset() {
* Test base form url when url param is passed with multiple parameters (&)
*
*/
- function testFormCreateQuerystringParams() {
+ function testCreateQuerystringParams() {
$encoding = strtolower(Configure::read('App.encoding'));
$result = $this->Form->create('Contact', array(
'type' => 'post',

0 comments on commit ac302bb

Please sign in to comment.