Skip to content
This repository has been archived by the owner on Jan 30, 2020. It is now read-only.

Commit

Permalink
Merge branch 'hotfix/3708' into develop
Browse files Browse the repository at this point in the history
  • Loading branch information
Show file tree
Hide file tree
Showing 3 changed files with 79 additions and 96 deletions.
24 changes: 18 additions & 6 deletions src/File/RenameUpload.php
Original file line number Diff line number Diff line change
Expand Up @@ -149,22 +149,34 @@ public function filter($value)
}

$this->checkFileExists($targetFile);
$this->moveUploadedFile($sourceFile, $targetFile);

if ($isFileUpload) {
$uploadData['tmp_name'] = $targetFile;
return $uploadData;
}
return $targetFile;
}

/**
* @param string $sourceFile Source file path
* @param string $targetFile Target file path
* @throws \Zend\Filter\Exception\RuntimeException
* @return boolean
*/
protected function moveUploadedFile($sourceFile, $targetFile)
{
ErrorHandler::start();
$result = move_uploaded_file($sourceFile, $targetFile);
$warningException = ErrorHandler::stop();
if (!$result || null !== $warningException) {
throw new Exception\RuntimeException(
sprintf("File '%s' could not be renamed. An error occurred while processing the file.", $value),
sprintf("File '%s' could not be renamed. An error occurred while processing the file.", $sourceFile),
0, $warningException
);
}

if ($isFileUpload) {
$uploadData['tmp_name'] = $targetFile;
return $uploadData;
}
return $targetFile;
return $result;
}

/**
Expand Down
25 changes: 25 additions & 0 deletions test/File/RenameUploadMock.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php
/**
* Zend Framework (http://framework.zend.com/)
*
* @link http://github.com/zendframework/zf2 for the canonical source repository
* @copyright Copyright (c) 2005-2013 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
*/

namespace ZendTest\Filter\File;

use Zend\Filter\File\RenameUpload;

class RenameUploadMock extends RenameUpload
{
/**
* @param string $sourceFile Source file path
* @param string $targetFile Target file path
* @return boolean
*/
protected function moveUploadedFile($sourceFile, $targetFile)
{
return rename($sourceFile, $targetFile);
}
}
126 changes: 36 additions & 90 deletions test/File/RenameUploadTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,6 @@ class RenameUploadTest extends \PHPUnit_Framework_TestCase
*/
protected $_filesPath;

/**
* Original testfile
*
* @var string
*/
protected $_origFile;

/**
* Testfile
*
Expand Down Expand Up @@ -69,26 +62,19 @@ class RenameUploadTest extends \PHPUnit_Framework_TestCase
*/
public function setUp()
{
$this->_filesPath = dirname(__DIR__) . DIRECTORY_SEPARATOR . '_files' . DIRECTORY_SEPARATOR;
$this->_origFile = $this->_filesPath . 'original.file';
$this->_oldFile = $this->_filesPath . 'testfile.txt';
$this->_newFile = $this->_filesPath . 'newfile.xml';
$this->_newDir = $this->_filesPath . DIRECTORY_SEPARATOR . '_testDir2';
$this->_newDirFile = $this->_newDir . DIRECTORY_SEPARATOR . 'testfile.txt';

if (file_exists($this->_origFile)) {
unlink($this->_origFile);
}
$this->_filesPath = dirname(__DIR__) . DIRECTORY_SEPARATOR . '_files' . DIRECTORY_SEPARATOR . 'RenameUploadTest';
$this->_newDir = $this->_filesPath . DIRECTORY_SEPARATOR . '_testDir2';

if (file_exists($this->_newFile)) {
unlink($this->_newFile);
}
$this->tearDown();

if (file_exists($this->_newDirFile)) {
unlink($this->_newDirFile);
}
mkdir($this->_filesPath);
mkdir($this->_newDir);

$this->_oldFile = $this->_filesPath . '/testfile.txt';
$this->_newFile = $this->_filesPath . '/newfile.xml';
$this->_newDirFile = $this->_newDir . '/testfile.txt';

copy($this->_oldFile, $this->_origFile);
touch($this->_oldFile);
}

/**
Expand All @@ -98,28 +84,23 @@ public function setUp()
*/
public function tearDown()
{
if (!file_exists($this->_oldFile)) {
copy($this->_origFile, $this->_oldFile);
}

if (file_exists($this->_origFile)) {
unlink($this->_origFile);
}
$this->removeDir($this->_newDir);
$this->removeDir($this->_filesPath);
}

if (file_exists($this->_newFile)) {
unlink($this->_newFile);
protected function removeDir($dir)
{
if (! is_dir($dir)) {
return;
}

if (file_exists($this->_newDirFile)) {
unlink($this->_newDirFile);
foreach (glob($dir . DIRECTORY_SEPARATOR . '*') as $file) {
if (is_file($file)) {
unlink($file);
}
}

if (function_exists("runkit_function_rename")
&& function_exists('move_uploaded_file_orig')
) {
runkit_function_rename('move_uploaded_file', 'move_uploaded_file_mock');
runkit_function_rename('move_uploaded_file_orig', 'move_uploaded_file');
}
rmdir($dir);
}

/**
Expand All @@ -138,25 +119,6 @@ public function testThrowsExceptionWithNonUploadedFile()
$this->assertEquals($this->_newFile, $filter($this->_oldFile));
}

/**
* Mock the move_uploaded_file() function with rename() functionality
*
* @return void
*/
protected function setUpMockMoveUploadedFile()
{
if (!function_exists("runkit_function_rename")
|| !ini_get('runkit.internal_override')
) {
$this->markTestSkipped(
'move_uploaded_file cannot be unit tested without runkit module'
);
return;
}
runkit_function_rename('move_uploaded_file', 'move_uploaded_file_orig');
runkit_function_rename('move_uploaded_file_mock', 'move_uploaded_file');
}

/**
* @return void
*/
Expand Down Expand Up @@ -185,9 +147,7 @@ public function testOptions()
*/
public function testStringConstructorParam()
{
$this->setUpMockMoveUploadedFile();

$filter = new FileRenameUpload($this->_newFile);
$filter = new RenameUploadMock($this->_newFile);
$this->assertEquals($this->_newFile, $filter->getTarget());
$this->assertEquals($this->_newFile, $filter($this->_oldFile));
$this->assertEquals('falsefile', $filter('falsefile'));
Expand All @@ -198,9 +158,7 @@ public function testStringConstructorParam()
*/
public function testStringConstructorWithFilesArray()
{
$this->setUpMockMoveUploadedFile();

$filter = new FileRenameUpload($this->_newFile);
$filter = new RenameUploadMock($this->_newFile);
$this->assertEquals($this->_newFile, $filter->getTarget());
$this->assertEquals(
array(
Expand All @@ -220,9 +178,7 @@ public function testStringConstructorWithFilesArray()
*/
public function testArrayConstructorParam()
{
$this->setUpMockMoveUploadedFile();

$filter = new FileRenameUpload(array(
$filter = new RenameUploadMock(array(
'target' => $this->_newFile,
));
$this->assertEquals($this->_newFile, $filter->getTarget());
Expand All @@ -246,9 +202,7 @@ public function testConstructTruncatedTarget()
*/
public function testTargetDirectory()
{
$this->setUpMockMoveUploadedFile();

$filter = new FileRenameUpload($this->_newDir);
$filter = new RenameUploadMock($this->_newDir);
$this->assertEquals($this->_newDir, $filter->getTarget());
$this->assertEquals($this->_newDirFile, $filter($this->_oldFile));
$this->assertEquals('falsefile', $filter('falsefile'));
Expand All @@ -259,27 +213,23 @@ public function testTargetDirectory()
*/
public function testOverwriteWithExistingFile()
{
$this->setUpMockMoveUploadedFile();

$filter = new FileRenameUpload(array(
$filter = new RenameUploadMock(array(
'target' => $this->_newFile,
'overwrite' => true,
));

copy($this->_oldFile, $this->_newFile);

$this->assertEquals($this->_newFile, $filter->getTarget());
$this->assertEquals($this->_newFile, $filter->filter($this->_oldFile));
$this->assertEquals($this->_newFile, $filter($this->_oldFile));
}

/**
* @return void
*/
public function testCannotOverwriteExistingFile()
{
$this->setUpMockMoveUploadedFile();

$filter = new FileRenameUpload(array(
$filter = new RenameUploadMock(array(
'target' => $this->_newFile,
'overwrite' => false,
));
Expand All @@ -291,43 +241,39 @@ public function testCannotOverwriteExistingFile()
$this->setExpectedException(
'Zend\Filter\Exception\InvalidArgumentException', 'already exists'
);
$this->assertEquals($this->_newFile, $filter->filter($this->_oldFile));
$this->assertEquals($this->_newFile, $filter($this->_oldFile));
}

/**
* @return void
*/
public function testGetRandomizedFile()
{
$this->setUpMockMoveUploadedFile();

$filter = new FileRenameUpload(array(
$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->getNewName($this->_oldFile));
$fileNoExt = $this->_filesPath . '/newfile';
$this->assertRegExp('#' . $fileNoExt . '_.{13}\.xml#', $filter($this->_oldFile));
}

/**
* @return void
*/
public function testGetRandomizedFileWithoutExtension()
{
$this->setUpMockMoveUploadedFile();

$fileNoExt = $this->_filesPath . 'newfile';
$filter = new FileRenameUpload(array(
$fileNoExt = $this->_filesPath . '/newfile';
$filter = new RenameUploadMock(array(
'target' => $fileNoExt,
'randomize' => true,
));

$this->assertEquals($fileNoExt, $filter->getTarget());
$this->assertTrue($filter->getRandomize());
$this->assertRegExp('#' . $fileNoExt . '_.{13}#', $filter->getNewName($this->_oldFile));
$this->assertRegExp('#' . $fileNoExt . '_.{13}#', $filter($this->_oldFile));
}

/**
Expand Down

0 comments on commit c3425d2

Please sign in to comment.