From 5af2ced50cfb22a58a4bd556003e94e07541be86 Mon Sep 17 00:00:00 2001 From: Grzegorz Sadowski Date: Thu, 26 Oct 2017 15:53:01 +0200 Subject: [PATCH] [Core] Fix issue with deleted images after try to remove the product --- .../managing_products/deleting_product.feature | 2 +- .../EventListener/ImagesRemoveListener.php | 17 +++++++++++------ .../EventListener/ImagesRemoveListenerSpec.php | 8 ++++++-- 3 files changed, 18 insertions(+), 9 deletions(-) diff --git a/features/product/managing_products/deleting_product.feature b/features/product/managing_products/deleting_product.feature index 00b76385473..abe799775f6 100644 --- a/features/product/managing_products/deleting_product.feature +++ b/features/product/managing_products/deleting_product.feature @@ -25,7 +25,7 @@ Feature: Deleting a product Then I should be notified that this product cannot be deleted And the product "Lamborghini Gallardo model" should still be in the shop - @ui @javascript @todo + @ui @javascript Scenario: Deleting used product should not remove the image Given this product has an image "lamborghini.jpg" with "thumbnail" type And there is a customer "batman@dc.com" that placed an order diff --git a/src/Sylius/Bundle/CoreBundle/EventListener/ImagesRemoveListener.php b/src/Sylius/Bundle/CoreBundle/EventListener/ImagesRemoveListener.php index 61ae4cca058..bf0877ce894 100644 --- a/src/Sylius/Bundle/CoreBundle/EventListener/ImagesRemoveListener.php +++ b/src/Sylius/Bundle/CoreBundle/EventListener/ImagesRemoveListener.php @@ -16,7 +16,7 @@ use Doctrine\ORM\Event\LifecycleEventArgs; use Liip\ImagineBundle\Imagine\Cache\CacheManager; use Liip\ImagineBundle\Imagine\Filter\FilterManager; -use Sylius\Component\Core\Model\ImageInterface; +use Sylius\Component\Core\Model\ImagesAwareInterface; use Sylius\Component\Core\Uploader\ImageUploaderInterface; final class ImagesRemoveListener @@ -39,11 +39,16 @@ public function __construct(ImageUploaderInterface $imageUploader, CacheManager public function postRemove(LifecycleEventArgs $event): void { - $image = $event->getEntity(); - - if ($image instanceof ImageInterface) { - $this->imageUploader->remove($image->getPath()); - $this->cacheManager->remove($image->getPath(), array_keys($this->filterManager->getFilterConfiguration()->all())); + $entity = $event->getEntity(); + + if ($entity instanceof ImagesAwareInterface) { + foreach ($entity->getImages() as $image) { + $this->imageUploader->remove($image->getPath()); + $this->cacheManager->remove( + $image->getPath(), + array_keys($this->filterManager->getFilterConfiguration()->all()) + ); + } } } } diff --git a/src/Sylius/Bundle/CoreBundle/spec/EventListener/ImagesRemoveListenerSpec.php b/src/Sylius/Bundle/CoreBundle/spec/EventListener/ImagesRemoveListenerSpec.php index 8c9d1bc80f9..192f3890a27 100644 --- a/src/Sylius/Bundle/CoreBundle/spec/EventListener/ImagesRemoveListenerSpec.php +++ b/src/Sylius/Bundle/CoreBundle/spec/EventListener/ImagesRemoveListenerSpec.php @@ -13,6 +13,7 @@ namespace spec\Sylius\Bundle\CoreBundle\EventListener; +use Doctrine\Common\Collections\ArrayCollection; use Doctrine\ORM\Event\LifecycleEventArgs; use Liip\ImagineBundle\Imagine\Cache\CacheManager; use Liip\ImagineBundle\Imagine\Filter\FilterConfiguration; @@ -21,6 +22,7 @@ use Prophecy\Argument; use Sylius\Bundle\CoreBundle\EventListener\ImagesRemoveListener; use Sylius\Component\Core\Model\ImageInterface; +use Sylius\Component\Core\Model\ImagesAwareInterface; use Sylius\Component\Core\Uploader\ImageUploaderInterface; final class ImagesRemoveListenerSpec extends ObjectBehavior @@ -40,10 +42,12 @@ function it_removes_file_on_post_remove_event( CacheManager $cacheManager, FilterManager $filterManager, LifecycleEventArgs $event, + ImagesAwareInterface $imagesAwareEntity, ImageInterface $image, FilterConfiguration $filterConfiguration ): void { - $event->getEntity()->willReturn($image); + $event->getEntity()->willReturn($imagesAwareEntity); + $imagesAwareEntity->getImages()->willReturn(new ArrayCollection([$image->getWrappedObject()])); $image->getPath()->willReturn('image/path'); $filterManager->getFilterConfiguration()->willReturn($filterConfiguration); @@ -54,7 +58,7 @@ function it_removes_file_on_post_remove_event( $this->postRemove($event); } - function it_does_nothing_if_entity_is_not_image( + function it_does_nothing_if_entity_is_not_image_aware( ImageUploaderInterface $imageUploader, CacheManager $cacheManager, FilterManager $filterManager,