|
| 1 | +<?php |
| 2 | +/************************************************************************ |
| 3 | + * |
| 4 | + * Copyright 2024 Adobe |
| 5 | + * All Rights Reserved. |
| 6 | + * |
| 7 | + * NOTICE: All information contained herein is, and remains |
| 8 | + * the property of Adobe and its suppliers, if any. The intellectual |
| 9 | + * and technical concepts contained herein are proprietary to Adobe |
| 10 | + * and its suppliers and are protected by all applicable intellectual |
| 11 | + * property laws, including trade secret and copyright laws. |
| 12 | + * Dissemination of this information or reproduction of this material |
| 13 | + * is strictly forbidden unless prior written permission is obtained |
| 14 | + * from Adobe. |
| 15 | + * ************************************************************************ |
| 16 | + */ |
| 17 | +declare(strict_types=1); |
| 18 | + |
| 19 | +namespace Magento\Catalog\Model\ResourceModel; |
| 20 | + |
| 21 | +use Magento\Catalog\Api\ProductRepositoryInterface; |
| 22 | +use Magento\Catalog\Model\Product\Media\Config; |
| 23 | +use Magento\Catalog\Test\Fixture\Product as ProductFixture; |
| 24 | +use Magento\Framework\App\Filesystem\DirectoryList; |
| 25 | +use Magento\Framework\Filesystem; |
| 26 | +use Magento\Framework\Filesystem\Directory\ReadInterface; |
| 27 | +use Magento\TestFramework\Fixture\DataFixture; |
| 28 | +use Magento\TestFramework\Helper\Bootstrap; |
| 29 | +use PHPUnit\Framework\TestCase; |
| 30 | + |
| 31 | +class MediaImageDeleteProcessorTest extends TestCase |
| 32 | +{ |
| 33 | + /** |
| 34 | + * @var MediaImageDeleteProcessor|null |
| 35 | + */ |
| 36 | + private ?MediaImageDeleteProcessor $mediaImageDeleteProcessor; |
| 37 | + |
| 38 | + /** |
| 39 | + * @var ProductRepositoryInterface|null |
| 40 | + */ |
| 41 | + private ?ProductRepositoryInterface $productRepository; |
| 42 | + |
| 43 | + /** |
| 44 | + * @var ReadInterface|null |
| 45 | + */ |
| 46 | + private ?ReadInterface $mediaDirectory; |
| 47 | + |
| 48 | + /** |
| 49 | + * @var Config|null |
| 50 | + */ |
| 51 | + private ?Config $config; |
| 52 | + |
| 53 | + /** |
| 54 | + * @return void |
| 55 | + */ |
| 56 | + public function setUp(): void |
| 57 | + { |
| 58 | + parent::setUp(); |
| 59 | + $om = Bootstrap::getObjectManager(); |
| 60 | + |
| 61 | + $this->mediaImageDeleteProcessor = $om->get(MediaImageDeleteProcessor::class); |
| 62 | + $this->productRepository = $om->get(ProductRepositoryInterface::class); |
| 63 | + |
| 64 | + $this->mediaDirectory = $om->get(Filesystem::class)->getDirectoryRead(DirectoryList::MEDIA); |
| 65 | + $this->config = $om->get(Config::class); |
| 66 | + } |
| 67 | + |
| 68 | + #[ |
| 69 | + DataFixture( |
| 70 | + ProductFixture::class, |
| 71 | + [ |
| 72 | + 'sku' => 'simple', |
| 73 | + 'media_gallery_entries' => [ |
| 74 | + [] |
| 75 | + ] |
| 76 | + ] |
| 77 | + ), |
| 78 | + ] |
| 79 | + public function testOnlyImageFileDeleted() |
| 80 | + { |
| 81 | + $product = $this->productRepository->get('simple'); |
| 82 | + $image = $product->getMediaGalleryEntries()[0]; |
| 83 | + $imageFilePath = $this->config->getBaseMediaPath() . $image['file']; |
| 84 | + |
| 85 | + $this->assertTrue( |
| 86 | + $this->mediaDirectory->isExist($imageFilePath), |
| 87 | + 'The image file not existed.' |
| 88 | + ); |
| 89 | + $this->mediaImageDeleteProcessor->execute($product); |
| 90 | + $this->assertFalse( |
| 91 | + $this->mediaDirectory->isExist($imageFilePath), |
| 92 | + 'The image file must be deleted.' |
| 93 | + ); |
| 94 | + } |
| 95 | +} |
0 commit comments