Merge pull request #338 from robert-h-curry/7059-uploadfieldtest-incomplete-test-rc

ENHANCEMENT: Fixes #7059. Add test for allowedMaxFileNumber to UploadFieldTest.
This commit is contained in:
Sean Harvey 2012-04-17 19:29:35 -07:00
commit 1d058acdef
2 changed files with 99 additions and 10 deletions

View File

@ -4,14 +4,14 @@
* Field for uploading single or multiple files of all types, including images.
* <b>NOTE: this Field will call write() on the supplied record</b>
*
* <b>Features (some might not be avaliable to old browsers):</b>
* <b>Features (some might not be available to old browsers):</b>
*
* - File Drag&Drop support
* - Progressbar
* - Image thumbnail/file icons even before upload finished
* - Saving into relations
* - Edit file
* - allowedExtensions is by default File::$allowed_extensions<li>maxFileSize the vaule of min(upload_max_filesize, post_max_size) from php.ini
* - allowedExtensions is by default File::$allowed_extensions<li>maxFileSize the value of min(upload_max_filesize, post_max_size) from php.ini
*
* @example <code>
* $UploadField = new UploadField('myFiles', 'Please upload some images <span>(max. 5 files)</span>');
@ -74,7 +74,7 @@ class UploadField extends FileField {
*/
'autoUpload' => true,
/**
* php validation of allowedMaxFileNumber only works when a db relation is avaliable, set to null to allow unlimited
* php validation of allowedMaxFileNumber only works when a db relation is available, set to null to allow unlimited
* if record has a has_one and allowedMaxFileNumber is null, it will be set to 1
* @var int
*/
@ -122,7 +122,7 @@ class UploadField extends FileField {
/**
* @param string $name The internal field name, passed to forms.
* @param string $title The field label.
* @param SS_List $items If no items are defined, the field will try to auto-detect an existion relation on {@link $record},
* @param SS_List $items If no items are defined, the field will try to auto-detect an existing relation on {@link $record},
* with the same name as the field name.
* @param Form $form Reference to the container form
*/
@ -175,7 +175,7 @@ class UploadField extends FileField {
/**
* Force a record to be used as "Parent" for uploaded Files (eg a Page with a has_one to File)
* @param DataOjbect $record
* @param DataObject $record
*/
public function setRecord($record) {
$this->record = $record;
@ -310,7 +310,7 @@ class UploadField extends FileField {
$name = $this->getName();
// if there is a has_one relation with that name on the record and
// allowedMaxFileNumber has not been set, its wanted to be 1
// allowedMaxFileNumber has not been set, it's wanted to be 1
if(
$record && $record->exists()
&& $record->has_one($name) && !$this->getConfig('allowedMaxFileNumber')
@ -633,7 +633,7 @@ class UploadField_ItemHandler extends RequestHandler {
}
/**
* Action to handle removeing a single file from the db relation
* Action to handle removing a single file from the db relation
*
* @param SS_HTTPRequest $request
* @return SS_HTTPResponse

View File

@ -98,8 +98,73 @@
$this->assertEquals($record->ManyManyFiles()->Last()->Name, $tmpFileName);
}
function testAllowedMaxFileNumber() {
$this->markTestIncomplete();
function testAllowedMaxFileNumberWithHasOne() {
$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).
// Each of them should function in the same way - attaching the first file should work, the
// 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);
}
}
function testAllowedMaxFileNumberWithHasMany() {
$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);
}
function testRemoveFromHasOne() {
@ -553,10 +618,13 @@ static $db = array(
static $has_one = array(
'HasOneFile' => 'File',
'HasOneFileMaxOne' => 'File',
'HasOneFileMaxTwo' => 'File',
);
static $has_many = array(
'HasManyFiles' => 'File',
'HasManyFilesMaxTwo' => 'File',
);
static $many_many = array(
@ -570,7 +638,7 @@ class UploadFieldTest_FileExtension extends DataExtension implements TestOnly {
function extraStatics($class = null, $extension = null) {
return array(
'has_one' => array('Record' => 'UploadFieldTest_Record')
);
);
}
function canDelete($member = null) {
@ -601,10 +669,25 @@ class UploadFieldTest_Controller extends Controller implements TestOnly {
$fieldHasOne->setFolderName('UploadFieldTest');
$fieldHasOne->setRecord($record);
$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);
$fieldHasMany = new UploadField('HasManyFiles');
$fieldHasMany->setFolderName('UploadFieldTest');
$fieldHasMany->setRecord($record);
$fieldHasManyMaxTwo = new UploadField('HasManyFilesMaxTwo');
$fieldHasManyMaxTwo->setFolderName('UploadFieldTest');
$fieldHasManyMaxTwo->setConfig('allowedMaxFileNumber', 2);
$fieldHasManyMaxTwo->setRecord($record);
$fieldManyMany = new UploadField('ManyManyFiles');
$fieldManyMany->setFolderName('UploadFieldTest');
$fieldManyMany->setRecord($record);
@ -629,7 +712,10 @@ class UploadFieldTest_Controller extends Controller implements TestOnly {
new FieldList(
$fieldNoRelation,
$fieldHasOne,
$fieldHasOneMaxOne,
$fieldHasOneMaxTwo,
$fieldHasMany,
$fieldHasManyMaxTwo,
$fieldManyMany,
$fieldReadonly,
$fieldDisabled,
@ -641,7 +727,10 @@ class UploadFieldTest_Controller extends Controller implements TestOnly {
new RequiredFields(
'NoRelationField',
'HasOneFile',
'HasOneFileMaxOne',
'HasOneFileMaxTwo',
'HasManyFiles',
'HasManyFilesMaxTwo',
'ManyManyFiles',
'ReadonlyField',
'DisabledField',