Skip to content

Commit

Permalink
Merge pull request #4767 from izayoi256/feature/keep-referenced-image
Browse files Browse the repository at this point in the history
商品画像削除時、他の商品画像が参照するファイルは削除しないよう変更
  • Loading branch information
okazy authored Nov 16, 2020
2 parents 9de60f8 + 0da4c71 commit 40a16bb
Show file tree
Hide file tree
Showing 4 changed files with 142 additions and 3 deletions.
13 changes: 13 additions & 0 deletions src/Eccube/Controller/Admin/Product/CsvImportController.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
use Eccube\Repository\DeliveryDurationRepository;
use Eccube\Repository\Master\ProductStatusRepository;
use Eccube\Repository\Master\SaleTypeRepository;
use Eccube\Repository\ProductImageRepository;
use Eccube\Repository\ProductRepository;
use Eccube\Repository\TagRepository;
use Eccube\Repository\TaxRuleRepository;
Expand Down Expand Up @@ -74,6 +75,11 @@ class CsvImportController extends AbstractCsvImportController
*/
protected $classCategoryRepository;

/**
* @var ProductImageRepository
*/
protected $productImageRepository;

/**
* @var ProductStatusRepository
*/
Expand Down Expand Up @@ -109,6 +115,7 @@ class CsvImportController extends AbstractCsvImportController
* @param TagRepository $tagRepository
* @param CategoryRepository $categoryRepository
* @param ClassCategoryRepository $classCategoryRepository
* @param ProductImageRepository $productImageRepository
* @param ProductStatusRepository $productStatusRepository
* @param ProductRepository $productRepository
* @param TaxRuleRepository $taxRuleRepository
Expand All @@ -122,6 +129,7 @@ public function __construct(
TagRepository $tagRepository,
CategoryRepository $categoryRepository,
ClassCategoryRepository $classCategoryRepository,
ProductImageRepository $productImageRepository,
ProductStatusRepository $productStatusRepository,
ProductRepository $productRepository,
TaxRuleRepository $taxRuleRepository,
Expand All @@ -133,6 +141,7 @@ public function __construct(
$this->tagRepository = $tagRepository;
$this->categoryRepository = $categoryRepository;
$this->classCategoryRepository = $classCategoryRepository;
$this->productImageRepository = $productImageRepository;
$this->productStatusRepository = $productStatusRepository;
$this->productRepository = $productRepository;
$this->taxRuleRepository = $taxRuleRepository;
Expand Down Expand Up @@ -629,7 +638,11 @@ public function csvProduct(Request $request, CacheUtil $cacheUtil)

// 画像ファイルの削除(commit後に削除させる)
foreach ($deleteImages as $images) {
/** @var ProductImage $image */
foreach ($images as $image) {
if ($this->productImageRepository->findOneBy(['file_name' => $image->getFileName()])) {
continue;
}
try {
$fs = new Filesystem();
$fs->remove($this->eccubeConfig['eccube_save_image_dir'].'/'.$image);
Expand Down
13 changes: 10 additions & 3 deletions src/Eccube/Controller/Admin/Product/ProductController.php
Original file line number Diff line number Diff line change
Expand Up @@ -545,10 +545,13 @@ public function edit(Request $request, $id = null, RouterInterface $router, Cach
$this->entityManager->remove($ProductImage);
}
$this->entityManager->persist($Product);
$this->entityManager->flush();

// 削除
$fs = new Filesystem();
$fs->remove($this->eccubeConfig['eccube_save_image_dir'].'/'.$delete_image);
if (!$this->productImageRepository->findOneBy(['file_name' => $delete_image])) {
// 削除
$fs = new Filesystem();
$fs->remove($this->eccubeConfig['eccube_save_image_dir'] . '/' . $delete_image);
}
}
$this->entityManager->persist($Product);
$this->entityManager->flush();
Expand Down Expand Up @@ -719,7 +722,11 @@ public function delete(Request $request, $id = null, CacheUtil $cacheUtil)
$deleteImages = $event->getArgument('deleteImages');

// 画像ファイルの削除(commit後に削除させる)
/** @var ProductImage $deleteImage */
foreach ($deleteImages as $deleteImage) {
if ($this->productImageRepository->findOneBy(['file_name' => $deleteImage->getFileName()])) {
continue;
}
try {
$fs = new Filesystem();
$fs->remove($this->eccubeConfig['eccube_save_image_dir'].'/'.$deleteImage);
Expand Down
36 changes: 36 additions & 0 deletions tests/Eccube/Tests/Web/Admin/Product/CsvImportControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
use Eccube\Entity\BaseInfo;
use Eccube\Entity\Product;
use Eccube\Entity\ProductClass;
use Eccube\Entity\ProductImage;
use Eccube\Repository\CategoryRepository;
use Eccube\Repository\ProductRepository;
use Eccube\Tests\Web\Admin\AbstractAdminWebTestCase;
Expand Down Expand Up @@ -928,4 +929,39 @@ public function dataTaxRuleProvider()
[false, '', null],
];
}

/**
* 商品を削除する際に、他の商品画像が参照しているファイルは削除せず、それ以外は削除することをテスト
*/
public function testDeleteImage()
{
/** @var \Eccube\Tests\Fixture\Generator $generator */
$generator = $this->container->get(\Eccube\Tests\Fixture\Generator::class);
$Product1 = $generator->createProduct(null, 0, 'abstract');
$Product2 = $generator->createProduct(null, 0, 'abstract');

$DuplicatedImage = $Product1->getProductImage()->first();
assert($DuplicatedImage instanceof ProductImage);

$NotDuplicatedImage = $Product1->getProductImage()->last();
assert($NotDuplicatedImage instanceof ProductImage);

$NewProduct2Image = new ProductImage();
$NewProduct2Image
->setProduct($Product2)
->setFileName($DuplicatedImage->getFileName())
->setSortNo(999);
$Product2->addProductImage($NewProduct2Image);
$this->entityManager->persist($NewProduct2Image);
$this->entityManager->flush();

$csv[] = ['商品ID', '公開ステータス(ID)', '商品名', '商品削除フラグ', '販売種別(ID)', '販売価格'];
$csv[] = [$Product1->getId(), '1', 'hoge', '1', '1', '1000'];
$this->filepath = $this->createCsvFromArray($csv);
$this->scenario();

$dir = __DIR__ . '/../../../../../../html/upload/save_image/';
$this->assertTrue(file_exists($dir . $DuplicatedImage->getFileName()));
$this->assertFalse(file_exists($dir . $NotDuplicatedImage->getFileName()));
}
}
83 changes: 83 additions & 0 deletions tests/Eccube/Tests/Web/Admin/Product/ProductContorllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,11 @@
use Eccube\Entity\Master\ProductStatus;
use Eccube\Entity\Master\RoundingType;
use Eccube\Entity\ProductClass;
use Eccube\Entity\ProductImage;
use Eccube\Entity\ProductTag;
use Eccube\Entity\Tag;
use Eccube\Entity\TaxRule;
use Eccube\Tests\Fixture\Generator;
use Eccube\Tests\Web\Admin\AbstractAdminWebTestCase;
use Eccube\Util\StringUtil;
use Symfony\Component\DomCrawler\Crawler;
Expand Down Expand Up @@ -1049,4 +1051,85 @@ private function createSearchForm()

return $post;
}

/**
* 商品画像を削除する際に、他の商品画像が参照しているファイルは削除せず、それ以外は削除することをテスト
*/
public function testDeleteImage()
{
/** @var Generator $generator */
$generator = $this->container->get(Generator::class);
$Product1 = $generator->createProduct(null, 0, 'abstract');
$Product2 = $generator->createProduct(null, 0, 'abstract');

$DuplicatedImage = $Product1->getProductImage()->first();
assert($DuplicatedImage instanceof ProductImage);

$NotDuplicatedImage = $Product1->getProductImage()->last();
assert($NotDuplicatedImage instanceof ProductImage);

$NewProduct2Image = new ProductImage();
$NewProduct2Image
->setProduct($Product2)
->setFileName($DuplicatedImage->getFileName())
->setSortNo(999)
;
$Product2->addProductImage($NewProduct2Image);
$this->entityManager->persist($NewProduct2Image);
$this->entityManager->flush();

$data = $this->createFormData();
$data['delete_images'] = $Product1->getProductImage()->map(static function (ProductImage $ProductImage) {
return $ProductImage->getFileName();
})->toArray();
$this->client->request(
'POST',
$this->generateUrl('admin_product_product_edit', ['id' => $Product1->getId()]),
['admin_product' => $data]
);
$this->assertTrue($this->client->getResponse()->isRedirect());

$dir = __DIR__.'/../../../../../../html/upload/save_image/';
$this->assertTrue(file_exists($dir . $DuplicatedImage->getFileName()));
$this->assertFalse(file_exists($dir . $NotDuplicatedImage->getFileName()));
}

public function testDeleteAndDeleteProductImage()
{
/** @var Generator $generator */
$generator = $this->container->get(Generator::class);
$Product1 = $generator->createProduct(null, 0, 'abstract');
$Product2 = $generator->createProduct(null, 0, 'abstract');

$DuplicatedImage = $Product1->getProductImage()->first();
assert($DuplicatedImage instanceof ProductImage);

$NotDuplicatedImage = $Product1->getProductImage()->last();
assert($NotDuplicatedImage instanceof ProductImage);

$NewProduct2Image = new ProductImage();
$NewProduct2Image
->setProduct($Product2)
->setFileName($DuplicatedImage->getFileName())
->setSortNo(999)
;
$Product2->addProductImage($NewProduct2Image);
$this->entityManager->persist($NewProduct2Image);
$this->entityManager->flush();

$params = [
'id' => $Product1->getId(),
Constant::TOKEN_NAME => 'dummy',
];

$this->client->request('DELETE', $this->generateUrl('admin_product_product_delete', $params));

$rUrl = $this->generateUrl('admin_product_page', ['page_no' => 1]).'?resume=1';

$this->assertTrue($this->client->getResponse()->isRedirect($rUrl));

$dir = __DIR__.'/../../../../../../html/upload/save_image/';
$this->assertTrue(file_exists($dir . $DuplicatedImage->getFileName()));
$this->assertFalse(file_exists($dir . $NotDuplicatedImage->getFileName()));
}
}

0 comments on commit 40a16bb

Please sign in to comment.