2008-08-25 03:42:27 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Tests for the File class
|
|
|
|
*/
|
|
|
|
class FileTest extends SapphireTest {
|
|
|
|
static $fixture_file = 'sapphire/tests/filesystem/FileTest.yml';
|
|
|
|
|
|
|
|
function testLinkAndRelativeLink() {
|
|
|
|
$file = $this->objFromFixture('File', 'asdf');
|
ENHANCEMENT Introduced constants for system paths like /sapphire in preparation for a more flexible directory reorganisation. Instead of hardcoding your path, please use the following constants: BASE_PATH, BASE_URL, SAPPHIRE_DIR, SAPPHIRE_PATH, CMS_DIR, CMS_PATH, THIRDPARTY_DIR, THIRDPARTY_PATH, ASSETS_DIR, ASSETS_PATH, THEMES_DIR, THEMES_PATH
git-svn-id: svn://svn.silverstripe.com/silverstripe/open/modules/sapphire/trunk@63154 467b73ca-7a2a-4603-9d3b-597d59a354a9
2008-09-27 18:02:38 +02:00
|
|
|
$this->assertEquals(ASSETS_DIR . '/asdfjkl.txt', $file->RelativeLink());
|
|
|
|
$this->assertEquals(Director::baseURL() . ASSETS_DIR . '/asdfjkl.txt', $file->Link());
|
2008-08-25 03:42:27 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
function testNameAndTitleGeneration() {
|
|
|
|
/* If objects are loaded into the system with just a Filename, then Name is generated but Title isn't */
|
|
|
|
$file = $this->objFromFixture('File', 'asdf');
|
|
|
|
$this->assertEquals('asdfjkl.txt', $file->Name);
|
|
|
|
$this->assertNull($file->Title);
|
|
|
|
|
|
|
|
/* However, if Name is set instead of Filename, then Title is set */
|
|
|
|
$file = $this->objFromFixture('File', 'setfromname');
|
ENHANCEMENT Introduced constants for system paths like /sapphire in preparation for a more flexible directory reorganisation. Instead of hardcoding your path, please use the following constants: BASE_PATH, BASE_URL, SAPPHIRE_DIR, SAPPHIRE_PATH, CMS_DIR, CMS_PATH, THIRDPARTY_DIR, THIRDPARTY_PATH, ASSETS_DIR, ASSETS_PATH, THEMES_DIR, THEMES_PATH
git-svn-id: svn://svn.silverstripe.com/silverstripe/open/modules/sapphire/trunk@63154 467b73ca-7a2a-4603-9d3b-597d59a354a9
2008-09-27 18:02:38 +02:00
|
|
|
$this->assertEquals(ASSETS_DIR . '/asdfjkl.png', $file->Filename);
|
2008-08-25 03:42:27 +02:00
|
|
|
$this->assertEquals('asdfjkl', $file->Title);
|
|
|
|
}
|
|
|
|
|
|
|
|
function testChangingNameAndFilenameAndParentID() {
|
|
|
|
$file = $this->objFromFixture('File', 'asdf');
|
|
|
|
|
|
|
|
/* If you alter the Name attribute of a file, then the filesystem is also affected */
|
|
|
|
$file->Name = 'asdfjkl2.txt';
|
|
|
|
clearstatcache();
|
ENHANCEMENT Introduced constants for system paths like /sapphire in preparation for a more flexible directory reorganisation. Instead of hardcoding your path, please use the following constants: BASE_PATH, BASE_URL, SAPPHIRE_DIR, SAPPHIRE_PATH, CMS_DIR, CMS_PATH, THIRDPARTY_DIR, THIRDPARTY_PATH, ASSETS_DIR, ASSETS_PATH, THEMES_DIR, THEMES_PATH
git-svn-id: svn://svn.silverstripe.com/silverstripe/open/modules/sapphire/trunk@63154 467b73ca-7a2a-4603-9d3b-597d59a354a9
2008-09-27 18:02:38 +02:00
|
|
|
$this->assertFileNotExists(ASSETS_PATH . "/asdfjkl.txt");
|
|
|
|
$this->assertFileExists(ASSETS_PATH . "/asdfjkl2.txt");
|
2008-08-25 03:42:27 +02:00
|
|
|
/* The Filename field is also updated */
|
ENHANCEMENT Introduced constants for system paths like /sapphire in preparation for a more flexible directory reorganisation. Instead of hardcoding your path, please use the following constants: BASE_PATH, BASE_URL, SAPPHIRE_DIR, SAPPHIRE_PATH, CMS_DIR, CMS_PATH, THIRDPARTY_DIR, THIRDPARTY_PATH, ASSETS_DIR, ASSETS_PATH, THEMES_DIR, THEMES_PATH
git-svn-id: svn://svn.silverstripe.com/silverstripe/open/modules/sapphire/trunk@63154 467b73ca-7a2a-4603-9d3b-597d59a354a9
2008-09-27 18:02:38 +02:00
|
|
|
$this->assertEquals(ASSETS_DIR . '/asdfjkl2.txt', $file->Filename);
|
2008-08-25 03:42:27 +02:00
|
|
|
|
|
|
|
/* However, if you alter the Filename attribute, the the filesystem isn't affected. Altering Filename directly isn't
|
|
|
|
recommended */
|
ENHANCEMENT Introduced constants for system paths like /sapphire in preparation for a more flexible directory reorganisation. Instead of hardcoding your path, please use the following constants: BASE_PATH, BASE_URL, SAPPHIRE_DIR, SAPPHIRE_PATH, CMS_DIR, CMS_PATH, THIRDPARTY_DIR, THIRDPARTY_PATH, ASSETS_DIR, ASSETS_PATH, THEMES_DIR, THEMES_PATH
git-svn-id: svn://svn.silverstripe.com/silverstripe/open/modules/sapphire/trunk@63154 467b73ca-7a2a-4603-9d3b-597d59a354a9
2008-09-27 18:02:38 +02:00
|
|
|
$file->Filename = ASSETS_DIR . '/asdfjkl3.txt';
|
2008-08-25 03:42:27 +02:00
|
|
|
clearstatcache();
|
ENHANCEMENT Introduced constants for system paths like /sapphire in preparation for a more flexible directory reorganisation. Instead of hardcoding your path, please use the following constants: BASE_PATH, BASE_URL, SAPPHIRE_DIR, SAPPHIRE_PATH, CMS_DIR, CMS_PATH, THIRDPARTY_DIR, THIRDPARTY_PATH, ASSETS_DIR, ASSETS_PATH, THEMES_DIR, THEMES_PATH
git-svn-id: svn://svn.silverstripe.com/silverstripe/open/modules/sapphire/trunk@63154 467b73ca-7a2a-4603-9d3b-597d59a354a9
2008-09-27 18:02:38 +02:00
|
|
|
$this->assertFileExists(ASSETS_PATH . "/asdfjkl2.txt");
|
|
|
|
$this->assertFileNotExists(ASSETS_PATH . "/asdfjkl3.txt");
|
2008-08-25 03:42:27 +02:00
|
|
|
|
ENHANCEMENT Introduced constants for system paths like /sapphire in preparation for a more flexible directory reorganisation. Instead of hardcoding your path, please use the following constants: BASE_PATH, BASE_URL, SAPPHIRE_DIR, SAPPHIRE_PATH, CMS_DIR, CMS_PATH, THIRDPARTY_DIR, THIRDPARTY_PATH, ASSETS_DIR, ASSETS_PATH, THEMES_DIR, THEMES_PATH
git-svn-id: svn://svn.silverstripe.com/silverstripe/open/modules/sapphire/trunk@63154 467b73ca-7a2a-4603-9d3b-597d59a354a9
2008-09-27 18:02:38 +02:00
|
|
|
$file->Filename = ASSETS_DIR . '/asdfjkl2.txt';
|
2008-08-25 03:42:27 +02:00
|
|
|
$file->write();
|
|
|
|
|
|
|
|
/* Instead, altering Name and ParentID is the recommended way of changing the name and location of a file */
|
|
|
|
$file->ParentID = $this->idFromFixture('Folder', 'subfolder');
|
|
|
|
clearstatcache();
|
ENHANCEMENT Introduced constants for system paths like /sapphire in preparation for a more flexible directory reorganisation. Instead of hardcoding your path, please use the following constants: BASE_PATH, BASE_URL, SAPPHIRE_DIR, SAPPHIRE_PATH, CMS_DIR, CMS_PATH, THIRDPARTY_DIR, THIRDPARTY_PATH, ASSETS_DIR, ASSETS_PATH, THEMES_DIR, THEMES_PATH
git-svn-id: svn://svn.silverstripe.com/silverstripe/open/modules/sapphire/trunk@63154 467b73ca-7a2a-4603-9d3b-597d59a354a9
2008-09-27 18:02:38 +02:00
|
|
|
$this->assertFileExists(ASSETS_PATH . "/subfolder/asdfjkl2.txt");
|
|
|
|
$this->assertFileNotExists(ASSETS_PATH . "/asdfjkl2.txt");
|
|
|
|
$this->assertEquals(ASSETS_DIR . '/subfolder/asdfjkl2.txt', $file->Filename);
|
2008-08-25 03:42:27 +02:00
|
|
|
$file->write();
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
function testSizeAndAbsoluteSizeParameters() {
|
|
|
|
$file = $this->objFromFixture('File', 'asdf');
|
|
|
|
|
|
|
|
/* AbsoluteSize will give the integer number */
|
|
|
|
$this->assertEquals(1000000, $file->AbsoluteSize);
|
|
|
|
/* Size will give a humanised number */
|
|
|
|
$this->assertEquals('977 KB', $file->Size);
|
|
|
|
}
|
|
|
|
|
|
|
|
function testFileType() {
|
|
|
|
$file = $this->objFromFixture('File', 'gif');
|
2008-10-16 06:33:35 +02:00
|
|
|
$this->assertEquals("GIF image - good for diagrams", $file->FileType);
|
2008-08-25 03:42:27 +02:00
|
|
|
|
|
|
|
$file = $this->objFromFixture('File', 'pdf');
|
|
|
|
$this->assertEquals("Adobe Acrobat PDF file", $file->FileType);
|
|
|
|
|
|
|
|
/* Only a few file types are given special descriptions; the rest are unknown */
|
|
|
|
$file = $this->objFromFixture('File', 'asdf');
|
|
|
|
$this->assertEquals("unknown", $file->FileType);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Test the File::format_size() method
|
|
|
|
*/
|
|
|
|
function testFormatSize() {
|
|
|
|
$this->assertEquals("1000 bytes", File::format_size(1000));
|
|
|
|
$this->assertEquals("1023 bytes", File::format_size(1023));
|
|
|
|
$this->assertEquals("1 KB", File::format_size(1025));
|
|
|
|
$this->assertEquals("9.8 KB", File::format_size(10000));
|
|
|
|
$this->assertEquals("49 KB", File::format_size(50000));
|
|
|
|
$this->assertEquals("977 KB", File::format_size(1000000));
|
|
|
|
$this->assertEquals("1 MB", File::format_size(1024*1024));
|
|
|
|
$this->assertEquals("954 MB", File::format_size(1000000000));
|
|
|
|
$this->assertEquals("1 GB", File::format_size(1024*1024*1024));
|
|
|
|
$this->assertEquals("9.3 GB", File::format_size(10000000000));
|
|
|
|
// It use any denomination higher than GB. It also doesn't overflow with >32 bit integers
|
|
|
|
$this->assertEquals("93132.3 GB", File::format_size(100000000000000));
|
|
|
|
}
|
|
|
|
|
|
|
|
/////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
function setUp() {
|
|
|
|
parent::setUp();
|
|
|
|
|
|
|
|
/* Create a test folders for each of the fixture references */
|
|
|
|
$fileIDs = $this->allFixtureIDs('Folder');
|
|
|
|
foreach($fileIDs as $fileID) {
|
|
|
|
$file = DataObject::get_by_id('Folder', $fileID);
|
2010-01-28 06:10:32 +01:00
|
|
|
if(!file_exists(BASE_PATH."/$file->Filename")) mkdir(BASE_PATH."/$file->Filename");
|
2008-08-25 03:42:27 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Create a test files for each of the fixture references */
|
|
|
|
$fileIDs = $this->allFixtureIDs('File');
|
|
|
|
foreach($fileIDs as $fileID) {
|
|
|
|
$file = DataObject::get_by_id('File', $fileID);
|
2010-01-28 06:10:32 +01:00
|
|
|
$fh = fopen(BASE_PATH."/$file->Filename", "w");
|
2008-08-25 03:42:27 +02:00
|
|
|
fwrite($fh, str_repeat('x',1000000));
|
|
|
|
fclose($fh);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function tearDown() {
|
|
|
|
/* Remove the test files that we've created */
|
|
|
|
$fileIDs = $this->allFixtureIDs('File');
|
|
|
|
foreach($fileIDs as $fileID) {
|
|
|
|
$file = DataObject::get_by_id('File', $fileID);
|
2010-01-28 06:10:32 +01:00
|
|
|
if(file_exists(BASE_PATH."/$file->Filename")) unlink(BASE_PATH."/$file->Filename");
|
2008-08-25 03:42:27 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Remove the test folders that we've crated */
|
|
|
|
$fileIDs = $this->allFixtureIDs('Folder');
|
|
|
|
foreach($fileIDs as $fileID) {
|
|
|
|
$file = DataObject::get_by_id('Folder', $fileID);
|
2010-01-28 06:10:32 +01:00
|
|
|
if(file_exists(BASE_PATH."/$file->Filename")) rmdir(BASE_PATH."/$file->Filename");
|
2008-08-25 03:42:27 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
parent::tearDown();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
}
|