2015-09-03 07:46:08 +02:00
|
|
|
<?php
|
|
|
|
|
2015-12-09 22:19:23 +01:00
|
|
|
use League\Flysystem\Adapter\Local;
|
|
|
|
use League\Flysystem\AdapterInterface;
|
2015-09-03 07:46:08 +02:00
|
|
|
use League\Flysystem\Filesystem;
|
|
|
|
use SilverStripe\Filesystem\Flysystem\FlysystemAssetStore;
|
|
|
|
use SilverStripe\Filesystem\Flysystem\FlysystemUrlPlugin;
|
2015-12-09 22:19:23 +01:00
|
|
|
use SilverStripe\Filesystem\Flysystem\ProtectedAssetAdapter;
|
|
|
|
use SilverStripe\Filesystem\Flysystem\PublicAssetAdapter;
|
2015-09-15 04:52:02 +02:00
|
|
|
use SilverStripe\Filesystem\Storage\AssetContainer;
|
2015-09-03 07:46:08 +02:00
|
|
|
use SilverStripe\Filesystem\Storage\AssetStore;
|
2015-11-26 03:19:43 +01:00
|
|
|
use SilverStripe\Filesystem\Storage\FlysystemGeneratedAssetHandler;
|
2015-09-03 07:46:08 +02:00
|
|
|
|
|
|
|
class AssetStoreTest extends SapphireTest {
|
|
|
|
|
|
|
|
public function setUp() {
|
|
|
|
parent::setUp();
|
|
|
|
|
2015-09-15 04:52:02 +02:00
|
|
|
// Set backend and base url
|
2015-12-09 22:19:23 +01:00
|
|
|
AssetStoreTest_SpyStore::activate('AssetStoreTest');
|
2015-09-03 07:46:08 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public function tearDown() {
|
2015-09-15 04:52:02 +02:00
|
|
|
AssetStoreTest_SpyStore::reset();
|
2015-09-03 07:46:08 +02:00
|
|
|
parent::tearDown();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2015-09-15 04:52:02 +02:00
|
|
|
* @return AssetStoreTest_SpyStore
|
2015-09-03 07:46:08 +02:00
|
|
|
*/
|
|
|
|
protected function getBackend() {
|
|
|
|
return Injector::inst()->get('AssetStore');
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Test different storage methods
|
|
|
|
*/
|
|
|
|
public function testStorageMethods() {
|
|
|
|
$backend = $this->getBackend();
|
|
|
|
|
|
|
|
// Test setFromContent
|
|
|
|
$puppies1 = 'puppies';
|
|
|
|
$puppies1Tuple = $backend->setFromString($puppies1, 'pets/my-puppy.txt');
|
|
|
|
$this->assertEquals(
|
|
|
|
array (
|
|
|
|
'Hash' => '2a17a9cb4be918774e73ba83bd1c1e7d000fdd53',
|
|
|
|
'Filename' => 'pets/my-puppy.txt',
|
|
|
|
'Variant' => '',
|
|
|
|
),
|
|
|
|
$puppies1Tuple
|
|
|
|
);
|
|
|
|
|
|
|
|
// Test setFromStream (seekable)
|
2015-09-15 04:52:02 +02:00
|
|
|
$fish1 = realpath(__DIR__ .'/../model/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(
|
|
|
|
array (
|
|
|
|
'Hash' => 'a870de278b475cb75f5d9f451439b2d378e13af1',
|
|
|
|
'Filename' => 'parent/awesome-fish.jpg',
|
|
|
|
'Variant' => '',
|
|
|
|
),
|
|
|
|
$fish1Tuple
|
|
|
|
);
|
|
|
|
|
|
|
|
// Test with non-seekable streams
|
|
|
|
AssetStoreTest_SpyStore::$seekable_override = false;
|
2015-09-15 04:52:02 +02:00
|
|
|
$fish2 = realpath(__DIR__ .'/../model/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(
|
|
|
|
array (
|
|
|
|
'Hash' => '33be1b95cba0358fe54e8b13532162d52f97421c',
|
|
|
|
'Filename' => 'parent/mediocre-fish.jpg',
|
|
|
|
'Variant' => '',
|
|
|
|
),
|
|
|
|
$fish2Tuple
|
|
|
|
);
|
|
|
|
AssetStoreTest_SpyStore::$seekable_override = null;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Test that the backend correctly resolves conflicts
|
|
|
|
*/
|
|
|
|
public function testConflictResolution() {
|
|
|
|
$backend = $this->getBackend();
|
|
|
|
|
|
|
|
// Put a file in
|
2015-09-15 04:52:02 +02:00
|
|
|
$fish1 = realpath(__DIR__ .'/../model/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
|
2015-09-15 04:52:02 +02:00
|
|
|
$fish2 = realpath(__DIR__ .'/../model/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)
|
|
|
|
);
|
2015-09-03 07:46:08 +02:00
|
|
|
} catch(Exception $ex) {
|
|
|
|
return $this->fail('Writing file with different sha to same location failed with exception');
|
|
|
|
}
|
|
|
|
$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
|
|
|
|
*/
|
|
|
|
public function testGetOriginalFilename() {
|
|
|
|
$store = new AssetStoreTest_SpyStore();
|
|
|
|
$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
|
|
|
|
*/
|
|
|
|
public function testGetFileID() {
|
|
|
|
$store = new AssetStoreTest_SpyStore();
|
|
|
|
$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
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testGetMetadata() {
|
|
|
|
$backend = $this->getBackend();
|
|
|
|
|
|
|
|
// jpg
|
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
|
|
|
$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
|
|
|
|
*/
|
|
|
|
public function testLegacyFilenames() {
|
|
|
|
Config::inst()->update(get_class(new FlysystemAssetStore()), 'legacy_filenames', true);
|
|
|
|
|
|
|
|
$backend = $this->getBackend();
|
|
|
|
|
|
|
|
// Put a file in
|
2015-09-15 04:52:02 +02:00
|
|
|
$fish1 = realpath(__DIR__ .'/../model/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
|
2015-09-15 04:52:02 +02:00
|
|
|
$fish2 = realpath(__DIR__ .'/../model/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)
|
|
|
|
);
|
2015-09-03 07:46:08 +02:00
|
|
|
return $this->fail('Writing file with different sha to same location should throw exception');
|
|
|
|
} catch(Exception $ex) {
|
|
|
|
// 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
|
|
|
|
*/
|
|
|
|
public function testDefaultConflictResolution() {
|
|
|
|
$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
|
|
|
|
*/
|
|
|
|
public function testProtect() {
|
|
|
|
$backend = $this->getBackend();
|
|
|
|
$fish = realpath(__DIR__ .'/../model/testimages/test-image-high-quality.jpg');
|
|
|
|
$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
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Spy!
|
|
|
|
*/
|
|
|
|
class AssetStoreTest_SpyStore extends FlysystemAssetStore {
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Set to true|false to override all isSeekableStream calls
|
2015-09-15 04:52:02 +02:00
|
|
|
*
|
2015-09-03 07:46:08 +02:00
|
|
|
* @var null|bool
|
|
|
|
*/
|
|
|
|
public static $seekable_override = null;
|
|
|
|
|
2015-09-15 04:52:02 +02:00
|
|
|
/**
|
|
|
|
* Base dir of current file
|
|
|
|
*
|
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
public static $basedir = null;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Set this store as the new asset backend
|
|
|
|
*
|
|
|
|
* @param string $basedir Basedir to store assets, which will be placed beneath 'assets' folder
|
|
|
|
*/
|
|
|
|
public static function activate($basedir) {
|
|
|
|
// Assign this as the new store
|
2015-12-09 22:19:23 +01:00
|
|
|
$publicAdapter = new PublicAssetAdapter(ASSETS_PATH . '/' . $basedir);
|
|
|
|
$publicFilesystem = new Filesystem($publicAdapter, [
|
|
|
|
'visibility' => AdapterInterface::VISIBILITY_PUBLIC
|
|
|
|
]);
|
|
|
|
$protectedAdapter = new ProtectedAssetAdapter(ASSETS_PATH . '/' . $basedir . '/.protected');
|
|
|
|
$protectedFilesystem = new Filesystem($protectedAdapter, [
|
|
|
|
'visibility' => AdapterInterface::VISIBILITY_PRIVATE
|
|
|
|
]);
|
|
|
|
|
2015-09-15 04:52:02 +02:00
|
|
|
$backend = new AssetStoreTest_SpyStore();
|
2015-12-09 22:19:23 +01:00
|
|
|
$backend->setPublicFilesystem($publicFilesystem);
|
|
|
|
$backend->setProtectedFilesystem($protectedFilesystem);
|
2015-09-15 04:52:02 +02:00
|
|
|
Injector::inst()->registerService($backend, 'AssetStore');
|
|
|
|
|
2015-11-26 03:19:43 +01:00
|
|
|
// Assign flysystem backend to generated asset handler at the same time
|
|
|
|
$generated = new FlysystemGeneratedAssetHandler();
|
2015-12-09 22:19:23 +01:00
|
|
|
$generated->setFilesystem($publicFilesystem);
|
2015-11-26 03:19:43 +01:00
|
|
|
Injector::inst()->registerService($generated, 'GeneratedAssetHandler');
|
2015-11-30 03:03:46 +01:00
|
|
|
Requirements::backend()->setAssetHandler($generated);
|
2015-11-26 03:19:43 +01:00
|
|
|
|
2015-09-15 04:52:02 +02:00
|
|
|
// Disable legacy and set defaults
|
|
|
|
Config::inst()->remove(get_class(new FlysystemAssetStore()), 'legacy_filenames');
|
|
|
|
Config::inst()->update('Director', 'alternate_base_url', '/');
|
|
|
|
DBFile::config()->force_resample = false;
|
|
|
|
File::config()->force_resample = false;
|
|
|
|
self::reset();
|
|
|
|
self::$basedir = $basedir;
|
|
|
|
|
|
|
|
// Ensure basedir exists
|
2015-12-09 22:19:23 +01:00
|
|
|
\Filesystem::makeFolder(self::base_path());
|
2015-10-15 00:09:04 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get absolute path to basedir
|
|
|
|
*
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public static function base_path() {
|
|
|
|
if(self::$basedir) {
|
|
|
|
return ASSETS_PATH . '/' . self::$basedir;
|
|
|
|
}
|
2015-09-15 04:52:02 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Reset defaults for this store
|
|
|
|
*/
|
|
|
|
public static function reset() {
|
2015-10-23 00:46:58 +02:00
|
|
|
// Remove all files in this store
|
2015-09-15 04:52:02 +02:00
|
|
|
if(self::$basedir) {
|
2015-10-15 00:09:04 +02:00
|
|
|
$path = self::base_path();
|
2015-09-15 04:52:02 +02:00
|
|
|
if(file_exists($path)) {
|
2015-12-09 22:19:23 +01:00
|
|
|
\Filesystem::removeFolder($path);
|
2015-09-15 04:52:02 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
self::$seekable_override = null;
|
|
|
|
self::$basedir = null;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Helper method to get local filesystem path for this file
|
|
|
|
*
|
|
|
|
* @param AssetContainer $asset
|
|
|
|
*/
|
|
|
|
public static function getLocalPath(AssetContainer $asset) {
|
|
|
|
if($asset instanceof Folder) {
|
2015-10-15 00:09:04 +02:00
|
|
|
return self::base_path() . '/' . $asset->getFilename();
|
2015-09-15 04:52:02 +02:00
|
|
|
}
|
2015-10-23 00:46:58 +02:00
|
|
|
if($asset instanceof File) {
|
|
|
|
$asset = $asset->File;
|
|
|
|
}
|
2015-12-09 22:19:23 +01:00
|
|
|
// Extract filesystem used to store this object
|
|
|
|
/** @var AssetStoreTest_SpyStore $assetStore */
|
|
|
|
$assetStore = Injector::inst()->get('AssetStore');
|
|
|
|
$fileID = $assetStore->getFileID($asset->Filename, $asset->Hash, $asset->Variant);
|
|
|
|
$filesystem = $assetStore->getProtectedFilesystem();
|
|
|
|
if(!$filesystem->has($fileID)) {
|
|
|
|
$filesystem = $assetStore->getPublicFilesystem();
|
2015-10-23 00:46:58 +02:00
|
|
|
}
|
2015-12-09 22:19:23 +01:00
|
|
|
/** @var Local $adapter */
|
|
|
|
$adapter = $filesystem->getAdapter();
|
|
|
|
return $adapter->applyPathPrefix($fileID);
|
|
|
|
|
2015-09-15 04:52:02 +02:00
|
|
|
}
|
|
|
|
|
2015-09-03 07:46:08 +02:00
|
|
|
public function cleanFilename($filename) {
|
|
|
|
return parent::cleanFilename($filename);
|
|
|
|
}
|
2015-09-15 04:52:02 +02:00
|
|
|
|
|
|
|
public function getFileID($filename, $hash, $variant = null) {
|
|
|
|
return parent::getFileID($filename, $hash, $variant);
|
2015-09-03 07:46:08 +02:00
|
|
|
}
|
|
|
|
|
2015-09-15 04:52:02 +02:00
|
|
|
|
2015-12-09 22:19:23 +01:00
|
|
|
public function getOriginalFilename($fileID) {
|
|
|
|
return parent::getOriginalFilename($fileID);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function removeVariant($fileID) {
|
|
|
|
return parent::removeVariant($fileID);
|
2015-09-03 07:46:08 +02:00
|
|
|
}
|
|
|
|
|
2015-09-15 04:52:02 +02:00
|
|
|
public function getDefaultConflictResolution($variant) {
|
|
|
|
return parent::getDefaultConflictResolution($variant);
|
|
|
|
}
|
|
|
|
|
2015-09-03 07:46:08 +02:00
|
|
|
protected function isSeekableStream($stream) {
|
|
|
|
if(isset(self::$seekable_override)) {
|
|
|
|
return self::$seekable_override;
|
|
|
|
}
|
|
|
|
return parent::isSeekableStream($stream);
|
|
|
|
}
|
|
|
|
}
|