Skip to content

Commit

Permalink
Keep the file extension in the temporary copy and test that it exists (
Browse files Browse the repository at this point in the history
…closes #7482)
  • Loading branch information
thewilkybarkid authored and fabpot committed Mar 27, 2013
1 parent b898b13 commit 4cf06c1
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 3 deletions.
12 changes: 9 additions & 3 deletions src/Symfony/Component/DomCrawler/Field/FileFormField.php
Expand Up @@ -59,11 +59,17 @@ public function setValue($value)
if (null !== $value && is_readable($value)) {
$error = UPLOAD_ERR_OK;
$size = filesize($value);
$name = basename($value);
$info = pathinfo($value);
$name = $info['basename'];

// copy to a tmp location
$tmp = tempnam(sys_get_temp_dir(), 'upload');
unlink($tmp);
$tmp = sys_get_temp_dir().'/'.sha1(uniqid(mt_rand(), true));
if (array_key_exists('extension', $info)) {
$tmp .= '.'.$info['extension'];
}
if (is_file($tmp)) {
unlink($tmp);
}
copy($value, $tmp);
$value = $tmp;
} else {
Expand Down
18 changes: 18 additions & 0 deletions src/Symfony/Component/DomCrawler/Tests/Field/FileFormFieldTest.php
Expand Up @@ -56,8 +56,26 @@ public function testSetValue($method)
$this->assertEquals(basename(__FILE__), $value['name'], "->$method() sets the name of the file field");
$this->assertEquals('', $value['type'], "->$method() sets the type of the file field");
$this->assertInternalType('string', $value['tmp_name'], "->$method() sets the tmp_name of the file field");
$this->assertFileExists($value['tmp_name'], "->$method() creates a copy of the file at the tmp_name path");
$this->assertEquals(0, $value['error'], "->$method() sets the error of the file field");
$this->assertEquals(filesize(__FILE__), $value['size'], "->$method() sets the size of the file field");

$origInfo = pathinfo(__FILE__);
$tmpInfo = pathinfo($value['tmp_name']);
$this->assertEquals(
$origInfo['extension'],
$tmpInfo['extension'],
"->$method() keeps the same file extension in the tmp_name copy"
);

$field->$method(__DIR__.'/../Fixtures/no-extension');
$value = $field->getValue();

$this->assertArrayNotHasKey(
'extension',
pathinfo($value['tmp_name']),
"->$method() does not add a file extension in the tmp_name copy"
);
}

public function getSetValueMethods()
Expand Down
@@ -0,0 +1 @@
Test

0 comments on commit 4cf06c1

Please sign in to comment.