Skip to content

Commit

Permalink
Minor refactoring.
Browse files Browse the repository at this point in the history
Adding tests for form->create() with file type forms.
  • Loading branch information
markstory committed Oct 31, 2009
1 parent 15da4a7 commit ac302bb
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 7 deletions.
7 changes: 4 additions & 3 deletions cake/libs/view/helpers/form.php
Expand Up @@ -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']])
);

Expand All @@ -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,
Expand Down Expand Up @@ -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'), '', ' '
Expand Down
34 changes: 30 additions & 4 deletions cake/tests/cases/libs/view/helpers/form.test.php
Expand Up @@ -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'));
Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -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');
Expand All @@ -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(
Expand Down Expand Up @@ -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',
Expand Down

0 comments on commit ac302bb

Please sign in to comment.