Skip to content
Permalink
Browse files

Implement withUploadedFiles

Include validation to ensure only valid files get added.
  • Loading branch information...
markstory committed Sep 4, 2016
1 parent be47f25 commit 7f43e3d80813d56739133fdb79f18c4919c898c6
Showing with 87 additions and 1 deletion.
  1. +37 −0 src/Network/Request.php
  2. +50 −1 tests/TestCase/Network/RequestTest.php
@@ -1701,6 +1701,43 @@ public function getUploadedFiles()
return $this->uploadedFiles;
}
/**
* Update the request replacing the files, and creating a new instance.
*
* @param array $files An array of uploaded file objects.
* @return static
* @throws InvalidArgumentException when $files contains an invalid object.
*/
public function withUploadedFiles(array $files)
{
$this->validateUploadedFiles($files, '');
$new = clone $this;
$new->uploadedFiles = $files;
return $new;
}
/**
* Recursively validate uploaded file data.
*
* @param array $uploadedFiles
* @param string $path The path thus far.
* @throws InvalidArgumentException If any leaf elements are not valid files.
*/
private function validateUploadedFiles(array $uploadedFiles, $path)
{
foreach ($uploadedFiles as $key => $file) {
if (is_array($file)) {
$this->validateUploadedFiles($file, $key . '.');
continue;
}
if (!$file instanceof UploadedFileInterface) {
throw new InvalidArgumentException("Invalid file at '{$path}{$key}'");
}
}
}
/**
* Array access read implementation
*
@@ -19,6 +19,7 @@
use Cake\Network\Request;
use Cake\Network\Session;
use Cake\TestSuite\TestCase;
use Zend\Diactoros\UploadedFile;
/**
* TestRequest
@@ -467,7 +468,7 @@ public function testFilesNested()
*
* @return void
*/
public function testProcessFilesFlat()
public function testFilesFlat()
{
$files = [
'birth_cert' => [
@@ -527,6 +528,54 @@ public function testFilesZeroithIndex()
$this->assertEquals($files[0]['name'], $uploads[0]->getClientFilename());
}
/**
* Test replacing files.
*
* @return void
*/
public function testWithUploadedFiles()
{
$file = new UploadedFile(
__FILE__,
123,
UPLOAD_ERR_OK,
'test.php',
'text/plain'
);
$request = new Request();
$new = $request->withUploadedFiles(['picture' => $file]);
$this->assertSame([], $request->getUploadedFiles());
$this->assertNotSame($new, $request);
$this->assertSame(['picture' => $file], $new->getUploadedFiles());
}
/**
* Test replacing files with an invalid file
*
* @expectedException InvalidArgumentException
* @expectedExceptionMessage Invalid file at 'avatar'
* @return void
*/
public function testWithUploadedFilesInvalidFile()
{
$request = new Request();
$request->withUploadedFiles(['avatar' => 'not a file']);
}
/**
* Test replacing files with an invalid file
*
* @expectedException InvalidArgumentException
* @expectedExceptionMessage Invalid file at 'user.avatar'
* @return void
*/
public function testWithUploadedFilesInvalidFileNested()
{
$request = new Request();
$request->withUploadedFiles(['user' => ['avatar' => 'not a file']]);
}
/**
* Test method overrides coming in from POST data.
*

0 comments on commit 7f43e3d

Please sign in to comment.
You can’t perform that action at this time.