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
|
|
|
|
|
|
|
static $fixture_file = 'UploadFieldTest.yml';
|
|
|
|
|
|
|
|
protected $extraDataObjects = array('UploadFieldTest_Record');
|
|
|
|
|
|
|
|
protected $requiredExtensions = array(
|
|
|
|
'File' => array('UploadFieldTest_FileExtension')
|
|
|
|
);
|
|
|
|
|
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';
|
|
|
|
$_FILES = array('NoRelationField' => $this->getUploadFile($tmpFileName));
|
|
|
|
$response = $this->post(
|
|
|
|
'UploadFieldTest_Controller/Form/field/NoRelationField/upload',
|
|
|
|
array('NoRelationField' => $this->getUploadFile($tmpFileName))
|
|
|
|
);
|
|
|
|
$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
|
|
|
}
|
|
|
|
|
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();
|
|
|
|
|
|
|
|
$tmpFileName = 'testUploadHasOneRelation.txt';
|
|
|
|
$_FILES = array('HasOneFile' => $this->getUploadFile($tmpFileName));
|
|
|
|
$response = $this->post(
|
|
|
|
'UploadFieldTest_Controller/Form/field/HasOneFile/upload',
|
|
|
|
array('HasOneFile' => $this->getUploadFile($tmpFileName))
|
|
|
|
);
|
|
|
|
$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
|
|
|
|
|
|
|
$record = DataObject::get_by_id($record->class, $record->ID, false);
|
|
|
|
$this->assertTrue($record->HasOneFile()->exists());
|
|
|
|
$this->assertEquals($record->HasOneFile()->Name, $tmpFileName);
|
|
|
|
}
|
|
|
|
|
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();
|
|
|
|
|
|
|
|
$tmpFileName = 'testUploadHasOneRelationWithExtendedFile.txt';
|
|
|
|
$_FILES = array('HasOneExtendedFile' => $this->getUploadFile($tmpFileName));
|
|
|
|
$response = $this->post(
|
|
|
|
'UploadFieldTest_Controller/Form/field/HasOneExtendedFile/upload',
|
|
|
|
array('HasOneExtendedFile' => $this->getUploadFile($tmpFileName))
|
|
|
|
);
|
|
|
|
$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');
|
|
|
|
|
|
|
|
$record = DataObject::get_by_id($record->class, $record->ID, false);
|
|
|
|
$this->assertTrue($record->HasOneExtendedFile()->exists(), 'The extended file is attached to the class');
|
|
|
|
$this->assertEquals($record->HasOneExtendedFile()->Name, $tmpFileName, 'Proper file has been attached');
|
|
|
|
}
|
|
|
|
|
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');
|
|
|
|
|
|
|
|
$tmpFileName = 'testUploadHasManyRelation.txt';
|
|
|
|
$_FILES = array('HasManyFiles' => $this->getUploadFile($tmpFileName));
|
|
|
|
$response = $this->post(
|
|
|
|
'UploadFieldTest_Controller/Form/field/HasManyFiles/upload',
|
|
|
|
array('HasManyFiles' => $this->getUploadFile($tmpFileName))
|
|
|
|
);
|
|
|
|
$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
|
|
|
|
|
|
|
$record = DataObject::get_by_id($record->class, $record->ID, false);
|
|
|
|
$this->assertEquals(3, $record->HasManyFiles()->Count());
|
|
|
|
$this->assertEquals($record->HasManyFiles()->Last()->Name, $tmpFileName);
|
|
|
|
}
|
|
|
|
|
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();
|
|
|
|
|
|
|
|
$tmpFileName = 'testUploadManyManyRelation.txt';
|
|
|
|
$_FILES = array('ManyManyFiles' => $this->getUploadFile($tmpFileName));
|
|
|
|
$response = $this->post(
|
|
|
|
'UploadFieldTest_Controller/Form/field/ManyManyFiles/upload',
|
|
|
|
array('ManyManyFiles' => $this->getUploadFile($tmpFileName))
|
|
|
|
);
|
|
|
|
$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
|
|
|
|
|
|
|
$record = DataObject::get_by_id($record->class, $record->ID, false);
|
|
|
|
$this->assertEquals($relationCount+1, $record->ManyManyFiles()->Count());
|
|
|
|
$this->assertEquals($record->ManyManyFiles()->Last()->Name, $tmpFileName);
|
|
|
|
}
|
|
|
|
|
2013-07-12 13:16:25 +02:00
|
|
|
/**
|
|
|
|
* Partially covered by {@link UploadTest->testUploadAcceptsAllowedExtension()},
|
|
|
|
* but this test additionally verifies that those constraints are actually enforced
|
|
|
|
* in this controller method.
|
|
|
|
*/
|
|
|
|
public function testAllowedExtensions() {
|
|
|
|
$this->loginWithPermission('ADMIN');
|
|
|
|
|
|
|
|
$invalidFile = 'invalid.php';
|
|
|
|
$_FILES = array('AllowedExtensionsField' => $this->getUploadFile($invalidFile));
|
|
|
|
$response = $this->post(
|
|
|
|
'UploadFieldTest_Controller/Form/field/AllowedExtensionsField/upload',
|
|
|
|
array('AllowedExtensionsField' => $this->getUploadFile($invalidFile))
|
|
|
|
);
|
|
|
|
$this->assertTrue($response->isError());
|
|
|
|
$this->assertContains('Extension is not allowed', $response->getBody());
|
|
|
|
|
|
|
|
$validFile = 'valid.jpg';
|
|
|
|
$_FILES = array('AllowedExtensionsField' => $this->getUploadFile($validFile));
|
|
|
|
$response = $this->post(
|
|
|
|
'UploadFieldTest_Controller/Form/field/AllowedExtensionsField/upload',
|
|
|
|
array('AllowedExtensionsField' => $this->getUploadFile($validFile))
|
|
|
|
);
|
|
|
|
$this->assertFalse($response->isError());
|
|
|
|
$this->assertNotContains('Extension is not allowed', $response->getBody());
|
|
|
|
}
|
|
|
|
|
2012-09-19 12:07:39 +02:00
|
|
|
public function testAllowedMaxFileNumberWithHasOne() {
|
2012-04-17 07:12:56 +02:00
|
|
|
$this->loginWithPermission('ADMIN');
|
|
|
|
|
|
|
|
// 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) {
|
|
|
|
// Unset existing has_one relation before re-uploading
|
|
|
|
$record = $this->objFromFixture('UploadFieldTest_Record', 'record1');
|
|
|
|
$record->{$recordName . 'ID'} = null;
|
|
|
|
$record->write();
|
|
|
|
|
|
|
|
$tmpFileName = 'testUploadHasOneRelation.txt';
|
|
|
|
$_FILES = array($recordName => $this->getUploadFile($tmpFileName));
|
|
|
|
$response = $this->post(
|
|
|
|
"UploadFieldTest_Controller/Form/field/$recordName/upload",
|
|
|
|
array($recordName => $this->getUploadFile($tmpFileName))
|
|
|
|
);
|
|
|
|
$body = json_decode($response->getBody());
|
|
|
|
$this->assertEquals(0, $body[0]->error);
|
|
|
|
|
|
|
|
// Write to it again, should result in an error.
|
|
|
|
$response = $this->post(
|
|
|
|
"UploadFieldTest_Controller/Form/field/$recordName/upload",
|
|
|
|
array($recordName => $this->getUploadFile($tmpFileName))
|
|
|
|
);
|
|
|
|
$body = json_decode($response->getBody());
|
|
|
|
$this->assertNotEquals(0, $body[0]->error);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-09-19 12:07:39 +02:00
|
|
|
public function testAllowedMaxFileNumberWithHasMany() {
|
2012-04-17 07:12:56 +02:00
|
|
|
$this->loginWithPermission('ADMIN');
|
|
|
|
|
|
|
|
// 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();
|
|
|
|
|
|
|
|
$tmpFileName = 'testUploadHasManyRelation.txt';
|
|
|
|
$_FILES = array('HasManyFilesMaxTwo' => $this->getUploadFile($tmpFileName));
|
|
|
|
|
|
|
|
// Write the first element, should be okay.
|
|
|
|
$response = $this->post(
|
|
|
|
'UploadFieldTest_Controller/Form/field/HasManyFilesMaxTwo/upload',
|
|
|
|
array('HasManyFilesMaxTwo' => $this->getUploadFile($tmpFileName))
|
|
|
|
);
|
|
|
|
$body = json_decode($response->getBody());
|
|
|
|
$this->assertEquals(0, $body[0]->error);
|
|
|
|
|
|
|
|
// Write the second element, should be okay.
|
|
|
|
$response = $this->post(
|
|
|
|
'UploadFieldTest_Controller/Form/field/HasManyFilesMaxTwo/upload',
|
|
|
|
array('HasManyFilesMaxTwo' => $this->getUploadFile($tmpFileName))
|
|
|
|
);
|
|
|
|
$body = json_decode($response->getBody());
|
|
|
|
$this->assertEquals(0, $body[0]->error);
|
|
|
|
|
|
|
|
// Write the third element, should result in error.
|
|
|
|
$response = $this->post(
|
|
|
|
'UploadFieldTest_Controller/Form/field/HasManyFilesMaxTwo/upload',
|
|
|
|
array('HasManyFilesMaxTwo' => $this->getUploadFile($tmpFileName))
|
|
|
|
);
|
|
|
|
$body = json_decode($response->getBody());
|
|
|
|
$this->assertNotEquals(0, $body[0]->error);
|
2012-02-03 00:59:40 +01:00
|
|
|
}
|
|
|
|
|
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');
|
|
|
|
|
|
|
|
$this->assertTrue($record->HasOneFile()->exists());
|
|
|
|
$response = $this->post(
|
|
|
|
'UploadFieldTest_Controller/Form/field/HasOneFile/item/' . $file1->ID . '/remove',
|
|
|
|
array()
|
|
|
|
);
|
|
|
|
$this->assertFalse($response->isError());
|
|
|
|
$record = DataObject::get_by_id($record->class, $record->ID, false);
|
|
|
|
$this->assertFalse($record->HasOneFile()->exists());
|
|
|
|
$this->assertFileExists($file1->FullPath, 'File is only detached, not deleted from filesystem');
|
|
|
|
}
|
|
|
|
|
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');
|
|
|
|
|
|
|
|
$this->assertEquals(array('File2', 'File3'), $record->HasManyFiles()->column('Title'));
|
|
|
|
$response = $this->post(
|
|
|
|
'UploadFieldTest_Controller/Form/field/HasManyFiles/item/' . $file2->ID . '/remove',
|
|
|
|
array()
|
|
|
|
);
|
|
|
|
$this->assertFalse($response->isError());
|
|
|
|
$record = DataObject::get_by_id($record->class, $record->ID, false);
|
|
|
|
$this->assertEquals(array('File3'), $record->HasManyFiles()->column('Title'));
|
|
|
|
$this->assertFileExists($file3->FullPath, 'File is only detached, not deleted from filesystem');
|
|
|
|
}
|
|
|
|
|
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');
|
|
|
|
|
|
|
|
$this->assertContains('File4', $record->ManyManyFiles()->column('Title'));
|
|
|
|
$this->assertContains('File5', $record->ManyManyFiles()->column('Title'));
|
|
|
|
$response = $this->post(
|
|
|
|
'UploadFieldTest_Controller/Form/field/ManyManyFiles/item/' . $file4->ID . '/remove',
|
|
|
|
array()
|
|
|
|
);
|
|
|
|
$this->assertFalse($response->isError());
|
|
|
|
$record = DataObject::get_by_id($record->class, $record->ID, false);
|
|
|
|
$this->assertNotContains('File4', $record->ManyManyFiles()->column('Title'));
|
|
|
|
$this->assertContains('File5', $record->ManyManyFiles()->column('Title'));
|
|
|
|
$this->assertFileExists($file4->FullPath, 'File is only detached, not deleted from 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');
|
|
|
|
|
|
|
|
$this->assertTrue($record->HasOneFile()->exists());
|
|
|
|
$response = $this->post(
|
|
|
|
'UploadFieldTest_Controller/Form/field/HasOneFile/item/' . $file1->ID . '/delete',
|
|
|
|
array()
|
|
|
|
);
|
|
|
|
$this->assertFalse($response->isError());
|
|
|
|
$record = DataObject::get_by_id($record->class, $record->ID, false);
|
|
|
|
$this->assertFalse($record->HasOneFile()->exists());
|
|
|
|
$this->assertFileNotExists($file1->FullPath, 'File is also removed from 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');
|
|
|
|
|
|
|
|
$this->assertEquals(array('File2', 'File3'), $record->HasManyFiles()->column('Title'));
|
|
|
|
$response = $this->post(
|
|
|
|
'UploadFieldTest_Controller/Form/field/HasManyFiles/item/' . $file2->ID . '/delete',
|
|
|
|
array()
|
|
|
|
);
|
|
|
|
$this->assertFalse($response->isError());
|
|
|
|
$record = DataObject::get_by_id($record->class, $record->ID, false);
|
|
|
|
$this->assertEquals(array('File3'), $record->HasManyFiles()->column('Title'));
|
|
|
|
$this->assertFileNotExists($file2->FullPath, 'File is also removed from filesystem');
|
|
|
|
|
|
|
|
$fileNotOnRelationship = $this->objFromFixture('File', 'file1');
|
|
|
|
$response = $this->post(
|
|
|
|
'UploadFieldTest_Controller/Form/field/HasManyFiles/item/' . $fileNotOnRelationship->ID . '/delete',
|
|
|
|
array()
|
|
|
|
);
|
2012-09-26 23:34:00 +02:00
|
|
|
$this->assertEquals(403, $response->getStatusCode(),
|
|
|
|
"Denies deleting files if they're not on the current relationship");
|
2012-02-03 00:59:40 +01:00
|
|
|
}
|
|
|
|
|
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');
|
|
|
|
|
|
|
|
$this->assertContains('File4', $record->ManyManyFiles()->column('Title'));
|
|
|
|
$this->assertContains('File5', $record->ManyManyFiles()->column('Title'));
|
|
|
|
$response = $this->post(
|
|
|
|
'UploadFieldTest_Controller/Form/field/ManyManyFiles/item/' . $file4->ID . '/delete',
|
|
|
|
array()
|
|
|
|
);
|
|
|
|
$this->assertFalse($response->isError());
|
|
|
|
$record = DataObject::get_by_id($record->class, $record->ID, false);
|
|
|
|
$this->assertNotContains('File4', $record->ManyManyFiles()->column('Title'));
|
|
|
|
$this->assertContains('File5', $record->ManyManyFiles()->column('Title'));
|
|
|
|
$this->assertFileNotExists($file4->FullPath, 'File is also removed from filesystem');
|
|
|
|
|
|
|
|
// Test record-based permissions
|
|
|
|
$response = $this->post(
|
|
|
|
'UploadFieldTest_Controller/Form/field/ManyManyFiles/item/' . $fileNoDelete->ID . '/delete',
|
|
|
|
array()
|
|
|
|
);
|
|
|
|
$this->assertEquals(403, $response->getStatusCode());
|
|
|
|
}
|
|
|
|
|
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());
|
|
|
|
$items = $parser->getBySelector('#ManyManyFiles .ss-uploadfield-files .ss-uploadfield-item');
|
|
|
|
$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();
|
|
|
|
|
|
|
|
$field = new UploadField('MyField');
|
|
|
|
$field->setForm($form);
|
|
|
|
$this->assertNull($field->getRecord(), 'Returns no record by default');
|
|
|
|
|
|
|
|
$field = new UploadField('MyField');
|
|
|
|
$field->setForm($form);
|
|
|
|
$form->loadDataFrom($record);
|
|
|
|
$this->assertEquals($record, $field->getRecord(), 'Returns record from form if available');
|
|
|
|
|
|
|
|
$field = new UploadField('MyField');
|
|
|
|
$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');
|
|
|
|
$form = $this->getMockForm();
|
|
|
|
$items = new ArrayList(array(
|
|
|
|
$this->objFromFixture('File', 'file1'),
|
|
|
|
$this->objFromFixture('File', 'file2')
|
|
|
|
));
|
|
|
|
|
|
|
|
// Anonymous field
|
|
|
|
$field = new UploadField('MyField');
|
|
|
|
$field->setForm($form);
|
|
|
|
$field->setRecord($record);
|
|
|
|
$field->setItems($items);
|
|
|
|
$this->assertEquals(array('File1', 'File2'), $field->getItems()->column('Title'));
|
|
|
|
|
|
|
|
// Field with has_one auto-detected
|
|
|
|
$field = new UploadField('HasOneFile');
|
|
|
|
$field->setForm($form);
|
|
|
|
$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');
|
|
|
|
$form = $this->getMockForm();
|
|
|
|
|
|
|
|
// Anonymous field
|
|
|
|
$field = new UploadField('MyField');
|
|
|
|
$field->setForm($form);
|
|
|
|
$field->setRecord($record);
|
|
|
|
$this->assertEquals(array(), $field->getItems()->column('Title'));
|
|
|
|
|
|
|
|
// Field with has_one auto-detected
|
|
|
|
$field = new UploadField('HasOneFile');
|
|
|
|
$field->setForm($form);
|
|
|
|
$field->setRecord($record);
|
|
|
|
$this->assertEquals(array('File1'), $field->getItems()->column('Title'));
|
|
|
|
|
|
|
|
// Field with has_many auto-detected
|
|
|
|
$field = new UploadField('HasManyFiles');
|
|
|
|
$field->setForm($form);
|
|
|
|
$field->setRecord($record);
|
|
|
|
$this->assertEquals(array('File2', 'File3'), $field->getItems()->column('Title'));
|
|
|
|
|
|
|
|
// Field with many_many auto-detected
|
|
|
|
$field = new UploadField('ManyManyFiles');
|
|
|
|
$field->setForm($form);
|
|
|
|
$field->setRecord($record);
|
|
|
|
$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());
|
2012-09-26 23:34:00 +02:00
|
|
|
$this->assertFalse((bool)$parser->getBySelector(
|
|
|
|
'#ReadonlyField .ss-uploadfield-files .ss-uploadfield-item .ss-ui-button'),
|
|
|
|
'Removes all buttons on items');
|
|
|
|
$this->assertFalse((bool)$parser->getBySelector('#ReadonlyField .ss-uploadfield-dropzone'),
|
|
|
|
'Removes dropzone');
|
|
|
|
$this->assertFalse((bool)$parser->getBySelector(
|
|
|
|
'#ReadonlyField .ss-uploadfield-addfile .ss-ui-button'),
|
|
|
|
'Removes all buttons from "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(
|
|
|
|
(bool)$parser->getBySelector('#DisabledField .ss-uploadfield-files .ss-uploadfield-item .ss-ui-button'),
|
|
|
|
'Removes all buttons on items');
|
|
|
|
$this->assertFalse((bool)$parser->getBySelector('#DisabledField .ss-uploadfield-dropzone'),
|
|
|
|
'Removes dropzone');
|
|
|
|
$this->assertFalse(
|
|
|
|
(bool)$parser->getBySelector('#DisabledField .ss-uploadfield-addfile .ss-ui-button'),
|
|
|
|
'Removes all buttons from "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());
|
|
|
|
$this->assertFalse((bool)$parser->getBySelector('#CanUploadFalseField .ss-uploadfield-dropzone'),
|
|
|
|
'Removes dropzone');
|
|
|
|
$this->assertTrue(
|
|
|
|
(bool)$parser->getBySelector('#CanUploadFalseField .ss-uploadfield-fromfiles'),
|
|
|
|
'Keeps "From files" button'
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testCanUploadWithPermissionCode() {
|
|
|
|
$field = new UploadField('MyField');
|
|
|
|
|
|
|
|
$field->setConfig('canUpload', true);
|
|
|
|
$this->assertTrue($field->canUpload());
|
|
|
|
|
|
|
|
$field->setConfig('canUpload', false);
|
|
|
|
$this->assertFalse($field->canUpload());
|
|
|
|
|
|
|
|
$this->loginWithPermission('ADMIN');
|
|
|
|
|
|
|
|
$field->setConfig('canUpload', false);
|
|
|
|
$this->assertFalse($field->canUpload());
|
|
|
|
|
|
|
|
$field->setConfig('canUpload', 'ADMIN');
|
|
|
|
$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(
|
|
|
|
(bool)$parser->getBySelector('#CanAttachExistingFalseField .ss-uploadfield-fromcomputer-fileinput'),
|
|
|
|
'Keeps input file control'
|
|
|
|
);
|
|
|
|
$this->assertFalse(
|
|
|
|
(bool)$parser->getBySelector('#CanAttachExistingFalseField .ss-uploadfield-fromfiles'),
|
|
|
|
'Removes "From files" button'
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2012-09-19 12:07:39 +02:00
|
|
|
public function testIsSaveable() {
|
2012-06-15 17:44:34 +02:00
|
|
|
$form = $this->getMockForm();
|
|
|
|
|
|
|
|
$field = new UploadField('MyField');
|
|
|
|
$this->assertTrue($field->isSaveable(), 'Field without relation is always marked as saveable');
|
|
|
|
|
|
|
|
$field = new UploadField('HasOneFile');
|
|
|
|
$this->assertTrue($field->isSaveable(), 'Field with has_one relation is saveable without record on form');
|
|
|
|
|
|
|
|
$field = new UploadField('HasOneFile');
|
|
|
|
$newRecord = new UploadFieldTest_Record();
|
|
|
|
$form->loadDataFrom($newRecord);
|
|
|
|
$field->setForm($form);
|
|
|
|
$this->assertFalse($field->isSaveable(), 'Field with has_one relation not saveable with new record on form');
|
|
|
|
|
|
|
|
$field = new UploadField('HasOneFile');
|
|
|
|
$existingRecord = $this->objFromFixture('UploadFieldTest_Record', 'record1');
|
|
|
|
$form->loadDataFrom($existingRecord);
|
|
|
|
$field->setForm($form);
|
|
|
|
$this->assertTrue($field->isSaveable(), 'Field with has_one relation saveable with saved record on form');
|
|
|
|
}
|
|
|
|
|
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-09-19 12:07:39 +02:00
|
|
|
public function testAttachHasOne() {
|
2012-02-08 00:58:58 +01:00
|
|
|
$this->loginWithPermission('ADMIN');
|
|
|
|
|
|
|
|
$record = $this->objFromFixture('UploadFieldTest_Record', 'record1');
|
|
|
|
$file1 = $this->objFromFixture('File', 'file1');
|
|
|
|
$file2 = $this->objFromFixture('File', 'file2');
|
|
|
|
$file3AlreadyAttached = $this->objFromFixture('File', 'file3');
|
|
|
|
|
|
|
|
$response = $this->post(
|
|
|
|
'UploadFieldTest_Controller/Form/field/HasOneFile/attach',
|
|
|
|
array('ids' => array($file1->ID/* first file should be ignored */, $file2->ID))
|
|
|
|
);
|
|
|
|
$this->assertFalse($response->isError());
|
|
|
|
|
|
|
|
$record = DataObject::get_by_id($record->class, $record->ID, false);
|
|
|
|
$this->assertEquals($file2->ID, $record->HasOneFileID, 'Attaches new relations');
|
|
|
|
}
|
|
|
|
|
2012-09-19 12:07:39 +02:00
|
|
|
public function testAttachHasMany() {
|
2012-02-08 00:58:58 +01:00
|
|
|
$this->loginWithPermission('ADMIN');
|
|
|
|
|
|
|
|
$record = $this->objFromFixture('UploadFieldTest_Record', 'record1');
|
|
|
|
$file1 = $this->objFromFixture('File', 'file1');
|
|
|
|
$file2 = $this->objFromFixture('File', 'file2');
|
|
|
|
$file3AlreadyAttached = $this->objFromFixture('File', 'file3');
|
|
|
|
|
|
|
|
$response = $this->post(
|
|
|
|
'UploadFieldTest_Controller/Form/field/HasManyFiles/attach',
|
|
|
|
array('ids' => array($file1->ID, $file2->ID))
|
|
|
|
);
|
|
|
|
$this->assertFalse($response->isError());
|
|
|
|
|
|
|
|
$record = DataObject::get_by_id($record->class, $record->ID, false);
|
2012-09-26 23:34:00 +02:00
|
|
|
$this->assertContains($file1->ID, $record->HasManyFiles()->column('ID'),
|
|
|
|
'Attaches new relations');
|
|
|
|
$this->assertContains($file2->ID, $record->HasManyFiles()->column('ID'),
|
|
|
|
'Attaches new relations');
|
|
|
|
$this->assertContains($file3AlreadyAttached->ID, $record->HasManyFiles()->column('ID'),
|
|
|
|
'Does not detach existing relations');
|
2012-02-08 00:58:58 +01:00
|
|
|
}
|
|
|
|
|
2012-09-19 12:07:39 +02:00
|
|
|
public function testAttachManyMany() {
|
2012-02-08 00:58:58 +01:00
|
|
|
$this->loginWithPermission('ADMIN');
|
|
|
|
|
|
|
|
$record = $this->objFromFixture('UploadFieldTest_Record', 'record1');
|
|
|
|
$file1 = $this->objFromFixture('File', 'file1');
|
|
|
|
$file2 = $this->objFromFixture('File', 'file2');
|
|
|
|
$file5AlreadyAttached = $this->objFromFixture('File', 'file5');
|
|
|
|
|
|
|
|
$response = $this->post(
|
|
|
|
'UploadFieldTest_Controller/Form/field/ManyManyFiles/attach',
|
|
|
|
array('ids' => array($file1->ID, $file2->ID))
|
|
|
|
);
|
|
|
|
$this->assertFalse($response->isError());
|
|
|
|
|
|
|
|
$record = DataObject::get_by_id($record->class, $record->ID, false);
|
2012-09-26 23:34:00 +02:00
|
|
|
$this->assertContains($file1->ID, $record->ManyManyFiles()->column('ID'),
|
|
|
|
'Attaches new relations');
|
|
|
|
$this->assertContains($file2->ID, $record->ManyManyFiles()->column('ID'),
|
|
|
|
'Attaches new relations');
|
|
|
|
$this->assertContains($file5AlreadyAttached->ID, $record->ManyManyFiles()->column('ID'),
|
|
|
|
'Does not detach existing relations');
|
2012-02-08 00:58:58 +01:00
|
|
|
}
|
|
|
|
|
2012-09-19 12:07:39 +02:00
|
|
|
public function testManagesRelation() {
|
2012-02-08 11:27:10 +01:00
|
|
|
$record = $this->objFromFixture('UploadFieldTest_Record', 'record1');
|
|
|
|
|
|
|
|
$field = new UploadField('ManyManyFiles');
|
|
|
|
$this->assertFalse($field->managesRelation(), 'False if no record is set');
|
|
|
|
|
|
|
|
$field = new UploadField('NoRelationField');
|
|
|
|
$field->setRecord($record);
|
|
|
|
$this->assertFalse($field->managesRelation(), 'False if no relation found by name');
|
|
|
|
|
|
|
|
$field = new UploadField('HasOneFile');
|
|
|
|
$field->setRecord($record);
|
|
|
|
$this->assertTrue($field->managesRelation(), 'True for has_one');
|
|
|
|
|
|
|
|
$field = new UploadField('HasManyFiles');
|
|
|
|
$field->setRecord($record);
|
|
|
|
$this->assertTrue($field->managesRelation(), 'True for has_many');
|
|
|
|
|
|
|
|
$field = new UploadField('ManyManyFiles');
|
|
|
|
$field->setRecord($record);
|
|
|
|
$this->assertTrue($field->managesRelation(), 'True for many_many');
|
|
|
|
}
|
|
|
|
|
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(
|
|
|
|
'name' => $tmpFileName,
|
|
|
|
'type' => 'text/plaintext',
|
|
|
|
'size' => filesize($tmpFilePath),
|
|
|
|
'tmp_name' => $tmpFilePath,
|
|
|
|
'extension' => 'txt',
|
|
|
|
'error' => UPLOAD_ERR_OK,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
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 {
|
|
|
|
|
2012-04-18 22:30:12 +02:00
|
|
|
static $db = array(
|
|
|
|
'Title' => 'Text',
|
|
|
|
);
|
2012-02-03 00:59:40 +01:00
|
|
|
|
2012-04-18 22:30:12 +02:00
|
|
|
static $has_one = array(
|
|
|
|
'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
|
|
|
|
2012-04-18 22:30:12 +02:00
|
|
|
static $has_many = array(
|
|
|
|
'HasManyFiles' => 'File',
|
|
|
|
'HasManyFilesMaxTwo' => 'File',
|
|
|
|
);
|
2012-02-03 00:59:40 +01:00
|
|
|
|
2012-04-18 22:30:12 +02:00
|
|
|
static $many_many = array(
|
|
|
|
'ManyManyFiles' => 'File',
|
|
|
|
);
|
2012-02-03 00:59:40 +01:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
class UploadFieldTest_FileExtension extends DataExtension implements TestOnly {
|
|
|
|
|
2012-04-18 22:30:12 +02:00
|
|
|
public static $has_one = array(
|
|
|
|
'Record' => 'UploadFieldTest_Record'
|
|
|
|
);
|
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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
class UploadFieldTest_Controller extends Controller implements TestOnly {
|
|
|
|
|
|
|
|
protected $template = 'BlankPage';
|
|
|
|
|
2012-09-19 12:07:39 +02:00
|
|
|
public function Form() {
|
2012-02-03 00:59:40 +01:00
|
|
|
$record = DataObject::get_one('UploadFieldTest_Record', '"Title" = \'Record 1\'');
|
|
|
|
|
|
|
|
$fieldNoRelation = new UploadField('NoRelationField');
|
|
|
|
$fieldNoRelation->setFolderName('UploadFieldTest');
|
|
|
|
$fieldNoRelation->setRecord($record);
|
|
|
|
|
|
|
|
$fieldHasOne = new UploadField('HasOneFile');
|
|
|
|
$fieldHasOne->setFolderName('UploadFieldTest');
|
|
|
|
$fieldHasOne->setRecord($record);
|
2012-04-18 05:22:40 +02:00
|
|
|
|
|
|
|
$fieldHasOneExtendedFile = new UploadField('HasOneExtendedFile');
|
|
|
|
$fieldHasOneExtendedFile->setFolderName('UploadFieldTest');
|
|
|
|
$fieldHasOneExtendedFile->setRecord($record);
|
2012-02-03 00:59:40 +01:00
|
|
|
|
2012-04-17 07:12:56 +02:00
|
|
|
$fieldHasOneMaxOne = new UploadField('HasOneFileMaxOne');
|
|
|
|
$fieldHasOneMaxOne->setFolderName('UploadFieldTest');
|
|
|
|
$fieldHasOneMaxOne->setConfig('allowedMaxFileNumber', 1);
|
|
|
|
$fieldHasOneMaxOne->setRecord($record);
|
|
|
|
|
|
|
|
$fieldHasOneMaxTwo = new UploadField('HasOneFileMaxTwo');
|
|
|
|
$fieldHasOneMaxTwo->setFolderName('UploadFieldTest');
|
|
|
|
$fieldHasOneMaxTwo->setConfig('allowedMaxFileNumber', 2);
|
|
|
|
$fieldHasOneMaxTwo->setRecord($record);
|
|
|
|
|
2012-02-03 00:59:40 +01:00
|
|
|
$fieldHasMany = new UploadField('HasManyFiles');
|
|
|
|
$fieldHasMany->setFolderName('UploadFieldTest');
|
|
|
|
$fieldHasMany->setRecord($record);
|
|
|
|
|
2012-04-17 07:12:56 +02:00
|
|
|
$fieldHasManyMaxTwo = new UploadField('HasManyFilesMaxTwo');
|
|
|
|
$fieldHasManyMaxTwo->setFolderName('UploadFieldTest');
|
|
|
|
$fieldHasManyMaxTwo->setConfig('allowedMaxFileNumber', 2);
|
|
|
|
$fieldHasManyMaxTwo->setRecord($record);
|
|
|
|
|
2012-02-03 00:59:40 +01:00
|
|
|
$fieldManyMany = new UploadField('ManyManyFiles');
|
|
|
|
$fieldManyMany->setFolderName('UploadFieldTest');
|
|
|
|
$fieldManyMany->setRecord($record);
|
|
|
|
|
|
|
|
$fieldReadonly = new UploadField('ReadonlyField');
|
|
|
|
$fieldReadonly->setFolderName('UploadFieldTest');
|
|
|
|
$fieldReadonly->setRecord($record);
|
|
|
|
$fieldReadonly = $fieldReadonly->performReadonlyTransformation();
|
|
|
|
|
|
|
|
$fieldDisabled = new UploadField('DisabledField');
|
|
|
|
$fieldDisabled->setFolderName('UploadFieldTest');
|
|
|
|
$fieldDisabled->setRecord($record);
|
|
|
|
$fieldDisabled = $fieldDisabled->performDisabledTransformation();
|
|
|
|
|
2012-02-08 00:58:58 +01:00
|
|
|
$fieldSubfolder = new UploadField('SubfolderField');
|
|
|
|
$fieldSubfolder->setFolderName('UploadFieldTest/subfolder1');
|
|
|
|
$fieldSubfolder->setRecord($record);
|
|
|
|
|
2013-01-09 11:34:02 +01:00
|
|
|
$fieldCanUploadFalse = new UploadField('CanUploadFalseField');
|
|
|
|
$fieldCanUploadFalse->setConfig('canUpload', false);
|
|
|
|
$fieldCanUploadFalse->setRecord($record);
|
|
|
|
|
2013-01-11 05:33:06 +01:00
|
|
|
$fieldCanAttachExisting = new UploadField('CanAttachExistingFalseField');
|
|
|
|
$fieldCanAttachExisting->setConfig('canAttachExisting', false);
|
|
|
|
$fieldCanAttachExisting->setRecord($record);
|
|
|
|
|
2013-07-12 13:16:25 +02:00
|
|
|
$fieldAllowedExtensions = new UploadField('AllowedExtensionsField');
|
|
|
|
$fieldAllowedExtensions->getValidator()->setAllowedExtensions(array('jpg'));
|
|
|
|
|
2012-02-03 00:59:40 +01:00
|
|
|
$form = new Form(
|
|
|
|
$this,
|
|
|
|
'Form',
|
|
|
|
new FieldList(
|
|
|
|
$fieldNoRelation,
|
|
|
|
$fieldHasOne,
|
2012-04-17 07:12:56 +02:00
|
|
|
$fieldHasOneMaxOne,
|
|
|
|
$fieldHasOneMaxTwo,
|
2012-04-18 05:22:40 +02:00
|
|
|
$fieldHasOneExtendedFile,
|
2012-02-03 00:59:40 +01:00
|
|
|
$fieldHasMany,
|
2012-04-17 07:12:56 +02:00
|
|
|
$fieldHasManyMaxTwo,
|
2012-02-03 00:59:40 +01:00
|
|
|
$fieldManyMany,
|
|
|
|
$fieldReadonly,
|
2012-02-08 00:58:58 +01:00
|
|
|
$fieldDisabled,
|
2013-01-09 11:34:02 +01:00
|
|
|
$fieldSubfolder,
|
2013-01-11 05:33:06 +01:00
|
|
|
$fieldCanUploadFalse,
|
2013-07-12 13:16:25 +02:00
|
|
|
$fieldCanAttachExisting,
|
|
|
|
$fieldAllowedExtensions
|
2012-02-03 00:59:40 +01:00
|
|
|
),
|
|
|
|
new FieldList(
|
|
|
|
new FormAction('submit')
|
|
|
|
),
|
|
|
|
new RequiredFields(
|
|
|
|
'NoRelationField',
|
|
|
|
'HasOneFile',
|
2012-04-17 07:12:56 +02:00
|
|
|
'HasOneFileMaxOne',
|
|
|
|
'HasOneFileMaxTwo',
|
2012-04-18 05:22:40 +02:00
|
|
|
'HasOneExtendedFile',
|
2012-02-03 00:59:40 +01:00
|
|
|
'HasManyFiles',
|
2012-04-17 07:12:56 +02:00
|
|
|
'HasManyFilesMaxTwo',
|
2012-02-03 00:59:40 +01:00
|
|
|
'ManyManyFiles',
|
|
|
|
'ReadonlyField',
|
2012-02-08 00:58:58 +01:00
|
|
|
'DisabledField',
|
2013-01-09 11:34:02 +01:00
|
|
|
'SubfolderField',
|
2013-01-11 05:33:06 +01:00
|
|
|
'CanUploadFalseField',
|
|
|
|
'CanAttachExistingField'
|
2012-02-03 00:59:40 +01:00
|
|
|
)
|
|
|
|
);
|
|
|
|
return $form;
|
|
|
|
}
|
|
|
|
|
2012-09-19 12:07:39 +02:00
|
|
|
public function submit($data, $form) {
|
2012-02-03 00:59:40 +01:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2012-03-27 06:04:11 +02:00
|
|
|
}
|
2012-04-18 05:22:40 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Used for testing the create-on-upload
|
|
|
|
*/
|
|
|
|
class UploadFieldTest_ExtendedFile extends File implements TestOnly {
|
|
|
|
|
|
|
|
}
|