silverstripe-framework/tests/php/Assets/FolderTest.php

243 lines
8.2 KiB
PHP
Raw Normal View History

<?php
2016-10-14 03:30:05 +02:00
namespace SilverStripe\Assets\Tests;
use SilverStripe\ORM\Versioning\Versioned;
use SilverStripe\ORM\DataObject;
use SilverStripe\Assets\Folder;
use SilverStripe\Assets\Filesystem;
use SilverStripe\Assets\File;
use SilverStripe\Core\Config\Config;
use SilverStripe\Dev\SapphireTest;
2016-10-14 03:30:05 +02:00
use SilverStripe\Assets\Tests\Storage\AssetStoreTest\TestAssetStore;
/**
* @author Ingo Schommer (ingo at silverstripe dot com)
*/
class FolderTest extends SapphireTest {
2014-08-15 08:53:05 +02:00
protected static $fixture_file = 'FileTest.yml';
public function setUp() {
parent::setUp();
$this->logInWithPermission('ADMIN');
Versioned::set_stage(Versioned::DRAFT);
// Set backend root to /FolderTest
2016-10-14 03:30:05 +02:00
TestAssetStore::activate('FolderTest');
// Set the File Name Filter replacements so files have the expected names
Config::inst()->update('SilverStripe\\Assets\\FileNameFilter', 'default_replacements', array(
'/\s/' => '-', // remove whitespace
'/_/' => '-', // underscores to dashes
'/[^A-Za-z0-9+.\-]+/' => '', // remove non-ASCII chars, only allow alphanumeric plus dash and dot
'/[\-]{2,}/' => '-', // remove duplicate dashes
'/^[\.\-_]+/' => '', // Remove all leading dots, dashes or underscores
));
// Create a test folders for each of the fixture references
foreach(Folder::get() as $folder) {
2016-10-14 03:30:05 +02:00
$path = TestAssetStore::getLocalPath($folder);
Filesystem::makeFolder($path);
}
// Create a test files for each of the fixture references
2016-10-14 03:30:05 +02:00
$files = File::get()->exclude('ClassName', Folder::class);
foreach($files as $file) {
2016-10-14 03:30:05 +02:00
$path = TestAssetStore::getLocalPath($file);
Filesystem::makeFolder(dirname($path));
$fh = fopen($path, "w+");
fwrite($fh, str_repeat('x', 1000000));
fclose($fh);
}
}
public function tearDown() {
2016-10-14 03:30:05 +02:00
TestAssetStore::reset();
parent::tearDown();
}
public function testCreateFromNameAndParentIDSetsFilename() {
2016-10-14 03:30:05 +02:00
$folder1 = $this->objFromFixture(Folder::class, 'folder1');
$newFolder = new Folder();
$newFolder->Name = 'CreateFromNameAndParentID';
$newFolder->ParentID = $folder1->ID;
$newFolder->write();
2014-08-15 08:53:05 +02:00
$this->assertEquals($folder1->Filename . 'CreateFromNameAndParentID/', $newFolder->Filename);
}
2014-08-15 08:53:05 +02:00
public function testAllChildrenIncludesFolders() {
2016-10-14 03:30:05 +02:00
$folder1 = $this->objFromFixture(Folder::class, 'folder1');
$subfolder1 = $this->objFromFixture(Folder::class, 'folder1-subfolder1');
$file1 = $this->objFromFixture(File::class, 'file1-folder1');
2014-08-15 08:53:05 +02:00
$children = $folder1->allChildren();
$this->assertEquals(2, $children->Count());
$this->assertContains($subfolder1->ID, $children->column('ID'));
$this->assertContains($file1->ID, $children->column('ID'));
}
2014-08-15 08:53:05 +02:00
public function testFindOrMake() {
$path = 'parent/testFindOrMake/';
$folder = Folder::find_or_make($path);
$this->assertEquals(
ASSETS_PATH . '/FolderTest/' . $path,
2016-10-14 03:30:05 +02:00
TestAssetStore::getLocalPath($folder),
'Nested path information is correctly saved to database (with trailing slash)'
);
// Folder does not exist until it contains files
$this->assertFileNotExists(
2016-10-14 03:30:05 +02:00
TestAssetStore::getLocalPath($folder),
'Empty folder does not have a filesystem record automatically'
);
2016-03-08 21:50:18 +01:00
2016-10-14 03:30:05 +02:00
$parentFolder = DataObject::get_one(Folder::class, array(
'"File"."Name"' => 'parent'
));
$this->assertNotNull($parentFolder);
$this->assertEquals($parentFolder->ID, $folder->ParentID);
2014-08-15 08:53:05 +02:00
$path = 'parent/testFindOrMake'; // no trailing slash
$folder = Folder::find_or_make($path);
$this->assertEquals(
ASSETS_PATH . '/FolderTest/' . $path . '/', // Slash is automatically added here
2016-10-14 03:30:05 +02:00
TestAssetStore::getLocalPath($folder),
'Path information is correctly saved to database (without trailing slash)'
);
2014-08-15 08:53:05 +02:00
$path = 'assets/'; // relative to "assets/" folder, should produce "assets/assets/"
$folder = Folder::find_or_make($path);
$this->assertEquals(
ASSETS_PATH . '/FolderTest/' . $path,
2016-10-14 03:30:05 +02:00
TestAssetStore::getLocalPath($folder),
'A folder named "assets/" within "assets/" is allowed'
);
}
2014-08-15 08:53:05 +02:00
/**
* Tests for the bug #5994 - Moving folder after executing Folder::findOrMake will not set the Filenames properly
*/
public function testFindOrMakeFolderThenMove() {
2016-10-14 03:30:05 +02:00
$folder1 = $this->objFromFixture(Folder::class, 'folder1');
Folder::find_or_make($folder1->Filename);
2016-10-14 03:30:05 +02:00
$folder2 = $this->objFromFixture(Folder::class, 'folder2');
2014-08-15 08:53:05 +02:00
// Publish file1
2016-04-01 05:27:59 +02:00
/** @var File $file1 */
2016-10-14 03:30:05 +02:00
$file1 = DataObject::get_by_id(File::class, $this->idFromFixture(File::class, 'file1-folder1'), false);
2016-04-01 05:27:59 +02:00
$file1->publishRecursive();
// set ParentID. This should cause updateFilesystem to be called on all children
$folder1->ParentID = $folder2->ID;
$folder1->write();
2014-08-15 08:53:05 +02:00
// Check if the file in the folder moved along
2016-04-01 05:27:59 +02:00
/** @var File $file1Draft */
2016-10-14 03:30:05 +02:00
$file1Draft = Versioned::get_by_stage(File::class, Versioned::DRAFT)->byID($file1->ID);
$this->assertFileExists(TestAssetStore::getLocalPath($file1Draft));
2016-03-08 21:50:18 +01:00
$this->assertEquals(
'FileTest-folder2/FileTest-folder1/File1.txt',
$file1Draft->Filename,
'The file DataObject has updated path'
);
// File should be located in new folder
$this->assertEquals(
ASSETS_PATH . '/FolderTest/.protected/FileTest-folder2/FileTest-folder1/55b443b601/File1.txt',
2016-10-14 03:30:05 +02:00
TestAssetStore::getLocalPath($file1Draft)
);
// Published (live) version remains in the old location
2016-04-01 05:27:59 +02:00
/** @var File $file1Live */
2016-10-14 03:30:05 +02:00
$file1Live = Versioned::get_by_stage(File::class, Versioned::LIVE)->byID($file1->ID);
$this->assertEquals(
ASSETS_PATH . '/FolderTest/FileTest-folder1/55b443b601/File1.txt',
2016-10-14 03:30:05 +02:00
TestAssetStore::getLocalPath($file1Live)
);
// Publishing the draft to live should move the new file to the public store
2016-04-01 05:27:59 +02:00
$file1Draft->publishRecursive();
$this->assertEquals(
ASSETS_PATH . '/FolderTest/FileTest-folder2/FileTest-folder1/55b443b601/File1.txt',
2016-10-14 03:30:05 +02:00
TestAssetStore::getLocalPath($file1Draft)
);
}
/**
* Tests for the bug #5994 - if you don't execute get_by_id prior to the rename or move, it will fail.
*/
public function testRenameFolderAndCheckTheFile() {
// ID is prefixed in case Folder is subclassed by project/other module.
2016-10-14 03:30:05 +02:00
$folder1 = DataObject::get_one(Folder::class, array(
'"File"."ID"' => $this->idFromFixture(Folder::class, 'folder1')
));
2014-08-15 08:53:05 +02:00
$folder1->Name = 'FileTest-folder1-changed';
$folder1->write();
2014-08-15 08:53:05 +02:00
// Check if the file in the folder moved along
2016-10-14 03:30:05 +02:00
$file1 = DataObject::get_by_id(File::class, $this->idFromFixture(File::class, 'file1-folder1'), false);
$this->assertFileExists(
2016-10-14 03:30:05 +02:00
TestAssetStore::getLocalPath($file1)
);
$this->assertEquals(
$file1->Filename,
'FileTest-folder1-changed/File1.txt',
'The file DataObject path uses renamed folder'
);
// File should be located in new folder
$this->assertEquals(
ASSETS_PATH . '/FolderTest/.protected/FileTest-folder1-changed/55b443b601/File1.txt',
2016-10-14 03:30:05 +02:00
TestAssetStore::getLocalPath($file1)
);
}
2014-08-15 08:53:05 +02:00
/**
* URL and Link are undefined for folder dataobjects
*/
public function testLinkAndRelativeLink() {
2016-10-14 03:30:05 +02:00
$folder = $this->objFromFixture(Folder::class, 'folder1');
$this->assertEmpty($folder->getURL());
$this->assertEmpty($folder->Link());
}
2014-08-15 08:53:05 +02:00
public function testIllegalFilenames() {
2014-08-15 08:53:05 +02:00
// Test that generating a filename with invalid characters generates a correctly named folder.
$folder = Folder::find_or_make('/FolderTest/EN_US Lang');
$this->assertEquals('FolderTest/EN-US-Lang/', $folder->getFilename());
2014-08-15 08:53:05 +02:00
// Test repeatitions of folder
$folder2 = Folder::find_or_make('/FolderTest/EN_US Lang');
$this->assertEquals($folder->ID, $folder2->ID);
2014-08-15 08:53:05 +02:00
$folder3 = Folder::find_or_make('/FolderTest/EN--US_L!ang');
$this->assertEquals($folder->ID, $folder3->ID);
2014-08-15 08:53:05 +02:00
$folder4 = Folder::find_or_make('/FolderTest/EN-US-Lang');
$this->assertEquals($folder->ID, $folder4->ID);
}
public function testTitleTiedToName() {
$newFolder = new Folder();
$newFolder->Name = 'TestNameCopiedToTitle';
$this->assertEquals($newFolder->Name, $newFolder->Title);
$newFolder->Title = 'TestTitleCopiedToName';
$this->assertEquals($newFolder->Name, $newFolder->Title);
$newFolder->Name = 'TestNameWithIllegalCharactersCopiedToTitle <!BANG!>';
$this->assertEquals($newFolder->Name, $newFolder->Title);
$newFolder->Title = 'TestTitleWithIllegalCharactersCopiedToName <!BANG!>';
$this->assertEquals($newFolder->Name, $newFolder->Title);
}
}