Skip to content

Commit

Permalink
Merge pull request #338 from robert-h-curry/7059-uploadfieldtest-inco…
Browse files Browse the repository at this point in the history
…mplete-test-rc

ENHANCEMENT: Fixes #7059. Add test for allowedMaxFileNumber to UploadFieldTest.
  • Loading branch information
Sean Harvey committed Apr 18, 2012
2 parents 7d84aff + 5603fbe commit 1d058ac
Show file tree
Hide file tree
Showing 2 changed files with 99 additions and 10 deletions.
14 changes: 7 additions & 7 deletions forms/UploadField.php
Expand Up @@ -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>');
Expand Down Expand Up @@ -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
*/
Expand Down Expand Up @@ -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
*/
Expand Down Expand Up @@ -175,7 +175,7 @@ public function getTemplateFileEdit() {

/**
* 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;
Expand Down Expand Up @@ -310,7 +310,7 @@ public function Field($properties = array()) {
$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')
Expand Down Expand Up @@ -633,7 +633,7 @@ public function EditLink() {
}

/**
* 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
Expand Down
95 changes: 92 additions & 3 deletions tests/forms/uploadfield/UploadFieldTest.php
Expand Up @@ -98,8 +98,73 @@ function testUploadManyManyRelation() {
$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() {
Expand Down Expand Up @@ -553,10 +618,13 @@ class UploadFieldTest_Record extends DataObject implements TestOnly {

static $has_one = array(
'HasOneFile' => 'File',
'HasOneFileMaxOne' => 'File',
'HasOneFileMaxTwo' => 'File',
);

static $has_many = array(
'HasManyFiles' => 'File',
'HasManyFilesMaxTwo' => 'File',
);

static $many_many = array(
Expand All @@ -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) {
Expand Down Expand Up @@ -601,10 +669,25 @@ function Form() {
$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);
Expand All @@ -629,7 +712,10 @@ function Form() {
new FieldList(
$fieldNoRelation,
$fieldHasOne,
$fieldHasOneMaxOne,
$fieldHasOneMaxTwo,
$fieldHasMany,
$fieldHasManyMaxTwo,
$fieldManyMany,
$fieldReadonly,
$fieldDisabled,
Expand All @@ -641,7 +727,10 @@ function Form() {
new RequiredFields(
'NoRelationField',
'HasOneFile',
'HasOneFileMaxOne',
'HasOneFileMaxTwo',
'HasManyFiles',
'HasManyFilesMaxTwo',
'ManyManyFiles',
'ReadonlyField',
'DisabledField',
Expand Down

0 comments on commit 1d058ac

Please sign in to comment.