From 63cabc7fc84f295a95e88f2ce37f940b61b97223 Mon Sep 17 00:00:00 2001 From: Luke Edwards Date: Tue, 2 Oct 2018 08:55:00 +1300 Subject: [PATCH 1/2] BUG Keep folder Name and Title in sync on update --- src/Folder.php | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/src/Folder.php b/src/Folder.php index 2d74e00d..925962d1 100644 --- a/src/Folder.php +++ b/src/Folder.php @@ -90,6 +90,20 @@ public function onBeforeDelete() parent::onBeforeDelete(); } + public function onBeforeWrite() + { + // Make sure Title is updated if Name is chaged (as we keep these in sync for folders) + if ($this->isChanged('Name')) { + $name = $this->Name; + $this->setField( + 'Title', + str_replace(array('-','_'), ' ', preg_replace('/\.[^.]+$/', '', $name)) + ); + } + + parent::onBeforeWrite(); + } + /** * Return the relative URL of an icon for this file type * From 1a08051c495a680a715383fca5a4d71a0fe43331 Mon Sep 17 00:00:00 2001 From: Luke Edwards Date: Mon, 8 Oct 2018 11:29:04 +1300 Subject: [PATCH 2/2] Add unit test for writing Folder titles --- src/Folder.php | 11 ++--------- tests/php/FolderTest.php | 13 +++++++++++++ 2 files changed, 15 insertions(+), 9 deletions(-) diff --git a/src/Folder.php b/src/Folder.php index 925962d1..5e2f06ac 100644 --- a/src/Folder.php +++ b/src/Folder.php @@ -92,16 +92,9 @@ public function onBeforeDelete() public function onBeforeWrite() { - // Make sure Title is updated if Name is chaged (as we keep these in sync for folders) - if ($this->isChanged('Name')) { - $name = $this->Name; - $this->setField( - 'Title', - str_replace(array('-','_'), ' ', preg_replace('/\.[^.]+$/', '', $name)) - ); - } - parent::onBeforeWrite(); + + $this->Title = $this->getField('Name'); } /** diff --git a/tests/php/FolderTest.php b/tests/php/FolderTest.php index b9623c8f..6f26e1d7 100644 --- a/tests/php/FolderTest.php +++ b/tests/php/FolderTest.php @@ -293,6 +293,19 @@ public function testTitleTiedToName() $newFolder->Title = 'TestTitleWithIllegalCharactersCopiedToName '; $this->assertEquals($newFolder->Name, $newFolder->Title); $this->assertEquals($newFolder->Title, 'TestTitleWithIllegalCharactersCopiedToName '); + + // Title should be populated from name on first write + $writeFolder = new Folder(); + $writeFolder->Name = 'TestNameWrittenToTitle'; + $writeFolder->write(); + $newFolderWritten = Folder::get_one(Folder::class, "\"Title\" = 'TestNameWrittenToTitle'"); + $this->assertNotNull($newFolderWritten); + + // Title should be populated from name on subsequent writes + $writeFolder->Name = 'TestNameWrittenToTitleOnUpdate'; + $writeFolder->write(); + $newFolderWritten = Folder::get_one(Folder::class, "\"Title\" = 'TestNameWrittenToTitleOnUpdate'"); + $this->assertNotNull($newFolderWritten); } public function testRootFolder()