Skip to content

Commit

Permalink
Implement withUploadedFiles
Browse files Browse the repository at this point in the history
Include validation to ensure only valid files get added.
  • Loading branch information
markstory committed Sep 4, 2016
1 parent be47f25 commit 7f43e3d
Show file tree
Hide file tree
Showing 2 changed files with 87 additions and 1 deletion.
37 changes: 37 additions & 0 deletions src/Network/Request.php
Expand Up @@ -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
*
Expand Down
51 changes: 50 additions & 1 deletion tests/TestCase/Network/RequestTest.php
Expand Up @@ -19,6 +19,7 @@
use Cake\Network\Request;
use Cake\Network\Session;
use Cake\TestSuite\TestCase;
use Zend\Diactoros\UploadedFile;

/**
* TestRequest
Expand Down Expand Up @@ -467,7 +468,7 @@ public function testFilesNested()
*
* @return void
*/
public function testProcessFilesFlat()
public function testFilesFlat()
{
$files = [
'birth_cert' => [
Expand Down Expand Up @@ -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.
*
Expand Down

0 comments on commit 7f43e3d

Please sign in to comment.