Permalink
Browse files

Merge pull request #2903 from tractorcow/pulls/fix-fileupload-fileexists

BUG Fix issues with uploading to /assets
  • Loading branch information...
2 parents bdd936d + c1e0f98 commit 279b927df1c0bb291c7d57e0e8836c27b3694364 @simonwelsh simonwelsh committed Mar 15, 2014
Showing with 63 additions and 3 deletions.
  1. +3 −1 filesystem/Upload.php
  2. +3 −1 forms/UploadField.php
  3. +57 −1 tests/forms/uploadfield/UploadFieldTest.php
View
4 filesystem/Upload.php
@@ -134,7 +134,9 @@ public function load($tmpFile, $folderPath = false) {
$file = $nameFilter->filter($tmpFile['name']);
$fileName = basename($file);
- $relativeFilePath = $parentFolder ? $parentFolder->getRelativePath() . "$fileName" : $fileName;
+ $relativeFilePath = $parentFolder
+ ? $parentFolder->getRelativePath() . "$fileName"
+ : ASSETS_DIR . "/" . $fileName;
// Create a new file record (or try to retrieve an existing one)
if(!$this->file) {
View
4 forms/UploadField.php
@@ -1276,7 +1276,9 @@ public function fileexists(SS_HTTPRequest $request) {
// Resolve expected folder name
$folderName = $this->getFolderName();
$folder = Folder::find_or_make($folderName);
- $parentPath = BASE_PATH."/".$folder->getFilename();
+ $parentPath = $folder
+ ? BASE_PATH."/".$folder->getFilename()
+ : ASSETS_PATH."/";
// check if either file exists
$exists = false;
View
58 tests/forms/uploadfield/UploadFieldTest.php
@@ -643,6 +643,45 @@ public function testSelect() {
$this->assertNotContains($fileSubfolder->ID, $itemIDs, 'Does not contain file in subfolder');
}
+ /**
+ * Tests that UploadField::fileexist works
+ */
+ public function testFileExists() {
+ $this->loginWithPermission('ADMIN');
+
+ // Check that fileexist works on subfolders
+ $nonFile = uniqid().'.txt';
+ $responseEmpty = $this->mockFileExists('NoRelationField', $nonFile);
+ $responseEmptyData = json_decode($responseEmpty->getBody());
+ $this->assertFalse($responseEmpty->isError());
+ $this->assertFalse($responseEmptyData->exists);
+
+ // Check that filexists works on root folder
+ $responseRoot = $this->mockFileExists('RootFolderTest', $nonFile);
+ $responseRootData = json_decode($responseRoot->getBody());
+ $this->assertFalse($responseRoot->isError());
+ $this->assertFalse($responseRootData->exists);
+
+ // Check that uploaded files can be detected in the root
+ $tmpFileName = 'testUploadBasic.txt';
+ $response = $this->mockFileUpload('RootFolderTest', $tmpFileName);
+ $this->assertFalse($response->isError());
+ $this->assertFileExists(ASSETS_PATH . "/$tmpFileName");
+ $responseExists = $this->mockFileExists('RootFolderTest', $tmpFileName);
+ $responseExistsData = json_decode($responseExists->getBody());
+ $this->assertFalse($responseExists->isError());
+ $this->assertTrue($responseExistsData->exists);
+
+ // Check that uploaded files can be detected
+ $response = $this->mockFileUpload('NoRelationField', $tmpFileName);
+ $this->assertFalse($response->isError());
+ $this->assertFileExists(ASSETS_PATH . "/UploadFieldTest/$tmpFileName");
+ $responseExists = $this->mockFileExists('NoRelationField', $tmpFileName);
+ $responseExistsData = json_decode($responseExists->getBody());
+ $this->assertFalse($responseExists->isError());
+ $this->assertTrue($responseExistsData->exists);
+ }
+
protected function getMockForm() {
return new Form(new Controller(), 'Form', new FieldList(), new FieldList());
}
@@ -717,6 +756,12 @@ protected function mockFileUpload($fileField, $tmpFileName) {
);
}
+ protected function mockFileExists($fileField, $fileName) {
+ return $this->get(
+ "UploadFieldTest_Controller/Form/field/{$fileField}/fileexists?filename=".urlencode($fileName)
+ );
+ }
+
/**
* Simulates a physical file deletion
*
@@ -773,7 +818,14 @@ public function tearDown() {
}
// Remove left over folders and any files that may exist
- if(file_exists('../assets/UploadFieldTest')) Filesystem::removeFolder('../assets/UploadFieldTest');
+ if(file_exists(ASSETS_PATH.'/UploadFieldTest')) {
+ Filesystem::removeFolder(ASSETS_PATH.'/UploadFieldTest');
+ }
+
+ // Remove file uploaded to root folder
+ if(file_exists(ASSETS_PATH.'/testUploadBasic.txt')) {
+ unlink(ASSETS_PATH.'/testUploadBasic.txt');
+ }
}
}
@@ -859,6 +911,9 @@ function __construct($controller = null, $name = 'Form') {
if(empty($controller)) {
$controller = new UploadFieldTest_Controller();
}
+
+ $fieldRootFolder = UploadField::create('RootFolderTest')
+ ->setFolderName('/');
$fieldNoRelation = UploadField::create('NoRelationField')
->setFolderName('UploadFieldTest');
@@ -911,6 +966,7 @@ function __construct($controller = null, $name = 'Form') {
$fieldAllowedExtensions->getValidator()->setAllowedExtensions(array('txt'));
$fields = new FieldList(
+ $fieldRootFolder,
$fieldNoRelation,
$fieldHasOne,
$fieldHasOneMaxOne,

0 comments on commit 279b927

Please sign in to comment.