Skip to content

Commit 5d49d67

Browse files
committed
Merge remote-tracking branch 'origin/imported-magento-magento2-31646' into 2.4-develop-pr121
2 parents 21eebde + 2c7573e commit 5d49d67

File tree

2 files changed

+52
-3
lines changed

2 files changed

+52
-3
lines changed

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,12 @@
33
* Copyright © Magento, Inc. All rights reserved.
44
* See COPYING.txt for license details.
55
*/
6+
67
namespace Magento\Catalog\Model;
78

89
use Magento\Framework\App\Filesystem\DirectoryList;
9-
use Magento\Framework\Exception\LocalizedException;
10-
use Magento\Framework\File\Uploader;
1110
use Magento\Framework\App\ObjectManager;
11+
use Magento\Framework\Exception\LocalizedException;
1212
use Magento\Framework\File\Name;
1313
use Magento\Framework\Filesystem;
1414
use Magento\Framework\Filesystem\Directory\WriteInterface;
@@ -211,7 +211,7 @@ public function moveFileFromTmp($imageName, $returnRelativePath = false)
211211
$baseTmpImagePath = $this->getFilePath($baseTmpPath, $imageName);
212212

213213
try {
214-
$this->coreFileStorageDatabase->copyFile(
214+
$this->coreFileStorageDatabase->renameFile(
215215
$baseTmpImagePath,
216216
$baseImagePath
217217
);

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

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,9 @@
1212
use Magento\Framework\Filesystem;
1313
use Magento\Framework\Filesystem\Directory\WriteInterface;
1414
use Magento\Framework\ObjectManagerInterface;
15+
use Magento\MediaStorage\Model\File\Storage;
16+
use Magento\MediaStorage\Helper\File\Storage\Database;
17+
use Magento\MediaStorage\Model\File\Storage\Directory\DatabaseFactory;
1518
use Magento\TestFramework\Helper\Bootstrap;
1619
use PHPUnit\Framework\TestCase;
1720

@@ -58,11 +61,13 @@ protected function setUp(): void
5861
$this->filesystem = $this->objectManager->get(Filesystem::class);
5962
$this->mediaDirectory = $this->filesystem->getDirectoryWrite(DirectoryList::MEDIA);
6063
$this->tmpDirectory = $this->filesystem->getDirectoryWrite(DirectoryList::SYS_TMP);
64+
$dbStorage = $this->objectManager->create(Database::class);
6165
$this->imageUploader = $this->objectManager->create(
6266
ImageUploader::class,
6367
[
6468
'baseTmpPath' => self::BASE_TMP_PATH,
6569
'basePath' => self::BASE_PATH,
70+
'coreFileStorageDatabase' => $dbStorage,
6671
'allowedExtensions' => ['jpg', 'jpeg', 'gif', 'png'],
6772
'allowedMimeTypes' => ['image/jpg', 'image/jpeg', 'image/gif', 'image/png']
6873
]
@@ -129,6 +134,50 @@ public function testMoveFileFromTmp(): void
129134
$this->assertFileExists($this->mediaDirectory->getAbsolutePath($expectedFilePath));
130135
}
131136

137+
/**
138+
* Verify image path will be updated in db in case file moved from tmp dir.
139+
*
140+
* @magentoDataFixture Magento/Catalog/_files/catalog_category_image.php
141+
* @magentoDataFixture Magento/Catalog/_files/catalog_tmp_category_image.php
142+
* @magentoConfigFixture default/system/media_storage_configuration/media_storage 1
143+
* @magentoDbIsolation disabled
144+
*
145+
* @return void
146+
*/
147+
public function testMoveFileFromTmpWithMediaStorageDatabase(): void
148+
{
149+
$fileName = 'magento_small_image.jpg';
150+
$storage = $this->objectManager->get(Storage::class);
151+
$databaseStorage = $this->objectManager->get(Storage\Database::class);
152+
$directory = $this->objectManager->get(DatabaseFactory::class)->create();
153+
// Synchronize media.
154+
$storage->synchronize(
155+
[
156+
'type' => 1,
157+
'connection' => 'default_setup'
158+
]
159+
);
160+
// Upload file.
161+
$fixtureDir = realpath(__DIR__ . '/../_files');
162+
$filePath = $this->tmpDirectory->getAbsolutePath($fileName);
163+
copy($fixtureDir . DIRECTORY_SEPARATOR . $fileName, $filePath);
164+
$_FILES['image'] = [
165+
'name' => $fileName,
166+
'type' => 'image/jpeg',
167+
'tmp_name' => $filePath,
168+
'error' => 0,
169+
'size' => 12500,
170+
];
171+
$result = $this->imageUploader->saveFileToTmpDir('image');
172+
// Move file from tmp dir.
173+
$moveResult = $this->imageUploader->moveFileFromTmp($result['name'], true);
174+
// Verify file moved to new dir.
175+
$databaseStorage->loadByFilename($moveResult);
176+
$directory->loadByPath('catalog/category');
177+
$this->assertEquals('catalog/category', $databaseStorage->getDirectory());
178+
$this->assertEquals($directory->getId(), $databaseStorage->getDirectoryId());
179+
}
180+
132181
/**
133182
* @return void
134183
*/

0 commit comments

Comments
 (0)