2012-02-03 00:59:40 +01:00
|
|
|
<?php
|
|
|
|
/**
|
2012-04-12 08:02:46 +02:00
|
|
|
* @package framework
|
2012-02-03 00:59:40 +01:00
|
|
|
* @subpackage tests
|
|
|
|
*/
|
|
|
|
|
2012-12-08 12:20:20 +01:00
|
|
|
class UploadFieldTest extends FunctionalTest {
|
2012-02-03 00:59:40 +01:00
|
|
|
|
2013-03-21 19:48:54 +01:00
|
|
|
protected static $fixture_file = 'UploadFieldTest.yml';
|
2012-02-03 00:59:40 +01:00
|
|
|
|
|
|
|
protected $extraDataObjects = array('UploadFieldTest_Record');
|
|
|
|
|
|
|
|
protected $requiredExtensions = array(
|
|
|
|
'File' => array('UploadFieldTest_FileExtension')
|
|
|
|
);
|
|
|
|
|
2013-05-27 05:22:59 +02:00
|
|
|
/**
|
|
|
|
* Test that files can be uploaded against an object with no relation
|
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public function testUploadNoRelation() {
|
2012-02-03 00:59:40 +01:00
|
|
|
$this->loginWithPermission('ADMIN');
|
|
|
|
|
|
|
|
$record = $this->objFromFixture('UploadFieldTest_Record', 'record1');
|
|
|
|
|
|
|
|
$tmpFileName = 'testUploadBasic.txt';
|
2013-05-27 05:22:59 +02:00
|
|
|
$response = $this->mockFileUpload('NoRelationField', $tmpFileName);
|
2012-02-03 00:59:40 +01:00
|
|
|
$this->assertFalse($response->isError());
|
|
|
|
$this->assertFileExists(ASSETS_PATH . "/UploadFieldTest/$tmpFileName");
|
|
|
|
$uploadedFile = DataObject::get_one('File', sprintf('"Name" = \'%s\'', $tmpFileName));
|
2012-04-18 04:46:54 +02:00
|
|
|
$this->assertTrue(is_object($uploadedFile), 'The file object is created');
|
2012-02-03 00:59:40 +01:00
|
|
|
}
|
|
|
|
|
2013-05-27 05:22:59 +02:00
|
|
|
/**
|
|
|
|
* Test that an object can be uploaded against an object with a has_one relation
|
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public function testUploadHasOneRelation() {
|
2012-02-03 00:59:40 +01:00
|
|
|
$this->loginWithPermission('ADMIN');
|
|
|
|
|
|
|
|
// Unset existing has_one relation before re-uploading
|
|
|
|
$record = $this->objFromFixture('UploadFieldTest_Record', 'record1');
|
|
|
|
$record->HasOneFileID = null;
|
|
|
|
$record->write();
|
|
|
|
|
2013-05-27 05:22:59 +02:00
|
|
|
// Firstly, ensure the file can be uploaded
|
2012-02-03 00:59:40 +01:00
|
|
|
$tmpFileName = 'testUploadHasOneRelation.txt';
|
2013-05-27 05:22:59 +02:00
|
|
|
$response = $this->mockFileUpload('HasOneFile', $tmpFileName);
|
2012-02-03 00:59:40 +01:00
|
|
|
$this->assertFalse($response->isError());
|
|
|
|
$this->assertFileExists(ASSETS_PATH . "/UploadFieldTest/$tmpFileName");
|
|
|
|
$uploadedFile = DataObject::get_one('File', sprintf('"Name" = \'%s\'', $tmpFileName));
|
2012-04-18 04:46:54 +02:00
|
|
|
$this->assertTrue(is_object($uploadedFile), 'The file object is created');
|
2012-02-03 00:59:40 +01:00
|
|
|
|
2013-05-27 05:22:59 +02:00
|
|
|
// Secondly, ensure that simply uploading an object does not save the file against the relation
|
|
|
|
$record = DataObject::get_by_id($record->class, $record->ID, false);
|
|
|
|
$this->assertFalse($record->HasOneFile()->exists());
|
|
|
|
|
|
|
|
// Thirdly, test submitting the form with the encoded data
|
|
|
|
$response = $this->mockUploadFileIDs('HasOneFile', array($uploadedFile->ID));
|
|
|
|
$this->assertEmpty($response['errors']);
|
2012-02-03 00:59:40 +01:00
|
|
|
$record = DataObject::get_by_id($record->class, $record->ID, false);
|
|
|
|
$this->assertTrue($record->HasOneFile()->exists());
|
|
|
|
$this->assertEquals($record->HasOneFile()->Name, $tmpFileName);
|
|
|
|
}
|
|
|
|
|
2013-05-27 05:22:59 +02:00
|
|
|
/**
|
|
|
|
* Tests that has_one relations work with subclasses of File
|
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public function testUploadHasOneRelationWithExtendedFile() {
|
2012-04-18 05:22:40 +02:00
|
|
|
$this->loginWithPermission('ADMIN');
|
|
|
|
|
|
|
|
// Unset existing has_one relation before re-uploading
|
|
|
|
$record = $this->objFromFixture('UploadFieldTest_Record', 'record1');
|
|
|
|
$record->HasOneExtendedFileID = null;
|
|
|
|
$record->write();
|
|
|
|
|
2013-05-27 05:22:59 +02:00
|
|
|
// Test that the file can be safely uploaded
|
2012-04-18 05:22:40 +02:00
|
|
|
$tmpFileName = 'testUploadHasOneRelationWithExtendedFile.txt';
|
2013-05-27 05:22:59 +02:00
|
|
|
$response = $this->mockFileUpload('HasOneExtendedFile', $tmpFileName);
|
2012-04-18 05:22:40 +02:00
|
|
|
$this->assertFalse($response->isError());
|
|
|
|
$this->assertFileExists(ASSETS_PATH . "/UploadFieldTest/$tmpFileName");
|
|
|
|
$uploadedFile = DataObject::get_one('UploadFieldTest_ExtendedFile', sprintf('"Name" = \'%s\'', $tmpFileName));
|
|
|
|
$this->assertTrue(is_object($uploadedFile), 'The file object is created');
|
|
|
|
|
2013-05-27 05:22:59 +02:00
|
|
|
// Test that the record isn't written to automatically
|
|
|
|
$record = DataObject::get_by_id($record->class, $record->ID, false);
|
|
|
|
$this->assertFalse($record->HasOneExtendedFile()->exists());
|
|
|
|
|
|
|
|
// Test that saving the form writes the record
|
|
|
|
$response = $this->mockUploadFileIDs('HasOneExtendedFile', array($uploadedFile->ID));
|
|
|
|
$this->assertEmpty($response['errors']);
|
2012-04-18 05:22:40 +02:00
|
|
|
$record = DataObject::get_by_id($record->class, $record->ID, false);
|
2013-05-27 05:22:59 +02:00
|
|
|
$this->assertTrue($record->HasOneExtendedFile()->exists());
|
|
|
|
$this->assertEquals($record->HasOneExtendedFile()->Name, $tmpFileName);
|
2012-04-18 05:22:40 +02:00
|
|
|
}
|
|
|
|
|
2013-05-27 05:22:59 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Test that has_many relations work with files
|
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public function testUploadHasManyRelation() {
|
2012-02-03 00:59:40 +01:00
|
|
|
$this->loginWithPermission('ADMIN');
|
|
|
|
|
|
|
|
$record = $this->objFromFixture('UploadFieldTest_Record', 'record1');
|
|
|
|
|
2013-05-27 05:22:59 +02:00
|
|
|
// Test that uploaded files can be posted to a has_many relation
|
2012-02-03 00:59:40 +01:00
|
|
|
$tmpFileName = 'testUploadHasManyRelation.txt';
|
2013-05-27 05:22:59 +02:00
|
|
|
$response = $this->mockFileUpload('HasManyFiles', $tmpFileName);
|
2012-02-03 00:59:40 +01:00
|
|
|
$this->assertFalse($response->isError());
|
|
|
|
$this->assertFileExists(ASSETS_PATH . "/UploadFieldTest/$tmpFileName");
|
|
|
|
$uploadedFile = DataObject::get_one('File', sprintf('"Name" = \'%s\'', $tmpFileName));
|
2012-04-18 04:46:54 +02:00
|
|
|
$this->assertTrue(is_object($uploadedFile), 'The file object is created');
|
2012-02-03 00:59:40 +01:00
|
|
|
|
2013-05-27 05:22:59 +02:00
|
|
|
// Test that the record isn't written to automatically
|
|
|
|
$record = DataObject::get_by_id($record->class, $record->ID, false);
|
|
|
|
$this->assertEquals(2, $record->HasManyFiles()->Count()); // Existing two files should be retained
|
|
|
|
|
|
|
|
// Test that saving the form writes the record
|
|
|
|
$ids = array_merge($record->HasManyFiles()->getIDList(), array($uploadedFile->ID));
|
|
|
|
$response = $this->mockUploadFileIDs('HasManyFiles', $ids);
|
|
|
|
$this->assertEmpty($response['errors']);
|
2012-02-03 00:59:40 +01:00
|
|
|
$record = DataObject::get_by_id($record->class, $record->ID, false);
|
2013-05-27 05:22:59 +02:00
|
|
|
$this->assertEquals(3, $record->HasManyFiles()->Count()); // New record should appear here now
|
2012-02-03 00:59:40 +01:00
|
|
|
}
|
|
|
|
|
2013-05-27 05:22:59 +02:00
|
|
|
/**
|
|
|
|
* Test that many_many relationships work with files
|
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public function testUploadManyManyRelation() {
|
2012-02-03 00:59:40 +01:00
|
|
|
$this->loginWithPermission('ADMIN');
|
|
|
|
|
|
|
|
$record = $this->objFromFixture('UploadFieldTest_Record', 'record1');
|
|
|
|
$relationCount = $record->ManyManyFiles()->Count();
|
|
|
|
|
2013-05-27 05:22:59 +02:00
|
|
|
// Test that uploaded files can be posted to a many_many relation
|
2012-02-03 00:59:40 +01:00
|
|
|
$tmpFileName = 'testUploadManyManyRelation.txt';
|
2013-05-27 05:22:59 +02:00
|
|
|
$response = $this->mockFileUpload('ManyManyFiles', $tmpFileName);
|
2012-02-03 00:59:40 +01:00
|
|
|
$this->assertFalse($response->isError());
|
|
|
|
$this->assertFileExists(ASSETS_PATH . "/UploadFieldTest/$tmpFileName");
|
|
|
|
$uploadedFile = DataObject::get_one('File', sprintf('"Name" = \'%s\'', $tmpFileName));
|
2012-04-18 04:46:54 +02:00
|
|
|
$this->assertTrue(is_object($uploadedFile), 'The file object is created');
|
2012-02-03 00:59:40 +01:00
|
|
|
|
2013-05-27 05:22:59 +02:00
|
|
|
// Test that the record isn't written to automatically
|
|
|
|
$record = DataObject::get_by_id($record->class, $record->ID, false);
|
2013-05-27 05:42:10 +02:00
|
|
|
// Existing file count should be retained
|
|
|
|
$this->assertEquals($relationCount, $record->ManyManyFiles()->Count());
|
2013-05-27 05:22:59 +02:00
|
|
|
|
|
|
|
// Test that saving the form writes the record
|
|
|
|
$ids = array_merge($record->ManyManyFiles()->getIDList(), array($uploadedFile->ID));
|
|
|
|
$response = $this->mockUploadFileIDs('ManyManyFiles', $ids);
|
|
|
|
$this->assertEmpty($response['errors']);
|
2012-02-03 00:59:40 +01:00
|
|
|
$record = DataObject::get_by_id($record->class, $record->ID, false);
|
2013-05-27 05:22:59 +02:00
|
|
|
$record->flushCache();
|
2013-05-27 05:42:10 +02:00
|
|
|
// New record should appear here now
|
|
|
|
$this->assertEquals($relationCount + 1, $record->ManyManyFiles()->Count());
|
2012-02-03 00:59:40 +01:00
|
|
|
}
|
|
|
|
|
2013-05-27 05:22:59 +02:00
|
|
|
/**
|
|
|
|
* Test that has_one relations do not support multiple files
|
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public function testAllowedMaxFileNumberWithHasOne() {
|
2012-04-17 07:12:56 +02:00
|
|
|
$this->loginWithPermission('ADMIN');
|
|
|
|
|
2013-05-27 05:22:59 +02:00
|
|
|
// Get references for each file to upload
|
|
|
|
$file1 = $this->objFromFixture('File', 'file1');
|
|
|
|
$file2 = $this->objFromFixture('File', 'file2');
|
|
|
|
$fileIDs = array($file1->ID, $file2->ID);
|
|
|
|
|
2012-04-17 07:12:56 +02:00
|
|
|
// Test each of the three cases - has one with no max filel limit, has one with a limit of
|
|
|
|
// one, has one with a limit of more than one (makes no sense, but should test it anyway).
|
2012-09-19 12:07:39 +02:00
|
|
|
// Each of them should public function in the same way - attaching the first file should work, the
|
2012-04-17 07:12:56 +02:00
|
|
|
// second should cause an error.
|
|
|
|
foreach (array('HasOneFile', 'HasOneFileMaxOne', 'HasOneFileMaxTwo') as $recordName) {
|
2013-05-27 05:22:59 +02:00
|
|
|
|
2012-04-17 07:12:56 +02:00
|
|
|
// Unset existing has_one relation before re-uploading
|
|
|
|
$record = $this->objFromFixture('UploadFieldTest_Record', 'record1');
|
2013-05-27 05:22:59 +02:00
|
|
|
$record->{"{$recordName}ID"} = null;
|
2012-04-17 07:12:56 +02:00
|
|
|
$record->write();
|
2013-05-27 05:22:59 +02:00
|
|
|
|
|
|
|
// Post form with two files for this field, should result in an error
|
|
|
|
$response = $this->mockUploadFileIDs($recordName, $fileIDs);
|
|
|
|
$isError = !empty($response['errors']);
|
|
|
|
|
|
|
|
// Strictly, a has_one should not allow two files, but this is overridden
|
|
|
|
// by the setAllowedMaxFileNumber(2) call
|
|
|
|
$maxFiles = ($recordName === 'HasOneFileMaxTwo') ? 2 : 1;
|
|
|
|
|
|
|
|
// Assert that the form fails if the maximum number of files is exceeded
|
|
|
|
$this->assertTrue((count($fileIDs) > $maxFiles) == $isError);
|
2012-04-17 07:12:56 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-05-27 05:42:10 +02:00
|
|
|
/**
|
|
|
|
* Test that max number of items on has_many is validated
|
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public function testAllowedMaxFileNumberWithHasMany() {
|
2012-04-17 07:12:56 +02:00
|
|
|
$this->loginWithPermission('ADMIN');
|
2013-05-27 05:42:10 +02:00
|
|
|
|
2012-04-17 07:12:56 +02:00
|
|
|
// The 'HasManyFilesMaxTwo' field has a maximum of two files able to be attached to it.
|
|
|
|
// We want to add files to it until we attempt to add the third. We expect that the first
|
|
|
|
// two should work and the third will fail.
|
|
|
|
$record = $this->objFromFixture('UploadFieldTest_Record', 'record1');
|
|
|
|
$record->HasManyFilesMaxTwo()->removeAll();
|
2013-05-27 05:42:10 +02:00
|
|
|
|
2013-05-27 05:22:59 +02:00
|
|
|
// Get references for each file to upload
|
|
|
|
$file1 = $this->objFromFixture('File', 'file1');
|
|
|
|
$file2 = $this->objFromFixture('File', 'file2');
|
|
|
|
$file3 = $this->objFromFixture('File', 'file3');
|
2012-04-17 07:12:56 +02:00
|
|
|
|
|
|
|
// Write the first element, should be okay.
|
2013-05-27 05:22:59 +02:00
|
|
|
$response = $this->mockUploadFileIDs('HasManyFilesMaxTwo', array($file1->ID));
|
|
|
|
$this->assertEmpty($response['errors']);
|
2013-05-27 05:42:10 +02:00
|
|
|
|
2012-04-17 07:12:56 +02:00
|
|
|
// Write the second element, should be okay.
|
2013-05-27 05:22:59 +02:00
|
|
|
$response = $this->mockUploadFileIDs('HasManyFilesMaxTwo', array($file1->ID, $file2->ID));
|
|
|
|
$this->assertEmpty($response['errors']);
|
2013-05-27 05:42:10 +02:00
|
|
|
|
2012-04-17 07:12:56 +02:00
|
|
|
// Write the third element, should result in error.
|
2013-05-27 05:22:59 +02:00
|
|
|
$response = $this->mockUploadFileIDs('HasManyFilesMaxTwo', array($file1->ID, $file2->ID, $file3->ID));
|
|
|
|
$this->assertNotEmpty($response['errors']);
|
2012-02-03 00:59:40 +01:00
|
|
|
}
|
|
|
|
|
2013-05-27 05:22:59 +02:00
|
|
|
/**
|
|
|
|
* Test that files can be removed from has_one relations
|
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public function testRemoveFromHasOne() {
|
2012-02-03 00:59:40 +01:00
|
|
|
$record = $this->objFromFixture('UploadFieldTest_Record', 'record1');
|
|
|
|
$file1 = $this->objFromFixture('File', 'file1');
|
|
|
|
|
2013-05-27 05:22:59 +02:00
|
|
|
// Check record exists
|
2012-02-03 00:59:40 +01:00
|
|
|
$this->assertTrue($record->HasOneFile()->exists());
|
2013-05-27 05:22:59 +02:00
|
|
|
|
|
|
|
// Remove from record
|
|
|
|
$response = $this->mockUploadFileIDs('HasOneFile', array());
|
|
|
|
$this->assertEmpty($response['errors']);
|
|
|
|
|
|
|
|
// Check file is removed
|
2012-02-03 00:59:40 +01:00
|
|
|
$record = DataObject::get_by_id($record->class, $record->ID, false);
|
|
|
|
$this->assertFalse($record->HasOneFile()->exists());
|
2013-05-27 05:22:59 +02:00
|
|
|
|
|
|
|
// Check file object itself exists
|
2012-02-03 00:59:40 +01:00
|
|
|
$this->assertFileExists($file1->FullPath, 'File is only detached, not deleted from filesystem');
|
|
|
|
}
|
|
|
|
|
2013-05-27 05:22:59 +02:00
|
|
|
/**
|
|
|
|
* Test that items can be removed from has_many
|
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public function testRemoveFromHasMany() {
|
2012-02-03 00:59:40 +01:00
|
|
|
$record = $this->objFromFixture('UploadFieldTest_Record', 'record1');
|
|
|
|
$file2 = $this->objFromFixture('File', 'file2');
|
|
|
|
$file3 = $this->objFromFixture('File', 'file3');
|
|
|
|
|
2013-05-27 05:22:59 +02:00
|
|
|
// Check record has two files attached
|
2012-02-03 00:59:40 +01:00
|
|
|
$this->assertEquals(array('File2', 'File3'), $record->HasManyFiles()->column('Title'));
|
2013-05-27 05:22:59 +02:00
|
|
|
|
|
|
|
// Remove file 2
|
|
|
|
$response = $this->mockUploadFileIDs('HasManyFiles', array($file3->ID));
|
|
|
|
$this->assertEmpty($response['errors']);
|
|
|
|
|
|
|
|
// check only file 3 is left
|
2012-02-03 00:59:40 +01:00
|
|
|
$record = DataObject::get_by_id($record->class, $record->ID, false);
|
|
|
|
$this->assertEquals(array('File3'), $record->HasManyFiles()->column('Title'));
|
2013-05-27 05:22:59 +02:00
|
|
|
|
|
|
|
// Check file 2 object itself exists
|
2012-02-03 00:59:40 +01:00
|
|
|
$this->assertFileExists($file3->FullPath, 'File is only detached, not deleted from filesystem');
|
|
|
|
}
|
|
|
|
|
2013-05-27 05:22:59 +02:00
|
|
|
/**
|
|
|
|
* Test that items can be removed from many_many
|
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public function testRemoveFromManyMany() {
|
2012-02-03 00:59:40 +01:00
|
|
|
$record = $this->objFromFixture('UploadFieldTest_Record', 'record1');
|
|
|
|
$file4 = $this->objFromFixture('File', 'file4');
|
|
|
|
$file5 = $this->objFromFixture('File', 'file5');
|
|
|
|
|
2013-05-27 05:22:59 +02:00
|
|
|
// Check that both files are currently set
|
2012-02-03 00:59:40 +01:00
|
|
|
$this->assertContains('File4', $record->ManyManyFiles()->column('Title'));
|
|
|
|
$this->assertContains('File5', $record->ManyManyFiles()->column('Title'));
|
2013-05-27 05:22:59 +02:00
|
|
|
|
|
|
|
// Remove file 4
|
|
|
|
$response = $this->mockUploadFileIDs('ManyManyFiles', array($file5->ID));
|
|
|
|
$this->assertEmpty($response['errors']);
|
|
|
|
|
|
|
|
// check only file 5 is left
|
2012-02-03 00:59:40 +01:00
|
|
|
$record = DataObject::get_by_id($record->class, $record->ID, false);
|
|
|
|
$this->assertNotContains('File4', $record->ManyManyFiles()->column('Title'));
|
|
|
|
$this->assertContains('File5', $record->ManyManyFiles()->column('Title'));
|
2013-05-27 05:22:59 +02:00
|
|
|
|
|
|
|
// check file 4 object exists
|
2012-02-03 00:59:40 +01:00
|
|
|
$this->assertFileExists($file4->FullPath, 'File is only detached, not deleted from filesystem');
|
|
|
|
}
|
|
|
|
|
2013-05-27 05:22:59 +02:00
|
|
|
/**
|
|
|
|
* Test that files can be deleted from has_one and the filesystem
|
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public function testDeleteFromHasOne() {
|
2012-02-03 00:59:40 +01:00
|
|
|
$this->loginWithPermission('ADMIN');
|
|
|
|
|
|
|
|
$record = $this->objFromFixture('UploadFieldTest_Record', 'record1');
|
|
|
|
$file1 = $this->objFromFixture('File', 'file1');
|
|
|
|
|
2013-05-27 05:22:59 +02:00
|
|
|
// Check that file initially exists
|
2012-02-03 00:59:40 +01:00
|
|
|
$this->assertTrue($record->HasOneFile()->exists());
|
2013-05-27 05:22:59 +02:00
|
|
|
$this->assertFileExists($file1->FullPath);
|
|
|
|
|
|
|
|
// Delete physical file and update record
|
|
|
|
$response = $this->mockFileDelete('HasOneFile', $file1->ID);
|
2012-02-03 00:59:40 +01:00
|
|
|
$this->assertFalse($response->isError());
|
2013-05-27 05:22:59 +02:00
|
|
|
$response = $this->mockUploadFileIDs('HasOneFile', array());
|
|
|
|
$this->assertEmpty($response['errors']);
|
|
|
|
|
|
|
|
// Check that file is not set against record
|
2012-02-03 00:59:40 +01:00
|
|
|
$record = DataObject::get_by_id($record->class, $record->ID, false);
|
|
|
|
$this->assertFalse($record->HasOneFile()->exists());
|
2013-05-27 05:22:59 +02:00
|
|
|
|
|
|
|
// Check that the physical file is deleted
|
2012-02-03 00:59:40 +01:00
|
|
|
$this->assertFileNotExists($file1->FullPath, 'File is also removed from filesystem');
|
|
|
|
}
|
|
|
|
|
2013-05-27 05:22:59 +02:00
|
|
|
/**
|
|
|
|
* Test that files can be deleted from has_many and the filesystem
|
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public function testDeleteFromHasMany() {
|
2012-02-03 00:59:40 +01:00
|
|
|
$this->loginWithPermission('ADMIN');
|
|
|
|
|
|
|
|
$record = $this->objFromFixture('UploadFieldTest_Record', 'record1');
|
|
|
|
$file2 = $this->objFromFixture('File', 'file2');
|
|
|
|
$file3 = $this->objFromFixture('File', 'file3');
|
|
|
|
|
2013-05-27 05:22:59 +02:00
|
|
|
// Check that files initially exists
|
2012-02-03 00:59:40 +01:00
|
|
|
$this->assertEquals(array('File2', 'File3'), $record->HasManyFiles()->column('Title'));
|
2013-05-27 05:22:59 +02:00
|
|
|
$this->assertFileExists($file2->FullPath);
|
|
|
|
$this->assertFileExists($file3->FullPath);
|
|
|
|
|
|
|
|
// Delete physical file and update record without file 2
|
|
|
|
$response = $this->mockFileDelete('HasManyFiles', $file2->ID);
|
2012-02-03 00:59:40 +01:00
|
|
|
$this->assertFalse($response->isError());
|
2013-05-27 05:22:59 +02:00
|
|
|
$response = $this->mockUploadFileIDs('HasManyFiles', array($file3->ID));
|
|
|
|
$this->assertEmpty($response['errors']);
|
|
|
|
|
|
|
|
// Test that file is removed from record
|
2012-02-03 00:59:40 +01:00
|
|
|
$record = DataObject::get_by_id($record->class, $record->ID, false);
|
|
|
|
$this->assertEquals(array('File3'), $record->HasManyFiles()->column('Title'));
|
2013-05-27 05:22:59 +02:00
|
|
|
|
|
|
|
// Test that physical file is removed
|
2012-02-03 00:59:40 +01:00
|
|
|
$this->assertFileNotExists($file2->FullPath, 'File is also removed from filesystem');
|
|
|
|
}
|
|
|
|
|
2013-05-27 05:22:59 +02:00
|
|
|
/**
|
|
|
|
* Test that files can be deleted from many_many and the filesystem
|
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public function testDeleteFromManyMany() {
|
2012-02-03 00:59:40 +01:00
|
|
|
$this->loginWithPermission('ADMIN');
|
|
|
|
|
|
|
|
$record = $this->objFromFixture('UploadFieldTest_Record', 'record1');
|
|
|
|
$file4 = $this->objFromFixture('File', 'file4');
|
|
|
|
$file5 = $this->objFromFixture('File', 'file5');
|
|
|
|
$fileNoDelete = $this->objFromFixture('File', 'file-nodelete');
|
|
|
|
|
2013-05-27 05:22:59 +02:00
|
|
|
// Test that files initially exist
|
|
|
|
$setFiles = $record->ManyManyFiles()->column('Title');
|
|
|
|
$this->assertContains('File4', $setFiles);
|
|
|
|
$this->assertContains('File5', $setFiles);
|
|
|
|
$this->assertContains('nodelete.txt', $setFiles);
|
|
|
|
$this->assertFileExists($file4->FullPath);
|
|
|
|
$this->assertFileExists($file5->FullPath);
|
|
|
|
$this->assertFileExists($fileNoDelete->FullPath);
|
|
|
|
|
|
|
|
// Delete physical file and update record without file 4
|
|
|
|
$response = $this->mockFileDelete('ManyManyFiles', $file4->ID);
|
2012-02-03 00:59:40 +01:00
|
|
|
$this->assertFalse($response->isError());
|
2013-05-27 05:22:59 +02:00
|
|
|
|
|
|
|
// Check file is removed from record
|
2012-02-03 00:59:40 +01:00
|
|
|
$record = DataObject::get_by_id($record->class, $record->ID, false);
|
|
|
|
$this->assertNotContains('File4', $record->ManyManyFiles()->column('Title'));
|
|
|
|
$this->assertContains('File5', $record->ManyManyFiles()->column('Title'));
|
2013-05-27 05:22:59 +02:00
|
|
|
|
|
|
|
// Check physical file is removed from filesystem
|
2012-02-03 00:59:40 +01:00
|
|
|
$this->assertFileNotExists($file4->FullPath, 'File is also removed from filesystem');
|
|
|
|
|
|
|
|
// Test record-based permissions
|
2013-05-27 05:22:59 +02:00
|
|
|
$response = $this->mockFileDelete('ManyManyFiles/', $fileNoDelete->ID);
|
2012-02-03 00:59:40 +01:00
|
|
|
$this->assertEquals(403, $response->getStatusCode());
|
|
|
|
}
|
|
|
|
|
2013-05-27 05:22:59 +02:00
|
|
|
/**
|
|
|
|
* Test control output html
|
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public function testView() {
|
2012-02-03 00:59:40 +01:00
|
|
|
$this->loginWithPermission('ADMIN');
|
|
|
|
|
|
|
|
$record = $this->objFromFixture('UploadFieldTest_Record', 'record1');
|
|
|
|
$file4 = $this->objFromFixture('File', 'file4');
|
|
|
|
$file5 = $this->objFromFixture('File', 'file5');
|
|
|
|
$fileNoView = $this->objFromFixture('File', 'file-noview');
|
|
|
|
$fileNoEdit = $this->objFromFixture('File', 'file-noedit');
|
|
|
|
$fileNoDelete = $this->objFromFixture('File', 'file-nodelete');
|
|
|
|
|
|
|
|
$response = $this->get('UploadFieldTest_Controller');
|
|
|
|
$this->assertFalse($response->isError());
|
|
|
|
|
|
|
|
$parser = new CSSContentParser($response->getBody());
|
2013-05-31 19:59:51 +02:00
|
|
|
$items = $parser->getBySelector(
|
|
|
|
'#UploadFieldTestForm_Form_HasManyNoViewFiles_Holder .ss-uploadfield-files .ss-uploadfield-item'
|
|
|
|
);
|
2012-02-03 00:59:40 +01:00
|
|
|
$ids = array();
|
|
|
|
foreach($items as $item) $ids[] = (int)$item['data-fileid'];
|
|
|
|
|
|
|
|
$this->assertContains($file4->ID, $ids, 'Views related file');
|
|
|
|
$this->assertContains($file5->ID, $ids, 'Views related file');
|
|
|
|
$this->assertNotContains($fileNoView->ID, $ids, "Doesn't view files without view permissions");
|
|
|
|
$this->assertContains($fileNoEdit->ID, $ids, "Views files without edit permissions");
|
|
|
|
$this->assertContains($fileNoDelete->ID, $ids, "Views files without delete permissions");
|
|
|
|
}
|
|
|
|
|
2012-09-19 12:07:39 +02:00
|
|
|
public function testEdit() {
|
2012-02-03 00:59:40 +01:00
|
|
|
$this->loginWithPermission('ADMIN');
|
|
|
|
|
|
|
|
$record = $this->objFromFixture('UploadFieldTest_Record', 'record1');
|
|
|
|
$file4 = $this->objFromFixture('File', 'file4');
|
|
|
|
$file5 = $this->objFromFixture('File', 'file5');
|
|
|
|
$fileNoEdit = $this->objFromFixture('File', 'file-noedit');
|
|
|
|
$baseUrl = 'UploadFieldTest_Controller/Form/field/ManyManyFiles/item/' . $file4->ID;
|
|
|
|
|
|
|
|
$response = $this->get($baseUrl . '/edit');
|
|
|
|
$this->assertFalse($response->isError());
|
|
|
|
|
|
|
|
$response = $this->post($baseUrl . '/EditForm', array('Title' => 'File 4 modified'));
|
|
|
|
$this->assertFalse($response->isError());
|
|
|
|
|
|
|
|
$record = DataObject::get_by_id($record->class, $record->ID, false);
|
|
|
|
$file4 = DataObject::get_by_id($file4->class, $file4->ID, false);
|
|
|
|
$this->assertEquals('File 4 modified', $file4->Title);
|
|
|
|
|
|
|
|
// Test record-based permissions
|
|
|
|
$response = $this->post(
|
|
|
|
'UploadFieldTest_Controller/Form/field/ManyManyFiles/item/' . $fileNoEdit->ID . '/edit',
|
|
|
|
array()
|
|
|
|
);
|
|
|
|
$this->assertEquals(403, $response->getStatusCode());
|
|
|
|
}
|
|
|
|
|
2012-09-19 12:07:39 +02:00
|
|
|
public function testGetRecord() {
|
2012-02-03 00:59:40 +01:00
|
|
|
$record = $this->objFromFixture('UploadFieldTest_Record', 'record1');
|
|
|
|
$form = $this->getMockForm();
|
|
|
|
|
2013-05-27 05:22:59 +02:00
|
|
|
$field = UploadField::create('MyField');
|
2012-02-03 00:59:40 +01:00
|
|
|
$field->setForm($form);
|
|
|
|
$this->assertNull($field->getRecord(), 'Returns no record by default');
|
|
|
|
|
2013-05-27 05:22:59 +02:00
|
|
|
$field = UploadField::create('MyField');
|
2012-02-03 00:59:40 +01:00
|
|
|
$field->setForm($form);
|
|
|
|
$form->loadDataFrom($record);
|
|
|
|
$this->assertEquals($record, $field->getRecord(), 'Returns record from form if available');
|
|
|
|
|
2013-05-27 05:22:59 +02:00
|
|
|
$field = UploadField::create('MyField');
|
2012-02-03 00:59:40 +01:00
|
|
|
$field->setForm($form);
|
|
|
|
$field->setRecord($record);
|
|
|
|
$this->assertEquals($record, $field->getRecord(), 'Returns record when set explicitly');
|
|
|
|
}
|
|
|
|
|
2012-09-19 12:07:39 +02:00
|
|
|
public function testSetItems() {
|
2012-02-03 00:59:40 +01:00
|
|
|
$record = $this->objFromFixture('UploadFieldTest_Record', 'record1');
|
|
|
|
$items = new ArrayList(array(
|
|
|
|
$this->objFromFixture('File', 'file1'),
|
|
|
|
$this->objFromFixture('File', 'file2')
|
|
|
|
));
|
2013-05-27 05:22:59 +02:00
|
|
|
|
|
|
|
// Field with no record attached
|
|
|
|
$field = UploadField::create('DummyField');
|
|
|
|
$field->setItems($items);
|
|
|
|
$this->assertEquals(array('File1', 'File2'), $field->getItems()->column('Title'));
|
2012-02-03 00:59:40 +01:00
|
|
|
|
|
|
|
// Anonymous field
|
2013-05-27 05:22:59 +02:00
|
|
|
$field = UploadField::create('MyField');
|
2012-02-03 00:59:40 +01:00
|
|
|
$field->setRecord($record);
|
|
|
|
$field->setItems($items);
|
|
|
|
$this->assertEquals(array('File1', 'File2'), $field->getItems()->column('Title'));
|
|
|
|
|
|
|
|
// Field with has_one auto-detected
|
2013-05-27 05:22:59 +02:00
|
|
|
$field = UploadField::create('HasOneFile');
|
2012-02-03 00:59:40 +01:00
|
|
|
$field->setRecord($record);
|
|
|
|
$field->setItems($items);
|
|
|
|
$this->assertEquals(array('File1', 'File2'), $field->getItems()->column('Title'),
|
|
|
|
'Allows overwriting of items even when relationship is detected'
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2012-09-19 12:07:39 +02:00
|
|
|
public function testGetItems() {
|
2012-02-03 00:59:40 +01:00
|
|
|
$record = $this->objFromFixture('UploadFieldTest_Record', 'record1');
|
|
|
|
|
|
|
|
// Anonymous field
|
2013-05-27 05:22:59 +02:00
|
|
|
$field = UploadField::create('MyField');
|
|
|
|
$field->setValue(null, $record);
|
2012-02-03 00:59:40 +01:00
|
|
|
$this->assertEquals(array(), $field->getItems()->column('Title'));
|
|
|
|
|
|
|
|
// Field with has_one auto-detected
|
2013-05-27 05:22:59 +02:00
|
|
|
$field = UploadField::create('HasOneFile');
|
|
|
|
$field->setValue(null, $record);
|
2012-02-03 00:59:40 +01:00
|
|
|
$this->assertEquals(array('File1'), $field->getItems()->column('Title'));
|
|
|
|
|
|
|
|
// Field with has_many auto-detected
|
2013-05-27 05:22:59 +02:00
|
|
|
$field = UploadField::create('HasManyFiles');
|
|
|
|
$field->setValue(null, $record);
|
2012-02-03 00:59:40 +01:00
|
|
|
$this->assertEquals(array('File2', 'File3'), $field->getItems()->column('Title'));
|
|
|
|
|
|
|
|
// Field with many_many auto-detected
|
2013-05-27 05:22:59 +02:00
|
|
|
$field = UploadField::create('ManyManyFiles');
|
|
|
|
$field->setValue(null, $record);
|
2012-02-03 00:59:40 +01:00
|
|
|
$this->assertNotContains('File1',$field->getItems()->column('Title'));
|
|
|
|
$this->assertNotContains('File2',$field->getItems()->column('Title'));
|
|
|
|
$this->assertNotContains('File3',$field->getItems()->column('Title'));
|
|
|
|
$this->assertContains('File4',$field->getItems()->column('Title'));
|
|
|
|
$this->assertContains('File5',$field->getItems()->column('Title'));
|
|
|
|
}
|
|
|
|
|
2012-09-19 12:07:39 +02:00
|
|
|
public function testReadonly() {
|
2012-02-03 00:59:40 +01:00
|
|
|
$this->loginWithPermission('ADMIN');
|
|
|
|
|
|
|
|
$response = $this->get('UploadFieldTest_Controller');
|
|
|
|
$this->assertFalse($response->isError());
|
|
|
|
|
|
|
|
$parser = new CSSContentParser($response->getBody());
|
2013-05-27 05:22:59 +02:00
|
|
|
|
|
|
|
$this->assertFalse(
|
2013-05-31 19:59:51 +02:00
|
|
|
(bool)$parser->getBySelector(
|
|
|
|
'#UploadFieldTestForm_Form_ReadonlyField .ss-uploadfield-files .ss-uploadfield-item .ss-ui-button'
|
|
|
|
),
|
2012-09-26 23:34:00 +02:00
|
|
|
'Removes all buttons on items');
|
2013-05-27 05:22:59 +02:00
|
|
|
$this->assertFalse(
|
2013-05-31 19:59:51 +02:00
|
|
|
(bool)$parser->getBySelector('#UploadFieldTestForm_Form_ReadonlyField .ss-uploadfield-dropzone'),
|
2013-05-27 05:22:59 +02:00
|
|
|
'Removes dropzone'
|
|
|
|
);
|
2013-05-31 19:59:51 +02:00
|
|
|
$this->assertFalse(
|
|
|
|
(bool)$parser->getBySelector(
|
|
|
|
'#UploadFieldTestForm_Form_ReadonlyField .ss-uploadfield-addfile'
|
|
|
|
),
|
2013-05-27 05:22:59 +02:00
|
|
|
'Entire "add" area'
|
|
|
|
);
|
2012-02-03 00:59:40 +01:00
|
|
|
}
|
|
|
|
|
2012-09-19 12:07:39 +02:00
|
|
|
public function testDisabled() {
|
2012-02-03 00:59:40 +01:00
|
|
|
$this->loginWithPermission('ADMIN');
|
|
|
|
|
|
|
|
$response = $this->get('UploadFieldTest_Controller');
|
|
|
|
$this->assertFalse($response->isError());
|
|
|
|
|
|
|
|
$parser = new CSSContentParser($response->getBody());
|
2012-09-26 23:34:00 +02:00
|
|
|
$this->assertFalse(
|
2013-05-31 19:59:51 +02:00
|
|
|
(bool)$parser->getBySelector(
|
|
|
|
'#UploadFieldTestForm_Form_DisabledField .ss-uploadfield-files .ss-uploadfield-item .ss-ui-button'
|
|
|
|
),
|
2012-09-26 23:34:00 +02:00
|
|
|
'Removes all buttons on items');
|
2013-05-31 19:59:51 +02:00
|
|
|
$this->assertFalse((bool)$parser->getBySelector(
|
|
|
|
'#UploadFieldTestForm_Form_DisabledField .ss-uploadfield-dropzone'
|
|
|
|
),
|
2012-09-26 23:34:00 +02:00
|
|
|
'Removes dropzone');
|
|
|
|
$this->assertFalse(
|
2013-05-31 19:59:51 +02:00
|
|
|
(bool)$parser->getBySelector('#UploadFieldTestForm_Form_DisabledField .ss-uploadfield-addfile'),
|
2013-05-27 05:22:59 +02:00
|
|
|
'Entire "add" area'
|
|
|
|
);
|
2012-02-03 00:59:40 +01:00
|
|
|
}
|
|
|
|
|
2013-01-09 11:34:02 +01:00
|
|
|
public function testCanUpload() {
|
|
|
|
$this->loginWithPermission('ADMIN');
|
|
|
|
$response = $this->get('UploadFieldTest_Controller');
|
|
|
|
$this->assertFalse($response->isError());
|
|
|
|
|
|
|
|
$parser = new CSSContentParser($response->getBody());
|
2013-05-31 19:59:51 +02:00
|
|
|
var_dump($response->getBody());
|
|
|
|
$this->assertFalse(
|
|
|
|
(bool)$parser->getBySelector(
|
|
|
|
'#UploadFieldTestForm_Form_CanUploadFalseField_Holder .ss-uploadfield-dropzone'
|
|
|
|
),
|
2013-01-09 11:34:02 +01:00
|
|
|
'Removes dropzone');
|
|
|
|
$this->assertTrue(
|
2013-05-31 19:59:51 +02:00
|
|
|
(bool)$parser->getBySelector('#UploadFieldTestForm_Form_CanUploadFalseField_Holder .ss-uploadfield-fromfiles'),
|
2013-01-09 11:34:02 +01:00
|
|
|
'Keeps "From files" button'
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testCanUploadWithPermissionCode() {
|
2013-05-27 05:22:59 +02:00
|
|
|
$field = UploadField::create('MyField');
|
2013-01-09 11:34:02 +01:00
|
|
|
|
2013-05-27 05:22:59 +02:00
|
|
|
$field->setCanUpload(true);
|
2013-01-09 11:34:02 +01:00
|
|
|
$this->assertTrue($field->canUpload());
|
|
|
|
|
2013-05-27 05:22:59 +02:00
|
|
|
$field->setCanUpload(false);
|
2013-01-09 11:34:02 +01:00
|
|
|
$this->assertFalse($field->canUpload());
|
|
|
|
|
|
|
|
$this->loginWithPermission('ADMIN');
|
|
|
|
|
2013-05-27 05:22:59 +02:00
|
|
|
$field->setCanUpload(false);
|
2013-01-09 11:34:02 +01:00
|
|
|
$this->assertFalse($field->canUpload());
|
|
|
|
|
2013-05-27 05:22:59 +02:00
|
|
|
$field->setCanUpload('ADMIN');
|
2013-01-09 11:34:02 +01:00
|
|
|
$this->assertTrue($field->canUpload());
|
|
|
|
}
|
|
|
|
|
2013-01-11 05:33:06 +01:00
|
|
|
public function testCanAttachExisting() {
|
|
|
|
$this->loginWithPermission('ADMIN');
|
|
|
|
$response = $this->get('UploadFieldTest_Controller');
|
|
|
|
$this->assertFalse($response->isError());
|
|
|
|
|
|
|
|
$parser = new CSSContentParser($response->getBody());
|
|
|
|
$this->assertTrue(
|
2013-05-31 19:59:51 +02:00
|
|
|
(bool)$parser->getBySelector(
|
|
|
|
'#UploadFieldTestForm_Form_CanAttachExistingFalseField_Holder .ss-uploadfield-fromcomputer-fileinput'
|
|
|
|
),
|
2013-01-11 05:33:06 +01:00
|
|
|
'Keeps input file control'
|
|
|
|
);
|
|
|
|
$this->assertFalse(
|
2013-05-31 19:59:51 +02:00
|
|
|
(bool)$parser->getBySelector(
|
|
|
|
'#UploadFieldTestForm_Form_CanAttachExistingFalseField_Holder .ss-uploadfield-fromfiles'
|
|
|
|
),
|
2013-01-11 05:33:06 +01:00
|
|
|
'Removes "From files" button'
|
|
|
|
);
|
2012-06-15 17:44:34 +02:00
|
|
|
}
|
|
|
|
|
2012-09-19 12:07:39 +02:00
|
|
|
public function testSelect() {
|
2012-02-08 00:58:58 +01:00
|
|
|
$this->loginWithPermission('ADMIN');
|
|
|
|
|
|
|
|
$record = $this->objFromFixture('UploadFieldTest_Record', 'record1');
|
|
|
|
$file4 = $this->objFromFixture('File', 'file4');
|
|
|
|
$file5 = $this->objFromFixture('File', 'file5');
|
|
|
|
$fileSubfolder = $this->objFromFixture('File', 'file-subfolder');
|
|
|
|
$fileNoEdit = $this->objFromFixture('File', 'file-noedit');
|
|
|
|
|
|
|
|
$response = $this->get('UploadFieldTest_Controller/Form/field/ManyManyFiles/select/');
|
|
|
|
$this->assertFalse($response->isError());
|
|
|
|
|
|
|
|
// A bit too much coupling with GridField, but a full template overload would make things too complex
|
|
|
|
$parser = new CSSContentParser($response->getBody());
|
|
|
|
$items = $parser->getBySelector('.ss-gridfield-item');
|
|
|
|
$itemIDs = array_map(create_function('$el', 'return (int)$el["data-id"];'), $items);
|
|
|
|
$this->assertContains($file4->ID, $itemIDs, 'Contains file in assigned folder');
|
|
|
|
$this->assertNotContains($fileSubfolder->ID, $itemIDs, 'Does not contain file in subfolder');
|
|
|
|
}
|
|
|
|
|
2012-02-03 00:59:40 +01:00
|
|
|
protected function getMockForm() {
|
|
|
|
return new Form(new Controller(), 'Form', new FieldList(), new FieldList());
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return Array Emulating an entry in the $_FILES superglobal
|
|
|
|
*/
|
|
|
|
protected function getUploadFile($tmpFileName = 'UploadFieldTest-testUpload.txt') {
|
|
|
|
$tmpFilePath = TEMP_FOLDER . '/' . $tmpFileName;
|
|
|
|
$tmpFileContent = '';
|
|
|
|
for($i=0; $i<10000; $i++) $tmpFileContent .= '0';
|
|
|
|
file_put_contents($tmpFilePath, $tmpFileContent);
|
|
|
|
|
|
|
|
// emulates the $_FILES array
|
|
|
|
return array(
|
2013-05-27 05:22:59 +02:00
|
|
|
'name' => array('Uploads' => array($tmpFileName)),
|
|
|
|
'type' => array('Uploads' => array('text/plaintext')),
|
|
|
|
'size' => array('Uploads' => array(filesize($tmpFilePath))),
|
|
|
|
'tmp_name' => array('Uploads' => array($tmpFilePath)),
|
|
|
|
'error' => array('Uploads' => array(UPLOAD_ERR_OK)),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Simulates a form post to the test controller with the specified file IDs
|
|
|
|
*
|
|
|
|
* @param string $fileField Name of field to assign ids to
|
|
|
|
* @param array $ids list of file IDs
|
|
|
|
* @return boolean Array with key 'errors'
|
|
|
|
*/
|
|
|
|
protected function mockUploadFileIDs($fileField, $ids) {
|
|
|
|
|
|
|
|
// collate file ids
|
|
|
|
$files = array();
|
|
|
|
foreach($ids as $id) {
|
|
|
|
$files[$id] = $id;
|
|
|
|
}
|
|
|
|
|
|
|
|
$data = array(
|
|
|
|
'action_submit' => 1
|
|
|
|
);
|
|
|
|
if($files) {
|
|
|
|
// Normal post requests can't submit empty array values for fields
|
|
|
|
$data[$fileField] = array('Files' => $files);
|
|
|
|
}
|
|
|
|
|
|
|
|
$form = new UploadFieldTestForm();
|
|
|
|
$form->loadDataFrom($data, true);
|
|
|
|
if($form->validate()) {
|
|
|
|
$record = $form->getRecord();
|
|
|
|
$form->saveInto($record);
|
|
|
|
$record->write();
|
|
|
|
return array('errors' => null);
|
|
|
|
} else {
|
|
|
|
return array('errors' => $form->getValidator()->getErrors());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Simulates a file upload
|
|
|
|
*
|
|
|
|
* @param string $fileField Name of the field to mock upload for
|
|
|
|
* @param array $tmpFileName Name of temporary file to upload
|
|
|
|
* @return SS_HTTPResponse form response
|
|
|
|
*/
|
|
|
|
protected function mockFileUpload($fileField, $tmpFileName) {
|
|
|
|
$upload = $this->getUploadFile($tmpFileName);
|
|
|
|
$_FILES = array($fileField => $upload);
|
|
|
|
return $this->post(
|
|
|
|
"UploadFieldTest_Controller/Form/field/{$fileField}/upload",
|
|
|
|
array($fileField => $upload)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Simulates a physical file deletion
|
|
|
|
*
|
|
|
|
* @param string $fileField Name of the field
|
|
|
|
* @param integer $fileID ID of the file to delete
|
|
|
|
* @return SS_HTTPResponse form response
|
|
|
|
*/
|
|
|
|
protected function mockFileDelete($fileField, $fileID) {
|
|
|
|
return $this->post(
|
|
|
|
"UploadFieldTest_Controller/Form/field/HasOneFile/item/{$fileID}/delete",
|
|
|
|
array()
|
2012-02-03 00:59:40 +01:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2012-09-19 12:07:39 +02:00
|
|
|
public function setUp() {
|
2012-02-03 00:59:40 +01:00
|
|
|
parent::setUp();
|
|
|
|
|
|
|
|
if(!file_exists(ASSETS_PATH)) mkdir(ASSETS_PATH);
|
|
|
|
|
|
|
|
/* Create a test folders for each of the fixture references */
|
|
|
|
$folderIDs = $this->allFixtureIDs('Folder');
|
|
|
|
foreach($folderIDs as $folderID) {
|
|
|
|
$folder = DataObject::get_by_id('Folder', $folderID);
|
|
|
|
if(!file_exists(BASE_PATH."/$folder->Filename")) mkdir(BASE_PATH."/$folder->Filename");
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Create a test files for each of the fixture references */
|
|
|
|
$fileIDs = $this->allFixtureIDs('File');
|
|
|
|
foreach($fileIDs as $fileID) {
|
|
|
|
$file = DataObject::get_by_id('File', $fileID);
|
|
|
|
$fh = fopen(BASE_PATH."/$file->Filename", "w");
|
|
|
|
fwrite($fh, str_repeat('x',1000000));
|
|
|
|
fclose($fh);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-09-19 12:07:39 +02:00
|
|
|
public function tearDown() {
|
2012-02-03 00:59:40 +01:00
|
|
|
parent::tearDown();
|
|
|
|
|
|
|
|
/* Remove the test files that we've created */
|
|
|
|
$fileIDs = $this->allFixtureIDs('File');
|
|
|
|
foreach($fileIDs as $fileID) {
|
|
|
|
$file = DataObject::get_by_id('File', $fileID);
|
|
|
|
if($file && file_exists(BASE_PATH."/$file->Filename")) unlink(BASE_PATH."/$file->Filename");
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Remove the test folders that we've crated */
|
|
|
|
$folderIDs = $this->allFixtureIDs('Folder');
|
|
|
|
foreach($folderIDs as $folderID) {
|
|
|
|
$folder = DataObject::get_by_id('Folder', $folderID);
|
2012-09-26 23:34:00 +02:00
|
|
|
if($folder && file_exists(BASE_PATH."/$folder->Filename")) {
|
|
|
|
Filesystem::removeFolder(BASE_PATH."/$folder->Filename");
|
|
|
|
}
|
2012-02-03 00:59:40 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Remove left over folders and any files that may exist
|
2012-04-18 04:46:54 +02:00
|
|
|
if(file_exists('../assets/UploadFieldTest')) Filesystem::removeFolder('../assets/UploadFieldTest');
|
2012-02-03 00:59:40 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
class UploadFieldTest_Record extends DataObject implements TestOnly {
|
|
|
|
|
2013-03-21 19:48:54 +01:00
|
|
|
private static $db = array(
|
2012-04-18 22:30:12 +02:00
|
|
|
'Title' => 'Text',
|
|
|
|
);
|
2012-02-03 00:59:40 +01:00
|
|
|
|
2013-03-21 19:48:54 +01:00
|
|
|
private static $has_one = array(
|
2012-04-18 22:30:12 +02:00
|
|
|
'HasOneFile' => 'File',
|
|
|
|
'HasOneFileMaxOne' => 'File',
|
|
|
|
'HasOneFileMaxTwo' => 'File',
|
2012-04-18 05:22:40 +02:00
|
|
|
'HasOneExtendedFile' => 'UploadFieldTest_ExtendedFile'
|
2012-04-18 22:30:12 +02:00
|
|
|
);
|
2012-02-03 00:59:40 +01:00
|
|
|
|
2013-03-21 19:48:54 +01:00
|
|
|
private static $has_many = array(
|
2013-05-27 05:22:59 +02:00
|
|
|
'HasManyFiles' => 'File.HasManyRecord',
|
|
|
|
'HasManyFilesMaxTwo' => 'File.HasManyMaxTwoRecord',
|
|
|
|
'HasManyNoViewFiles' => 'File.HasManyNoViewRecord',
|
|
|
|
'ReadonlyField' => 'File.ReadonlyRecord'
|
2012-04-18 22:30:12 +02:00
|
|
|
);
|
2012-02-03 00:59:40 +01:00
|
|
|
|
2013-03-21 19:48:54 +01:00
|
|
|
private static $many_many = array(
|
2013-05-27 05:22:59 +02:00
|
|
|
'ManyManyFiles' => 'File'
|
2012-04-18 22:30:12 +02:00
|
|
|
);
|
2012-02-03 00:59:40 +01:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
class UploadFieldTest_FileExtension extends DataExtension implements TestOnly {
|
|
|
|
|
2013-03-21 19:48:54 +01:00
|
|
|
private static $has_one = array(
|
2013-05-27 05:22:59 +02:00
|
|
|
'HasManyRecord' => 'UploadFieldTest_Record',
|
|
|
|
'HasManyMaxTwoRecord' => 'UploadFieldTest_Record',
|
|
|
|
'HasManyNoViewRecord' => 'UploadFieldTest_Record',
|
|
|
|
'ReadonlyRecord' => 'UploadFieldTest_Record'
|
|
|
|
);
|
|
|
|
|
|
|
|
private static $has_many = array(
|
|
|
|
'HasOneRecords' => 'UploadFieldTest_Record.HasOneFile',
|
|
|
|
'HasOneMaxOneRecords' => 'UploadFieldTest_Record.HasOneFileMaxOne',
|
|
|
|
'HasOneMaxTwoRecords' => 'UploadFieldTest_Record.HasOneFileMaxTwo',
|
|
|
|
);
|
|
|
|
|
|
|
|
private static $belongs_many_many = array(
|
|
|
|
'ManyManyRecords' => 'UploadFieldTest_Record'
|
2012-04-18 22:30:12 +02:00
|
|
|
);
|
2012-02-03 00:59:40 +01:00
|
|
|
|
2012-09-19 12:07:39 +02:00
|
|
|
public function canDelete($member = null) {
|
2012-02-03 00:59:40 +01:00
|
|
|
if($this->owner->Name == 'nodelete.txt') return false;
|
|
|
|
}
|
|
|
|
|
2012-09-19 12:07:39 +02:00
|
|
|
public function canEdit($member = null) {
|
2012-02-03 00:59:40 +01:00
|
|
|
if($this->owner->Name == 'noedit.txt') return false;
|
|
|
|
}
|
|
|
|
|
2012-09-19 12:07:39 +02:00
|
|
|
public function canView($member = null) {
|
2012-02-03 00:59:40 +01:00
|
|
|
if($this->owner->Name == 'noview.txt') return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-05-27 05:22:59 +02:00
|
|
|
/**
|
|
|
|
* Used for testing the create-on-upload
|
|
|
|
*/
|
|
|
|
class UploadFieldTest_ExtendedFile extends File implements TestOnly {
|
|
|
|
|
|
|
|
private static $has_many = array(
|
|
|
|
'HasOneExtendedRecords' => 'UploadFieldTest_Record.HasOneExtendedFile'
|
|
|
|
);
|
|
|
|
}
|
2012-02-03 00:59:40 +01:00
|
|
|
|
2013-05-27 05:22:59 +02:00
|
|
|
class UploadFieldTestForm extends Form implements TestOnly {
|
|
|
|
|
|
|
|
public function getRecord() {
|
|
|
|
if(empty($this->record)) {
|
|
|
|
$this->record = DataObject::get_one('UploadFieldTest_Record', '"Title" = \'Record 1\'');
|
|
|
|
}
|
|
|
|
return $this->record;
|
2012-02-03 00:59:40 +01:00
|
|
|
}
|
2013-05-27 05:22:59 +02:00
|
|
|
|
|
|
|
function __construct($controller = null, $name = 'Form') {
|
|
|
|
if(empty($controller)) {
|
|
|
|
$controller = new UploadFieldTest_Controller();
|
|
|
|
}
|
|
|
|
|
|
|
|
$fieldNoRelation = UploadField::create('NoRelationField')
|
|
|
|
->setFolderName('UploadFieldTest');
|
|
|
|
|
|
|
|
$fieldHasOne = UploadField::create('HasOneFile')
|
|
|
|
->setFolderName('UploadFieldTest');
|
2012-02-03 00:59:40 +01:00
|
|
|
|
2013-05-27 05:22:59 +02:00
|
|
|
$fieldHasOneExtendedFile = UploadField::create('HasOneExtendedFile')
|
|
|
|
->setFolderName('UploadFieldTest');
|
2012-02-03 00:59:40 +01:00
|
|
|
|
2013-05-27 05:22:59 +02:00
|
|
|
$fieldHasOneMaxOne = UploadField::create('HasOneFileMaxOne')
|
|
|
|
->setFolderName('UploadFieldTest')
|
|
|
|
->setAllowedMaxFileNumber(1);
|
|
|
|
|
|
|
|
$fieldHasOneMaxTwo = UploadField::create('HasOneFileMaxTwo')
|
|
|
|
->setFolderName('UploadFieldTest')
|
|
|
|
->setAllowedMaxFileNumber(2);
|
|
|
|
|
|
|
|
$fieldHasMany = UploadField::create('HasManyFiles')
|
|
|
|
->setFolderName('UploadFieldTest');
|
|
|
|
|
|
|
|
$fieldHasManyMaxTwo = UploadField::create('HasManyFilesMaxTwo')
|
|
|
|
->setFolderName('UploadFieldTest')
|
|
|
|
->setAllowedMaxFileNumber(2);
|
|
|
|
|
|
|
|
$fieldManyMany = UploadField::create('ManyManyFiles')
|
|
|
|
->setFolderName('UploadFieldTest');
|
|
|
|
|
|
|
|
$fieldHasManyNoView = UploadField::create('HasManyNoViewFiles')
|
|
|
|
->setFolderName('UploadFieldTest');
|
|
|
|
|
|
|
|
$fieldReadonly = UploadField::create('ReadonlyField')
|
|
|
|
->setFolderName('UploadFieldTest')
|
|
|
|
->performReadonlyTransformation();
|
|
|
|
|
|
|
|
$fieldDisabled = UploadField::create('DisabledField')
|
|
|
|
->setFolderName('UploadFieldTest')
|
|
|
|
->performDisabledTransformation();
|
|
|
|
|
|
|
|
$fieldSubfolder = UploadField::create('SubfolderField')
|
|
|
|
->setFolderName('UploadFieldTest/subfolder1');
|
|
|
|
|
|
|
|
$fieldCanUploadFalse = UploadField::create('CanUploadFalseField')
|
|
|
|
->setCanUpload(false);
|
|
|
|
|
|
|
|
$fieldCanAttachExisting = UploadField::create('CanAttachExistingFalseField')
|
|
|
|
->setCanAttachExisting(false);
|
|
|
|
|
|
|
|
$fields = new FieldList(
|
|
|
|
$fieldNoRelation,
|
|
|
|
$fieldHasOne,
|
|
|
|
$fieldHasOneMaxOne,
|
|
|
|
$fieldHasOneMaxTwo,
|
|
|
|
$fieldHasOneExtendedFile,
|
|
|
|
$fieldHasMany,
|
|
|
|
$fieldHasManyMaxTwo,
|
|
|
|
$fieldManyMany,
|
|
|
|
$fieldHasManyNoView,
|
|
|
|
$fieldReadonly,
|
|
|
|
$fieldDisabled,
|
|
|
|
$fieldSubfolder,
|
|
|
|
$fieldCanUploadFalse,
|
|
|
|
$fieldCanAttachExisting
|
|
|
|
);
|
|
|
|
$actions = new FieldList(
|
|
|
|
new FormAction('submit')
|
|
|
|
);
|
|
|
|
$validator = new RequiredFields();
|
|
|
|
|
|
|
|
parent::__construct($controller, $name, $fields, $actions, $validator);
|
|
|
|
|
|
|
|
$this->loadDataFrom($this->getRecord());
|
2012-02-03 00:59:40 +01:00
|
|
|
}
|
|
|
|
|
2013-05-27 05:22:59 +02:00
|
|
|
public function submit($data, Form $form) {
|
|
|
|
$record = $this->getRecord();
|
|
|
|
$form->saveInto($record);
|
|
|
|
$record->write();
|
|
|
|
return json_encode($record->toMap());
|
|
|
|
}
|
2012-03-27 06:04:11 +02:00
|
|
|
}
|
2012-04-18 05:22:40 +02:00
|
|
|
|
|
|
|
|
2013-05-27 05:22:59 +02:00
|
|
|
class UploadFieldTest_Controller extends Controller implements TestOnly {
|
|
|
|
|
|
|
|
protected $template = 'BlankPage';
|
|
|
|
|
|
|
|
private static $allowed_actions = array('Form', 'index', 'submit');
|
|
|
|
|
|
|
|
public function Form() {
|
|
|
|
return new UploadFieldTestForm($this, 'Form');
|
|
|
|
}
|
|
|
|
}
|