Skip to content

Commit

Permalink
Merge pull request #603 from ndm2/8.x-fix-after-delete
Browse files Browse the repository at this point in the history
8.x - Fix `afterDelete` expecting an `UploadedFileInterface` object.
  • Loading branch information
ADmad authored Oct 22, 2023
2 parents dda6529 + b61880a commit 324dc73
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 37 deletions.
5 changes: 1 addition & 4 deletions src/Model/Behavior/UploadBehavior.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
54 changes: 21 additions & 33 deletions tests/TestCase/Model/Behavior/UploadBehaviorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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);
Expand Down Expand Up @@ -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')
Expand Down Expand Up @@ -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());
Expand All @@ -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')
Expand All @@ -582,15 +570,15 @@ 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()));
}

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')
Expand All @@ -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);
Expand All @@ -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]);

Expand All @@ -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')
Expand All @@ -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);
Expand All @@ -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]);

Expand Down

0 comments on commit 324dc73

Please sign in to comment.