From 3159ae672e900bfe317b5fb77cc2c68c9fb3bb81 Mon Sep 17 00:00:00 2001 From: Joris Vaesen Date: Sat, 26 Mar 2016 22:08:54 +0100 Subject: [PATCH 1/2] Fix when using empty settings --- src/Model/Behavior/UploadBehavior.php | 3 ++ .../Model/Behavior/UploadBehaviorTest.php | 53 +++++++++++++++++++ 2 files changed, 56 insertions(+) diff --git a/src/Model/Behavior/UploadBehavior.php b/src/Model/Behavior/UploadBehavior.php index f4d13507..4cfd984e 100644 --- a/src/Model/Behavior/UploadBehavior.php +++ b/src/Model/Behavior/UploadBehavior.php @@ -25,6 +25,7 @@ class UploadBehavior extends Behavior */ public function initialize(array $config) { + $this->_config = []; $this->config(Hash::normalize($config)); Type::map('upload.file', 'Josegonzalez\Upload\Database\Type\FileType'); @@ -74,6 +75,8 @@ public function beforeSave(Event $event, Entity $entity, ArrayObject $options) continue; } + $settings = is_array($settings) ? $settings : []; + $data = $entity->get($field); $path = $this->getPathProcessor($entity, $data, $field, $settings); $basepath = $path->basepath(); diff --git a/tests/TestCase/Model/Behavior/UploadBehaviorTest.php b/tests/TestCase/Model/Behavior/UploadBehaviorTest.php index 52fdff86..9490297f 100644 --- a/tests/TestCase/Model/Behavior/UploadBehaviorTest.php +++ b/tests/TestCase/Model/Behavior/UploadBehaviorTest.php @@ -71,6 +71,32 @@ public function testInitialize() $behavior->initialize($this->settings); } + public function testInitializeIndexedConfig() + { + $settings = ['field']; + $table = $this->getMock('Cake\ORM\Table'); + $schema = $this->getMock('Cake\Database\Schema\Table', [], [$table, []]); + $schema->expects($this->once()) + ->method('columnType') + ->with('field', 'upload.file'); + $table->expects($this->at(0)) + ->method('schema') + ->will($this->returnValue($schema)); + $table->expects($this->at(1)) + ->method('schema') + ->will($this->returnValue($schema)); + + $methods = array_diff($this->behaviorMethods, ['initialize', 'config']); + $behavior = $this->getMock('Josegonzalez\Upload\Model\Behavior\UploadBehavior', $methods, [$table, $settings], '', false); + $reflection = new ReflectionClass($behavior); + $property = $reflection->getProperty('_table'); + $property->setAccessible(true); + $property->setValue($behavior, $table); + $behavior->initialize($settings); + + $this->assertEquals(['field' => null], $behavior->config()); + } + public function testBeforeMarshalOk() { $validator = $this->getMock('Cake\Validation\Validator'); @@ -202,6 +228,33 @@ public function testBeforeSaveOk() $this->assertNull($behavior->beforeSave(new Event('fake.event'), $this->entity, new ArrayObject)); } + public function testBeforeSaveNormalizedConfig() + { + $settings = ['field' => null]; + $methods = array_diff($this->behaviorMethods, ['config', 'beforeSave']); + $behavior = $this->getMock('Josegonzalez\Upload\Model\Behavior\UploadBehavior', $methods, [$this->table, $settings]); + $behavior->config($this->settings); + $this->entity->expects($this->any()) + ->method('get') + ->with('field') + ->will($this->returnValue($this->dataOk['field'])); + $behavior->expects($this->any()) + ->method('getPathProcessor') + ->will($this->returnValue($this->processor)); + $behavior->expects($this->any()) + ->method('getWriter') + ->will($this->returnValue($this->writer)); + $behavior->expects($this->any()) + ->method('constructFiles') + ->will($this->returnValue([])); + $this->writer->expects($this->any()) + ->method('write') + ->will($this->returnValue([true])); + + $this->assertEquals(['field' => []], $behavior->config()); + $this->assertNull($behavior->beforeSave(new Event('fake.event'), $this->entity, new ArrayObject)); + } + public function testAfterDeleteOk() { $methods = array_diff($this->behaviorMethods, ['config', 'afterDelete']); From d26494aeefa64ffac29483b66880cfe951d626ca Mon Sep 17 00:00:00 2001 From: Joris Vaesen Date: Sat, 26 Mar 2016 22:33:09 +0100 Subject: [PATCH 2/2] Fix empty settings when initializing --- src/Model/Behavior/UploadBehavior.php | 13 +++++++-- .../Model/Behavior/UploadBehaviorTest.php | 29 +------------------ 2 files changed, 11 insertions(+), 31 deletions(-) diff --git a/src/Model/Behavior/UploadBehavior.php b/src/Model/Behavior/UploadBehavior.php index 4cfd984e..0928864e 100644 --- a/src/Model/Behavior/UploadBehavior.php +++ b/src/Model/Behavior/UploadBehavior.php @@ -25,8 +25,17 @@ class UploadBehavior extends Behavior */ public function initialize(array $config) { + $configs = []; + foreach ($config as $field => $settings) { + if (is_int($field)) { + $configs[$settings] = []; + } else { + $configs[$field] = $settings; + } + } + $this->_config = []; - $this->config(Hash::normalize($config)); + $this->config($configs); Type::map('upload.file', 'Josegonzalez\Upload\Database\Type\FileType'); $schema = $this->_table->schema(); @@ -75,8 +84,6 @@ public function beforeSave(Event $event, Entity $entity, ArrayObject $options) continue; } - $settings = is_array($settings) ? $settings : []; - $data = $entity->get($field); $path = $this->getPathProcessor($entity, $data, $field, $settings); $basepath = $path->basepath(); diff --git a/tests/TestCase/Model/Behavior/UploadBehaviorTest.php b/tests/TestCase/Model/Behavior/UploadBehaviorTest.php index 9490297f..4bb5641f 100644 --- a/tests/TestCase/Model/Behavior/UploadBehaviorTest.php +++ b/tests/TestCase/Model/Behavior/UploadBehaviorTest.php @@ -94,7 +94,7 @@ public function testInitializeIndexedConfig() $property->setValue($behavior, $table); $behavior->initialize($settings); - $this->assertEquals(['field' => null], $behavior->config()); + $this->assertEquals(['field' => []], $behavior->config()); } public function testBeforeMarshalOk() @@ -228,33 +228,6 @@ public function testBeforeSaveOk() $this->assertNull($behavior->beforeSave(new Event('fake.event'), $this->entity, new ArrayObject)); } - public function testBeforeSaveNormalizedConfig() - { - $settings = ['field' => null]; - $methods = array_diff($this->behaviorMethods, ['config', 'beforeSave']); - $behavior = $this->getMock('Josegonzalez\Upload\Model\Behavior\UploadBehavior', $methods, [$this->table, $settings]); - $behavior->config($this->settings); - $this->entity->expects($this->any()) - ->method('get') - ->with('field') - ->will($this->returnValue($this->dataOk['field'])); - $behavior->expects($this->any()) - ->method('getPathProcessor') - ->will($this->returnValue($this->processor)); - $behavior->expects($this->any()) - ->method('getWriter') - ->will($this->returnValue($this->writer)); - $behavior->expects($this->any()) - ->method('constructFiles') - ->will($this->returnValue([])); - $this->writer->expects($this->any()) - ->method('write') - ->will($this->returnValue([true])); - - $this->assertEquals(['field' => []], $behavior->config()); - $this->assertNull($behavior->beforeSave(new Event('fake.event'), $this->entity, new ArrayObject)); - } - public function testAfterDeleteOk() { $methods = array_diff($this->behaviorMethods, ['config', 'afterDelete']);