Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix beforeMarshall if file uploads are array #588

Merged
merged 2 commits into from
Oct 18, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion src/Model/Behavior/UploadBehavior.php
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,13 @@ public function beforeMarshal(EventInterface $event, ArrayObject $data, ArrayObj
if (!$validator->isEmptyAllowed($field, false)) {
continue;
}
if (!empty($dataArray[$field]) && $dataArray[$field]->getError() !== UPLOAD_ERR_NO_FILE) {
if (
!empty($dataArray[$field]) &&
($dataArray[$field] instanceof UploadedFileInterface
? $dataArray[$field]->getError()
: $dataArray[$field]['error']
) !== UPLOAD_ERR_NO_FILE
) {
continue;
}
if (isset($data[$field])) {
Expand Down
49 changes: 49 additions & 0 deletions tests/TestCase/Model/Behavior/UploadBehaviorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
use Josegonzalez\Upload\Model\Behavior\UploadBehavior;
use Josegonzalez\Upload\Test\Stub\ChildBehavior;
use Laminas\Diactoros\UploadedFile;
use Psr\Http\Message\UploadedFileInterface;
use ReflectionClass;

class UploadBehaviorTest extends TestCase
Expand Down Expand Up @@ -258,6 +259,40 @@ public function testBeforeMarshalEmptyAllowed()
$this->assertEquals(new ArrayObject($this->dataError), $data);
}

public function testBeforeMarshalDataAsArray()
{
$validator = $this->getMockBuilder('Cake\Validation\Validator')->getMock();
$validator->expects($this->atLeastOnce())
->method('isEmptyAllowed')
->will($this->returnValue(true));

$table = $this->getMockBuilder('Cake\ORM\Table')->getMock();
$table->expects($this->atLeastOnce())
->method('getValidator')
->will($this->returnValue($validator));

$methods = array_diff($this->behaviorMethods, ['beforeMarshal']);
$behavior = $this->getMockBuilder('Josegonzalez\Upload\Model\Behavior\UploadBehavior')
->onlyMethods($methods)
->setConstructorArgs([$table, $this->settings])
->getMock();
$behavior->expects($this->any())
->method('getConfig')
->will($this->returnValue($this->settings));

$data = new ArrayObject(
$this->transformUploadedFilesToArray($this->dataOk)
);
$behavior->beforeMarshal(new Event('fake.event'), $data, new ArrayObject());
$this->assertEquals(new ArrayObject($this->transformUploadedFilesToArray($this->dataOk)), $data);

$data = new ArrayObject(
$this->transformUploadedFilesToArray($this->dataError)
);
$behavior->beforeMarshal(new Event('fake.event'), $data, new ArrayObject());
$this->assertEquals(new ArrayObject([]), $data);
}

public function testBeforeSaveNoUpload()
{
$originalValue = rand(1000, 9999);
Expand Down Expand Up @@ -763,4 +798,18 @@ public function testNameCallback()

$this->assertEquals($expected, $behavior->constructedFiles);
}

private function transformUploadedFilesToArray(array $data): array
{
return array_map(
fn(UploadedFileInterface $file) => [
'tmp_name' => '',
'error' => $file->getError(),
'name' => $file->getClientFilename(),
'type' => $file->getClientMediaType(),
'size' => $file->getSize(),
],
$data
);
}
Comment on lines +804 to +814
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Parse error: syntax error, unexpected '$file' (T_VARIABLE), expecting ',' or ')' in /home/runner/work/cakephp-upload/cakephp-upload/tests/TestCase/Model/Behavior/UploadBehaviorTest.php on line 805

To fix the above error:

Suggested change
return array_map(
fn(UploadedFileInterface $file) => [
'tmp_name' => '',
'error' => $file->getError(),
'name' => $file->getClientFilename(),
'type' => $file->getClientMediaType(),
'size' => $file->getSize(),
],
$data
);
}
return array_map(
function (UploadedFileInterface $file) {
return [
'tmp_name' => '',
'error' => $file->getError(),
'name' => $file->getClientFilename(),
'type' => $file->getClientMediaType(),
'size' => $file->getSize(),
];
},
$data
);
}

}