diff --git a/filesystem/File.php b/filesystem/File.php index 8cdda3d11..cd9c417c3 100755 --- a/filesystem/File.php +++ b/filesystem/File.php @@ -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")) { diff --git a/tests/filesystem/FolderTest.php b/tests/filesystem/FolderTest.php index a1b03243d..32730c5b0 100644 --- a/tests/filesystem/FolderTest.php +++ b/tests/filesystem/FolderTest.php @@ -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()