MINOR Moving File->OwnerID setting to model layer

This commit is contained in:
Ingo Schommer 2012-02-07 18:32:04 +01:00
parent 7718c75e3a
commit 1b4dda491a
4 changed files with 25 additions and 2 deletions

View File

@ -351,6 +351,9 @@ class File extends DataObject {
protected function onBeforeWrite() {
parent::onBeforeWrite();
// Set default owner
if(!$this->ID) $this->OwnerID = (Member::currentUser() ? Member::currentUser()->ID : 0);
// Set default name
if(!$this->getField('Name')) $this->Name = "new-" . strtolower($this->class);
}

View File

@ -200,7 +200,6 @@ class FileIFrameField extends FileField {
$this->form->getRecord()->{$this->getName() . 'ID'} = $fileObject->ID;
$fileObject->OwnerID = (Member::currentUser() ? Member::currentUser()->ID : 0);
$fileObject->write();
}

View File

@ -447,7 +447,6 @@ class UploadField extends FileField {
$return['error'] = implode(' '.PHP_EOL, $this->upload->getErrors());
} else {
$file = $this->upload->getFile();
$file->OwnerID = (Member::currentUser() ? Member::currentUser()->ID : 0);
$file->write();
$this->attachFile($file);
$file = $this->customiseFile($file);

View File

@ -295,6 +295,28 @@ class FileTest extends SapphireTest {
File::$class_for_file_extension = $orig;
}
function testSetsOwnerOnFirstWrite() {
Session::set('loggedInAs', null);
$member1 = new Member();
$member1->write();
$member2 = new Member();
$member2->write();
$file1 = new File();
$file1->write();
$this->assertEquals(0, $file1->OwnerID, 'Owner not written when no user is logged in');
$member1->logIn();
$file2 = new File();
$file2->write();
$this->assertEquals($member1->ID, $file2->OwnerID, 'Owner written when user is logged in');
$member2->logIn();
$file2->forceChange();
$file2->write();
$this->assertEquals($member1->ID, $file2->OwnerID, 'Owner not overwritten on existing files');
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////