2015-09-15 04:52:02 +02:00
|
|
|
<?php
|
|
|
|
|
2016-10-14 03:30:05 +02:00
|
|
|
namespace SilverStripe\Assets\Tests;
|
|
|
|
|
2016-08-19 00:51:35 +02:00
|
|
|
use SilverStripe\Assets\File;
|
|
|
|
use SilverStripe\Assets\Filesystem;
|
|
|
|
use SilverStripe\Assets\FileMigrationHelper;
|
2016-10-14 03:30:05 +02:00
|
|
|
use SilverStripe\Assets\Folder;
|
|
|
|
use SilverStripe\Assets\Tests\FileMigrationHelperTest\Extension;
|
2016-08-19 00:51:35 +02:00
|
|
|
use SilverStripe\Core\Config\Config;
|
|
|
|
use SilverStripe\Dev\SapphireTest;
|
2016-10-14 03:30:05 +02:00
|
|
|
use SilverStripe\Assets\Tests\Storage\AssetStoreTest\TestAssetStore;
|
2015-09-15 04:52:02 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Ensures that File dataobjects can be safely migrated from 3.x
|
|
|
|
*/
|
|
|
|
class FileMigrationHelperTest extends SapphireTest {
|
|
|
|
|
|
|
|
protected static $fixture_file = 'FileMigrationHelperTest.yml';
|
|
|
|
|
|
|
|
protected $requiredExtensions = array(
|
2016-10-14 03:30:05 +02:00
|
|
|
File::class => array(
|
|
|
|
Extension::class
|
2015-09-15 04:52:02 +02:00
|
|
|
)
|
|
|
|
);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* get the BASE_PATH for this test
|
|
|
|
*
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
protected function getBasePath() {
|
2016-03-02 06:18:10 +01:00
|
|
|
// Note that the actual filesystem base is the 'assets' subdirectory within this
|
2015-09-15 04:52:02 +02:00
|
|
|
return ASSETS_PATH . '/FileMigrationHelperTest';
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public function setUp() {
|
|
|
|
Config::nest(); // additional nesting here necessary
|
2016-10-14 03:30:05 +02:00
|
|
|
Config::inst()->update(File::class, 'migrate_legacy_file', false);
|
2015-09-15 04:52:02 +02:00
|
|
|
parent::setUp();
|
|
|
|
|
|
|
|
// Set backend root to /FileMigrationHelperTest/assets
|
2016-10-14 03:30:05 +02:00
|
|
|
TestAssetStore::activate('FileMigrationHelperTest/assets');
|
2015-09-15 04:52:02 +02:00
|
|
|
|
|
|
|
// Ensure that each file has a local record file in this new assets base
|
2016-10-14 03:30:05 +02:00
|
|
|
$from = FRAMEWORK_PATH . '/tests/php/ORM/testimages/test-image-low-quality.jpg';
|
|
|
|
foreach(File::get()->exclude('ClassName', Folder::class) as $file) {
|
|
|
|
$dest = TestAssetStore::base_path() . '/' . $file->generateFilename();
|
2016-08-19 00:51:35 +02:00
|
|
|
Filesystem::makeFolder(dirname($dest));
|
2015-09-15 04:52:02 +02:00
|
|
|
copy($from, $dest);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public function tearDown() {
|
2016-10-14 03:30:05 +02:00
|
|
|
TestAssetStore::reset();
|
2016-08-19 00:51:35 +02:00
|
|
|
Filesystem::removeFolder($this->getBasePath());
|
2015-09-15 04:52:02 +02:00
|
|
|
parent::tearDown();
|
|
|
|
Config::unnest();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Test file migration
|
|
|
|
*/
|
|
|
|
public function testMigration() {
|
|
|
|
// Prior to migration, check that each file has empty Filename / Hash properties
|
2016-10-14 03:30:05 +02:00
|
|
|
foreach(File::get()->exclude('ClassName', Folder::class) as $file) {
|
2016-03-02 06:18:10 +01:00
|
|
|
$filename = $file->generateFilename();
|
2015-09-15 04:52:02 +02:00
|
|
|
$this->assertNotEmpty($filename, "File {$file->Name} has a filename");
|
|
|
|
$this->assertEmpty($file->File->getFilename(), "File {$file->Name} has no DBFile filename");
|
|
|
|
$this->assertEmpty($file->File->getHash(), "File {$file->Name} has no hash");
|
|
|
|
$this->assertFalse($file->exists(), "File with name {$file->Name} does not yet exist");
|
2016-01-26 04:56:07 +01:00
|
|
|
$this->assertFalse($file->isPublished(), "File is not published yet");
|
2015-09-15 04:52:02 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Do migration
|
|
|
|
$helper = new FileMigrationHelper();
|
|
|
|
$result = $helper->run($this->getBasePath());
|
|
|
|
$this->assertEquals(5, $result);
|
|
|
|
|
|
|
|
// Test that each file exists
|
2016-10-14 03:30:05 +02:00
|
|
|
foreach(File::get()->exclude('ClassName', Folder::class) as $file) {
|
2016-03-02 06:18:10 +01:00
|
|
|
$expectedFilename = $file->generateFilename();
|
2015-09-15 04:52:02 +02:00
|
|
|
$filename = $file->File->getFilename();
|
2016-03-02 06:18:10 +01:00
|
|
|
$this->assertTrue($file->exists(), "File with name {$filename} exists");
|
2015-09-15 04:52:02 +02:00
|
|
|
$this->assertNotEmpty($filename, "File {$file->Name} has a Filename");
|
2016-03-02 06:18:10 +01:00
|
|
|
$this->assertEquals($expectedFilename, $filename, "File {$file->Name} has retained its Filename value");
|
2015-09-15 04:52:02 +02:00
|
|
|
$this->assertEquals(
|
|
|
|
'33be1b95cba0358fe54e8b13532162d52f97421c',
|
|
|
|
$file->File->getHash(),
|
|
|
|
"File with name {$filename} has the correct hash"
|
|
|
|
);
|
2016-01-26 04:56:07 +01:00
|
|
|
$this->assertTrue($file->isPublished(), "File is published after migration");
|
2015-09-15 04:52:02 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|