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

Commit

Permalink
Preparing tests for a BC breaking change in the near future.
Browse files Browse the repository at this point in the history
  • Loading branch information
Florian Krämer committed Dec 30, 2015
1 parent 3c8014d commit f9719ed
Show file tree
Hide file tree
Showing 15 changed files with 43 additions and 31 deletions.
2 changes: 1 addition & 1 deletion docs/Documentation/Installation.md
Expand Up @@ -9,7 +9,7 @@ Using Composer
Installing the plugin via [Composer](https://getcomposer.org/) is very simple, just run in your project folder:

```
composer require burzum/file-storage:~1.0
composer require burzum/file-storage:1.1.*
```

Database Setup
Expand Down
2 changes: 2 additions & 0 deletions src/Model/Entity/FileStorage.php
Expand Up @@ -66,6 +66,8 @@ public function __construct(array $properties = [], array $options = []) {
'model' => true,
'foreign_key' => true,
'file' => true,
'old_file_id' => true,
// Keep this true for the next major release because it's a BC breaker
'*' => true,
];

Expand Down
26 changes: 18 additions & 8 deletions src/Model/Table/FileStorageTable.php
Expand Up @@ -93,14 +93,7 @@ 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;
}
}
$this->_checkEntityBeforeSave($entity);
$Event = $this->dispatchEvent('FileStorage.beforeSave', array(
'record' => $entity,
'storage' => $this->storageAdapter($entity->adapter)
Expand All @@ -111,6 +104,23 @@ public function beforeSave(Event $event, EntityInterface $entity, $options) {
return true;
}

/**
* _checkEntityBeforeSave
*
* @param \Cake\Datasource\EntityInterface $entity
* @return void
*/
protected function _checkEntityBeforeSave(EntityInterface &$entity) {
if ($entity->isNew()) {
if (empty($entity->model)) {
$entity->model = $this->table();
}
if (empty($entity->adapter)) {
$entity->adapter = $this->_defaultAdapter;
}
}
}

/**
* Gets information about the file that is being uploaded.
* - gets the file size
Expand Down
13 changes: 6 additions & 7 deletions tests/TestCase/Event/S3StorageListenerTest.php
Expand Up @@ -48,13 +48,12 @@ public function tearDown() {
*/
public function testBuildPath() {
$entity = $this->Table->newEntity(array(
'model' => 'Document',
'adapter' => 'Test',
'filename' => 'test.png',
'extension' => 'png',
'id' => '144c4170-6760-11e3-949a-0800200c9a66'
)
);
'model' => 'Document',
'adapter' => 'Test',
'filename' => 'test.png',
'extension' => 'png',
'id' => '144c4170-6760-11e3-949a-0800200c9a66'
), ['accessibleFields' => ['*' => true]]);

$adapterConfig = array(
'adapterClass' => '\Gaufrette\Adapter\AwsS3',
Expand Down
2 changes: 1 addition & 1 deletion tests/TestCase/Model/Entity/ImageStorageEntityTest.php
Expand Up @@ -60,7 +60,7 @@ public function testImageVersion() {
'path' => 'test/path/',
'extension' => 'jpg',
'adapter' => 'Local'
]);
], ['accessibleFields' => ['*' => true]]);
$result = $entity->imageVersion('t150');
$this->assertEquals($result, '/test/path/e479b480f60b11e1a21f0800200c9a66.c3f33c2a.jpg');
}
Expand Down
5 changes: 3 additions & 2 deletions tests/TestCase/Model/Table/FileStorageTableTest.php
Expand Up @@ -132,7 +132,7 @@ public function testFileSaving() {
'name' => 'tituts.jpg',
'tmp_name' => $this->fileFixtures . 'titus.jpg'
]
]);
], ['accessibleFields' => ['*' => true]]);
$this->FileStorage->configureUploadValidation([
'allowedExtensions' => ['jpg'],
'validateUploadArray' => true,
Expand All @@ -156,7 +156,8 @@ public function testBeforeSave() {
'error' => UPLOAD_ERR_OK,
'tmp_name' => $this->fileFixtures . 'titus.jpg'
]
]);
], ['accessibleFields' => ['*' => true]]);

$event = new Event('Model.beforeSave', $this->FileStorage, [
'entity' => $entity,
]);
Expand Down
6 changes: 3 additions & 3 deletions tests/TestCase/Model/Table/ImageStorageTableTest.php
Expand Up @@ -66,7 +66,7 @@ public function testProcessVersion() {
'tmp_name' => Plugin::path('Burzum/FileStorage') . DS . 'tests' . DS . 'Fixture' . DS . 'File' . DS . 'titus.jpg',
'error' => 0
]
]);
], ['accessibleFields' => ['*' => true]]);

$this->Image->save($entity);
$result = $this->Image->find()
Expand Down Expand Up @@ -214,7 +214,7 @@ public function testDeleteOldFileOnSave() {
'tmp_name' => Plugin::path('Burzum/FileStorage') . DS . 'tests' . DS . 'Fixture' . DS . 'File' . DS . 'titus.jpg',
'error' => 0
]
]);
], ['accessibleFields' => ['*' => true]]);

$this->Image->save($entity);
$result = $this->Image->find()
Expand All @@ -241,7 +241,7 @@ public function testDeleteOldFileOnSave() {
'error' => 0
],
'old_file_id' => $entity->id
]);
], ['accessibleFields' => ['*' => true]]);

$this->Image->save($secondEntity);
$result2 = $this->Image->find()
Expand Down
Expand Up @@ -61,7 +61,7 @@ public function setUp() {
'hash' => '',
'path' => '',
'adapter' => 'Local',
]);
], ['accessibleFields' => ['*' => true]]);
$this->entity->accessible('id', true);

Configure::write('FileStorage.imageSizes', [
Expand Down
Expand Up @@ -75,7 +75,7 @@ public function testAfterSave() {
'error' => UPLOAD_ERR_OK,
'tmp_name' => $this->fileFixtures . 'titus.jpg'
]
]);
], ['accessibleFields' => ['*' => true]]);

$event = new Event('FileStorage.afterSave', $this->FileStorage, [
'record' => $entity,
Expand Down
2 changes: 1 addition & 1 deletion tests/TestCase/Storage/Listener/LocalListenerTest.php
Expand Up @@ -84,7 +84,7 @@ public function testImagePath() {
'path' => 'test/path/',
'extension' => 'jpg',
'adapter' => 'Local'
]);
], ['accessibleFields' => ['*' => true]]);

$event = new Event('ImageVersion.getVersions', $this->FileStorage, [
'image' => $image,
Expand Down
2 changes: 1 addition & 1 deletion tests/TestCase/Storage/PathBuilder/BasePathBuilderTest.php
Expand Up @@ -32,7 +32,7 @@ public function setUp() {
'hash' => '',
'path' => '',
'adapter' => 'Local',
]);
], ['accessibleFields' => ['*' => true]]);
$this->entity->accessible('id', true);
}

Expand Down
2 changes: 1 addition & 1 deletion tests/TestCase/Storage/PathBuilder/S3PathBuilderTest.php
Expand Up @@ -37,7 +37,7 @@ public function setUp() {
'hash' => '',
'path' => '',
'adapter' => 'S3',
]);
], ['accessibleFields' => ['*' => true]]);
$this->entity->accessible('id', true);
}

Expand Down
2 changes: 1 addition & 1 deletion tests/TestCase/Storage/StorageExceptionTest.php
Expand Up @@ -14,7 +14,7 @@ class StorageExceptionTest extends TestCase {
*/
public function testSetAndGetEntity() {
$FileStorage = TableRegistry::get('Burzum/FileStorage.FileStorage');
$entity = $FileStorage->newEntity([]);
$entity = $FileStorage->newEntity([], ['accessibleFields' => ['*' => true]]);
$exception = new StorageException();
$exception->setEntity($entity);
$this->assertEquals($exception->getEntity(), $entity);
Expand Down
4 changes: 2 additions & 2 deletions tests/TestCase/View/Helper/ImageHelperTest.php
Expand Up @@ -71,7 +71,7 @@ public function testImage() {
'path' => 'test/path/',
'extension' => 'jpg',
'adapter' => 'Local'
]);
], ['accessibleFields' => ['*' => true]]);

// Testing the old deprecated listener
$result = $this->Image->display($image, 't150');
Expand Down Expand Up @@ -123,7 +123,7 @@ public function testImageUrlInvalidArgumentException() {
'path' => 'test/path/',
'extension' => 'jpg',
'adapter' => 'Local'
]);
], ['accessibleFields' => ['*' => true]]);
$this->Image->imageUrl($image, 'invalid-version!');
}

Expand Down
2 changes: 1 addition & 1 deletion tests/TestCase/View/Helper/StorageHelperTest.php
Expand Up @@ -62,7 +62,7 @@ public function testImage() {
'adapter' => 'Local'
);

$entity = $this->FileStorage->newEntity($image);
$entity = $this->FileStorage->newEntity($image, ['accessibleFields' => ['*' => true]]);
$result = $this->Storage->url($entity);
$expected = 'Test/5c/39/33/e479b480f60b11e1a21f0800200c9a66/e479b480f60b11e1a21f0800200c9a66.jpg';
$this->assertEquals($result, $expected);
Expand Down

0 comments on commit f9719ed

Please sign in to comment.