From 66f092df59bf3ef47bfea984a37b62e81d183f3e Mon Sep 17 00:00:00 2001 From: Michael Gusev Date: Wed, 8 Jun 2016 18:15:32 +0200 Subject: [PATCH] Possibility to restore original value of the field when upload failed. It helps to keep original value if we just have file field on form and don't want to upload it but keep original file. --- docs/configuration.rst | 4 +++ src/Model/Behavior/UploadBehavior.php | 4 +++ .../Model/Behavior/UploadBehaviorTest.php | 29 +++++++++++++++++++ 3 files changed, 37 insertions(+) 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..6a6ba775 100644 --- a/src/Model/Behavior/UploadBehavior.php +++ b/src/Model/Behavior/UploadBehavior.php @@ -81,6 +81,10 @@ 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)); + $entity->dirty($field, false); + } continue; } diff --git a/tests/TestCase/Model/Behavior/UploadBehaviorTest.php b/tests/TestCase/Model/Behavior/UploadBehaviorTest.php index 44f971f1..bdf300d3 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,20 @@ 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->entity->expects($this->once()) + ->method('dirty') + ->with('field', false); $this->assertNull($behavior->beforeSave(new Event('fake.event'), $this->entity, new ArrayObject)); } @@ -228,6 +244,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']);