2015-09-03 07:46:08 +02:00
|
|
|
<?php
|
|
|
|
|
2016-10-14 03:30:05 +02:00
|
|
|
namespace SilverStripe\Assets\Tests\Storage;
|
|
|
|
|
|
|
|
use Exception;
|
2016-08-19 00:51:35 +02:00
|
|
|
use SilverStripe\Assets\Flysystem\FlysystemAssetStore;
|
|
|
|
use SilverStripe\Assets\Storage\AssetStore;
|
2016-10-14 03:30:05 +02:00
|
|
|
use SilverStripe\Assets\Tests\Storage\AssetStoreTest\TestAssetStore;
|
2016-08-19 00:51:35 +02:00
|
|
|
use SilverStripe\Core\Config\Config;
|
|
|
|
use SilverStripe\Core\Injector\Injector;
|
|
|
|
use SilverStripe\Dev\SapphireTest;
|
|
|
|
|
2016-10-14 03:30:05 +02:00
|
|
|
class AssetStoreTest extends SapphireTest
|
|
|
|
{
|
2015-09-03 07:46:08 +02:00
|
|
|
|
2016-10-14 03:30:05 +02:00
|
|
|
public function setUp()
|
|
|
|
{
|
2015-09-03 07:46:08 +02:00
|
|
|
parent::setUp();
|
|
|
|
|
2015-09-15 04:52:02 +02:00
|
|
|
// Set backend and base url
|
2016-10-14 03:30:05 +02:00
|
|
|
/** @skipUpgrade */
|
|
|
|
TestAssetStore::activate('AssetStoreTest');
|
2015-09-03 07:46:08 +02:00
|
|
|
}
|
|
|
|
|
2016-10-14 03:30:05 +02:00
|
|
|
public function tearDown()
|
|
|
|
{
|
|
|
|
TestAssetStore::reset();
|
2015-09-03 07:46:08 +02:00
|
|
|
parent::tearDown();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2016-10-14 03:30:05 +02:00
|
|
|
* @return TestAssetStore
|
2015-09-03 07:46:08 +02:00
|
|
|
*/
|
2016-10-14 03:30:05 +02:00
|
|
|
protected function getBackend()
|
|
|
|
{
|
2015-09-03 07:46:08 +02:00
|
|
|
return Injector::inst()->get('AssetStore');
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Test different storage methods
|
|
|
|
*/
|
2016-10-14 03:30:05 +02:00
|
|
|
public function testStorageMethods()
|
|
|
|
{
|
2015-09-03 07:46:08 +02:00
|
|
|
$backend = $this->getBackend();
|
|
|
|
|
|
|
|
// Test setFromContent
|
|
|
|
$puppies1 = 'puppies';
|
|
|
|
$puppies1Tuple = $backend->setFromString($puppies1, 'pets/my-puppy.txt');
|
|
|
|
$this->assertEquals(
|
2016-10-14 03:30:05 +02:00
|
|
|
array(
|
2015-09-03 07:46:08 +02:00
|
|
|
'Hash' => '2a17a9cb4be918774e73ba83bd1c1e7d000fdd53',
|
|
|
|
'Filename' => 'pets/my-puppy.txt',
|
|
|
|
'Variant' => '',
|
|
|
|
),
|
|
|
|
$puppies1Tuple
|
|
|
|
);
|
|
|
|
|
|
|
|
// Test setFromStream (seekable)
|
2016-10-14 03:30:05 +02:00
|
|
|
$fish1 = realpath(__DIR__ . '/../../ORM/testimages/test-image-high-quality.jpg');
|
2015-09-03 07:46:08 +02:00
|
|
|
$fish1Stream = fopen($fish1, 'r');
|
|
|
|
$fish1Tuple = $backend->setFromStream($fish1Stream, 'parent/awesome-fish.jpg');
|
|
|
|
fclose($fish1Stream);
|
|
|
|
$this->assertEquals(
|
2016-10-14 03:30:05 +02:00
|
|
|
array(
|
2015-09-03 07:46:08 +02:00
|
|
|
'Hash' => 'a870de278b475cb75f5d9f451439b2d378e13af1',
|
|
|
|
'Filename' => 'parent/awesome-fish.jpg',
|
|
|
|
'Variant' => '',
|
|
|
|
),
|
|
|
|
$fish1Tuple
|
|
|
|
);
|
|
|
|
|
|
|
|
// Test with non-seekable streams
|
2016-10-14 03:30:05 +02:00
|
|
|
TestAssetStore::$seekable_override = false;
|
|
|
|
$fish2 = realpath(__DIR__ . '/../../ORM/testimages/test-image-low-quality.jpg');
|
2015-09-03 07:46:08 +02:00
|
|
|
$fish2Stream = fopen($fish2, 'r');
|
|
|
|
$fish2Tuple = $backend->setFromStream($fish2Stream, 'parent/mediocre-fish.jpg');
|
|
|
|
fclose($fish2Stream);
|
|
|
|
|
|
|
|
$this->assertEquals(
|
2016-10-14 03:30:05 +02:00
|
|
|
array(
|
2015-09-03 07:46:08 +02:00
|
|
|
'Hash' => '33be1b95cba0358fe54e8b13532162d52f97421c',
|
|
|
|
'Filename' => 'parent/mediocre-fish.jpg',
|
|
|
|
'Variant' => '',
|
|
|
|
),
|
|
|
|
$fish2Tuple
|
|
|
|
);
|
2016-10-14 03:30:05 +02:00
|
|
|
TestAssetStore::$seekable_override = null;
|
2015-09-03 07:46:08 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Test that the backend correctly resolves conflicts
|
|
|
|
*/
|
2016-10-14 03:30:05 +02:00
|
|
|
public function testConflictResolution()
|
|
|
|
{
|
2015-09-03 07:46:08 +02:00
|
|
|
$backend = $this->getBackend();
|
|
|
|
|
|
|
|
// Put a file in
|
2016-10-14 03:30:05 +02:00
|
|
|
$fish1 = realpath(__DIR__ . '/../../ORM/testimages/test-image-high-quality.jpg');
|
2015-09-03 07:46:08 +02:00
|
|
|
$this->assertFileExists($fish1);
|
|
|
|
$fish1Tuple = $backend->setFromLocalFile($fish1, 'directory/lovely-fish.jpg');
|
|
|
|
$this->assertEquals(
|
|
|
|
array(
|
|
|
|
'Hash' => 'a870de278b475cb75f5d9f451439b2d378e13af1',
|
|
|
|
'Filename' => 'directory/lovely-fish.jpg',
|
|
|
|
'Variant' => '',
|
|
|
|
),
|
|
|
|
$fish1Tuple
|
|
|
|
);
|
|
|
|
$this->assertEquals(
|
2015-12-09 22:19:23 +01:00
|
|
|
'/assets/AssetStoreTest/directory/a870de278b/lovely-fish.jpg',
|
2015-09-15 04:52:02 +02:00
|
|
|
$backend->getAsURL($fish1Tuple['Filename'], $fish1Tuple['Hash'])
|
2015-09-03 07:46:08 +02:00
|
|
|
);
|
|
|
|
|
|
|
|
// Write a different file with same name. Should not detect duplicates since sha are different
|
2016-10-14 03:30:05 +02:00
|
|
|
$fish2 = realpath(__DIR__ . '/../../ORM/testimages/test-image-low-quality.jpg');
|
2015-09-03 07:46:08 +02:00
|
|
|
try {
|
2015-12-09 22:19:23 +01:00
|
|
|
$fish2Tuple = $backend->setFromLocalFile(
|
|
|
|
$fish2,
|
|
|
|
'directory/lovely-fish.jpg',
|
|
|
|
null,
|
|
|
|
null,
|
|
|
|
array('conflict' => AssetStore::CONFLICT_EXCEPTION)
|
|
|
|
);
|
2016-10-14 03:30:05 +02:00
|
|
|
} catch (Exception $ex) {
|
2016-08-19 00:51:35 +02:00
|
|
|
$this->fail('Writing file with different sha to same location failed with exception');
|
|
|
|
return;
|
2015-09-03 07:46:08 +02:00
|
|
|
}
|
|
|
|
$this->assertEquals(
|
|
|
|
array(
|
|
|
|
'Hash' => '33be1b95cba0358fe54e8b13532162d52f97421c',
|
|
|
|
'Filename' => 'directory/lovely-fish.jpg',
|
|
|
|
'Variant' => '',
|
|
|
|
),
|
|
|
|
$fish2Tuple
|
|
|
|
);
|
|
|
|
$this->assertEquals(
|
2015-12-09 22:19:23 +01:00
|
|
|
'/assets/AssetStoreTest/directory/33be1b95cb/lovely-fish.jpg',
|
2015-09-15 04:52:02 +02:00
|
|
|
$backend->getAsURL($fish2Tuple['Filename'], $fish2Tuple['Hash'])
|
2015-09-03 07:46:08 +02:00
|
|
|
);
|
|
|
|
|
|
|
|
// Write original file back with rename
|
|
|
|
$this->assertFileExists($fish1);
|
2015-12-09 22:19:23 +01:00
|
|
|
$fish3Tuple = $backend->setFromLocalFile(
|
|
|
|
$fish1,
|
|
|
|
'directory/lovely-fish.jpg',
|
|
|
|
null,
|
|
|
|
null,
|
|
|
|
array('conflict' => AssetStore::CONFLICT_RENAME)
|
|
|
|
);
|
2015-09-03 07:46:08 +02:00
|
|
|
$this->assertEquals(
|
|
|
|
array(
|
|
|
|
'Hash' => 'a870de278b475cb75f5d9f451439b2d378e13af1',
|
|
|
|
'Filename' => 'directory/lovely-fish-v2.jpg',
|
|
|
|
'Variant' => '',
|
|
|
|
),
|
|
|
|
$fish3Tuple
|
|
|
|
);
|
|
|
|
$this->assertEquals(
|
2015-12-09 22:19:23 +01:00
|
|
|
'/assets/AssetStoreTest/directory/a870de278b/lovely-fish-v2.jpg',
|
2015-09-15 04:52:02 +02:00
|
|
|
$backend->getAsURL($fish3Tuple['Filename'], $fish3Tuple['Hash'])
|
2015-09-03 07:46:08 +02:00
|
|
|
);
|
|
|
|
|
|
|
|
// Write another file should increment to -v3
|
2015-12-09 22:19:23 +01:00
|
|
|
$fish4Tuple = $backend->setFromLocalFile(
|
|
|
|
$fish1,
|
|
|
|
'directory/lovely-fish-v2.jpg',
|
|
|
|
null,
|
|
|
|
null,
|
|
|
|
array('conflict' => AssetStore::CONFLICT_RENAME)
|
|
|
|
);
|
2015-09-03 07:46:08 +02:00
|
|
|
$this->assertEquals(
|
|
|
|
array(
|
|
|
|
'Hash' => 'a870de278b475cb75f5d9f451439b2d378e13af1',
|
|
|
|
'Filename' => 'directory/lovely-fish-v3.jpg',
|
|
|
|
'Variant' => '',
|
|
|
|
),
|
|
|
|
$fish4Tuple
|
|
|
|
);
|
|
|
|
$this->assertEquals(
|
2015-12-09 22:19:23 +01:00
|
|
|
'/assets/AssetStoreTest/directory/a870de278b/lovely-fish-v3.jpg',
|
2015-09-15 04:52:02 +02:00
|
|
|
$backend->getAsURL($fish4Tuple['Filename'], $fish4Tuple['Hash'])
|
2015-09-03 07:46:08 +02:00
|
|
|
);
|
|
|
|
|
|
|
|
// Test conflict use existing file
|
2015-12-09 22:19:23 +01:00
|
|
|
$fish5Tuple = $backend->setFromLocalFile(
|
|
|
|
$fish1,
|
|
|
|
'directory/lovely-fish.jpg',
|
|
|
|
null,
|
|
|
|
null,
|
|
|
|
array('conflict' => AssetStore::CONFLICT_USE_EXISTING)
|
|
|
|
);
|
2015-09-03 07:46:08 +02:00
|
|
|
$this->assertEquals(
|
|
|
|
array(
|
|
|
|
'Hash' => 'a870de278b475cb75f5d9f451439b2d378e13af1',
|
|
|
|
'Filename' => 'directory/lovely-fish.jpg',
|
|
|
|
'Variant' => '',
|
|
|
|
),
|
|
|
|
$fish5Tuple
|
|
|
|
);
|
|
|
|
$this->assertEquals(
|
2015-12-09 22:19:23 +01:00
|
|
|
'/assets/AssetStoreTest/directory/a870de278b/lovely-fish.jpg',
|
2015-09-15 04:52:02 +02:00
|
|
|
$backend->getAsURL($fish5Tuple['Filename'], $fish5Tuple['Hash'])
|
2015-09-03 07:46:08 +02:00
|
|
|
);
|
|
|
|
|
|
|
|
// Test conflict use existing file
|
2015-12-09 22:19:23 +01:00
|
|
|
$fish6Tuple = $backend->setFromLocalFile(
|
|
|
|
$fish1,
|
|
|
|
'directory/lovely-fish.jpg',
|
|
|
|
null,
|
|
|
|
null,
|
|
|
|
array('conflict' => AssetStore::CONFLICT_OVERWRITE)
|
|
|
|
);
|
2015-09-03 07:46:08 +02:00
|
|
|
$this->assertEquals(
|
|
|
|
array(
|
|
|
|
'Hash' => 'a870de278b475cb75f5d9f451439b2d378e13af1',
|
|
|
|
'Filename' => 'directory/lovely-fish.jpg',
|
|
|
|
'Variant' => '',
|
|
|
|
),
|
|
|
|
$fish6Tuple
|
|
|
|
);
|
|
|
|
$this->assertEquals(
|
2015-12-09 22:19:23 +01:00
|
|
|
'/assets/AssetStoreTest/directory/a870de278b/lovely-fish.jpg',
|
2015-09-15 04:52:02 +02:00
|
|
|
$backend->getAsURL($fish6Tuple['Filename'], $fish6Tuple['Hash'])
|
2015-09-03 07:46:08 +02:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Test that flysystem can regenerate the original filename from fileID
|
|
|
|
*/
|
2016-10-14 03:30:05 +02:00
|
|
|
public function testGetOriginalFilename()
|
|
|
|
{
|
|
|
|
$store = new TestAssetStore();
|
2015-09-03 07:46:08 +02:00
|
|
|
$this->assertEquals(
|
|
|
|
'directory/lovely-fish.jpg',
|
2015-12-09 22:19:23 +01:00
|
|
|
$store->getOriginalFilename('directory/a870de278b/lovely-fish.jpg')
|
2015-09-03 07:46:08 +02:00
|
|
|
);
|
|
|
|
$this->assertEquals(
|
|
|
|
'directory/lovely-fish.jpg',
|
2015-12-09 22:19:23 +01:00
|
|
|
$store->getOriginalFilename('directory/a870de278b/lovely-fish__variant.jpg')
|
2015-09-03 07:46:08 +02:00
|
|
|
);
|
|
|
|
$this->assertEquals(
|
|
|
|
'directory/lovely_fish.jpg',
|
2015-12-09 22:19:23 +01:00
|
|
|
$store->getOriginalFilename('directory/a870de278b/lovely_fish__vari_ant.jpg')
|
2015-09-03 07:46:08 +02:00
|
|
|
);
|
|
|
|
$this->assertEquals(
|
|
|
|
'directory/lovely_fish.jpg',
|
2015-12-09 22:19:23 +01:00
|
|
|
$store->getOriginalFilename('directory/a870de278b/lovely_fish.jpg')
|
2015-09-03 07:46:08 +02:00
|
|
|
);
|
|
|
|
$this->assertEquals(
|
|
|
|
'lovely-fish.jpg',
|
2015-12-09 22:19:23 +01:00
|
|
|
$store->getOriginalFilename('a870de278b/lovely-fish.jpg')
|
2015-09-03 07:46:08 +02:00
|
|
|
);
|
|
|
|
$this->assertEquals(
|
|
|
|
'lovely-fish.jpg',
|
2015-12-09 22:19:23 +01:00
|
|
|
$store->getOriginalFilename('a870de278b/lovely-fish__variant.jpg')
|
2015-09-03 07:46:08 +02:00
|
|
|
);
|
|
|
|
$this->assertEquals(
|
|
|
|
'lovely_fish.jpg',
|
2015-12-09 22:19:23 +01:00
|
|
|
$store->getOriginalFilename('a870de278b/lovely_fish__vari__ant.jpg')
|
2015-09-03 07:46:08 +02:00
|
|
|
);
|
|
|
|
$this->assertEquals(
|
|
|
|
'lovely_fish.jpg',
|
2015-12-09 22:19:23 +01:00
|
|
|
$store->getOriginalFilename('a870de278b/lovely_fish.jpg')
|
2015-09-03 07:46:08 +02:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Test internal file Id generation
|
|
|
|
*/
|
2016-10-14 03:30:05 +02:00
|
|
|
public function testGetFileID()
|
|
|
|
{
|
|
|
|
$store = new TestAssetStore();
|
2015-09-03 07:46:08 +02:00
|
|
|
$this->assertEquals(
|
|
|
|
'directory/2a17a9cb4b/file.jpg',
|
2015-09-15 04:52:02 +02:00
|
|
|
$store->getFileID('directory/file.jpg', sha1('puppies'))
|
2015-09-03 07:46:08 +02:00
|
|
|
);
|
|
|
|
$this->assertEquals(
|
|
|
|
'2a17a9cb4b/file.jpg',
|
2015-09-15 04:52:02 +02:00
|
|
|
$store->getFileID('file.jpg', sha1('puppies'))
|
2015-09-03 07:46:08 +02:00
|
|
|
);
|
|
|
|
$this->assertEquals(
|
|
|
|
'dir_ectory/2a17a9cb4b/fil_e.jpg',
|
2015-09-15 04:52:02 +02:00
|
|
|
$store->getFileID('dir__ectory/fil__e.jpg', sha1('puppies'))
|
2015-09-03 07:46:08 +02:00
|
|
|
);
|
|
|
|
$this->assertEquals(
|
|
|
|
'directory/2a17a9cb4b/file_variant.jpg',
|
2015-09-15 04:52:02 +02:00
|
|
|
$store->getFileID('directory/file__variant.jpg', sha1('puppies'), null)
|
2015-09-03 07:46:08 +02:00
|
|
|
);
|
|
|
|
$this->assertEquals(
|
|
|
|
'directory/2a17a9cb4b/file__variant.jpg',
|
2015-09-15 04:52:02 +02:00
|
|
|
$store->getFileID('directory/file.jpg', sha1('puppies'), 'variant')
|
2015-09-03 07:46:08 +02:00
|
|
|
);
|
|
|
|
$this->assertEquals(
|
|
|
|
'2a17a9cb4b/file__var__iant.jpg',
|
2015-09-15 04:52:02 +02:00
|
|
|
$store->getFileID('file.jpg', sha1('puppies'), 'var__iant')
|
2015-09-03 07:46:08 +02:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2016-10-14 03:30:05 +02:00
|
|
|
public function testGetMetadata()
|
|
|
|
{
|
2015-09-03 07:46:08 +02:00
|
|
|
$backend = $this->getBackend();
|
|
|
|
|
|
|
|
// jpg
|
2016-10-14 03:30:05 +02:00
|
|
|
$fish = realpath(__DIR__ . '/../../ORM/testimages/test-image-high-quality.jpg');
|
2015-09-03 07:46:08 +02:00
|
|
|
$fishTuple = $backend->setFromLocalFile($fish, 'parent/awesome-fish.jpg');
|
|
|
|
$this->assertEquals(
|
|
|
|
'image/jpeg',
|
2015-09-15 04:52:02 +02:00
|
|
|
$backend->getMimeType($fishTuple['Filename'], $fishTuple['Hash'])
|
2015-09-03 07:46:08 +02:00
|
|
|
);
|
2015-09-15 04:52:02 +02:00
|
|
|
$fishMeta = $backend->getMetadata($fishTuple['Filename'], $fishTuple['Hash']);
|
2015-09-03 07:46:08 +02:00
|
|
|
$this->assertEquals(151889, $fishMeta['size']);
|
|
|
|
$this->assertEquals('file', $fishMeta['type']);
|
|
|
|
$this->assertNotEmpty($fishMeta['timestamp']);
|
|
|
|
|
|
|
|
// text
|
|
|
|
$puppies = 'puppies';
|
|
|
|
$puppiesTuple = $backend->setFromString($puppies, 'pets/my-puppy.txt');
|
|
|
|
$this->assertEquals(
|
|
|
|
'text/plain',
|
2015-09-15 04:52:02 +02:00
|
|
|
$backend->getMimeType($puppiesTuple['Filename'], $puppiesTuple['Hash'])
|
2015-09-03 07:46:08 +02:00
|
|
|
);
|
2015-09-15 04:52:02 +02:00
|
|
|
$puppiesMeta = $backend->getMetadata($puppiesTuple['Filename'], $puppiesTuple['Hash']);
|
2015-09-03 07:46:08 +02:00
|
|
|
$this->assertEquals(7, $puppiesMeta['size']);
|
|
|
|
$this->assertEquals('file', $puppiesMeta['type']);
|
|
|
|
$this->assertNotEmpty($puppiesMeta['timestamp']);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Test that legacy filenames work as expected
|
|
|
|
*/
|
2016-10-14 03:30:05 +02:00
|
|
|
public function testLegacyFilenames()
|
|
|
|
{
|
2015-09-03 07:46:08 +02:00
|
|
|
Config::inst()->update(get_class(new FlysystemAssetStore()), 'legacy_filenames', true);
|
|
|
|
|
|
|
|
$backend = $this->getBackend();
|
|
|
|
|
|
|
|
// Put a file in
|
2016-10-14 03:30:05 +02:00
|
|
|
$fish1 = realpath(__DIR__ . '/../../ORM/testimages/test-image-high-quality.jpg');
|
2015-09-03 07:46:08 +02:00
|
|
|
$this->assertFileExists($fish1);
|
|
|
|
$fish1Tuple = $backend->setFromLocalFile($fish1, 'directory/lovely-fish.jpg');
|
|
|
|
$this->assertEquals(
|
|
|
|
array(
|
|
|
|
'Hash' => 'a870de278b475cb75f5d9f451439b2d378e13af1',
|
|
|
|
'Filename' => 'directory/lovely-fish.jpg',
|
|
|
|
'Variant' => '',
|
|
|
|
),
|
|
|
|
$fish1Tuple
|
|
|
|
);
|
|
|
|
$this->assertEquals(
|
2015-12-09 22:19:23 +01:00
|
|
|
'/assets/AssetStoreTest/directory/lovely-fish.jpg',
|
2015-09-15 04:52:02 +02:00
|
|
|
$backend->getAsURL($fish1Tuple['Filename'], $fish1Tuple['Hash'])
|
2015-09-03 07:46:08 +02:00
|
|
|
);
|
|
|
|
|
|
|
|
// Write a different file with same name.
|
|
|
|
// Since we are using legacy filenames, this should generate a new filename
|
2016-10-14 03:30:05 +02:00
|
|
|
$fish2 = realpath(__DIR__ . '/../../ORM/testimages/test-image-low-quality.jpg');
|
2015-09-03 07:46:08 +02:00
|
|
|
try {
|
2015-12-09 22:19:23 +01:00
|
|
|
$backend->setFromLocalFile(
|
|
|
|
$fish2,
|
|
|
|
'directory/lovely-fish.jpg',
|
|
|
|
null,
|
|
|
|
null,
|
|
|
|
array('conflict' => AssetStore::CONFLICT_EXCEPTION)
|
|
|
|
);
|
2016-08-19 00:51:35 +02:00
|
|
|
$this->fail('Writing file with different sha to same location should throw exception');
|
|
|
|
return;
|
2016-10-14 03:30:05 +02:00
|
|
|
} catch (Exception $ex) {
|
2015-09-03 07:46:08 +02:00
|
|
|
// Success
|
|
|
|
}
|
|
|
|
|
|
|
|
// Re-attempt this file write with conflict_rename
|
2015-12-09 22:19:23 +01:00
|
|
|
$fish3Tuple = $backend->setFromLocalFile(
|
|
|
|
$fish2,
|
|
|
|
'directory/lovely-fish.jpg',
|
|
|
|
null,
|
|
|
|
null,
|
|
|
|
array('conflict' => AssetStore::CONFLICT_RENAME)
|
|
|
|
);
|
2015-09-03 07:46:08 +02:00
|
|
|
$this->assertEquals(
|
|
|
|
array(
|
|
|
|
'Hash' => '33be1b95cba0358fe54e8b13532162d52f97421c',
|
|
|
|
'Filename' => 'directory/lovely-fish-v2.jpg',
|
|
|
|
'Variant' => '',
|
|
|
|
),
|
|
|
|
$fish3Tuple
|
|
|
|
);
|
|
|
|
$this->assertEquals(
|
2015-12-09 22:19:23 +01:00
|
|
|
'/assets/AssetStoreTest/directory/lovely-fish-v2.jpg',
|
2015-09-15 04:52:02 +02:00
|
|
|
$backend->getAsURL($fish3Tuple['Filename'], $fish3Tuple['Hash'])
|
2015-09-03 07:46:08 +02:00
|
|
|
);
|
|
|
|
|
|
|
|
// Write back original file, but with CONFLICT_EXISTING. The file should not change
|
2015-12-09 22:19:23 +01:00
|
|
|
$fish4Tuple = $backend->setFromLocalFile(
|
|
|
|
$fish1,
|
|
|
|
'directory/lovely-fish-v2.jpg',
|
|
|
|
null,
|
|
|
|
null,
|
|
|
|
array('conflict' => AssetStore::CONFLICT_USE_EXISTING)
|
|
|
|
);
|
2015-09-03 07:46:08 +02:00
|
|
|
$this->assertEquals(
|
|
|
|
array(
|
|
|
|
'Hash' => '33be1b95cba0358fe54e8b13532162d52f97421c',
|
|
|
|
'Filename' => 'directory/lovely-fish-v2.jpg',
|
|
|
|
'Variant' => '',
|
|
|
|
),
|
|
|
|
$fish4Tuple
|
|
|
|
);
|
|
|
|
$this->assertEquals(
|
2015-12-09 22:19:23 +01:00
|
|
|
'/assets/AssetStoreTest/directory/lovely-fish-v2.jpg',
|
2015-09-15 04:52:02 +02:00
|
|
|
$backend->getAsURL($fish4Tuple['Filename'], $fish4Tuple['Hash'])
|
2015-09-03 07:46:08 +02:00
|
|
|
);
|
|
|
|
|
|
|
|
// Write back original file with CONFLICT_OVERWRITE. The file sha should now be updated
|
2015-12-09 22:19:23 +01:00
|
|
|
$fish5Tuple = $backend->setFromLocalFile(
|
|
|
|
$fish1,
|
|
|
|
'directory/lovely-fish-v2.jpg',
|
|
|
|
null,
|
|
|
|
null,
|
|
|
|
array('conflict' => AssetStore::CONFLICT_OVERWRITE)
|
|
|
|
);
|
2015-09-03 07:46:08 +02:00
|
|
|
$this->assertEquals(
|
|
|
|
array(
|
|
|
|
'Hash' => 'a870de278b475cb75f5d9f451439b2d378e13af1',
|
|
|
|
'Filename' => 'directory/lovely-fish-v2.jpg',
|
|
|
|
'Variant' => '',
|
|
|
|
),
|
|
|
|
$fish5Tuple
|
|
|
|
);
|
|
|
|
$this->assertEquals(
|
2015-12-09 22:19:23 +01:00
|
|
|
'/assets/AssetStoreTest/directory/lovely-fish-v2.jpg',
|
2015-09-15 04:52:02 +02:00
|
|
|
$backend->getAsURL($fish5Tuple['Filename'], $fish5Tuple['Hash'])
|
2015-09-03 07:46:08 +02:00
|
|
|
);
|
|
|
|
}
|
2015-09-15 04:52:02 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Test default conflict resolution
|
|
|
|
*/
|
2016-10-14 03:30:05 +02:00
|
|
|
public function testDefaultConflictResolution()
|
|
|
|
{
|
2015-09-15 04:52:02 +02:00
|
|
|
$store = $this->getBackend();
|
|
|
|
|
|
|
|
// Disable legacy filenames
|
|
|
|
Config::inst()->update(get_class(new FlysystemAssetStore()), 'legacy_filenames', false);
|
|
|
|
$this->assertEquals(AssetStore::CONFLICT_OVERWRITE, $store->getDefaultConflictResolution(null));
|
|
|
|
$this->assertEquals(AssetStore::CONFLICT_OVERWRITE, $store->getDefaultConflictResolution('somevariant'));
|
|
|
|
|
|
|
|
// Enable legacy filenames
|
|
|
|
Config::inst()->update(get_class(new FlysystemAssetStore()), 'legacy_filenames', true);
|
|
|
|
$this->assertEquals(AssetStore::CONFLICT_RENAME, $store->getDefaultConflictResolution(null));
|
|
|
|
$this->assertEquals(AssetStore::CONFLICT_OVERWRITE, $store->getDefaultConflictResolution('somevariant'));
|
|
|
|
}
|
2015-12-09 22:19:23 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Test protect / publish mechanisms
|
|
|
|
*/
|
2016-10-14 03:30:05 +02:00
|
|
|
public function testProtect()
|
|
|
|
{
|
2015-12-09 22:19:23 +01:00
|
|
|
$backend = $this->getBackend();
|
2016-10-14 03:30:05 +02:00
|
|
|
$fish = realpath(__DIR__ . '/../../ORM/testimages/test-image-high-quality.jpg');
|
2015-12-09 22:19:23 +01:00
|
|
|
$fishTuple = $backend->setFromLocalFile($fish, 'parent/lovely-fish.jpg');
|
|
|
|
$fishVariantTuple = $backend->setFromLocalFile($fish, $fishTuple['Filename'], $fishTuple['Hash'], 'copy');
|
|
|
|
|
|
|
|
// Test public file storage
|
|
|
|
$this->assertFileExists(ASSETS_PATH . '/AssetStoreTest/parent/a870de278b/lovely-fish.jpg');
|
|
|
|
$this->assertFileExists(ASSETS_PATH . '/AssetStoreTest/parent/a870de278b/lovely-fish__copy.jpg');
|
|
|
|
$this->assertEquals(
|
|
|
|
AssetStore::VISIBILITY_PUBLIC,
|
|
|
|
$backend->getVisibility($fishTuple['Filename'], $fishTuple['Hash'])
|
|
|
|
);
|
|
|
|
$this->assertEquals(
|
|
|
|
'/assets/AssetStoreTest/parent/a870de278b/lovely-fish.jpg',
|
|
|
|
$backend->getAsURL($fishTuple['Filename'], $fishTuple['Hash'])
|
|
|
|
);
|
|
|
|
$this->assertEquals(
|
|
|
|
'/assets/AssetStoreTest/parent/a870de278b/lovely-fish__copy.jpg',
|
|
|
|
$backend->getAsURL($fishVariantTuple['Filename'], $fishVariantTuple['Hash'], $fishVariantTuple['Variant'])
|
|
|
|
);
|
|
|
|
|
|
|
|
// Test access rights to public files cannot be revoked
|
|
|
|
$backend->revoke($fishTuple['Filename'], $fishTuple['Hash']); // can't revoke public assets
|
|
|
|
$this->assertTrue($backend->canView($fishTuple['Filename'], $fishTuple['Hash']));
|
|
|
|
|
|
|
|
// Test protected file storage
|
|
|
|
$backend->protect($fishTuple['Filename'], $fishTuple['Hash']);
|
|
|
|
$this->assertFileNotExists(ASSETS_PATH . '/AssetStoreTest/parent/a870de278b/lovely-fish.jpg');
|
|
|
|
$this->assertFileNotExists(ASSETS_PATH . '/AssetStoreTest/parent/a870de278b/lovely-fish__copy.jpg');
|
|
|
|
$this->assertFileExists(ASSETS_PATH . '/AssetStoreTest/.protected/parent/a870de278b/lovely-fish.jpg');
|
|
|
|
$this->assertFileExists(ASSETS_PATH . '/AssetStoreTest/.protected/parent/a870de278b/lovely-fish__copy.jpg');
|
|
|
|
$this->assertEquals(
|
|
|
|
AssetStore::VISIBILITY_PROTECTED,
|
|
|
|
$backend->getVisibility($fishTuple['Filename'], $fishTuple['Hash'])
|
|
|
|
);
|
|
|
|
|
|
|
|
// Test access rights
|
|
|
|
$backend->revoke($fishTuple['Filename'], $fishTuple['Hash']);
|
|
|
|
$this->assertFalse($backend->canView($fishTuple['Filename'], $fishTuple['Hash']));
|
|
|
|
$backend->grant($fishTuple['Filename'], $fishTuple['Hash']);
|
|
|
|
$this->assertTrue($backend->canView($fishTuple['Filename'], $fishTuple['Hash']));
|
|
|
|
|
|
|
|
// Protected urls should go through asset routing mechanism
|
|
|
|
$this->assertEquals(
|
|
|
|
'/assets/parent/a870de278b/lovely-fish.jpg',
|
|
|
|
$backend->getAsURL($fishTuple['Filename'], $fishTuple['Hash'])
|
|
|
|
);
|
|
|
|
$this->assertEquals(
|
|
|
|
'/assets/parent/a870de278b/lovely-fish__copy.jpg',
|
|
|
|
$backend->getAsURL($fishVariantTuple['Filename'], $fishVariantTuple['Hash'], $fishVariantTuple['Variant'])
|
|
|
|
);
|
|
|
|
|
|
|
|
// Publish reverts visibility
|
|
|
|
$backend->publish($fishTuple['Filename'], $fishTuple['Hash']);
|
|
|
|
$this->assertFileExists(ASSETS_PATH . '/AssetStoreTest/parent/a870de278b/lovely-fish.jpg');
|
|
|
|
$this->assertFileExists(ASSETS_PATH . '/AssetStoreTest/parent/a870de278b/lovely-fish__copy.jpg');
|
|
|
|
$this->assertFileNotExists(ASSETS_PATH . '/AssetStoreTest/.protected/parent/a870de278b/lovely-fish.jpg');
|
|
|
|
$this->assertFileNotExists(ASSETS_PATH . '/AssetStoreTest/.protected/parent/a870de278b/lovely-fish__copy.jpg');
|
|
|
|
$this->assertEquals(
|
|
|
|
AssetStore::VISIBILITY_PUBLIC,
|
|
|
|
$backend->getVisibility($fishTuple['Filename'], $fishTuple['Hash'])
|
|
|
|
);
|
|
|
|
}
|
2015-09-03 07:46:08 +02:00
|
|
|
}
|