Skip to content

Commit

Permalink
Test allowedExtensions in UploadField, return correct HTTP status
Browse files Browse the repository at this point in the history
  • Loading branch information
chillu committed Jul 12, 2013
1 parent c2c8498 commit 920edf8
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 1 deletion.
2 changes: 2 additions & 0 deletions forms/UploadField.php
Expand Up @@ -525,6 +525,7 @@ public function upload(SS_HTTPRequest $request) {
}

// Get the uploaded file into a new file object.
// The loadIntoFile() method also validates constraints like allowed extensions
try {
$this->upload->loadIntoFile($tmpfile, $fileObject, $this->folderName);
} catch (Exception $e) {
Expand Down Expand Up @@ -559,6 +560,7 @@ public function upload(SS_HTTPRequest $request) {
}
$response = new SS_HTTPResponse(Convert::raw2json(array($return)));
$response->addHeader('Content-Type', 'text/plain');
if($return['error']) $response->setStatusCode(403);
return $response;
}

Expand Down
33 changes: 32 additions & 1 deletion tests/forms/uploadfield/UploadFieldTest.php
Expand Up @@ -123,6 +123,33 @@ public function testUploadManyManyRelation() {
$this->assertEquals($record->ManyManyFiles()->Last()->Name, $tmpFileName);
}

/**
* Partially covered by {@link UploadTest->testUploadAcceptsAllowedExtension()},
* but this test additionally verifies that those constraints are actually enforced
* in this controller method.
*/
public function testAllowedExtensions() {
$this->loginWithPermission('ADMIN');

$invalidFile = 'invalid.php';
$_FILES = array('AllowedExtensionsField' => $this->getUploadFile($invalidFile));
$response = $this->post(
'UploadFieldTest_Controller/Form/field/AllowedExtensionsField/upload',
array('AllowedExtensionsField' => $this->getUploadFile($invalidFile))
);
$this->assertTrue($response->isError());
$this->assertContains('Extension is not allowed', $response->getBody());

$validFile = 'valid.jpg';
$_FILES = array('AllowedExtensionsField' => $this->getUploadFile($validFile));
$response = $this->post(
'UploadFieldTest_Controller/Form/field/AllowedExtensionsField/upload',
array('AllowedExtensionsField' => $this->getUploadFile($validFile))
);
$this->assertFalse($response->isError());
$this->assertNotContains('Extension is not allowed', $response->getBody());
}

public function testAllowedMaxFileNumberWithHasOne() {
$this->loginWithPermission('ADMIN');

Expand Down Expand Up @@ -831,6 +858,9 @@ public function Form() {
$fieldCanAttachExisting->setConfig('canAttachExisting', false);
$fieldCanAttachExisting->setRecord($record);

$fieldAllowedExtensions = new UploadField('AllowedExtensionsField');
$fieldAllowedExtensions->getValidator()->setAllowedExtensions(array('jpg'));

$form = new Form(
$this,
'Form',
Expand All @@ -847,7 +877,8 @@ public function Form() {
$fieldDisabled,
$fieldSubfolder,
$fieldCanUploadFalse,
$fieldCanAttachExisting
$fieldCanAttachExisting,
$fieldAllowedExtensions
),
new FieldList(
new FormAction('submit')
Expand Down

0 comments on commit 920edf8

Please sign in to comment.