Skip to content

Commit

Permalink
Merge pull request #1424 from jim-parry/fix/filemovingtest
Browse files Browse the repository at this point in the history
Fix FileMovingTest leaving cruft
  • Loading branch information
jim-parry authored Nov 8, 2018
2 parents ec43078 + 8453a3c commit 6c86e11
Showing 1 changed file with 110 additions and 77 deletions.
187 changes: 110 additions & 77 deletions tests/system/HTTP/Files/FileMovingTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,21 +11,31 @@ public function setUp()
{
parent::setUp();

$this->root = vfsStream::setup();
$this->path = '_support/Files/';
$this->root = vfsStream::setup();
$this->path = '_support/Files/';
vfsStream::copyFromFileSystem(TESTPATH . $this->path, $this->root);
$this->start = $this->root->url() . '/';

$this->destination = $this->start . 'destination';
if (is_dir($this->destination))
{
rmdir($this->destination);
}

$_FILES = [];
}

public function tearDown()
{
parent::tearDown();

$this->root = null;
if (is_dir('/tmp/destination'))
rmdir('/tmp/destination');

// cleanup folder being left behind (why?)
$leftover = WRITEPATH . 'uploads/vfs:';
if (is_dir($leftover))
{
rrmdir($leftover);
}
}

//--------------------------------------------------------------------
Expand All @@ -35,19 +45,19 @@ public function testMove()
$finalFilename = 'fileA';

$_FILES = [
'userfile1' => [
'name' => $finalFilename . '.txt',
'type' => 'text/plain',
'size' => 124,
'tmp_name' => '/tmp/fileA.txt',
'error' => 0
'userfile1' => [
'name' => $finalFilename . '.txt',
'type' => 'text/plain',
'size' => 124,
'tmp_name' => '/tmp/fileA.txt',
'error' => 0,
],
'userfile2' => [
'name' => 'fileA.txt',
'type' => 'text/csv',
'size' => 248,
'tmp_name' => '/tmp/fileB.txt',
'error' => 0
'userfile2' => [
'name' => 'fileA.txt',
'type' => 'text/csv',
'size' => 248,
'tmp_name' => '/tmp/fileB.txt',
'error' => 0,
],
];

Expand All @@ -56,7 +66,7 @@ public function testMove()
$this->assertTrue($collection->hasFile('userfile1'));
$this->assertTrue($collection->hasFile('userfile2'));

$destination = $this->root->url() . '/destination';
$destination = $this->destination;

// Create the destination if not exists
is_dir($destination) || mkdir($destination, 0777, true);
Expand All @@ -78,26 +88,26 @@ public function testMoveOverwriting()
$finalFilename = 'file_with_delimiters_underscore';

$_FILES = [
'userfile1' => [
'name' => $finalFilename . '.txt',
'type' => 'text/plain',
'size' => 124,
'tmp_name' => '/tmp/fileA.txt',
'error' => 0
'userfile1' => [
'name' => $finalFilename . '.txt',
'type' => 'text/plain',
'size' => 124,
'tmp_name' => '/tmp/fileA.txt',
'error' => 0,
],
'userfile2' => [
'name' => $finalFilename . '.txt',
'type' => 'text/csv',
'size' => 248,
'tmp_name' => '/tmp/fileB.txt',
'error' => 0
'userfile2' => [
'name' => $finalFilename . '.txt',
'type' => 'text/csv',
'size' => 248,
'tmp_name' => '/tmp/fileB.txt',
'error' => 0,
],
'userfile3' => [
'name' => $finalFilename . '.txt',
'type' => 'text/csv',
'size' => 248,
'tmp_name' => '/tmp/fileC.txt',
'error' => 0
'userfile3' => [
'name' => $finalFilename . '.txt',
'type' => 'text/csv',
'size' => 248,
'tmp_name' => '/tmp/fileC.txt',
'error' => 0,
],
];

Expand All @@ -107,7 +117,7 @@ public function testMoveOverwriting()
$this->assertTrue($collection->hasFile('userfile2'));
$this->assertTrue($collection->hasFile('userfile3'));

$destination = $this->root->url() . '/destination';
$destination = $this->destination;

// Create the destination if not exists
is_dir($destination) || mkdir($destination, 0777, true);
Expand All @@ -132,19 +142,19 @@ public function testMoved()

$_FILES = [
'userfile1' => [
'name' => $finalFilename . '.txt',
'type' => 'text/plain',
'size' => 124,
'tmp_name' => '/tmp/fileA.txt',
'error' => 0
'name' => $finalFilename . '.txt',
'type' => 'text/plain',
'size' => 124,
'tmp_name' => '/tmp/fileA.txt',
'error' => 0,
],
];

$collection = new FileCollection();

$this->assertTrue($collection->hasFile('userfile1'));

$destination = $this->root->url() . '/destination';
$destination = $this->destination;

// Create the destination if not exists
is_dir($destination) || mkdir($destination, 0777, true);
Expand All @@ -165,27 +175,27 @@ public function testStore()

$_FILES = [
'userfile1' => [
'name' => $finalFilename . '.txt',
'type' => 'text/plain',
'size' => 124,
'tmp_name' => '/tmp/fileA.txt',
'error' => 0
'name' => $finalFilename . '.txt',
'type' => 'text/plain',
'size' => 124,
'tmp_name' => '/tmp/fileA.txt',
'error' => 0,
],
];

$collection = new FileCollection();

$this->assertTrue($collection->hasFile('userfile1'));

$destination = 'destination/';
$destination = $this->destination;

// Create the destination if not exists
is_dir($destination) || mkdir($destination, 0777, true);

$file = $collection->getFile('userfile1');

$this->assertInstanceOf(UploadedFile::class, $file);
$path = $file->store($destination, $file->getName(), false);
$path = $file->store($destination, $file->getName());
$this->assertEquals($destination . '/fileA.txt', $path);
}

Expand All @@ -197,19 +207,19 @@ public function testAlreadyMoved()

$_FILES = [
'userfile1' => [
'name' => $finalFilename . '.txt',
'type' => 'text/plain',
'size' => 124,
'tmp_name' => '/tmp/fileA.txt',
'error' => 0
'name' => $finalFilename . '.txt',
'type' => 'text/plain',
'size' => 124,
'tmp_name' => '/tmp/fileA.txt',
'error' => 0,
],
];

$collection = new FileCollection();

$this->assertTrue($collection->hasFile('userfile1'));

$destination = $this->root->url() . '/destination';
$destination = $this->destination;

// Create the destination if not exists
is_dir($destination) || mkdir($destination, 0777, true);
Expand All @@ -229,19 +239,18 @@ public function testInvalidFile()
{
$_FILES = [
'userfile' => [
'name' => 'someFile.txt',
'type' => 'text/plain',
'size' => '124',
'tmp_name' => '/tmp/myTempFile.txt',
'error' => UPLOAD_ERR_INI_SIZE
]
'name' => 'someFile.txt',
'type' => 'text/plain',
'size' => '124',
'tmp_name' => '/tmp/myTempFile.txt',
'error' => UPLOAD_ERR_INI_SIZE,
],
];

$destination = $this->root->url() . '/destination';
// don't create the folder, so setPath() is invoked.
$destination = $this->destination;

$collection = new FileCollection();
$file = $collection->getFile('userfile');
$collection = new FileCollection();
$file = $collection->getFile('userfile');

$this->expectException(HTTPException::class);
$file->move($destination, $file->getName(), false);
Expand All @@ -253,20 +262,21 @@ public function testFailedMove()
{
$_FILES = [
'userfile' => [
'name' => 'someFile.txt',
'type' => 'text/plain',
'size' => '124',
'tmp_name' => '/tmp/myTempFile.txt',
'error' => 0,
]
'name' => 'someFile.txt',
'type' => 'text/plain',
'size' => '124',
'tmp_name' => '/tmp/myTempFile.txt',
'error' => 0,
],
];

$destination = '/tmp/destination';
$destination = $this->destination;

// Create the destination and make it read only
is_dir($destination) || mkdir($destination, 0400, true);

$collection = new FileCollection();
$file = $collection->getFile('userfile');
$collection = new FileCollection();
$file = $collection->getFile('userfile');

$this->expectException(HTTPException::class);
$file->move($destination, $file->getName(), false);
Expand All @@ -284,7 +294,7 @@ public function testFailedMove()

function is_uploaded_file($filename)
{
if ( ! file_exists($filename))
if (! file_exists($filename))
{
file_put_contents($filename, 'data');
}
Expand All @@ -299,5 +309,28 @@ function is_uploaded_file($filename)

function move_uploaded_file($filename, $destination)
{
return copy($filename, $destination);
copy($filename, $destination);
unlink($filename);
}

function rrmdir($src)
{
$dir = opendir($src);
while (false !== ( $file = readdir($dir)))
{
if (( $file !== '.' ) && ( $file !== '..' ))
{
$full = $src . '/' . $file;
if (is_dir($full))
{
rrmdir($full);
}
else
{
unlink($full);
}
}
}
closedir($dir);
rmdir($src);
}

0 comments on commit 6c86e11

Please sign in to comment.