Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Merge branch 'hoftix/multiple-muf-renameupload' of git://github.com/S…

…lamdunk/zf2 into Slamdunk-hoftix/multiple-muf-renameupload
  • Loading branch information...
commit ce355fb0a1591e1601a8ab47e69aec930a4645ca 2 parents 4fe0377 + a6c8187
@ralphschindler ralphschindler authored
View
23 library/Zend/Filter/File/RenameUpload.php
@@ -26,6 +26,15 @@ class RenameUpload extends AbstractFilter
);
/**
+ * Store already filtered values, so we can filter multiple
+ * times the same file without being block by move_uploaded_file
+ * internal checks
+ *
+ * @var array
+ */
+ protected $alreadyFiltered = array();
+
+ /**
* Constructor
*
* @param array|string $targetOrOptions The target file path or an options array
@@ -143,6 +152,10 @@ public function filter($value)
$sourceFile = $value;
}
+ if (isset($this->alreadyFiltered[$sourceFile])) {
+ return $this->alreadyFiltered[$sourceFile];
+ }
+
$targetFile = $this->getFinalTarget($uploadData);
if (!file_exists($sourceFile) || $sourceFile == $targetFile) {
return $value;
@@ -151,11 +164,15 @@ public function filter($value)
$this->checkFileExists($targetFile);
$this->moveUploadedFile($sourceFile, $targetFile);
+ $return = $targetFile;
if ($isFileUpload) {
- $uploadData['tmp_name'] = $targetFile;
- return $uploadData;
+ $return = $uploadData;
+ $return['tmp_name'] = $targetFile;
}
- return $targetFile;
+
+ $this->alreadyFiltered[$sourceFile] = $return;
+
+ return $return;
}
/**
View
25 tests/ZendTest/Filter/File/RenameUploadTest.php
@@ -249,14 +249,12 @@ public function testCannotOverwriteExistingFile()
*/
public function testGetRandomizedFile()
{
+ $fileNoExt = $this->_filesPath . '/newfile';
$filter = new RenameUploadMock(array(
'target' => $this->_newFile,
'randomize' => true,
));
- $this->assertEquals($this->_newFile, $filter->getTarget());
- $this->assertTrue($filter->getRandomize());
- $fileNoExt = $this->_filesPath . '/newfile';
$this->assertRegExp('#' . $fileNoExt . '_.{13}\.xml#', $filter($this->_oldFile));
}
@@ -271,8 +269,6 @@ public function testGetRandomizedFileWithoutExtension()
'randomize' => true,
));
- $this->assertEquals($fileNoExt, $filter->getTarget());
- $this->assertTrue($filter->getRandomize());
$this->assertRegExp('#' . $fileNoExt . '_.{13}#', $filter($this->_oldFile));
}
@@ -284,4 +280,23 @@ public function testInvalidConstruction()
$this->setExpectedException('\Zend\Filter\Exception\InvalidArgumentException', 'Invalid target');
$filter = new FileRenameUpload(1234);
}
+
+ /**
+ * @return void
+ */
+ public function testCanFilterMultipleTimesWithSameResult()
+ {
+ $filter = new RenameUploadMock(array(
+ 'target' => $this->_newFile,
+ 'randomize' => true,
+ ));
+
+ $firstResult = $filter($this->_oldFile);
+
+ $this->assertContains('newfile', $firstResult);
+
+ $secondResult = $filter($this->_oldFile);
+
+ $this->assertSame($firstResult, $secondResult);
+ }
}
Please sign in to comment.
Something went wrong with that request. Please try again.