From b61880a4333b99edfbea6af536ea595ec05d0e70 Mon Sep 17 00:00:00 2001 From: ndm2 Date: Sun, 22 Oct 2023 02:32:02 +0200 Subject: [PATCH] Fix `afterDelete` expecting an `UploadedFileInterface` object. --- src/Model/Behavior/UploadBehavior.php | 5 +- .../Model/Behavior/UploadBehaviorTest.php | 54 ++++++++----------- 2 files changed, 22 insertions(+), 37 deletions(-) diff --git a/src/Model/Behavior/UploadBehavior.php b/src/Model/Behavior/UploadBehavior.php index abde394..583f12f 100644 --- a/src/Model/Behavior/UploadBehavior.php +++ b/src/Model/Behavior/UploadBehavior.php @@ -176,10 +176,7 @@ public function afterDelete(EventInterface $event, EntityInterface $entity, Arra if ($callback && is_callable($callback)) { $files = $callback($path, $entity, $field, $settings); } else { - /** @var \Psr\Http\Message\UploadedFileInterface $uploaded */ - $uploaded = $entity->get($field); - - $files = [$path . $uploaded->getClientFilename()]; + $files = [$path . $entity->get($field)]; } $writer = $this->getWriter($entity, null, $field, $settings); diff --git a/tests/TestCase/Model/Behavior/UploadBehaviorTest.php b/tests/TestCase/Model/Behavior/UploadBehaviorTest.php index 45e7b9b..465e23a 100644 --- a/tests/TestCase/Model/Behavior/UploadBehaviorTest.php +++ b/tests/TestCase/Model/Behavior/UploadBehaviorTest.php @@ -438,7 +438,7 @@ public function testAfterDeleteOk() $this->entity->expects($this->any()) ->method('get') ->with('field') - ->will($this->returnValue($this->dataOk['field'])); + ->will($this->returnValue('file.txt')); $behavior->expects($this->any()) ->method('getPathProcessor') ->willReturn($this->processor); @@ -463,7 +463,7 @@ public function testAfterDeleteFail() $this->entity->expects($this->any()) ->method('get') ->with('field') - ->will($this->returnValue($this->dataOk['field'])); + ->will($this->returnValue('file.txt')); $behavior->expects($this->any()) ->method('getPathProcessor') ->willReturn($this->processor); @@ -498,13 +498,7 @@ public function testAfterDeleteSkip() public function testAfterDeleteUsesPathProcessorToDetectPathToTheFile() { $dir = '/some/path/'; - $field = new UploadedFile( - fopen('php://temp', 'wb+'), - 1, - UPLOAD_ERR_OK, - 'file.txt', - 'text/plain' - ); + $field = 'file.txt'; $methods = array_diff($this->behaviorMethods, ['afterDelete', 'config', 'setConfig', 'getConfig']); $behavior = $this->getMockBuilder('Josegonzalez\Upload\Model\Behavior\UploadBehavior') @@ -540,7 +534,7 @@ public function testAfterDeleteUsesPathProcessorToDetectPathToTheFile() // and here we check that file with right path will be deleted $this->writer->expects($this->once()) ->method('delete') - ->with([$dir . $field->getClientFilename()]) + ->with([$dir . $field]) ->willReturn([true]); $behavior->afterDelete(new Event('fake.event'), $this->entity, new ArrayObject()); @@ -549,13 +543,7 @@ public function testAfterDeleteUsesPathProcessorToDetectPathToTheFile() public function testAfterDeletePrefersStoredPathOverPathProcessor() { $dir = '/some/path/'; - $field = new UploadedFile( - fopen('php://temp', 'wb+'), - 1, - UPLOAD_ERR_OK, - 'file.txt', - 'text/plain' - ); + $field = 'file.txt'; $methods = array_diff($this->behaviorMethods, ['afterDelete', 'config', 'setConfig', 'getConfig']); $behavior = $this->getMockBuilder('Josegonzalez\Upload\Model\Behavior\UploadBehavior') @@ -582,7 +570,7 @@ public function testAfterDeletePrefersStoredPathOverPathProcessor() $this->writer->expects($this->once()) ->method('delete') - ->with([$dir . $field->getClientFilename()]) + ->with([$dir . $field]) ->will($this->returnValue([true])); $this->assertTrue($behavior->afterDelete(new Event('fake.event'), $this->entity, new ArrayObject())); @@ -590,7 +578,7 @@ public function testAfterDeletePrefersStoredPathOverPathProcessor() public function testAfterDeleteNoDeleteCallback() { - $this->entity->field = rand(1000, 9999); + $field = (string)rand(1000, 9999); $path = rand(1000, 9999) . DIRECTORY_SEPARATOR; $methods = array_diff($this->behaviorMethods, ['afterDelete', 'config', 'setConfig', 'getConfig']); $behavior = $this->getMockBuilder('Josegonzalez\Upload\Model\Behavior\UploadBehavior') @@ -601,12 +589,12 @@ public function testAfterDeleteNoDeleteCallback() $this->configOk['field']['deleteCallback'] = null; $behavior->setConfig($this->configOk); - $this->entity->expects($this->any()) + $this->entity->expects($this->exactly(2)) ->method('get') ->with('field') - ->will($this->returnValue($this->dataOk['field'])); + ->will($this->returnValue($field)); $behavior->expects($this->once())->method('getPathProcessor') - ->with($this->entity, $this->dataOk['field'], 'field', $this->configOk['field']) + ->with($this->entity, $field, 'field', $this->configOk['field']) ->willReturn($this->processor); $this->processor->expects($this->once())->method('basepath') ->willReturn($path); @@ -616,7 +604,7 @@ public function testAfterDeleteNoDeleteCallback() $this->writer->expects($this->once()) ->method('delete') ->with([ - $path . $this->entity->field . $this->dataOk['field']->getClientFilename(), + $path . $field, ]) ->willReturn([true, true, true]); @@ -625,7 +613,7 @@ public function testAfterDeleteNoDeleteCallback() public function testAfterDeleteUsesDeleteCallback() { - $this->entity->field = rand(1000, 9999); + $field = (string)rand(1000, 9999); $path = rand(1000, 9999) . DIRECTORY_SEPARATOR; $methods = array_diff($this->behaviorMethods, ['afterDelete', 'config', 'setConfig', 'getConfig']); $behavior = $this->getMockBuilder('Josegonzalez\Upload\Model\Behavior\UploadBehavior') @@ -635,19 +623,19 @@ public function testAfterDeleteUsesDeleteCallback() $this->configOk['field']['deleteCallback'] = function ($path, $entity, $field, $settings) { return [ - $path . $entity->{$field}, - $path . 'sm-' . $entity->{$field}, - $path . 'lg-' . $entity->{$field}, + $path . $entity->get($field), + $path . 'sm-' . $entity->get($field), + $path . 'lg-' . $entity->get($field), ]; }; $behavior->setConfig($this->configOk); - $this->entity->expects($this->any()) + $this->entity->expects($this->exactly(4)) ->method('get') ->with('field') - ->will($this->returnValue($this->dataOk['field'])); + ->will($this->returnValue($field)); $behavior->expects($this->once())->method('getPathProcessor') - ->with($this->entity, $this->dataOk['field'], 'field', $this->configOk['field']) + ->with($this->entity, $field, 'field', $this->configOk['field']) ->willReturn($this->processor); $this->processor->expects($this->once())->method('basepath') ->willReturn($path); @@ -657,9 +645,9 @@ public function testAfterDeleteUsesDeleteCallback() $this->writer->expects($this->once()) ->method('delete') ->with([ - $path . $this->entity->field, - $path . 'sm-' . $this->entity->field, - $path . 'lg-' . $this->entity->field, + $path . $field, + $path . 'sm-' . $field, + $path . 'lg-' . $field, ]) ->willReturn([true, true, true]);