Skip to content
This repository has been archived by the owner on Mar 5, 2022. It is now read-only.

Commit

Permalink
Fixing an issue when saving again without adapter and model
Browse files Browse the repository at this point in the history
The model and adapter was overwritten when the entity was saved a second time and the values were not provided.

Fixed also the doc block indentation of a few more tests.
  • Loading branch information
Florian Krämer committed Dec 30, 2015
1 parent b00ac8d commit 3c8014d
Show file tree
Hide file tree
Showing 8 changed files with 184 additions and 149 deletions.
5 changes: 5 additions & 0 deletions src/Model/Entity/FileStorage.php
Expand Up @@ -45,6 +45,7 @@ public function __construct(array $properties = [], array $options = []) {
'pathBuilder' => $this->_pathBuilderClass,
'pathBuilderOptions' => $this->_pathBuilderOptions
];

parent::__construct($properties, $options);

if (!empty($options['pathBuilder'])) {
Expand All @@ -61,6 +62,10 @@ public function __construct(array $properties = [], array $options = []) {
* @var array
*/
protected $_accessible = [
'filename' => true,
'model' => true,
'foreign_key' => true,
'file' => true,
'*' => true,
];

Expand Down
23 changes: 16 additions & 7 deletions src/Model/Table/FileStorageTable.php
Expand Up @@ -41,6 +41,13 @@ class FileStorageTable extends Table {
*/
public $record = [];

/**
* Default Storage Adapter to use when unspecified.
*
* @var string
*/
protected $_defaultAdapter = 'Local';

/**
* Initialize
*
Expand Down Expand Up @@ -86,9 +93,17 @@ public function beforeMarshal(Event $event, ArrayAccess $data) {
* @return boolean true on success
*/
public function beforeSave(Event $event, EntityInterface $entity, $options) {
if ($entity->isNew()) {
if (empty($entity->model)) {
$entity->model = $this->table();
}
if (empty($entity->adapter)) {
$entity->adapter = $this->_defaultAdapter;
}
}
$Event = $this->dispatchEvent('FileStorage.beforeSave', array(
'record' => $entity,
'storage' => $this->storageAdapter($entity['adapter'])
'storage' => $this->storageAdapter($entity->adapter)
));
if ($Event->isStopped()) {
return false;
Expand Down Expand Up @@ -118,12 +133,6 @@ public function getFileInfoFromUpload(&$upload, $field = 'file') {
$upload['extension'] = pathinfo($upload[$field]['name'], PATHINFO_EXTENSION);
$upload['filename'] = $upload[$field]['name'];
}
if (empty($upload['model'])) {
$upload['model'] = $this->table();
}
if (empty($upload['adapter'])) {
$upload['adapter'] = 'Local';
}
}

/**
Expand Down
4 changes: 2 additions & 2 deletions tests/TestCase/Model/Entity/ImageStorageEntityTest.php
Expand Up @@ -10,8 +10,8 @@
/**
* File Storage Entity Test
*
* @author Florian Krämer
* @copyright 2012 - 2015 Florian Krämer
* @author Florian Krämer
* @copyright 2012 - 2015 Florian Krämer
* @license MIT
*/
class ImageStorageEntityTest extends FileStorageTestCase {
Expand Down
98 changes: 59 additions & 39 deletions tests/TestCase/Model/Table/FileStorageTableTest.php
Expand Up @@ -16,55 +16,55 @@
*/
class FileStorageTableTest extends FileStorageTestCase {

/**
* Fixtures
*
* @var array
*/
/**
* Fixtures
*
* @var array
*/
public $fixtures = array(
'plugin.Burzum\FileStorage.FileStorage'
);

/**
* startTest
*
* @return void
*/
/**
* startTest
*
* @return void
*/
public function setUp() {
parent::setUp();
$this->FileStorage = TableRegistry::get('Burzum/FileStorage.FileStorage');
}

/**
* endTest
*
* @return void
*/
/**
* endTest
*
* @return void
*/
public function tearDown() {
parent::tearDown();
unset($this->FileStorage);
unset($this->FileStorageBehavior);
TableRegistry::clear();
}

/**
* testBeforeDelete
*
* @return void
*/
/**
* testBeforeDelete
*
* @return void
*/
public function testBeforeDelete() {
$entity = $this->FileStorage->get('file-storage-1');
$event = new Event('Model.beforeDelete', $this->FileStorage);
$this->FileStorage->beforeDelete($event, $entity);
$this->assertEquals($this->FileStorage->record, $entity);
}

/**
* testAfterDelete
*
* @todo Create a mock of FileStorage::getStorageAdapter() and test it
* @return void
*/
/**
* testAfterDelete
*
* @todo Create a mock of FileStorage::getStorageAdapter() and test it
* @return void
*/
public function testAfterDelete() {
$entity = $this->FileStorage->get('file-storage-1');
$entity->adapter = 'Local';
Expand All @@ -86,11 +86,11 @@ public function testAfterDelete() {
$this->assertFalse($result);
}

/**
* testGetFileInfoFromUpload
*
* @return void
*/
/**
* testGetFileInfoFromUpload
*
* @return void
*/
public function testGetFileInfoFromUpload() {
$filename = Plugin::path('Burzum/FileStorage') . DS . 'tests' . DS . 'Fixture' . DS . 'File' . DS . 'titus.jpg';

Expand All @@ -104,18 +104,16 @@ public function testGetFileInfoFromUpload() {
$this->FileStorage->getFileInfoFromUpload($data);

$this->assertEquals(332643, $data['filesize']);
$this->assertEquals('Local', $data['adapter']);
$this->assertEquals('image/jpeg', $data['mime_type']);
$this->assertEquals('jpg', $data['extension']);
$this->assertEquals('file_storage', $data['model']);
}

/**
* Testing a complete save call
*
* @link https://github.com/burzum/cakephp-file-storage/issues/85
* @return void
*/
/**
* Testing a complete save call
*
* @link https://github.com/burzum/cakephp-file-storage/issues/85
* @return void
*/
public function testFileSaving() {
$listenersToTest = [
'LocalListener',
Expand Down Expand Up @@ -146,4 +144,26 @@ public function testFileSaving() {
$results[] = $entity;
}
}

/**
* testBeforeSave
*
* @return void
*/
public function testBeforeSave() {
$entity = $this->FileStorage->newEntity([
'file' => [
'error' => UPLOAD_ERR_OK,
'tmp_name' => $this->fileFixtures . 'titus.jpg'
]
]);
$event = new Event('Model.beforeSave', $this->FileStorage, [
'entity' => $entity,
]);
$this->FileStorage->beforeSave($event, $entity, []);
$this->assertEquals($entity->adapter, 'Local');
$this->assertEquals($entity->filesize, 332643);
$this->assertEquals($entity->mime_type, 'image/jpeg');
$this->assertEquals($entity->model, 'file_storage');
}
}
80 changes: 40 additions & 40 deletions tests/TestCase/Model/Table/ImageStorageTableTest.php
Expand Up @@ -21,41 +21,41 @@
*/
class ImageStorageTableTest extends FileStorageTestCase {

/**
* Fixtures
*
* @var array
*/
/**
* Fixtures
*
* @var array
*/
public $fixtures = array(
'plugin.Burzum\FileStorage.FileStorage'
);

/**
* setUp
*
* @return void
*/
/**
* setUp
*
* @return void
*/
public function setUp() {
parent::setUp();
$this->Image = TableRegistry::get('Burzum/FileStorage.ImageStorage');
}

/**
* tearDown
*
* @return void
/**
* tearDown
*
* @return void
*/
public function tearDown() {
parent::tearDown();
unset($this->Image);
TableRegistry::clear();
}

/**
* testProcessVersion
*
* @return void
*/
/**
* testProcessVersion
*
* @return void
*/
public function testProcessVersion() {
$entity = $this->Image->newEntity([
'foreign_key' => 'test-1',
Expand Down Expand Up @@ -135,11 +135,11 @@ public function testProcessVersion() {
$this->assertEquals(count($folderResult[1]), 3);
}

/**
* testGetImageVersions
*
* @return void
*/
/**
* testGetImageVersions
*
* @return void
*/
public function testGetImageVersions() {
Configure::write('FileStorage.imageSizes', [
'Item' => [
Expand Down Expand Up @@ -169,22 +169,22 @@ public function testGetImageVersions() {
$this->assertEquals($result, $expected);
}

/**
* testValidateImageSize
*
* @expectedException \InvalidArgumentException
* @return void
*/
/**
* testValidateImageSize
*
* @expectedException \InvalidArgumentException
* @return void
*/
public function testValidateImageSizeInvalidArgumentException() {
$file = $this->fileFixtures . 'titus.jpg';
$this->Image->validateImageSize($file);
}

/**
* testValidateImageSize
*
* @return void
*/
/**
* testValidateImageSize
*
* @return void
*/
public function testValidateImageSize() {
$file = $this->fileFixtures . 'titus.jpg';
$result = $this->Image->validateImageSize($file, ['height' => ['>', 100]]);
Expand All @@ -199,11 +199,11 @@ public function testValidateImageSize() {
$this->assertFalse($result);
}

/**
* testDeleteOldFileOnSave
*
* @return void
*/
/**
* testDeleteOldFileOnSave
*
* @return void
*/
public function testDeleteOldFileOnSave() {
$entity = $this->Image->newEntity([
'foreign_key' => 'test-1',
Expand Down
20 changes: 10 additions & 10 deletions tests/TestCase/Storage/Listener/AbstractListenerTest.php
Expand Up @@ -15,20 +15,20 @@ public function implementedEvents() {

class AbstractListenerTest extends TestCase {

/**
* Fixtures
*
* @var array
*/
/**
* Fixtures
*
* @var array
*/
public $fixtures = array(
'plugin.Burzum\FileStorage.FileStorage'
);

/**
* testPathBuilder
*
* @return void
*/
/**
* testPathBuilder
*
* @return void
*/
public function testPathBuilder() {
$Listener = new TestAbstractListener([
'pathBuilder' => 'Base'
Expand Down

0 comments on commit 3c8014d

Please sign in to comment.