Skip to content

Commit

Permalink
Updated file() to use new widget.
Browse files Browse the repository at this point in the history
  • Loading branch information
ADmad committed Feb 19, 2014
1 parent f86063e commit e572156
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 27 deletions.
12 changes: 7 additions & 5 deletions src/View/Helper/FormHelper.php
Expand Up @@ -171,6 +171,7 @@ class FormHelper extends Helper {
protected $_defaultTemplates = [
'button' => '<button{{attrs}}>{{text}}</button>',
'checkbox' => '<input type="checkbox" name="{{name}}" value="{{value}}"{{attrs}}>',
'file' => '<input type="file" name="{{name}}"{{attrs}}>',
'formstart' => '<form{{attrs}}>',
'formend' => '</form>',
'hiddenblock' => '<div style="display:none;">{{content}}</div>',
Expand Down Expand Up @@ -492,7 +493,7 @@ public function secure($fields = array(), $secureAttributes = array()) {
}
$locked = array();
$unlockedFields = $this->_unlockedFields;

foreach ($fields as $key => $value) {
if (!is_int($key)) {
$locked[$key] = $value;
Expand Down Expand Up @@ -1522,14 +1523,15 @@ public function file($fieldName, $options = array()) {
$options['secure'] = static::SECURE_SKIP;

$options = $this->_initInputField($fieldName, $options);
$field = $this->entity();

foreach (array('name', 'type', 'tmp_name', 'error', 'size') as $suffix) {
$this->_secure($secure, array_merge($field, array($suffix)));
$this->_secure(
$secure,
$this->_secureFieldName(['name' => $options['name'] . '[' . $suffix . ']'])
);
}

$exclude = array('name' => null, 'value' => null);
return $this->Html->useTag('file', $options['name'], array_diff_key($options, $exclude));
return $this->widget('file', $options);
}

/**
Expand Down
3 changes: 2 additions & 1 deletion src/View/Widget/File.php
Expand Up @@ -54,7 +54,8 @@ public function render(array $data) {
'escape' => true,
];
unset($data['val']);
return $this->_templates->format('fileinput', [

return $this->_templates->format('file', [
'name' => $data['name'],
'attrs' => $this->_templates->formatAttributes(
$data, ['name', 'val']
Expand Down
34 changes: 14 additions & 20 deletions tests/TestCase/View/Helper/FormHelperTest.php
Expand Up @@ -1886,14 +1886,13 @@ public function testSecuredInputCustomName() {
* @return void
*/
public function testFormSecuredFileInput() {
$this->markTestIncomplete('Need to revisit once models work again.');
$this->Form->request->params['_csrfToken'] = 'testKey';
$this->assertEquals(array(), $this->Form->fields);

$this->Form->file('Attachment.file');
$expected = array(
'Attachment.file.name', 'Attachment.file.type', 'Attachment.file.tmp_name',
'Attachment.file.error', 'Attachment.file.size'
'Attachment.file.name', 'Attachment.file.type',
'Attachment.file.tmp_name', 'Attachment.file.error',
'Attachment.file.size'
);
$this->assertEquals($expected, $this->Form->fields);
}
Expand Down Expand Up @@ -6818,25 +6817,21 @@ public function testHiddenField() {
* @return void
*/
public function testFileUploadField() {
$this->markTestIncomplete('Need to revisit once models work again.');
$expected = ['input' => ['type' => 'file', 'name' => 'Model[upload]']];

$result = $this->Form->file('Model.upload');
$this->assertTags($result, array('input' => array('type' => 'file', 'name' => 'Model[upload]', 'id' => 'ModelUpload')));
$this->assertTags($result, $expected);

$this->Form->request->data['Model.upload'] = array("name" => "", "type" => "", "tmp_name" => "", "error" => 4, "size" => 0);
$result = $this->Form->input('Model.upload', array('type' => 'file'));
$expected = array(
'div' => array('class' => 'input file'),
'label' => array('for' => 'ModelUpload'),
'Upload',
'/label',
'input' => array('type' => 'file', 'name' => 'Model[upload]', 'id' => 'ModelUpload'),
'/div'
);
$this->Form->request->data['Model']['upload'] = [
'name' => '', 'type' => '', 'tmp_name' => '',
'error' => 4, 'size' => 0
];
$result = $this->Form->file('Model.upload');
$this->assertTags($result, $expected);

$this->Form->request->data['Model']['upload'] = 'no data should be set in value';
$result = $this->Form->file('Model.upload');
$this->assertTags($result, array('input' => array('type' => 'file', 'name' => 'Model[upload]', 'id' => 'ModelUpload')));
$this->assertTags($result, $expected);
}

/**
Expand All @@ -6845,11 +6840,10 @@ public function testFileUploadField() {
* @return void
*/
public function testFileUploadOnOtherModel() {
$this->markTestIncomplete('Need to revisit once models work again.');
$this->Form->create('ValidateUser', array('type' => 'file'));
$this->Form->create($this->article, array('type' => 'file'));
$result = $this->Form->file('ValidateProfile.city');
$expected = array(
'input' => array('type' => 'file', 'name' => 'ValidateProfile[city]', 'id' => 'ValidateProfileCity')
'input' => array('type' => 'file', 'name' => 'ValidateProfile[city]')
);
$this->assertTags($result, $expected);
}
Expand Down
2 changes: 1 addition & 1 deletion tests/TestCase/View/Widget/FileTest.php
Expand Up @@ -31,7 +31,7 @@ class FileTest extends TestCase {
public function setUp() {
parent::setUp();
$templates = [
'fileinput' => '<input type="file" name="{{name}}"{{attrs}}>',
'file' => '<input type="file" name="{{name}}"{{attrs}}>',
];
$this->templates = new StringTemplate($templates);
}
Expand Down

0 comments on commit e572156

Please sign in to comment.