diff --git a/docs/configuration.rst b/docs/configuration.rst index 6fc28e52..3b7afb2a 100644 --- a/docs/configuration.rst +++ b/docs/configuration.rst @@ -65,3 +65,7 @@ passed in under each field in your behavior configuration. - ``keepFilesOnDelete``: Keep *all* files when deleting a record. - Default: (boolean) ``true`` + +- ``restoreValueOnFailure``: Restores original value of the current field when uploaded file has error + + - Defaults: (boolean) ``true`` diff --git a/src/Model/Behavior/UploadBehavior.php b/src/Model/Behavior/UploadBehavior.php index e393f534..d64bf621 100644 --- a/src/Model/Behavior/UploadBehavior.php +++ b/src/Model/Behavior/UploadBehavior.php @@ -81,6 +81,9 @@ public function beforeSave(Event $event, Entity $entity, ArrayObject $options) { foreach ($this->config() as $field => $settings) { if (Hash::get((array)$entity->get($field), 'error') !== UPLOAD_ERR_OK) { + if (Hash::get($settings, 'restoreValueOnFailure', true)) { + $entity->set($field, $entity->getOriginal($field)); + } continue; } diff --git a/tests/TestCase/Model/Behavior/UploadBehaviorTest.php b/tests/TestCase/Model/Behavior/UploadBehaviorTest.php index 44f971f1..9e3ada0b 100644 --- a/tests/TestCase/Model/Behavior/UploadBehaviorTest.php +++ b/tests/TestCase/Model/Behavior/UploadBehaviorTest.php @@ -168,6 +168,8 @@ public function testBeforeMarshalEmptyAllowed() public function testBeforeSaveUploadError() { + $originalValue = rand(1000, 9999); + $methods = array_diff($this->behaviorMethods, ['config', 'beforeSave']); $behavior = $this->getMock('Josegonzalez\Upload\Model\Behavior\UploadBehavior', $methods, [$this->table, $this->settings]); $behavior->config($this->settings); @@ -175,6 +177,17 @@ public function testBeforeSaveUploadError() ->method('get') ->with('field') ->will($this->returnValue($this->dataError['field'])); + $this->entity->expects($this->any()) + ->method('get') + ->with('field') + ->will($this->returnValue($this->dataError['field'])); + $this->entity->expects($this->any()) + ->method('getOriginal') + ->with('field') + ->will($this->returnValue($originalValue)); + $this->entity->expects($this->once()) + ->method('set') + ->with('field', $originalValue); $this->assertNull($behavior->beforeSave(new Event('fake.event'), $this->entity, new ArrayObject)); } @@ -228,6 +241,19 @@ public function testBeforeSaveOk() $this->assertNull($behavior->beforeSave(new Event('fake.event'), $this->entity, new ArrayObject)); } + public function testBeforeSaveDoesNotRestoreOriginalValue() + { + $settings = $this->settings; + $settings['field']['restoreValueOnFailure'] = false; + + $methods = array_diff($this->behaviorMethods, ['config', 'beforeSave']); + $behavior = $this->getMock('Josegonzalez\Upload\Model\Behavior\UploadBehavior', $methods, [$this->table, $this->settings]); + $behavior->config($settings); + $this->entity->expects($this->never())->method('getOriginal'); + $this->entity->expects($this->never())->method('set'); + $behavior->beforeSave(new Event('fake.event'), $this->entity, new ArrayObject); + } + public function testAfterDeleteOk() { $methods = array_diff($this->behaviorMethods, ['config', 'afterDelete']);