Skip to content

Commit

Permalink
Add unit test for writing Folder titles
Browse files Browse the repository at this point in the history
  • Loading branch information
Luke Edwards committed Oct 7, 2018
1 parent 63cabc7 commit 1a08051
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 9 deletions.
11 changes: 2 additions & 9 deletions src/Folder.php
Original file line number Diff line number Diff line change
Expand Up @@ -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');
}

/**
Expand Down
13 changes: 13 additions & 0 deletions tests/php/FolderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -293,6 +293,19 @@ public function testTitleTiedToName()
$newFolder->Title = 'TestTitleWithIllegalCharactersCopiedToName <!BANG!>';
$this->assertEquals($newFolder->Name, $newFolder->Title);
$this->assertEquals($newFolder->Title, 'TestTitleWithIllegalCharactersCopiedToName <!BANG!>');

// 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()
Expand Down

0 comments on commit 1a08051

Please sign in to comment.