mirror of
https://github.com/silverstripe/silverstripe-framework
synced 2024-10-22 14:05:37 +02:00
BUGFIX Fixed Folder writing by overloading validate() (was inheriting File->validate() which does extension checks)
BUGFIX Fixed Folder::findOrMake() not to create "new-folder" through File->setName() if using a trailing slash in the path (which causes an empty name). Added FolderTest to verify this. (from r101266) git-svn-id: svn://svn.silverstripe.com/silverstripe/open/modules/sapphire/trunk@111984 467b73ca-7a2a-4603-9d3b-597d59a354a9
This commit is contained in:
parent
0d5a0e73fe
commit
767bfedd30
@ -1,10 +1,10 @@
|
||||
<?php
|
||||
/**
|
||||
* This class handles the representation of a File within Sapphire
|
||||
* Note: The files are stored in the "/assets/" directory, but sapphire
|
||||
* Note: The files are stored in the assets/ directory, but sapphire
|
||||
* looks at the db object to gather information about a file such as URL
|
||||
* It then uses this for all processing functions (like image manipulation).
|
||||
*
|
||||
* It then uses this for all processing functions (like image manipulation)
|
||||
* @package sapphire
|
||||
* @subpackage filesystem
|
||||
*/
|
||||
@ -443,6 +443,11 @@ class File extends DataObject {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns
|
||||
*
|
||||
* @return String
|
||||
*/
|
||||
function getRelativePath() {
|
||||
if($this->ParentID) {
|
||||
$p = DataObject::get_by_id('Folder', $this->ParentID);
|
||||
|
@ -1,6 +1,7 @@
|
||||
<?php
|
||||
/**
|
||||
* Represents a folder in the assets directory.
|
||||
* Represents a folder in the assets/ directory.
|
||||
*
|
||||
* @package sapphire
|
||||
* @subpackage filesystem
|
||||
*/
|
||||
@ -54,11 +55,13 @@ class Folder extends File {
|
||||
$folderPath = trim(Director::makeRelative($folderPath));
|
||||
// replace leading and trailing slashes
|
||||
$folderPath = preg_replace('/^\/?(.*)\/?$/', '$1', $folderPath);
|
||||
|
||||
$parts = explode("/",$folderPath);
|
||||
|
||||
$parentID = 0;
|
||||
|
||||
foreach($parts as $part) {
|
||||
if(!$part) continue; // happens for paths with a trailing slash
|
||||
|
||||
$item = DataObject::get_one("Folder", "\"Name\" = '$part' AND \"ParentID\" = $parentID");
|
||||
if(!$item) {
|
||||
$item = new Folder();
|
||||
@ -271,6 +274,9 @@ class Folder extends File {
|
||||
}
|
||||
}
|
||||
|
||||
function validate() {
|
||||
return new ValidationResult(true);
|
||||
}
|
||||
|
||||
//-------------------------------------------------------------------------------------------------
|
||||
// Data Model Definition
|
||||
|
36
tests/filesystem/FolderTest.php
Normal file
36
tests/filesystem/FolderTest.php
Normal file
@ -0,0 +1,36 @@
|
||||
<?php
|
||||
/**
|
||||
* @author Ingo Schommer (ingo at silverstripe dot com)
|
||||
* @todo There's currently no way to save outside of assets/ folder
|
||||
*
|
||||
* @package sapphire
|
||||
* @subpackage tests
|
||||
*/
|
||||
class FolderTest extends SapphireTest {
|
||||
|
||||
function tearDown() {
|
||||
$testPath = ASSETS_PATH . '/FolderTest';
|
||||
if(file_exists($testPath)) Filesystem::removeFolder($testPath);
|
||||
|
||||
parent::tearDown();
|
||||
}
|
||||
|
||||
function testFindOrMake() {
|
||||
$path = '/FolderTest/testFindOrMake/';
|
||||
$folder = Folder::findOrMake($path);
|
||||
$this->assertEquals(ASSETS_DIR . $path,$folder->getRelativePath(),
|
||||
'Nested path information is correctly saved to database (with trailing slash)'
|
||||
);
|
||||
|
||||
$this->assertTrue(file_exists(ASSETS_PATH . $path), 'File');
|
||||
$parentFolder = DataObject::get_one('Folder', '"Name" = \'FolderTest\'');
|
||||
$this->assertNotNull($parentFolder);
|
||||
$this->assertEquals($parentFolder->ID, $folder->ParentID);
|
||||
|
||||
$path = '/FolderTest/testFindOrMake';
|
||||
$folder = Folder::findOrMake($path);
|
||||
$this->assertEquals(ASSETS_DIR . $path . '/',$folder->getRelativePath(),
|
||||
'Path information is correctly saved to database (without trailing slash)'
|
||||
);
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user