diff --git a/filesystem/Folder.php b/filesystem/Folder.php index 3b2dac629..3b8a8e04d 100755 --- a/filesystem/Folder.php +++ b/filesystem/Folder.php @@ -61,7 +61,8 @@ class Folder extends File { * Find the given folder or create it both as {@link Folder} database records * and on the filesystem. If necessary, creates parent folders as well. * - * @param $folderPath string Absolute or relative path to the file + * @param $folderPath string Absolute or relative path to the file. + * If path is relative, its interpreted relative to the "assets/" directory. * @return Folder */ static function findOrMake($folderPath) { @@ -74,10 +75,9 @@ class Folder extends File { $parts = explode("/",$folderPath); $parentID = 0; - + $item = null; 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(); @@ -91,6 +91,7 @@ class Folder extends File { } $parentID = $item->ID; } + return $item; } diff --git a/tests/filesystem/FolderTest.php b/tests/filesystem/FolderTest.php index b08012ca9..a1b03243d 100644 --- a/tests/filesystem/FolderTest.php +++ b/tests/filesystem/FolderTest.php @@ -43,11 +43,17 @@ class FolderTest extends SapphireTest { $this->assertNotNull($parentFolder); $this->assertEquals($parentFolder->ID, $folder->ParentID); - $path = '/FolderTest/testFindOrMake'; + $path = '/FolderTest/testFindOrMake'; // no trailing slash $folder = Folder::findOrMake($path); $this->assertEquals(ASSETS_DIR . $path . '/',$folder->getRelativePath(), 'Path information is correctly saved to database (without trailing slash)' ); + + $path = '/assets/'; // relative to "assets/" folder, should produce "assets/assets/" + $folder = Folder::findOrMake($path); + $this->assertEquals(ASSETS_DIR . $path,$folder->getRelativePath(), + 'A folder named "assets/" within "assets/" is allowed' + ); } /**