From 1ce279ec9dfd19669b05c2b5222958a78dcf9f94 Mon Sep 17 00:00:00 2001 From: Sean Harvey Date: Mon, 29 Oct 2012 17:07:58 +1300 Subject: [PATCH] BUG FileNameFilter should remove any amount of underscores from start of filename When a user renames a file to "__test.txt" (two underscores or more), then FileNameFilter will only remove the very first underscore from the filename. This is not sufficient, as any number of underscores in the filename will be problematic when Filesystem::sync() is called, it will remove that File record thinking it's an internal file. This fixes it so any number of underscores are stripped out at the start of the filename. --- filesystem/FileNameFilter.php | 2 +- tests/filesystem/FileNameFilterTest.php | 14 +++++++++++++- 2 files changed, 14 insertions(+), 2 deletions(-) diff --git a/filesystem/FileNameFilter.php b/filesystem/FileNameFilter.php index 0341795ea..d1a000f12 100644 --- a/filesystem/FileNameFilter.php +++ b/filesystem/FileNameFilter.php @@ -41,7 +41,7 @@ class FileNameFilter extends Object { '/_/' => '-', // underscores to dashes '/[^A-Za-z0-9+.-]+/' => '', // remove non-ASCII chars, only allow alphanumeric plus dash and dot '/[\-]{2,}/' => '-', // remove duplicate dashes - '/^[\.\-_]/' => '', // Remove all leading dots, dashes or underscores + '/^[\.\-_]+/' => '', // Remove all leading dots, dashes or underscores ); /** diff --git a/tests/filesystem/FileNameFilterTest.php b/tests/filesystem/FileNameFilterTest.php index eba6ed175..371f4dcdf 100644 --- a/tests/filesystem/FileNameFilterTest.php +++ b/tests/filesystem/FileNameFilterTest.php @@ -53,5 +53,17 @@ class FileNameFilterTest extends SapphireTest { strlen($result) ); } - + + function testUnderscoresStartOfNameRemoved() { + $name = '_test.txt'; + $filter = new FileNameFilter(); + $this->assertEquals('test.txt', $filter->filter($name)); + } + + function testDoubleUnderscoresStartOfNameRemoved() { + $name = '__test.txt'; + $filter = new FileNameFilter(); + $this->assertEquals('test.txt', $filter->filter($name)); + } + }