Skip to content

Commit

Permalink
Gracefully handle missing uploaded files in upload action
Browse files Browse the repository at this point in the history
Resolves #142
  • Loading branch information
mzur committed Dec 28, 2018
1 parent 668cbf3 commit b02d3dc
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 6 deletions.
2 changes: 1 addition & 1 deletion src/Actions/UploadAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ protected function handleFile($field, $options)
{
$file = $this->form->data($field);

if (!is_array($file)) {
if (!is_array($file) || !isset($file['error']) || intval($file['error']) !== UPLOAD_ERR_OK) {
// If this is an array, kirby-form already recognized and validated the
// uploaded file. If the file is required, this should have been checked
// during validation.
Expand Down
24 changes: 19 additions & 5 deletions tests/Actions/UploadActionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ public function setUp()

public function testSkipMissingFile()
{
$this->form->data('testfield', ['error' => UPLOAD_ERR_NO_FILE]);
$action = new UploadActionStub($this->form, ['fields' => ['testfield' => []]]);
$action->perform();
$this->assertNull($action->source);
Expand All @@ -26,7 +27,10 @@ public function testSkipMissingFile()

public function testTargetRequired()
{
$this->form->data('testfield', ['name' => 'myfile.txt']);
$this->form->data('testfield', [
'name' => 'myfile.txt',
'error' => UPLOAD_ERR_OK,
]);
$action = new UploadActionStub($this->form, ['fields' => ['testfield' => []]]);
$this->expectException(PerformerException::class, 'target directory is missing');
$action->perform();
Expand All @@ -36,7 +40,10 @@ public function testTargetDirIsFile()
{
$path = $this->dir.'/uniform_abc123';
touch($path);
$this->form->data('testfield', ['name' => 'myfile.txt']);
$this->form->data('testfield', [
'name' => 'myfile.txt',
'error' => UPLOAD_ERR_OK,
]);
$action = new UploadActionStub($this->form, ['fields' => [
'testfield' => ['target' => $path],
]]);
Expand All @@ -53,7 +60,10 @@ public function testTargetFileIsFile()
$path = $this->dir.'/uniform_abc123';
@mkdir($path);
touch("{$path}/myfile.txt");
$this->form->data('testfield', ['name' => 'myfile.txt']);
$this->form->data('testfield', [
'name' => 'myfile.txt',
'error' => UPLOAD_ERR_OK,
]);
$action = new UploadActionStub($this->form, ['fields' => [
'testfield' => ['target' => $path, 'prefix' => false],
]]);
Expand All @@ -72,7 +82,10 @@ public function testTargetFileIsFileWithPrefix()
$path = $this->dir.'/uniform_abc123';
@mkdir($path);
touch("{$path}/prefixmyfile.txt");
$this->form->data('testfield', ['name' => 'myfile.txt']);
$this->form->data('testfield', [
'name' => 'myfile.txt',
'error' => UPLOAD_ERR_OK,
]);
$action = new UploadActionStub($this->form, ['fields' => [
'testfield' => ['target' => $path, 'prefix' => 'prefix'],
]]);
Expand All @@ -90,7 +103,8 @@ public function testHandleRollback()
{
$this->form->data('testfield', [
'tmp_name' => $this->dir.'/uniform_123abc',
'name' => 'myfile.txt'
'name' => 'myfile.txt',
'error' => UPLOAD_ERR_OK,
]);
$action = new UploadActionStub($this->form, ['fields' => [
'testfield' => ['target' => $this->dir.'/uniform_test'],
Expand Down

0 comments on commit b02d3dc

Please sign in to comment.