BUGFIX Moving folder after executing Folder::findOrMake will not set the Filenames properly. Invoking updateFilesystem() in File->onAfterWrite() instead of onBeforeWrite(), and avoid caching in FIle->getRelativePath() (fixes #5994 and #5937, thanks muzdowski)

git-svn-id: svn://svn.silverstripe.com/silverstripe/open/modules/sapphire/branches/2.4@111493 467b73ca-7a2a-4603-9d3b-597d59a354a9
This commit is contained in:
Ingo Schommer 2010-10-03 09:57:46 +00:00 committed by Sam Minnee
parent 6230d5bddd
commit 47fd9be0e0
2 changed files with 43 additions and 6 deletions

View File

@ -340,18 +340,22 @@ class File extends DataObject {
}
/**
* Event handler called before deleting from the database.
* You can overload this to clean up or otherwise process data before delete this
* record.
* Make sure the file has a name
*/
protected function onBeforeWrite() {
parent::onBeforeWrite();
// Set default name
if(!$this->getField('Name')) $this->Name = "new-" . strtolower($this->class);
}
/**
* Set name on filesystem. If the current object is a "Folder", will also update references
* to subfolders and contained file records (both in database and filesystem)
*/
protected function onAfterWrite() {
parent::onAfterWrite();
// Set name on filesystem. If the current object is a "Folder", will also update references
// to subfolders and contained file records (both in database and filesystem)
$this->updateFilesystem();
}
@ -574,7 +578,7 @@ class File extends DataObject {
*/
function getRelativePath() {
if($this->ParentID) {
$p = DataObject::get_by_id('Folder', $this->ParentID);
$p = DataObject::get_by_id('Folder', $this->ParentID, false); // Don't use the cache, the parent has just been changed
if($p && $p->exists()) return $p->getRelativePath() . $this->getField("Name");
else return ASSETS_DIR . "/" . $this->getField("Name");
} else if($this->getField("Name")) {

View File

@ -120,6 +120,39 @@ class FolderTest extends SapphireTest {
$this->assertFileNotExists($oldPathFolder1, 'Old path is removed after write()');
$this->assertFileExists($folder1->getFullPath(), 'New path is created after write()');
}
/**
* Tests for the bug #5994 - Moving folder after executing Folder::findOrMake will not set the Filenames properly
*/
function testFindOrMakeFolderThenMove() {
$folder1 = $this->objFromFixture('Folder', 'folder1');
Folder::findOrMake($folder1->Filename);
$folder2 = $this->objFromFixture('Folder', 'folder2');
// set ParentID
$folder1->ParentID = $folder2->ID;
$folder1->write();
// Check if the file in the folder moved along
$file1 = DataObject::get_by_id('File', $this->idFromFixture('File', 'file1-folder1'), false);
$this->assertFileExists($file1->getFullPath());
$this->assertEquals($file1->Filename, 'assets/FileTest-folder2/FileTest-folder1/File1.txt', 'The file DataObject has updated path');
}
/**
* Tests for the bug #5994 - if you don't execute get_by_id prior to the rename or move, it will fail.
*/
function testRenameFolderAndCheckTheFile() {
$folder1 = DataObject::get_one('Folder', 'ID='.$this->idFromFixture('Folder', 'folder1'));
$folder1->Name = 'FileTest-folder1-changed';
$folder1->write();
// Check if the file in the folder moved along
$file1 = DataObject::get_by_id('File', $this->idFromFixture('File', 'file1-folder1'), false);
$this->assertFileExists($file1->getFullPath());
$this->assertEquals($file1->Filename, 'assets/FileTest-folder1-changed/File1.txt', 'The file DataObject path uses renamed folder');
}
/**
* @see FileTest->testLinkAndRelativeLink()