You can not test for file upload when you do not really upload a file.

When you give no file you receive the fileUploadErrorFileNotFound error.
When you manipulate the $_FILES array you will get an Attacker error from PHP itself. When you set the content manually the file component misses the other needed informations and will also raise an exception.

But the attacker exception is the last validation which is made:
So you can do the following to test your code:
Manipulate the $_FILES array so it looks like an real upload.
And when you get an Attacker exception all must be ok, because it tried to finish the file upload by moving the file, which failed because of YOUR manipulation.

All other validations are made BEFORE.

Greetings
Thomas Weidner, I18N Team Leader, Zend Framework
http://www.thomasweidner.com

----- Original Message ----- From: "Ralf Eggert" <[EMAIL PROTECTED]>
To: <[email protected]>
Sent: Thursday, August 21, 2008 10:14 AM
Subject: [fw-general] How to simulate a file upload to test form


Hi,

I want to test a form with PHPUnIt which contains several fields and a
file element. Here is the very simplified test case (simplified means I
left out a couple of form elements):

   public function testValidatingCorrectInput()
   {
       // create instance of form
       $form = new EmployerEditForm();

       // define input data
       $inputData = array(
           'employer_id'              => '1',
           'employer_type'            => 'standard',
           'employer_company'         => 'Musterfirma',
           'submit_employer_edit'     => 'submit_employer_edit',
       );

       // define expected erros
       $expectedErrors = array(
           'employer_id' => array(
           ),
           'employer_type' => array(
           ),
           'employer_company' => array(
           ),
           'employer_logo' => array(
           ),
           'submit_employer_edit' => array(
           ),
       );

       // check if form is valid
       $this->assertTrue($form->isValid($inputData));
   }

This test fails and when I check the form errors with $form->getErrors()
I get the result that employer_logo has an error called
"fileUploadErrorFileNotFound". I tried to mess around with $_FILES
before the test is run to simulare an uploaded file but then I get the
error "fileUploadErrorAttack". I also set $inputData['employer_logo'] to
the path of a test image, but then I get an exception:

 Zend_File_Transfer_Exception: "employer_logo" not found by
 file transfer adapter

So what is the best way to test the isValid() method of a form which
contains a file element?

Thanks and best regards,

Ralf

Reply via email to