mirror of
https://github.com/silverstripe/silverstripe-framework
synced 2024-10-22 12:05:37 +00:00
MINOR Made File::get_file_extension() more readable, and added unit test (from r107267)
git-svn-id: svn://svn.silverstripe.com/silverstripe/open/modules/sapphire/trunk@112555 467b73ca-7a2a-4603-9d3b-597d59a354a9
This commit is contained in:
parent
6cf5372bf4
commit
96d1625102
@ -598,12 +598,19 @@ class File extends DataObject {
|
|||||||
/**
|
/**
|
||||||
* Gets the extension of a filepath or filename,
|
* Gets the extension of a filepath or filename,
|
||||||
* by stripping away everything before the last "dot".
|
* by stripping away everything before the last "dot".
|
||||||
|
* Caution: Only returns the last extension in "double-barrelled"
|
||||||
|
* extensions (e.g. "gz" for "tar.gz").
|
||||||
|
*
|
||||||
|
* Examples:
|
||||||
|
* - "myfile" returns ""
|
||||||
|
* - "myfile.txt" returns "txt"
|
||||||
|
* - "myfile.tar.gz" returns "gz"
|
||||||
*
|
*
|
||||||
* @param string $filename
|
* @param string $filename
|
||||||
* @return string
|
* @return string
|
||||||
*/
|
*/
|
||||||
public static function get_file_extension($filename) {
|
public static function get_file_extension($filename) {
|
||||||
return strtolower(substr($filename,strrpos($filename,'.')+1));
|
return pathinfo($filename, PATHINFO_EXTENSION);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -737,8 +744,9 @@ class File extends DataObject {
|
|||||||
|
|
||||||
function validate() {
|
function validate() {
|
||||||
if(File::$apply_restrictions_to_admin || !Permission::check('ADMIN')) {
|
if(File::$apply_restrictions_to_admin || !Permission::check('ADMIN')) {
|
||||||
$extension = strtolower(pathinfo($this->Name, PATHINFO_EXTENSION));
|
// Extension validation
|
||||||
|
// TODO Merge this with Upload_Validator
|
||||||
|
$extension = $this->getExtension();
|
||||||
if($extension && !in_array($extension, self::$allowed_extensions)) {
|
if($extension && !in_array($extension, self::$allowed_extensions)) {
|
||||||
$exts = self::$allowed_extensions;
|
$exts = self::$allowed_extensions;
|
||||||
sort($exts);
|
sort($exts);
|
||||||
|
@ -4,21 +4,35 @@
|
|||||||
* Tests for the File class
|
* Tests for the File class
|
||||||
*/
|
*/
|
||||||
class FileTest extends SapphireTest {
|
class FileTest extends SapphireTest {
|
||||||
|
|
||||||
static $fixture_file = 'sapphire/tests/filesystem/FileTest.yml';
|
static $fixture_file = 'sapphire/tests/filesystem/FileTest.yml';
|
||||||
|
|
||||||
|
function testGetExtension() {
|
||||||
|
$this->assertEquals('', File::get_file_extension('myfile'), 'No extension');
|
||||||
|
$this->assertEquals('txt', File::get_file_extension('myfile.txt'), 'Simple extension');
|
||||||
|
$this->assertEquals('gz', File::get_file_extension('myfile.tar.gz'), 'Double-barrelled extension only returns last bit');
|
||||||
|
}
|
||||||
|
|
||||||
function testValidateExtension() {
|
function testValidateExtension() {
|
||||||
$file = $this->objFromFixture('File', 'asdf');
|
Session::set('loggedInAs', null);
|
||||||
|
|
||||||
// Invalid
|
$origExts = File::$allowed_extensions;
|
||||||
|
File::$allowed_extensions = array('txt');
|
||||||
|
|
||||||
|
$file = $this->objFromFixture('File', 'asdf');
|
||||||
|
|
||||||
|
// Invalid ext
|
||||||
$file->Name = 'asdf.php';
|
$file->Name = 'asdf.php';
|
||||||
$v = $file->validate();
|
$v = $file->validate();
|
||||||
$this->assertFalse($v->valid());
|
$this->assertFalse($v->valid());
|
||||||
$this->assertContains('Extension is not allowed', $v->message());
|
$this->assertContains('Extension is not allowed', $v->message());
|
||||||
|
|
||||||
// Valid
|
// Valid ext
|
||||||
$file->Name = 'asdf.txt';
|
$file->Name = 'asdf.txt';
|
||||||
$v = $file->validate();
|
$v = $file->validate();
|
||||||
$this->assertTrue($v->valid());
|
$this->assertTrue($v->valid());
|
||||||
|
|
||||||
|
File::$allowed_extensions = $origExts;
|
||||||
}
|
}
|
||||||
|
|
||||||
function testLinkAndRelativeLink() {
|
function testLinkAndRelativeLink() {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user