From 1a3897ab1a51c9ab8d84774a940deb5fbbf5c135 Mon Sep 17 00:00:00 2001 From: Sam Minnee Date: Sun, 5 Dec 2010 08:35:06 +0000 Subject: [PATCH] ENHANCEMENT Validation for uploaded files (from r113420) git-svn-id: svn://svn.silverstripe.com/silverstripe/open/modules/sapphire/trunk@114534 467b73ca-7a2a-4603-9d3b-597d59a354a9 --- tests/forms/FileFieldTest.php | 80 +++++++++++++++++++++++++++++++++++ 1 file changed, 80 insertions(+) create mode 100644 tests/forms/FileFieldTest.php diff --git a/tests/forms/FileFieldTest.php b/tests/forms/FileFieldTest.php new file mode 100644 index 000000000..b9630739c --- /dev/null +++ b/tests/forms/FileFieldTest.php @@ -0,0 +1,80 @@ + 'aCV.txt', + 'type' => 'application/octet-stream', + 'tmp_name' => '/private/var/tmp/phpzTQbqP', + 'error' => 0, + 'size' => 3471 + ); + $fileField->setValue($fileFieldValue); + + $this->assertTrue( + $form->validate() + ); + } + + /** + * Test different scenarii for a failed upload : an error occured, no files where provided + */ + public function testUploadMissingRequiredFile() { + $form = new Form( + new Controller(), + 'Form', + new FieldSet( + $fileField = new FileField('cv', 'Upload your CV') + ), + new FieldSet(), + new RequiredFields('cv') + ); + // All fields are filled but for some reason an error occured when uploading the file => fails + $fileFieldValue = array( + 'name' => 'aCV.txt', + 'type' => 'application/octet-stream', + 'tmp_name' => '/private/var/tmp/phpzTQbqP', + 'error' => 1, + 'size' => 3471 + ); + $fileField->setValue($fileFieldValue); + + $this->assertFalse( + $form->validate(), + 'An error occured when uploading a file, but the validator returned true' + ); + + // We pass an empty set of parameters for the uploaded file => fails + $fileFieldValue = array(); + $fileField->setValue($fileFieldValue); + + $this->assertFalse( + $form->validate(), + 'An empty array was passed as parameter for an uploaded file, but the validator returned true' + ); + + // We pass an null value for the uploaded file => fails + $fileFieldValue = null; + $fileField->setValue($fileFieldValue); + + $this->assertFalse( + $form->validate(), + 'A null value was passed as parameter for an uploaded file, but the validator returned true' + ); + } +} \ No newline at end of file