First my code:
class ProfileController extends Zend_Controller_Action {
private function getUploadAvatarForm()
{
$form = new Zend_Form;
$form->setAttrib('enctype', 'multipart/form-data');
$form->setAction($this->view->baseUrl('/profile/upload-avatar'))
->setName('uploadAvatarForm')
->setMethod('POST');
$element = new Zend_Form_Element_File('avatar');
$element->setLabel('Upload an image:')
->addFilter('Rename',
array('target' => '/path/toimages/directory/' . $this->userId . '.png',
'overwrite' => true)
);
$element->addValidator('Count', false, 1);
$element->addValidator('Size', false, 102400);
$element->addValidator('Extension', false, 'png');
$form->addElement($element, 'avatar')
->addElement('submit', 'submit_upload', array('label' => 'upload'));
return $form;
}
public function uploadAvatarAction()
{
$form = $this->getUploadAvatarForm();
$this->view->form = $form;
if (!$this->getRequest()->isPost() || !$form->isValid($_POST)) {
return;
}
if (!$form->avatar->receive()) {
... error...
}
... ok ...
}
}
The question is: part of buissiness logic is placed into Rename filter. In my vision it worth it, but may be I'm wrong.
I'm trying to test it:
public function testUploadFile()
{
$this->_doLogin('user', 'password');
$this->getRequest()
->setMethod('POST'));
$this->mockFileUpload();
$this->dispatch('profile/upload-avatar');
var_dump($this->getResponse()->getBody());
}
private function mockFileUpload()
{
$_FILES = array(
'avatar' => array(
'name' => 'test.png',
'type' => 'image/png',
'tmp_name' => '/tmp/test.png',
'error' => 0,
'size' => 10127));
}
but got:
The file 'avatar' was illegal uploaded, possible attack
Could you please suggest me how to test this situation? (With filesystem everything will be ok - I'm going to use vfsStream for that, so the problem is only to emulate post upload)