2015-09-03 07:46:08 +02:00
|
|
|
<?php
|
2016-10-14 03:30:05 +02:00
|
|
|
|
|
|
|
namespace SilverStripe\Forms\Tests;
|
|
|
|
|
2016-08-19 00:51:35 +02:00
|
|
|
use SilverStripe\Assets\Storage\AssetStore;
|
2016-10-14 03:30:05 +02:00
|
|
|
use SilverStripe\Control\Director;
|
2016-08-19 00:51:35 +02:00
|
|
|
use SilverStripe\Dev\SapphireTest;
|
2016-10-14 03:30:05 +02:00
|
|
|
use SilverStripe\Assets\Tests\Storage\AssetStoreTest\TestAssetStore;
|
2015-09-03 07:46:08 +02:00
|
|
|
|
|
|
|
class DBFileTest extends SapphireTest {
|
|
|
|
|
|
|
|
protected $extraDataObjects = array(
|
2016-10-14 03:30:05 +02:00
|
|
|
DBFileTest\TestObject::class,
|
|
|
|
DBFileTest\Subclass::class,
|
2015-09-03 07:46:08 +02:00
|
|
|
);
|
2016-03-08 21:50:18 +01:00
|
|
|
|
2015-09-03 07:46:08 +02:00
|
|
|
protected $usesDatabase = true;
|
|
|
|
|
|
|
|
public function setUp() {
|
|
|
|
parent::setUp();
|
|
|
|
|
|
|
|
// Set backend
|
2016-10-14 03:30:05 +02:00
|
|
|
TestAssetStore::activate('DBFileTest');
|
|
|
|
Director::config()->update('alternate_base_url', '/mysite/');
|
2015-09-03 07:46:08 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public function tearDown() {
|
2016-10-14 03:30:05 +02:00
|
|
|
TestAssetStore::reset();
|
2015-09-03 07:46:08 +02:00
|
|
|
parent::tearDown();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Test that images in a DBFile are rendered properly
|
|
|
|
*/
|
|
|
|
public function testRender() {
|
2016-10-14 03:30:05 +02:00
|
|
|
$obj = new DBFileTest\TestObject();
|
2015-09-03 07:46:08 +02:00
|
|
|
|
|
|
|
// Test image tag
|
2016-11-13 08:35:43 +01:00
|
|
|
$fish = realpath(__DIR__ .'/../ORM/ImageTest/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() {
|
2016-10-14 03:30:05 +02:00
|
|
|
$obj = new DBFileTest\ImageOnly();
|
2016-03-08 21:50:18 +01:00
|
|
|
|
2015-09-15 04:52:02 +02:00
|
|
|
// Test from image
|
2016-11-13 08:35:43 +01:00
|
|
|
$fish = realpath(__DIR__ .'/../ORM/ImageTest/test-image-high-quality.jpg');
|
2015-09-15 04:52:02 +02:00
|
|
|
$this->assertFileExists($fish);
|
|
|
|
$obj->MyFile->setFromLocalFile($fish, 'awesome-fish.jpg');
|
|
|
|
|
|
|
|
// This should fail
|
2016-06-15 06:03:16 +02:00
|
|
|
$this->setExpectedException('SilverStripe\\ORM\\ValidationException');
|
2015-09-15 04:52:02 +02:00
|
|
|
$obj->MyFile->setFromString('puppies', 'subdir/puppy-document.txt');
|
|
|
|
}
|
|
|
|
|
2015-12-09 22:19:23 +01:00
|
|
|
public function testPermission() {
|
2016-10-14 03:30:05 +02:00
|
|
|
$obj = new DBFileTest\TestObject();
|
2015-12-09 22:19:23 +01:00
|
|
|
|
|
|
|
// Test from image
|
2016-11-13 08:35:43 +01:00
|
|
|
$fish = realpath(__DIR__ .'/../ORM/ImageTest/test-image-high-quality.jpg');
|
2015-12-09 22:19:23 +01:00
|
|
|
$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
|
|
|
}
|