Browse files

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.
  • Loading branch information...
1 parent 624f427 commit 1ce279ec9dfd19669b05c2b5222958a78dcf9f94 @halkyon halkyon committed Oct 29, 2012
Showing with 14 additions and 2 deletions.
  1. +1 −1 filesystem/FileNameFilter.php
  2. +13 −1 tests/filesystem/FileNameFilterTest.php
View
2 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
);
/**
View
14 tests/filesystem/FileNameFilterTest.php
@@ -53,5 +53,17 @@ public function testFilterWithEmptyString() {
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));
+ }
+
}

0 comments on commit 1ce279e

Please sign in to comment.