|
12 | 12 | use Magento\Framework\Filesystem;
|
13 | 13 | use Magento\Framework\Filesystem\Directory\WriteInterface;
|
14 | 14 | 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; |
15 | 18 | use Magento\TestFramework\Helper\Bootstrap;
|
16 | 19 | use PHPUnit\Framework\TestCase;
|
17 | 20 |
|
@@ -58,11 +61,13 @@ protected function setUp(): void
|
58 | 61 | $this->filesystem = $this->objectManager->get(Filesystem::class);
|
59 | 62 | $this->mediaDirectory = $this->filesystem->getDirectoryWrite(DirectoryList::MEDIA);
|
60 | 63 | $this->tmpDirectory = $this->filesystem->getDirectoryWrite(DirectoryList::SYS_TMP);
|
| 64 | + $dbStorage = $this->objectManager->create(Database::class); |
61 | 65 | $this->imageUploader = $this->objectManager->create(
|
62 | 66 | ImageUploader::class,
|
63 | 67 | [
|
64 | 68 | 'baseTmpPath' => self::BASE_TMP_PATH,
|
65 | 69 | 'basePath' => self::BASE_PATH,
|
| 70 | + 'coreFileStorageDatabase' => $dbStorage, |
66 | 71 | 'allowedExtensions' => ['jpg', 'jpeg', 'gif', 'png'],
|
67 | 72 | 'allowedMimeTypes' => ['image/jpg', 'image/jpeg', 'image/gif', 'image/png']
|
68 | 73 | ]
|
@@ -129,6 +134,50 @@ public function testMoveFileFromTmp(): void
|
129 | 134 | $this->assertFileExists($this->mediaDirectory->getAbsolutePath($expectedFilePath));
|
130 | 135 | }
|
131 | 136 |
|
| 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 | + |
132 | 181 | /**
|
133 | 182 | * @return void
|
134 | 183 | */
|
|
0 commit comments