2015-09-03 07:46:08 +02:00
|
|
|
<?php
|
2015-12-09 22:19:23 +01:00
|
|
|
use SilverStripe\Filesystem\Storage\AssetStore;
|
2015-09-03 07:46:08 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Description of DBFileTest
|
|
|
|
*
|
|
|
|
* @author dmooyman
|
|
|
|
*/
|
|
|
|
class DBFileTest extends SapphireTest {
|
|
|
|
|
|
|
|
protected $extraDataObjects = array(
|
|
|
|
'DBFileTest_Object',
|
|
|
|
'DBFileTest_Subclass'
|
|
|
|
);
|
|
|
|
|
|
|
|
protected $usesDatabase = true;
|
|
|
|
|
|
|
|
public function setUp() {
|
|
|
|
parent::setUp();
|
|
|
|
|
|
|
|
// Set backend
|
2015-09-15 04:52:02 +02:00
|
|
|
AssetStoreTest_SpyStore::activate('DBFileTest');
|
2015-09-03 07:46:08 +02:00
|
|
|
Config::inst()->update('Director', 'alternate_base_url', '/mysite/');
|
|
|
|
}
|
|
|
|
|
|
|
|
public function tearDown() {
|
2015-09-15 04:52:02 +02:00
|
|
|
AssetStoreTest_SpyStore::reset('DBFileTest');
|
2015-09-03 07:46:08 +02:00
|
|
|
parent::tearDown();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Test that images in a DBFile are rendered properly
|
|
|
|
*/
|
|
|
|
public function testRender() {
|
|
|
|
$obj = new DBFileTest_Object();
|
|
|
|
|
|
|
|
// Test image tag
|
2015-09-15 04:52:02 +02:00
|
|
|
$fish = realpath(__DIR__ .'/../model/testimages/test-image-high-quality.jpg');
|
2015-09-03 07:46:08 +02:00
|
|
|
$this->assertFileExists($fish);
|
|
|
|
$obj->MyFile->setFromLocalFile($fish, 'awesome-fish.jpg');
|
|
|
|
$this->assertEquals(
|
|
|
|
'<img src="/mysite/assets/DBFileTest/a870de278b/awesome-fish.jpg" alt="awesome-fish.jpg" />',
|
|
|
|
trim($obj->MyFile->forTemplate())
|
|
|
|
);
|
|
|
|
|
|
|
|
// Test download tag
|
|
|
|
$obj->MyFile->setFromString('puppies', 'subdir/puppy-document.txt');
|
|
|
|
$this->assertEquals(
|
|
|
|
'<a href="/mysite/assets/DBFileTest/subdir/2a17a9cb4b/puppy-document.txt" title="puppy-document.txt" download="puppy-document.txt"/>',
|
|
|
|
trim($obj->MyFile->forTemplate())
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2015-09-15 04:52:02 +02:00
|
|
|
public function testValidation() {
|
|
|
|
$obj = new DBFileTest_ImageOnly();
|
|
|
|
|
|
|
|
// Test from image
|
|
|
|
$fish = realpath(__DIR__ .'/../model/testimages/test-image-high-quality.jpg');
|
|
|
|
$this->assertFileExists($fish);
|
|
|
|
$obj->MyFile->setFromLocalFile($fish, 'awesome-fish.jpg');
|
|
|
|
|
|
|
|
// This should fail
|
|
|
|
$this->setExpectedException('ValidationException');
|
|
|
|
$obj->MyFile->setFromString('puppies', 'subdir/puppy-document.txt');
|
|
|
|
}
|
|
|
|
|
2015-12-09 22:19:23 +01:00
|
|
|
public function testPermission() {
|
|
|
|
$obj = new DBFileTest_Object();
|
|
|
|
|
|
|
|
// Test from image
|
|
|
|
$fish = realpath(__DIR__ .'/../model/testimages/test-image-high-quality.jpg');
|
|
|
|
$this->assertFileExists($fish);
|
|
|
|
$obj->MyFile->setFromLocalFile($fish, 'private/awesome-fish.jpg', null, null, array(
|
|
|
|
'visibility' => AssetStore::VISIBILITY_PROTECTED
|
|
|
|
));
|
|
|
|
|
|
|
|
// Test various file permissions work on DBFile
|
|
|
|
$this->assertFalse($obj->MyFile->canViewFile());
|
|
|
|
$obj->MyFile->getURL();
|
|
|
|
$this->assertTrue($obj->MyFile->canViewFile());
|
|
|
|
$obj->MyFile->revokeFile();
|
|
|
|
$this->assertFalse($obj->MyFile->canViewFile());
|
|
|
|
$obj->MyFile->getURL(false);
|
|
|
|
$this->assertFalse($obj->MyFile->canViewFile());
|
|
|
|
$obj->MyFile->grantFile();
|
|
|
|
$this->assertTrue($obj->MyFile->canViewFile());
|
|
|
|
}
|
|
|
|
|
2015-09-03 07:46:08 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @property DBFile $MyFile
|
|
|
|
*/
|
|
|
|
class DBFileTest_Object extends DataObject implements TestOnly {
|
|
|
|
private static $db = array(
|
2015-09-15 04:52:02 +02:00
|
|
|
"MyFile" => "DBFile"
|
2015-09-03 07:46:08 +02:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2015-12-09 22:19:23 +01:00
|
|
|
/**
|
|
|
|
* @property DBFile $AnotherFile
|
|
|
|
*/
|
2015-09-03 07:46:08 +02:00
|
|
|
class DBFileTest_Subclass extends DBFileTest_Object implements TestOnly {
|
|
|
|
private static $db = array(
|
2015-09-15 04:52:02 +02:00
|
|
|
"AnotherFile" => "DBFile"
|
2015-09-03 07:46:08 +02:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2015-12-09 22:19:23 +01:00
|
|
|
/**
|
|
|
|
* @property DBFile $MyFile
|
|
|
|
*/
|
2015-09-15 04:52:02 +02:00
|
|
|
class DBFileTest_ImageOnly extends DataObject implements TestOnly {
|
|
|
|
private static $db = array(
|
|
|
|
"MyFile" => "DBFile('image/supported')"
|
|
|
|
);
|
|
|
|
}
|
2015-09-03 07:46:08 +02:00
|
|
|
|