Merge pull request #901 from halkyon/filenamefilter_underscores_fix

BUG FileNameFilter should remove any amount of underscores from start of...
This commit is contained in:
Stig Lindqvist 2012-11-01 13:51:51 -07:00
commit a29bca3aa4
2 changed files with 56 additions and 2 deletions

View File

@ -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 File

@ -54,4 +54,58 @@ class FileNameFilterTest extends SapphireTest {
);
}
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));
}
function testDotsStartOfNameRemoved() {
$name = '.test.txt';
$filter = new FileNameFilter();
$this->assertEquals('test.txt', $filter->filter($name));
}
function testDoubleDotsStartOfNameRemoved() {
$name = '..test.txt';
$filter = new FileNameFilter();
$this->assertEquals('test.txt', $filter->filter($name));
}
function testMixedInvalidCharsStartOfNameRemoved() {
$name = '..#@$#@$^__test.txt';
$filter = new FileNameFilter();
$this->assertEquals('test.txt', $filter->filter($name));
}
function testWhitespaceRemoved() {
$name = ' test doc.txt';
$filter = new FileNameFilter();
$this->assertEquals('test-doc.txt', $filter->filter($name));
}
function testUnderscoresReplacedWithDashes() {
$name = 'test_doc.txt';
$filter = new FileNameFilter();
$this->assertEquals('test-doc.txt', $filter->filter($name));
}
function testNonAsciiCharsReplacedWithDashes() {
$name = '!@#$%^test_123@##@$#%^.txt';
$filter = new FileNameFilter();
$this->assertEquals('test-123.txt', $filter->filter($name));
}
function testDuplicateDashesRemoved() {
$name = 'test--document.txt';
$filter = new FileNameFilter();
$this->assertEquals('test-document.txt', $filter->filter($name));
}
}