From 2f579b64cb9cb8986489e312b253dba5061e304b Mon Sep 17 00:00:00 2001 From: Robbie Averill Date: Sun, 13 Aug 2017 21:46:47 +1200 Subject: [PATCH] FIX Files without extensions (folders) do not have a trailing period added --- filesystem/File.php | 7 +++++-- tests/filesystem/FileTest.php | 24 ++++++++++++++++++++++++ 2 files changed, 29 insertions(+), 2 deletions(-) diff --git a/filesystem/File.php b/filesystem/File.php index 6b66319ac..ff6d71b6b 100644 --- a/filesystem/File.php +++ b/filesystem/File.php @@ -634,7 +634,7 @@ class File extends DataObject { * * Does not change the filesystem itself, please use {@link write()} for this. * - * @param String $name + * @param string $name */ public function setName($name) { $oldName = $this->Name; @@ -663,7 +663,10 @@ class File extends DataObject { ))->first() ) { $suffix++; - $name = "$base-$suffix.$ext"; + $name = "$base-$suffix"; + if (!empty($ext)) { + $name .= ".$ext"; + } } } diff --git a/tests/filesystem/FileTest.php b/tests/filesystem/FileTest.php index b2ecf5e83..8e9fd0730 100644 --- a/tests/filesystem/FileTest.php +++ b/tests/filesystem/FileTest.php @@ -187,6 +187,30 @@ class FileTest extends SapphireTest { } } + /** + * Uses fixtures Folder.folder1 and File.setfromname + * @dataProvider setNameFileProvider + */ + public function testSetNameAddsUniqueSuffixWhenFilenameAlreadyExists($name, $expected) + { + $duplicate = new Folder; + $duplicate->setName($name); + $duplicate->write(); + + $this->assertSame($expected, $duplicate->Name); + } + + /** + * @return array[] + */ + public function setNameFileProvider() + { + return array( + array('FileTest-folder1', 'FileTest-folder1-2'), + array('FileTest.png', 'FileTest-2.png'), + ); + } + public function testLinkAndRelativeLink() { $file = $this->objFromFixture('File', 'asdf'); $this->assertEquals(ASSETS_DIR . '/FileTest.txt', $file->RelativeLink());