2008-08-25 03:42:27 +02:00
|
|
|
<?php
|
|
|
|
|
2015-09-15 04:52:02 +02:00
|
|
|
use Filesystem as SS_Filesystem;
|
2016-01-26 04:56:07 +01:00
|
|
|
use SilverStripe\Filesystem\Storage\AssetStore;
|
2016-06-15 06:03:16 +02:00
|
|
|
use SilverStripe\ORM\Versioning\Versioned;
|
|
|
|
use SilverStripe\ORM\DataObject;
|
|
|
|
use SilverStripe\ORM\ValidationException;
|
2016-06-23 01:37:22 +02:00
|
|
|
use SilverStripe\Security\Member;
|
2016-08-05 00:32:13 +02:00
|
|
|
use SilverStripe\CMS\Model\ErrorPage;
|
|
|
|
|
2016-06-23 01:37:22 +02:00
|
|
|
|
2016-06-15 06:03:16 +02:00
|
|
|
|
2015-09-15 04:52:02 +02:00
|
|
|
|
2008-08-25 03:42:27 +02:00
|
|
|
/**
|
|
|
|
* Tests for the File class
|
|
|
|
*/
|
|
|
|
class FileTest extends SapphireTest {
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2013-03-21 19:48:54 +01:00
|
|
|
protected static $fixture_file = 'FileTest.yml';
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2011-08-22 13:04:15 +02:00
|
|
|
protected $extraDataObjects = array('FileTest_MyCustomFile');
|
2012-03-01 09:13:42 +01:00
|
|
|
|
2015-09-15 04:52:02 +02:00
|
|
|
public function setUp() {
|
|
|
|
parent::setUp();
|
2016-01-26 04:56:07 +01:00
|
|
|
$this->logInWithPermission('ADMIN');
|
2016-03-02 06:18:10 +01:00
|
|
|
Versioned::set_stage(Versioned::DRAFT);
|
2015-09-15 04:52:02 +02:00
|
|
|
|
|
|
|
// Set backend root to /ImageTest
|
|
|
|
AssetStoreTest_SpyStore::activate('FileTest');
|
2016-03-08 21:50:18 +01:00
|
|
|
|
2015-09-15 04:52:02 +02:00
|
|
|
// Create a test folders for each of the fixture references
|
|
|
|
$folderIDs = $this->allFixtureIDs('Folder');
|
|
|
|
foreach($folderIDs as $folderID) {
|
|
|
|
$folder = DataObject::get_by_id('Folder', $folderID);
|
|
|
|
$filePath = ASSETS_PATH . '/FileTest/' . $folder->getFilename();
|
|
|
|
SS_Filesystem::makeFolder($filePath);
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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);
|
|
|
|
$root = ASSETS_PATH . '/FileTest/';
|
|
|
|
if($folder = $file->Parent()) {
|
|
|
|
$root .= $folder->getFilename();
|
|
|
|
}
|
|
|
|
$path = $root . substr($file->getHash(), 0, 10) . '/' . basename($file->getFilename());
|
|
|
|
SS_Filesystem::makeFolder(dirname($path));
|
|
|
|
$fh = fopen($path, "w+");
|
|
|
|
fwrite($fh, str_repeat('x', 1000000));
|
|
|
|
fclose($fh);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Conditional fixture creation in case the 'cms' module is installed
|
2016-08-05 00:32:13 +02:00
|
|
|
if(class_exists('SilverStripe\\CMS\\Model\\ErrorPage')) {
|
2015-09-15 04:52:02 +02:00
|
|
|
$page = new ErrorPage(array(
|
|
|
|
'Title' => 'Page not Found',
|
|
|
|
'ErrorCode' => 404
|
|
|
|
));
|
|
|
|
$page->write();
|
2016-04-01 05:27:59 +02:00
|
|
|
$page->copyVersionToStage('Stage', 'Live');
|
2015-09-15 04:52:02 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public function tearDown() {
|
|
|
|
AssetStoreTest_SpyStore::reset();
|
|
|
|
parent::tearDown();
|
|
|
|
}
|
|
|
|
|
2012-03-01 09:13:42 +01:00
|
|
|
public function testLinkShortcodeHandler() {
|
|
|
|
$testFile = $this->objFromFixture('File', 'asdf');
|
|
|
|
|
|
|
|
$parser = new ShortcodeParser();
|
2015-09-15 04:52:02 +02:00
|
|
|
$parser->register('file_link', array('File', 'handle_shortcode'));
|
2012-03-01 09:13:42 +01:00
|
|
|
|
2013-06-01 01:24:25 +02:00
|
|
|
$fileShortcode = sprintf('[file_link,id=%d]', $testFile->ID);
|
|
|
|
$fileEnclosed = sprintf('[file_link,id=%d]Example Content[/file_link]', $testFile->ID);
|
2012-03-01 09:13:42 +01:00
|
|
|
|
|
|
|
$fileShortcodeExpected = $testFile->Link();
|
2012-09-26 23:34:00 +02:00
|
|
|
$fileEnclosedExpected = sprintf(
|
|
|
|
'<a href="%s" class="file" data-type="txt" data-size="977 KB">Example Content</a>', $testFile->Link());
|
2012-03-01 09:13:42 +01:00
|
|
|
|
|
|
|
$this->assertEquals($fileShortcodeExpected, $parser->parse($fileShortcode), 'Test that simple linking works.');
|
|
|
|
$this->assertEquals($fileEnclosedExpected, $parser->parse($fileEnclosed), 'Test enclosed content is linked.');
|
|
|
|
|
|
|
|
$testFile->delete();
|
|
|
|
|
2013-06-01 01:24:25 +02:00
|
|
|
$fileShortcode = '[file_link,id="-1"]';
|
|
|
|
$fileEnclosed = '[file_link,id="-1"]Example Content[/file_link]';
|
2012-03-01 09:13:42 +01:00
|
|
|
|
|
|
|
$this->assertEquals('', $parser->parse('[file_link]'), 'Test that invalid ID attributes are not parsed.');
|
2013-06-01 01:24:25 +02:00
|
|
|
$this->assertEquals('', $parser->parse('[file_link,id="text"]'));
|
2012-03-01 09:13:42 +01:00
|
|
|
$this->assertEquals('', $parser->parse('[file_link]Example Content[/file_link]'));
|
2012-06-19 12:45:44 +02:00
|
|
|
|
2016-08-05 00:32:13 +02:00
|
|
|
if(class_exists('SilverStripe\\CMS\\Model\\ErrorPage')) {
|
2012-06-19 12:45:44 +02:00
|
|
|
$errorPage = ErrorPage::get()->filter('ErrorCode', 404)->First();
|
|
|
|
$this->assertEquals(
|
2014-08-15 08:53:05 +02:00
|
|
|
$errorPage->Link(),
|
|
|
|
$parser->parse($fileShortcode),
|
2012-06-19 12:45:44 +02:00
|
|
|
'Test link to 404 page if no suitable matches.'
|
|
|
|
);
|
|
|
|
$this->assertEquals(
|
2014-08-15 08:53:05 +02:00
|
|
|
sprintf('<a href="%s">Example Content</a>', $errorPage->Link()),
|
2012-06-19 12:45:44 +02:00
|
|
|
$parser->parse($fileEnclosed)
|
|
|
|
);
|
|
|
|
} else {
|
2012-09-26 23:34:00 +02:00
|
|
|
$this->assertEquals('', $parser->parse($fileShortcode),
|
|
|
|
'Short code is removed if file record is not present.');
|
2012-06-19 12:45:44 +02:00
|
|
|
$this->assertEquals('', $parser->parse($fileEnclosed));
|
|
|
|
}
|
2012-03-01 09:13:42 +01:00
|
|
|
}
|
|
|
|
|
2012-09-19 12:07:39 +02:00
|
|
|
public function testCreateWithFilenameWithSubfolder() {
|
2010-10-15 05:20:47 +02:00
|
|
|
// Note: We can't use fixtures/setUp() for this, as we want to create the db record manually.
|
|
|
|
// Creating the folder is necessary to avoid having "Filename" overwritten by setName()/setRelativePath(),
|
|
|
|
// because the parent folders don't exist in the database
|
2011-12-06 01:56:24 +01:00
|
|
|
$folder = Folder::find_or_make('/FileTest/');
|
2015-09-15 04:52:02 +02:00
|
|
|
$testfilePath = BASE_PATH . '/assets/FileTest/CreateWithFilenameHasCorrectPath.txt'; // Important: No leading slash
|
|
|
|
$fh = fopen($testfilePath, "w");
|
2010-10-15 05:20:47 +02:00
|
|
|
fwrite($fh, str_repeat('x',1000000));
|
|
|
|
fclose($fh);
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2010-10-15 05:20:47 +02:00
|
|
|
$file = new File();
|
2015-09-15 04:52:02 +02:00
|
|
|
$file->setFromLocalFile($testfilePath);
|
2010-10-15 05:20:47 +02:00
|
|
|
$file->ParentID = $folder->ID;
|
|
|
|
$file->write();
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2015-09-15 04:52:02 +02:00
|
|
|
$this->assertEquals(
|
|
|
|
'CreateWithFilenameHasCorrectPath.txt',
|
|
|
|
$file->Name,
|
|
|
|
'"Name" property is automatically set from "Filename"'
|
|
|
|
);
|
|
|
|
$this->assertEquals(
|
|
|
|
'FileTest/CreateWithFilenameHasCorrectPath.txt',
|
|
|
|
$file->Filename,
|
|
|
|
'"Filename" property remains unchanged'
|
|
|
|
);
|
2010-10-15 05:20:47 +02:00
|
|
|
|
|
|
|
// TODO This should be auto-detected, see File->updateFilesystem()
|
2012-05-09 12:43:22 +02:00
|
|
|
// $this->assertInstanceOf('Folder', $file->Parent(), 'Parent folder is created in database');
|
2015-09-15 04:52:02 +02:00
|
|
|
// $this->assertFileExists($file->Parent()->getURL(), 'Parent folder is created on filesystem');
|
2010-10-15 05:20:47 +02:00
|
|
|
// $this->assertEquals('FileTest', $file->Parent()->Name);
|
2012-05-09 12:43:22 +02:00
|
|
|
// $this->assertInstanceOf('Folder', $file->Parent()->Parent(), 'Grandparent folder is created in database');
|
2015-09-15 04:52:02 +02:00
|
|
|
// $this->assertFileExists($file->Parent()->Parent()->getURL(),
|
2012-09-26 23:34:00 +02:00
|
|
|
// 'Grandparent folder is created on filesystem');
|
2010-10-15 05:20:47 +02:00
|
|
|
// $this->assertEquals('assets', $file->Parent()->Parent()->Name);
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2012-09-19 12:07:39 +02:00
|
|
|
public function testGetExtension() {
|
2012-09-26 23:34:00 +02:00
|
|
|
$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');
|
2010-10-15 05:14:59 +02:00
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2012-09-19 12:07:39 +02:00
|
|
|
public function testValidateExtension() {
|
2010-10-15 05:14:59 +02:00
|
|
|
Session::set('loggedInAs', null);
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2013-03-21 19:48:54 +01:00
|
|
|
$orig = Config::inst()->get('File', 'allowed_extensions');
|
|
|
|
Config::inst()->remove('File', 'allowed_extensions');
|
|
|
|
Config::inst()->update('File', 'allowed_extensions', array('txt'));
|
2014-08-15 08:53:05 +02:00
|
|
|
|
|
|
|
$file = $this->objFromFixture('File', 'asdf');
|
|
|
|
|
2010-10-15 05:14:59 +02:00
|
|
|
// Invalid ext
|
2010-10-15 05:04:54 +02:00
|
|
|
$file->Name = 'asdf.php';
|
2015-06-17 05:51:30 +02:00
|
|
|
$v = $file->validate();
|
2010-10-15 05:04:54 +02:00
|
|
|
$this->assertFalse($v->valid());
|
|
|
|
$this->assertContains('Extension is not allowed', $v->message());
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2010-10-15 05:14:59 +02:00
|
|
|
// Valid ext
|
2010-10-15 05:04:54 +02:00
|
|
|
$file->Name = 'asdf.txt';
|
2015-06-17 05:51:30 +02:00
|
|
|
$v = $file->validate();
|
2010-10-15 05:04:54 +02:00
|
|
|
$this->assertTrue($v->valid());
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2010-10-15 05:53:54 +02:00
|
|
|
// Capital extension is valid as well
|
|
|
|
$file->Name = 'asdf.TXT';
|
2015-06-17 05:51:30 +02:00
|
|
|
$v = $file->validate();
|
2010-10-15 05:53:54 +02:00
|
|
|
$this->assertTrue($v->valid());
|
2013-03-21 19:48:54 +01:00
|
|
|
|
|
|
|
Config::inst()->remove('File', 'allowed_extensions');
|
|
|
|
Config::inst()->update('File', 'allowed_extensions', $orig);
|
2010-10-15 05:04:54 +02:00
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2015-09-15 04:52:02 +02:00
|
|
|
public function testAppCategory() {
|
|
|
|
// Test various categories
|
|
|
|
$this->assertEquals('image', File::get_app_category('jpg'));
|
|
|
|
$this->assertEquals('image', File::get_app_category('JPG'));
|
|
|
|
$this->assertEquals('image', File::get_app_category('JPEG'));
|
|
|
|
$this->assertEquals('image', File::get_app_category('png'));
|
|
|
|
$this->assertEquals('image', File::get_app_category('tif'));
|
|
|
|
$this->assertEquals('document', File::get_app_category('pdf'));
|
|
|
|
$this->assertEquals('video', File::get_app_category('mov'));
|
|
|
|
$this->assertEquals('audio', File::get_app_category('OGG'));
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testGetCategoryExtensions() {
|
|
|
|
// Test specific categories
|
|
|
|
$images = array(
|
|
|
|
'alpha', 'als', 'bmp', 'cel', 'gif', 'ico', 'icon', 'jpeg', 'jpg', 'pcx', 'png', 'ps', 'tif', 'tiff'
|
|
|
|
);
|
|
|
|
$this->assertEquals($images, File::get_category_extensions('image'));
|
|
|
|
$this->assertEquals(array('gif', 'jpeg', 'jpg', 'png'), File::get_category_extensions('image/supported'));
|
|
|
|
$this->assertEquals($images, File::get_category_extensions(array('image', 'image/supported')));
|
|
|
|
$this->assertEquals(
|
|
|
|
array('fla', 'gif', 'jpeg', 'jpg', 'png', 'swf'),
|
|
|
|
File::get_category_extensions(array('flash', 'image/supported'))
|
|
|
|
);
|
|
|
|
|
|
|
|
// Test other categories have at least one item
|
|
|
|
$this->assertNotEmpty(File::get_category_extensions('archive'));
|
|
|
|
$this->assertNotEmpty(File::get_category_extensions('audio'));
|
|
|
|
$this->assertNotEmpty(File::get_category_extensions('document'));
|
|
|
|
$this->assertNotEmpty(File::get_category_extensions('flash'));
|
|
|
|
$this->assertNotEmpty(File::get_category_extensions('video'));
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @dataProvider allowedExtensions
|
|
|
|
* @param string $extension
|
|
|
|
*/
|
|
|
|
public function testAllFilesHaveCategory($extension) {
|
|
|
|
$this->assertNotEmpty(
|
|
|
|
File::get_app_category($extension),
|
|
|
|
"Assert that extension {$extension} has a valid category"
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Gets the list of all extensions for testing
|
|
|
|
*
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
public function allowedExtensions() {
|
|
|
|
$args = array();
|
|
|
|
foreach(array_filter(File::config()->allowed_extensions) as $ext) {
|
|
|
|
$args[] = array($ext);
|
|
|
|
}
|
|
|
|
return $args;
|
|
|
|
}
|
|
|
|
|
2012-09-19 12:07:39 +02:00
|
|
|
public function testSetNameChangesFilesystemOnWrite() {
|
2016-04-01 05:27:59 +02:00
|
|
|
/** @var File $file */
|
2010-10-15 05:20:47 +02:00
|
|
|
$file = $this->objFromFixture('File', 'asdf');
|
2016-01-26 04:56:07 +01:00
|
|
|
$this->logInWithPermission('ADMIN');
|
2016-04-01 05:27:59 +02:00
|
|
|
$file->publishRecursive();
|
2016-01-26 04:56:07 +01:00
|
|
|
$oldTuple = $file->File->getValue();
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2016-01-26 04:56:07 +01:00
|
|
|
// Rename
|
2010-10-15 05:20:47 +02:00
|
|
|
$file->Name = 'renamed.txt';
|
2016-01-26 04:56:07 +01:00
|
|
|
$newTuple = $oldTuple;
|
2016-03-02 06:18:10 +01:00
|
|
|
$newTuple['Filename'] = $file->generateFilename();
|
2016-01-26 04:56:07 +01:00
|
|
|
|
|
|
|
// Before write()
|
|
|
|
$this->assertTrue(
|
|
|
|
$this->getAssetStore()->exists($oldTuple['Filename'], $oldTuple['Hash']),
|
|
|
|
'Old path is still present'
|
|
|
|
);
|
|
|
|
$this->assertFalse(
|
|
|
|
$this->getAssetStore()->exists($newTuple['Filename'], $newTuple['Hash']),
|
|
|
|
'New path is updated in memory, not written before write() is called'
|
|
|
|
);
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2010-10-15 05:20:47 +02:00
|
|
|
// After write()
|
2016-01-26 04:56:07 +01:00
|
|
|
$file->write();
|
|
|
|
$this->assertTrue(
|
|
|
|
$this->getAssetStore()->exists($oldTuple['Filename'], $oldTuple['Hash']),
|
|
|
|
'Old path exists after draft change'
|
|
|
|
);
|
|
|
|
$this->assertTrue(
|
|
|
|
$this->getAssetStore()->exists($newTuple['Filename'], $newTuple['Hash']),
|
|
|
|
'New path is created after write()'
|
|
|
|
);
|
|
|
|
|
|
|
|
// After publish
|
2016-04-01 05:27:59 +02:00
|
|
|
$file->publishRecursive();
|
2016-01-26 04:56:07 +01:00
|
|
|
$this->assertFalse(
|
|
|
|
$this->getAssetStore()->exists($oldTuple['Filename'], $oldTuple['Hash']),
|
|
|
|
'Old file is finally removed after publishing new file'
|
|
|
|
);
|
|
|
|
$this->assertTrue(
|
|
|
|
$this->getAssetStore()->exists($newTuple['Filename'], $newTuple['Hash']),
|
|
|
|
'New path is created after write()'
|
|
|
|
);
|
2010-10-15 05:20:47 +02:00
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2012-09-19 12:07:39 +02:00
|
|
|
public function testSetParentIDChangesFilesystemOnWrite() {
|
2010-10-15 05:20:47 +02:00
|
|
|
$file = $this->objFromFixture('File', 'asdf');
|
2016-01-26 04:56:07 +01:00
|
|
|
$this->logInWithPermission('ADMIN');
|
2016-04-01 05:27:59 +02:00
|
|
|
$file->publishRecursive();
|
2010-10-15 05:20:47 +02:00
|
|
|
$subfolder = $this->objFromFixture('Folder', 'subfolder');
|
2016-01-26 04:56:07 +01:00
|
|
|
$oldTuple = $file->File->getValue();
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2010-10-15 05:20:47 +02:00
|
|
|
// set ParentID
|
|
|
|
$file->ParentID = $subfolder->ID;
|
2016-01-26 04:56:07 +01:00
|
|
|
$newTuple = $oldTuple;
|
2016-03-02 06:18:10 +01:00
|
|
|
$newTuple['Filename'] = $file->generateFilename();
|
2010-10-15 05:20:47 +02:00
|
|
|
|
|
|
|
// Before write()
|
2016-01-26 04:56:07 +01:00
|
|
|
$this->assertTrue(
|
|
|
|
$this->getAssetStore()->exists($oldTuple['Filename'], $oldTuple['Hash']),
|
|
|
|
'Old path is still present'
|
|
|
|
);
|
|
|
|
$this->assertFalse(
|
|
|
|
$this->getAssetStore()->exists($newTuple['Filename'], $newTuple['Hash']),
|
|
|
|
'New path is updated in memory, not written before write() is called'
|
|
|
|
);
|
2010-10-15 05:20:47 +02:00
|
|
|
$file->write();
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2010-10-15 05:20:47 +02:00
|
|
|
// After write()
|
2016-01-26 04:56:07 +01:00
|
|
|
$file->write();
|
|
|
|
$this->assertTrue(
|
|
|
|
$this->getAssetStore()->exists($oldTuple['Filename'], $oldTuple['Hash']),
|
|
|
|
'Old path exists after draft change'
|
|
|
|
);
|
|
|
|
$this->assertTrue(
|
|
|
|
$this->getAssetStore()->exists($newTuple['Filename'], $newTuple['Hash']),
|
|
|
|
'New path is created after write()'
|
|
|
|
);
|
|
|
|
|
|
|
|
// After publish
|
2016-04-01 05:27:59 +02:00
|
|
|
$file->publishSingle();
|
2016-01-26 04:56:07 +01:00
|
|
|
$this->assertFalse(
|
|
|
|
$this->getAssetStore()->exists($oldTuple['Filename'], $oldTuple['Hash']),
|
|
|
|
'Old file is finally removed after publishing new file'
|
|
|
|
);
|
|
|
|
$this->assertTrue(
|
|
|
|
$this->getAssetStore()->exists($newTuple['Filename'], $newTuple['Hash']),
|
|
|
|
'New path is created after write()'
|
|
|
|
);
|
2010-10-15 05:20:47 +02:00
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2010-10-15 05:20:47 +02:00
|
|
|
/**
|
|
|
|
* @see http://open.silverstripe.org/ticket/5693
|
2012-11-23 14:55:19 +01:00
|
|
|
*
|
2016-06-15 06:03:16 +02:00
|
|
|
* @expectedException SilverStripe\ORM\ValidationException
|
2010-10-15 05:20:47 +02:00
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public function testSetNameWithInvalidExtensionDoesntChangeFilesystem() {
|
2013-03-21 19:48:54 +01:00
|
|
|
$orig = Config::inst()->get('File', 'allowed_extensions');
|
|
|
|
Config::inst()->remove('File', 'allowed_extensions');
|
|
|
|
Config::inst()->update('File', 'allowed_extensions', array('txt'));
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2010-10-15 05:20:47 +02:00
|
|
|
$file = $this->objFromFixture('File', 'asdf');
|
2015-09-15 04:52:02 +02:00
|
|
|
$oldPath = $file->getURL();
|
2014-08-15 08:53:05 +02:00
|
|
|
|
|
|
|
$file->Name = 'renamed.php'; // evil extension
|
2010-10-15 05:20:47 +02:00
|
|
|
try {
|
|
|
|
$file->write();
|
|
|
|
} catch(ValidationException $e) {
|
2013-03-21 19:48:54 +01:00
|
|
|
Config::inst()->remove('File', 'allowed_extensions');
|
|
|
|
Config::inst()->update('File', 'allowed_extensions', $orig);
|
2012-11-23 14:55:19 +01:00
|
|
|
throw $e;
|
2010-10-15 05:20:47 +02:00
|
|
|
}
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2012-09-19 12:07:39 +02:00
|
|
|
public function testGetURL() {
|
2010-10-19 02:52:57 +02:00
|
|
|
$rootfile = $this->objFromFixture('File', 'asdf');
|
2015-09-15 04:52:02 +02:00
|
|
|
$this->assertEquals('/assets/FileTest/55b443b601/FileTest.txt', $rootfile->getURL());
|
2010-10-19 02:52:57 +02:00
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2012-09-19 12:07:39 +02:00
|
|
|
public function testGetAbsoluteURL() {
|
2010-10-19 02:52:57 +02:00
|
|
|
$rootfile = $this->objFromFixture('File', 'asdf');
|
2015-09-15 04:52:02 +02:00
|
|
|
$this->assertEquals(
|
|
|
|
Director::absoluteBaseURL() . 'assets/FileTest/55b443b601/FileTest.txt',
|
|
|
|
$rootfile->getAbsoluteURL()
|
|
|
|
);
|
2010-10-19 02:52:57 +02:00
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2012-09-19 12:07:39 +02:00
|
|
|
public function testNameAndTitleGeneration() {
|
2015-09-15 04:52:02 +02:00
|
|
|
// When name is assigned, title is automatically assigned
|
|
|
|
$file = $this->objFromFixture('Image', 'setfromname');
|
2010-10-15 05:16:16 +02:00
|
|
|
$this->assertEquals('FileTest', $file->Title);
|
2008-08-25 03:42:27 +02:00
|
|
|
}
|
|
|
|
|
2012-09-19 12:07:39 +02:00
|
|
|
public function testSizeAndAbsoluteSizeParameters() {
|
2008-08-25 03:42:27 +02:00
|
|
|
$file = $this->objFromFixture('File', 'asdf');
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2008-08-25 03:42:27 +02:00
|
|
|
/* AbsoluteSize will give the integer number */
|
|
|
|
$this->assertEquals(1000000, $file->AbsoluteSize);
|
|
|
|
/* Size will give a humanised number */
|
|
|
|
$this->assertEquals('977 KB', $file->Size);
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2012-09-19 12:07:39 +02:00
|
|
|
public function testFileType() {
|
2015-09-15 04:52:02 +02:00
|
|
|
$file = $this->objFromFixture('Image', 'gif');
|
2008-10-16 06:33:35 +02:00
|
|
|
$this->assertEquals("GIF image - good for diagrams", $file->FileType);
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2008-08-25 03:42:27 +02:00
|
|
|
$file = $this->objFromFixture('File', 'pdf');
|
|
|
|
$this->assertEquals("Adobe Acrobat PDF file", $file->FileType);
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2015-09-15 04:52:02 +02:00
|
|
|
$file = $this->objFromFixture('Image', 'gifupper');
|
2015-05-04 16:10:16 +02:00
|
|
|
$this->assertEquals("GIF image - good for diagrams", $file->FileType);
|
2016-01-06 00:34:58 +01:00
|
|
|
|
2008-08-25 03:42:27 +02:00
|
|
|
/* Only a few file types are given special descriptions; the rest are unknown */
|
|
|
|
$file = $this->objFromFixture('File', 'asdf');
|
|
|
|
$this->assertEquals("unknown", $file->FileType);
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2008-08-25 03:42:27 +02:00
|
|
|
/**
|
|
|
|
* Test the File::format_size() method
|
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public function testFormatSize() {
|
2008-08-25 03:42:27 +02:00
|
|
|
$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));
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2015-12-09 22:19:23 +01:00
|
|
|
public function testDeleteFile() {
|
2016-04-01 05:27:59 +02:00
|
|
|
/** @var File $file */
|
2010-10-15 05:20:47 +02:00
|
|
|
$file = $this->objFromFixture('File', 'asdf');
|
2016-01-26 04:56:07 +01:00
|
|
|
$this->logInWithPermission('ADMIN');
|
2016-04-01 05:27:59 +02:00
|
|
|
$file->publishSingle();
|
2016-01-26 04:56:07 +01:00
|
|
|
$tuple = $file->File->getValue();
|
|
|
|
|
|
|
|
// Before delete
|
|
|
|
$this->assertTrue(
|
|
|
|
$this->getAssetStore()->exists($tuple['Filename'], $tuple['Hash']),
|
|
|
|
'File is still present'
|
|
|
|
);
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2016-01-26 04:56:07 +01:00
|
|
|
// after unpublish
|
|
|
|
$file->doUnpublish();
|
|
|
|
$this->assertTrue(
|
|
|
|
$this->getAssetStore()->exists($tuple['Filename'], $tuple['Hash']),
|
|
|
|
'File is still present after unpublish'
|
|
|
|
);
|
|
|
|
|
|
|
|
// after delete
|
|
|
|
$file->delete();
|
|
|
|
$this->assertFalse(
|
|
|
|
$this->getAssetStore()->exists($tuple['Filename'], $tuple['Hash']),
|
|
|
|
'File is deleted after unpublish and delete'
|
|
|
|
);
|
2010-10-15 05:20:47 +02:00
|
|
|
}
|
2011-03-23 01:23:43 +01:00
|
|
|
|
2012-09-19 12:07:39 +02:00
|
|
|
public function testRenameFolder() {
|
2011-03-23 01:23:43 +01:00
|
|
|
$newTitle = "FileTest-folder-renamed";
|
|
|
|
|
|
|
|
//rename a folder's title
|
|
|
|
$folderID = $this->objFromFixture("Folder","folder2")->ID;
|
|
|
|
$folder = DataObject::get_by_id('Folder',$folderID);
|
|
|
|
$folder->Title = $newTitle;
|
|
|
|
$folder->write();
|
|
|
|
|
|
|
|
//get folder again and see if the filename has changed
|
|
|
|
$folder = DataObject::get_by_id('Folder',$folderID);
|
2015-09-15 04:52:02 +02:00
|
|
|
$this->assertEquals(
|
|
|
|
$newTitle . "/",
|
|
|
|
$folder->Filename,
|
|
|
|
"Folder Filename updated after rename of Title"
|
|
|
|
);
|
2011-03-23 01:23:43 +01:00
|
|
|
|
|
|
|
//rename a folder's name
|
|
|
|
$newTitle2 = "FileTest-folder-renamed2";
|
|
|
|
$folder->Name = $newTitle2;
|
|
|
|
$folder->write();
|
|
|
|
|
|
|
|
//get folder again and see if the Title has changed
|
|
|
|
$folder = DataObject::get_by_id('Folder',$folderID);
|
2012-09-26 23:34:00 +02:00
|
|
|
$this->assertEquals($folder->Title, $newTitle2,
|
|
|
|
"Folder Title updated after rename of Name");
|
2011-03-23 01:23:43 +01:00
|
|
|
|
|
|
|
|
|
|
|
//rename a folder's Filename
|
|
|
|
$newTitle3 = "FileTest-folder-renamed3";
|
|
|
|
$folder->Filename = $newTitle3;
|
|
|
|
$folder->write();
|
|
|
|
|
|
|
|
//get folder again and see if the Title has changed
|
|
|
|
$folder = DataObject::get_by_id('Folder',$folderID);
|
2012-09-26 23:34:00 +02:00
|
|
|
$this->assertEquals($folder->Title, $newTitle3,
|
|
|
|
"Folder Title updated after rename of Filename");
|
2011-03-23 01:23:43 +01:00
|
|
|
}
|
|
|
|
|
2012-09-19 12:07:39 +02:00
|
|
|
public function testSetsOwnerOnFirstWrite() {
|
2012-02-07 18:32:04 +01:00
|
|
|
Session::set('loggedInAs', null);
|
|
|
|
$member1 = new Member();
|
|
|
|
$member1->write();
|
|
|
|
$member2 = new Member();
|
|
|
|
$member2->write();
|
|
|
|
|
|
|
|
$file1 = new File();
|
|
|
|
$file1->write();
|
|
|
|
$this->assertEquals(0, $file1->OwnerID, 'Owner not written when no user is logged in');
|
|
|
|
|
|
|
|
$member1->logIn();
|
|
|
|
$file2 = new File();
|
|
|
|
$file2->write();
|
|
|
|
$this->assertEquals($member1->ID, $file2->OwnerID, 'Owner written when user is logged in');
|
|
|
|
|
|
|
|
$member2->logIn();
|
|
|
|
$file2->forceChange();
|
|
|
|
$file2->write();
|
|
|
|
$this->assertEquals($member1->ID, $file2->OwnerID, 'Owner not overwritten on existing files');
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2014-12-16 03:43:24 +01:00
|
|
|
public function testCanEdit() {
|
2015-09-15 04:52:02 +02:00
|
|
|
$file = $this->objFromFixture('Image', 'gif');
|
2014-12-16 03:43:24 +01:00
|
|
|
|
|
|
|
// Test anonymous permissions
|
|
|
|
Session::set('loggedInAs', null);
|
|
|
|
$this->assertFalse($file->canEdit(), "Anonymous users can't edit files");
|
|
|
|
|
|
|
|
// Test permissionless user
|
2016-06-23 01:37:22 +02:00
|
|
|
$this->objFromFixture('SilverStripe\\Security\\Member', 'frontend')->logIn();
|
2014-12-16 03:43:24 +01:00
|
|
|
$this->assertFalse($file->canEdit(), "Permissionless users can't edit files");
|
|
|
|
|
2015-04-20 08:02:08 +02:00
|
|
|
// Test global CMS section users
|
2016-06-23 01:37:22 +02:00
|
|
|
$this->objFromFixture('SilverStripe\\Security\\Member', 'cms')->logIn();
|
2015-04-20 08:02:08 +02:00
|
|
|
$this->assertTrue($file->canEdit(), "Users with all CMS section access can edit files");
|
|
|
|
|
|
|
|
// Test cms access users without file access
|
2016-06-23 01:37:22 +02:00
|
|
|
$this->objFromFixture('SilverStripe\\Security\\Member', 'security')->logIn();
|
2015-04-20 08:02:08 +02:00
|
|
|
$this->assertFalse($file->canEdit(), "Security CMS users can't edit files");
|
2014-12-16 03:43:24 +01:00
|
|
|
|
|
|
|
// Test asset-admin user
|
2016-06-23 01:37:22 +02:00
|
|
|
$this->objFromFixture('SilverStripe\\Security\\Member', 'assetadmin')->logIn();
|
2014-12-16 03:43:24 +01:00
|
|
|
$this->assertTrue($file->canEdit(), "Asset admin users can edit files");
|
|
|
|
|
|
|
|
// Test admin
|
2016-06-23 01:37:22 +02:00
|
|
|
$this->objFromFixture('SilverStripe\\Security\\Member', 'admin')->logIn();
|
2014-12-16 03:43:24 +01:00
|
|
|
$this->assertTrue($file->canEdit(), "Admins can edit files");
|
|
|
|
}
|
2016-03-08 21:50:18 +01:00
|
|
|
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2015-09-15 04:52:02 +02:00
|
|
|
public function testJoinPaths() {
|
|
|
|
$this->assertEquals('name/file.jpg', File::join_paths('/name', 'file.jpg'));
|
|
|
|
$this->assertEquals('name/file.jpg', File::join_paths('name', 'file.jpg'));
|
|
|
|
$this->assertEquals('name/file.jpg', File::join_paths('/name', '/file.jpg'));
|
|
|
|
$this->assertEquals('name/file.jpg', File::join_paths('name/', '/', 'file.jpg'));
|
|
|
|
$this->assertEquals('file.jpg', File::join_paths('/', '/', 'file.jpg'));
|
|
|
|
$this->assertEquals('', File::join_paths('/', '/'));
|
2008-08-25 03:42:27 +02:00
|
|
|
}
|
2010-10-19 07:06:39 +02:00
|
|
|
|
2016-01-26 04:56:07 +01:00
|
|
|
/**
|
|
|
|
* @return AssetStore
|
|
|
|
*/
|
|
|
|
protected function getAssetStore() {
|
|
|
|
return Injector::inst()->get('AssetStore');
|
|
|
|
}
|
|
|
|
|
2011-08-22 13:04:15 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
class FileTest_MyCustomFile extends File implements TestOnly {
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2012-03-01 09:13:42 +01:00
|
|
|
}
|