Skip to content

Commit 8cfdffd

Browse files
committed
MC-17765: Category A image replaced by uploading to Category B
1 parent eefb665 commit 8cfdffd

File tree

7 files changed

+135
-27
lines changed

7 files changed

+135
-27
lines changed

app/code/Magento/Catalog/Model/Category/Attribute/Backend/Image.php

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,9 @@ public function __construct(
7070
}
7171

7272
/**
73-
* Gets image name from $value array. Will return empty string in a case when $value is not an array.
73+
* Gets image name from $value array.
74+
*
75+
* Will return empty string in a case when $value is not an array.
7476
*
7577
* @param array $value Attribute value
7678
* @return string
@@ -98,15 +100,15 @@ private function checkUniqueImageName(string $imageName): string
98100
$imageUploader->getBasePath() . DIRECTORY_SEPARATOR . $imageName
99101
);
100102

101-
if ($mediaDirectory->isExist($imageAbsolutePath)) {
102-
$imageName = Uploader::getNewFilename($imageAbsolutePath);
103-
}
103+
$imageName = Uploader::getNewFilename($imageAbsolutePath);
104104

105105
return $imageName;
106106
}
107107

108108
/**
109-
* Avoiding saving potential upload data to DB. Will set empty image attribute value if image was not uploaded.
109+
* Avoiding saving potential upload data to DB.
110+
*
111+
* Will set empty image attribute value if image was not uploaded.
110112
*
111113
* @param \Magento\Framework\DataObject $object
112114
* @return $this

app/code/Magento/Catalog/Model/ImageUploader.php

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -201,14 +201,16 @@ public function moveFileFromTmp($imageName)
201201
$baseTmpPath = $this->getBaseTmpPath();
202202
$basePath = $this->getBasePath();
203203

204-
$baseImagePath = $this->getFilePath($basePath, $imageName);
204+
$baseImagePath = $this->getFilePath(
205+
$basePath,
206+
Uploader::getNewFileName(
207+
$this->mediaDirectory->getAbsolutePath(
208+
$this->getFilePath($basePath, $imageName)
209+
)
210+
)
211+
);
205212
$baseTmpImagePath = $this->getFilePath($baseTmpPath, $imageName);
206213

207-
if ($this->mediaDirectory->isExist($baseImagePath)) {
208-
$newImageName = Uploader::getNewFileName($this->mediaDirectory->getAbsolutePath($baseImagePath));
209-
$baseImagePath = $this->getFilePath($basePath, $newImageName);
210-
}
211-
212214
try {
213215
$this->coreFileStorageDatabase->copyFile(
214216
$baseTmpImagePath,

dev/tests/integration/testsuite/Magento/Catalog/Model/ImageUploaderTest.php

Lines changed: 9 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -79,27 +79,21 @@ public function testSaveFileToTmpDir(): void
7979
$this->assertTrue(is_file($this->mediaDirectory->getAbsolutePath($filePath)));
8080
}
8181

82+
/**
83+
* Test that method rename files when move it with the same name into base directory.
84+
*
85+
* @return void
86+
* @magentoDataFixture Magento/Catalog/_files/catalog_category_image.php
87+
* @magentoDataFixture Magento/Catalog/_files/catalog_tmp_category_image.php
88+
*/
8289
public function testMoveFileFromTmp(): void
8390
{
84-
$fileName = 'magento_small_image.jpg';
8591
$expectedFileName = 'magento_small_image_1.jpg';
86-
$fixtureDir = realpath(__DIR__ . '/../_files');
87-
$tmpFilePath = $this->imageUploader->getBaseTmpPath() . DIRECTORY_SEPARATOR. $fileName;
88-
$this->mediaDirectory->create($this->imageUploader->getBaseTmpPath());
89-
90-
copy($fixtureDir . DIRECTORY_SEPARATOR . $fileName, $this->mediaDirectory->getAbsolutePath($tmpFilePath));
91-
92-
$this->imageUploader->moveFileFromTmp($fileName);
93-
94-
$filePath = $this->imageUploader->getBasePath() . DIRECTORY_SEPARATOR . $fileName;
95-
$this->assertTrue(is_file($this->mediaDirectory->getAbsolutePath($filePath)));
96-
97-
copy($fixtureDir . DIRECTORY_SEPARATOR . $fileName, $this->mediaDirectory->getAbsolutePath($tmpFilePath));
9892

99-
$this->imageUploader->moveFileFromTmp($fileName);
93+
$this->imageUploader->moveFileFromTmp('magento_small_image.jpg');
10094

10195
$expectedFilePath = $this->imageUploader->getBasePath() . DIRECTORY_SEPARATOR . $expectedFileName;
102-
$this->assertTrue(is_file($this->mediaDirectory->getAbsolutePath($expectedFilePath)));
96+
$this->assertFileExists($this->mediaDirectory->getAbsolutePath($expectedFilePath));
10397
}
10498

10599
/**
@@ -166,6 +160,5 @@ public static function tearDownAfterClass()
166160
/** @var \Magento\Framework\Filesystem\Directory\WriteInterface $mediaDirectory */
167161
$mediaDirectory = $filesystem->getDirectoryWrite(DirectoryList::MEDIA);
168162
$mediaDirectory->delete('tmp');
169-
$mediaDirectory->delete('catalog');
170163
}
171164
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
declare(strict_types=1);
7+
8+
use Magento\Framework\App\Filesystem\DirectoryList;
9+
10+
$objectManager = \Magento\TestFramework\Helper\Bootstrap::getObjectManager();
11+
12+
/** @var $mediaDirectory \Magento\Framework\Filesystem\Directory\WriteInterface */
13+
$mediaDirectory = $objectManager->get(\Magento\Framework\Filesystem::class)
14+
->getDirectoryWrite(DirectoryList::MEDIA);
15+
/** @var $imageUploader \Magento\Catalog\Model\ImageUploader */
16+
$imageUploader = $objectManager->create(
17+
\Magento\Catalog\Model\ImageUploader::class,
18+
[
19+
'baseTmpPath' => $mediaDirectory->getRelativePath('catalog/tmp/category'),
20+
'basePath' => $mediaDirectory->getRelativePath('catalog/category'),
21+
'allowedExtensions' => ['jpg', 'jpeg', 'gif', 'png'],
22+
'allowedMimeTypes' => ['image/jpg', 'image/jpeg', 'image/gif', 'image/png']
23+
]
24+
);
25+
$fileName = 'magento_small_image.jpg';
26+
$filePath = $imageUploader->getBasePath() . DIRECTORY_SEPARATOR. $fileName;
27+
$mediaDirectory->create($imageUploader->getBasePath());
28+
29+
copy(__DIR__ . DIRECTORY_SEPARATOR . $fileName, $mediaDirectory->getAbsolutePath($filePath));
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
declare(strict_types=1);
7+
8+
use Magento\Framework\App\Filesystem\DirectoryList;
9+
10+
/** @var \Magento\Framework\Filesystem\Directory\WriteInterface $mediaDirectory */
11+
$mediaDirectory = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()->get(
12+
\Magento\Framework\Filesystem::class
13+
)->getDirectoryWrite(
14+
DirectoryList::MEDIA
15+
);
16+
/** @var $imageUploader \Magento\Catalog\Model\ImageUploader */
17+
$imageUploader = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()->create(
18+
\Magento\Catalog\Model\ImageUploader::class,
19+
[
20+
'baseTmpPath' => $mediaDirectory->getRelativePath('catalog/tmp/category'),
21+
'basePath' => $mediaDirectory->getRelativePath('catalog/category'),
22+
'allowedExtensions' => ['jpg', 'jpeg', 'gif', 'png'],
23+
'allowedMimeTypes' => ['image/jpg', 'image/jpeg', 'image/gif', 'image/png']
24+
]
25+
);
26+
27+
$mediaDirectory->delete($imageUploader->getBasePath());
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
declare(strict_types=1);
7+
8+
use Magento\Framework\App\Filesystem\DirectoryList;
9+
10+
$objectManager = \Magento\TestFramework\Helper\Bootstrap::getObjectManager();
11+
12+
/** @var $mediaDirectory \Magento\Framework\Filesystem\Directory\WriteInterface */
13+
$mediaDirectory = $objectManager->get(\Magento\Framework\Filesystem::class)
14+
->getDirectoryWrite(DirectoryList::MEDIA);
15+
$imageUploader = $objectManager->create(
16+
\Magento\Catalog\Model\ImageUploader::class,
17+
[
18+
'baseTmpPath' => $mediaDirectory->getRelativePath('catalog/tmp/category'),
19+
'basePath' => $mediaDirectory->getRelativePath('catalog/category'),
20+
'allowedExtensions' => ['jpg', 'jpeg', 'gif', 'png'],
21+
'allowedMimeTypes' => ['image/jpg', 'image/jpeg', 'image/gif', 'image/png']
22+
]
23+
);
24+
$fileName = 'magento_small_image.jpg';
25+
$tmpFilePath = $imageUploader->getBaseTmpPath() . DIRECTORY_SEPARATOR. $fileName;
26+
$mediaDirectory->create($imageUploader->getBaseTmpPath());
27+
28+
copy(__DIR__ . DIRECTORY_SEPARATOR . $fileName, $mediaDirectory->getAbsolutePath($tmpFilePath));
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
declare(strict_types=1);
7+
8+
use Magento\Framework\App\Filesystem\DirectoryList;
9+
10+
/** @var \Magento\Framework\Filesystem\Directory\WriteInterface $mediaDirectory */
11+
$mediaDirectory = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()->get(
12+
\Magento\Framework\Filesystem::class
13+
)->getDirectoryWrite(
14+
DirectoryList::MEDIA
15+
);
16+
/** @var $imageUploader \Magento\Catalog\Model\ImageUploader */
17+
$imageUploader = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()->create(
18+
\Magento\Catalog\Model\ImageUploader::class,
19+
[
20+
'baseTmpPath' => $mediaDirectory->getRelativePath('catalog/tmp/category'),
21+
'basePath' => $mediaDirectory->getRelativePath('catalog/category'),
22+
'allowedExtensions' => ['jpg', 'jpeg', 'gif', 'png'],
23+
'allowedMimeTypes' => ['image/jpg', 'image/jpeg', 'image/gif', 'image/png']
24+
]
25+
);
26+
27+
$mediaDirectory->delete($imageUploader->getBaseTmpPath());

0 commit comments

Comments
 (0)