2010-10-13 01:02:10 +02:00
|
|
|
<?php
|
2015-09-15 04:52:02 +02:00
|
|
|
|
|
|
|
use Filesystem as SS_Filesystem;
|
|
|
|
|
2010-10-13 01:02:10 +02:00
|
|
|
/**
|
|
|
|
* @author Ingo Schommer (ingo at silverstripe dot com)
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
2012-04-12 08:02:46 +02:00
|
|
|
* @package framework
|
2010-10-13 01:02:10 +02:00
|
|
|
* @subpackage tests
|
|
|
|
*/
|
|
|
|
class FolderTest extends SapphireTest {
|
2014-08-15 08:53:05 +02:00
|
|
|
|
|
|
|
protected static $fixture_file = 'FileTest.yml';
|
|
|
|
|
2015-09-15 04:52:02 +02:00
|
|
|
public function setUp() {
|
|
|
|
parent::setUp();
|
|
|
|
|
2016-01-26 04:56:07 +01:00
|
|
|
$this->logInWithPermission('ADMIN');
|
|
|
|
Versioned::reading_stage('Stage');
|
|
|
|
|
2015-09-15 04:52:02 +02:00
|
|
|
// Set backend root to /FolderTest
|
|
|
|
AssetStoreTest_SpyStore::activate('FolderTest');
|
|
|
|
|
|
|
|
// Create a test folders for each of the fixture references
|
|
|
|
foreach(Folder::get() as $folder) {
|
|
|
|
$path = AssetStoreTest_SpyStore::getLocalPath($folder);
|
|
|
|
SS_Filesystem::makeFolder($path);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Create a test files for each of the fixture references
|
|
|
|
$files = File::get()->exclude('ClassName', 'Folder');
|
|
|
|
foreach($files as $file) {
|
|
|
|
$path = AssetStoreTest_SpyStore::getLocalPath($file);
|
|
|
|
SS_Filesystem::makeFolder(dirname($path));
|
|
|
|
$fh = fopen($path, "w+");
|
|
|
|
fwrite($fh, str_repeat('x', 1000000));
|
|
|
|
fclose($fh);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public function tearDown() {
|
|
|
|
AssetStoreTest_SpyStore::reset();
|
|
|
|
parent::tearDown();
|
|
|
|
}
|
|
|
|
|
2012-09-19 12:07:39 +02:00
|
|
|
public function testCreateFromNameAndParentIDSetsFilename() {
|
2010-10-15 05:20:47 +02:00
|
|
|
$folder1 = $this->objFromFixture('Folder', 'folder1');
|
|
|
|
$newFolder = new Folder();
|
|
|
|
$newFolder->Name = 'CreateFromNameAndParentID';
|
|
|
|
$newFolder->ParentID = $folder1->ID;
|
|
|
|
$newFolder->write();
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2010-10-15 05:20:47 +02:00
|
|
|
$this->assertEquals($folder1->Filename . 'CreateFromNameAndParentID/', $newFolder->Filename);
|
2010-10-13 01:02:10 +02:00
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2012-09-19 12:07:39 +02:00
|
|
|
public function testAllChildrenIncludesFolders() {
|
2010-10-15 05:20:47 +02:00
|
|
|
$folder1 = $this->objFromFixture('Folder', 'folder1');
|
|
|
|
$subfolder1 = $this->objFromFixture('Folder', 'folder1-subfolder1');
|
|
|
|
$file1 = $this->objFromFixture('File', 'file1-folder1');
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2010-10-15 05:20:47 +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
|
|
|
|
2012-09-19 12:07:39 +02:00
|
|
|
public function testFindOrMake() {
|
2015-09-15 04:52:02 +02:00
|
|
|
$path = 'parent/testFindOrMake/';
|
2011-12-06 01:56:24 +01:00
|
|
|
$folder = Folder::find_or_make($path);
|
2015-09-15 04:52:02 +02:00
|
|
|
$this->assertEquals(
|
|
|
|
ASSETS_PATH . '/FolderTest/' . $path,
|
|
|
|
AssetStoreTest_SpyStore::getLocalPath($folder),
|
2010-10-13 01:02:10 +02:00
|
|
|
'Nested path information is correctly saved to database (with trailing slash)'
|
|
|
|
);
|
|
|
|
|
2015-09-15 04:52:02 +02:00
|
|
|
// Folder does not exist until it contains files
|
|
|
|
$this->assertFileNotExists(
|
|
|
|
AssetStoreTest_SpyStore::getLocalPath($folder),
|
|
|
|
'Empty folder does not have a filesystem record automatically'
|
|
|
|
);
|
|
|
|
|
2013-06-21 00:32:08 +02:00
|
|
|
$parentFolder = DataObject::get_one('Folder', array(
|
2015-09-15 04:52:02 +02:00
|
|
|
'"File"."Name"' => 'parent'
|
2013-06-21 00:32:08 +02:00
|
|
|
));
|
2010-10-13 01:02:10 +02:00
|
|
|
$this->assertNotNull($parentFolder);
|
|
|
|
$this->assertEquals($parentFolder->ID, $folder->ParentID);
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2015-09-15 04:52:02 +02:00
|
|
|
$path = 'parent/testFindOrMake'; // no trailing slash
|
2011-12-06 01:56:24 +01:00
|
|
|
$folder = Folder::find_or_make($path);
|
2015-09-15 04:52:02 +02:00
|
|
|
$this->assertEquals(
|
|
|
|
ASSETS_PATH . '/FolderTest/' . $path . '/', // Slash is automatically added here
|
|
|
|
AssetStoreTest_SpyStore::getLocalPath($folder),
|
2010-10-13 01:02:10 +02:00
|
|
|
'Path information is correctly saved to database (without trailing slash)'
|
|
|
|
);
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2015-09-15 04:52:02 +02:00
|
|
|
$path = 'assets/'; // relative to "assets/" folder, should produce "assets/assets/"
|
2011-12-06 01:56:24 +01:00
|
|
|
$folder = Folder::find_or_make($path);
|
2015-09-15 04:52:02 +02:00
|
|
|
$this->assertEquals(
|
|
|
|
ASSETS_PATH . '/FolderTest/' . $path,
|
|
|
|
AssetStoreTest_SpyStore::getLocalPath($folder),
|
2010-10-15 05:21:41 +02:00
|
|
|
'A folder named "assets/" within "assets/" is allowed'
|
|
|
|
);
|
2010-10-13 01:02:10 +02:00
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2010-10-19 06:00:57 +02:00
|
|
|
/**
|
|
|
|
* Tests for the bug #5994 - Moving folder after executing Folder::findOrMake will not set the Filenames properly
|
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public function testFindOrMakeFolderThenMove() {
|
2010-10-19 06:00:57 +02:00
|
|
|
$folder1 = $this->objFromFixture('Folder', 'folder1');
|
2011-12-06 01:56:24 +01:00
|
|
|
Folder::find_or_make($folder1->Filename);
|
2010-10-19 06:00:57 +02:00
|
|
|
$folder2 = $this->objFromFixture('Folder', 'folder2');
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2015-09-15 04:52:02 +02:00
|
|
|
// set ParentID. This should cause updateFilesystem to be called on all children
|
2010-10-19 06:00:57 +02:00
|
|
|
$folder1->ParentID = $folder2->ID;
|
|
|
|
$folder1->write();
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2010-10-19 06:00:57 +02:00
|
|
|
// Check if the file in the folder moved along
|
|
|
|
$file1 = DataObject::get_by_id('File', $this->idFromFixture('File', 'file1-folder1'), false);
|
2015-09-15 04:52:02 +02:00
|
|
|
$this->assertFileExists(AssetStoreTest_SpyStore::getLocalPath($file1));
|
|
|
|
|
|
|
|
$this->assertEquals(
|
|
|
|
'FileTest-folder2/FileTest-folder1/File1.txt',
|
|
|
|
$file1->Filename,
|
|
|
|
'The file DataObject has updated path'
|
|
|
|
);
|
|
|
|
|
|
|
|
// File should be located in new folder
|
|
|
|
$this->assertEquals(
|
2016-01-26 04:56:07 +01:00
|
|
|
ASSETS_PATH . '/FolderTest/.protected/FileTest-folder2/FileTest-folder1/55b443b601/File1.txt',
|
2015-09-15 04:52:02 +02:00
|
|
|
AssetStoreTest_SpyStore::getLocalPath($file1)
|
|
|
|
);
|
2010-10-19 06:00:57 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Tests for the bug #5994 - if you don't execute get_by_id prior to the rename or move, it will fail.
|
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public function testRenameFolderAndCheckTheFile() {
|
2010-10-19 06:52:20 +02:00
|
|
|
// ID is prefixed in case Folder is subclassed by project/other module.
|
2013-06-21 00:32:08 +02:00
|
|
|
$folder1 = DataObject::get_one('Folder', array(
|
|
|
|
'"File"."ID"' => $this->idFromFixture('Folder', 'folder1')
|
|
|
|
));
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2010-10-19 06:00:57 +02:00
|
|
|
$folder1->Name = 'FileTest-folder1-changed';
|
|
|
|
$folder1->write();
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2010-10-19 06:00:57 +02:00
|
|
|
// Check if the file in the folder moved along
|
|
|
|
$file1 = DataObject::get_by_id('File', $this->idFromFixture('File', 'file1-folder1'), false);
|
2015-09-15 04:52:02 +02:00
|
|
|
$this->assertFileExists(
|
|
|
|
AssetStoreTest_SpyStore::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(
|
2016-01-26 04:56:07 +01:00
|
|
|
ASSETS_PATH . '/FolderTest/.protected/FileTest-folder1-changed/55b443b601/File1.txt',
|
2015-09-15 04:52:02 +02:00
|
|
|
AssetStoreTest_SpyStore::getLocalPath($file1)
|
|
|
|
);
|
2010-10-19 06:00:57 +02:00
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2010-10-15 05:20:47 +02:00
|
|
|
/**
|
2015-09-15 04:52:02 +02:00
|
|
|
* URL and Link are undefined for folder dataobjects
|
2010-10-15 05:20:47 +02:00
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public function testLinkAndRelativeLink() {
|
2010-10-15 05:20:47 +02:00
|
|
|
$folder = $this->objFromFixture('Folder', 'folder1');
|
2015-09-15 04:52:02 +02:00
|
|
|
$this->assertEmpty($folder->getURL());
|
|
|
|
$this->assertEmpty($folder->Link());
|
2013-05-11 05:07:12 +02:00
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2014-02-20 02:03:40 +01:00
|
|
|
public function testIllegalFilenames() {
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2014-02-20 02:03:40 +01:00
|
|
|
// Test that generating a filename with invalid characters generates a correctly named folder.
|
|
|
|
$folder = Folder::find_or_make('/FolderTest/EN_US Lang');
|
2015-09-15 04:52:02 +02:00
|
|
|
$this->assertEquals('FolderTest/EN-US-Lang/', $folder->getFilename());
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2014-02-20 02:03:40 +01: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
|
|
|
|
2014-02-20 02:03:40 +01: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
|
|
|
|
2014-02-20 02:03:40 +01:00
|
|
|
$folder4 = Folder::find_or_make('/FolderTest/EN-US-Lang');
|
|
|
|
$this->assertEquals($folder->ID, $folder4->ID);
|
|
|
|
}
|
2014-05-02 04:41:10 +02:00
|
|
|
|
|
|
|
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);
|
|
|
|
}
|
2012-03-24 04:04:52 +01:00
|
|
|
}
|