|
| 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 | +namespace Magento\Framework\File; |
| 9 | + |
| 10 | +use Magento\Customer\Model\FileProcessor; |
| 11 | +use Magento\Framework\App\Filesystem\DirectoryList; |
| 12 | + |
| 13 | +/** |
| 14 | + * Test for \Magento\MediaStorage\Model\File\Uploader |
| 15 | + */ |
| 16 | +class MediaStorageUploaderTest extends \PHPUnit\Framework\TestCase |
| 17 | +{ |
| 18 | + /** |
| 19 | + * @var \Magento\MediaStorage\Model\File\UploaderFactory |
| 20 | + */ |
| 21 | + private $uploaderFactory; |
| 22 | + |
| 23 | + /** |
| 24 | + * @var \Magento\Framework\Filesystem |
| 25 | + */ |
| 26 | + private $filesystem; |
| 27 | + |
| 28 | + /** |
| 29 | + * @inheritdoc |
| 30 | + */ |
| 31 | + protected function setUp(): void |
| 32 | + { |
| 33 | + $this->uploaderFactory = \Magento\TestFramework\Helper\Bootstrap::getObjectManager() |
| 34 | + ->get(\Magento\MediaStorage\Model\File\UploaderFactory::class); |
| 35 | + |
| 36 | + $this->filesystem = \Magento\TestFramework\Helper\Bootstrap::getObjectManager() |
| 37 | + ->get(\Magento\Framework\Filesystem::class); |
| 38 | + } |
| 39 | + |
| 40 | + /** |
| 41 | + * @return void |
| 42 | + */ |
| 43 | + public function testUploadFileFromAllowedFolder(): void |
| 44 | + { |
| 45 | + $mediaDirectory = $this->filesystem->getDirectoryWrite(DirectoryList::MEDIA); |
| 46 | + $fileName = 'text.txt'; |
| 47 | + $uploader = $this->createUploader($fileName); |
| 48 | + |
| 49 | + $uploader->save($mediaDirectory->getAbsolutePath(FileProcessor::TMP_DIR)); |
| 50 | + |
| 51 | + $this->assertTrue($mediaDirectory->isFile(FileProcessor::TMP_DIR . DIRECTORY_SEPARATOR . $fileName)); |
| 52 | + } |
| 53 | + |
| 54 | + /** |
| 55 | + * @return void |
| 56 | + */ |
| 57 | + public function testUploadFileFromNotAllowedFolder(): void |
| 58 | + { |
| 59 | + $this->expectException(\InvalidArgumentException::class); |
| 60 | + $this->expectExceptionMessage('Invalid parameter given. A valid $fileId[tmp_name] is expected.'); |
| 61 | + |
| 62 | + $fileName = 'text.txt'; |
| 63 | + $tmpDir = 'tmp'; |
| 64 | + $tmpDirectory = $this->filesystem->getDirectoryWrite(DirectoryList::LOG); |
| 65 | + $filePath = $tmpDirectory->getAbsolutePath() . $tmpDir . DIRECTORY_SEPARATOR . $fileName; |
| 66 | + |
| 67 | + $tmpDirectory->writeFile($tmpDir . DIRECTORY_SEPARATOR . $fileName, 'just a text'); |
| 68 | + |
| 69 | + $type = [ |
| 70 | + 'tmp_name' => $filePath, |
| 71 | + 'name' => $fileName, |
| 72 | + ]; |
| 73 | + |
| 74 | + $this->uploaderFactory->create(['fileId' => $type]); |
| 75 | + } |
| 76 | + |
| 77 | + /** |
| 78 | + * @return void |
| 79 | + */ |
| 80 | + public function testUploadFileWithExcessiveFolderName(): void |
| 81 | + { |
| 82 | + $this->expectException(\InvalidArgumentException::class); |
| 83 | + $this->expectExceptionMessage('Destination folder path is too long; must be 255 characters or less'); |
| 84 | + |
| 85 | + $uploader = $this->createUploader('text.txt'); |
| 86 | + $longStringFilePath = __DIR__ . '/_files/fixture_with_long_string.txt'; |
| 87 | + $longDirectoryFolderName = file_get_contents($longStringFilePath); |
| 88 | + |
| 89 | + $uploader->save($longDirectoryFolderName); |
| 90 | + } |
| 91 | + |
| 92 | + /** |
| 93 | + * Upload file test when `Old Media Gallery` is disabled |
| 94 | + * |
| 95 | + * @magentoConfigFixture system/media_gallery/enabled 1 |
| 96 | + * @magentoAppArea adminhtml |
| 97 | + * @dataProvider dirCodeDataProvider |
| 98 | + * |
| 99 | + * @param string $directoryCode |
| 100 | + * @return void |
| 101 | + */ |
| 102 | + public function testUploadFileWhenOldMediaGalleryDisabled(string $directoryCode): void |
| 103 | + { |
| 104 | + $destinationDirectory = $this->filesystem->getDirectoryWrite($directoryCode); |
| 105 | + $tmpDirectory = $this->filesystem->getDirectoryWrite(DirectoryList::SYS_TMP); |
| 106 | + |
| 107 | + $fileName = 'file.txt'; |
| 108 | + $destinationDir = 'tmp'; |
| 109 | + $filePath = $tmpDirectory->getAbsolutePath($fileName); |
| 110 | + |
| 111 | + $tmpDirectory->writeFile($fileName, 'some data'); |
| 112 | + |
| 113 | + $type = [ |
| 114 | + 'tmp_name' => $filePath, |
| 115 | + 'name' => $fileName, |
| 116 | + ]; |
| 117 | + |
| 118 | + $uploader = $this->uploaderFactory->create(['fileId' => $type]); |
| 119 | + $uploader->save($destinationDirectory->getAbsolutePath($destinationDir)); |
| 120 | + |
| 121 | + $this->assertTrue($destinationDirectory->isFile($destinationDir . DIRECTORY_SEPARATOR . $fileName)); |
| 122 | + } |
| 123 | + |
| 124 | + /** |
| 125 | + * DataProvider for testUploadFileWhenOldMediaGalleryDisabled |
| 126 | + * |
| 127 | + * @return array |
| 128 | + */ |
| 129 | + public function dirCodeDataProvider(): array |
| 130 | + { |
| 131 | + return [ |
| 132 | + 'media destination' => [DirectoryList::MEDIA], |
| 133 | + 'non-media destination' => [DirectoryList::VAR_DIR], |
| 134 | + ]; |
| 135 | + } |
| 136 | + |
| 137 | + /** |
| 138 | + * @inheritdoc |
| 139 | + */ |
| 140 | + protected function tearDown(): void |
| 141 | + { |
| 142 | + parent::tearDown(); |
| 143 | + |
| 144 | + $tmpDir = 'tmp'; |
| 145 | + $mediaDirectory = $this->filesystem->getDirectoryWrite(DirectoryList::MEDIA); |
| 146 | + $mediaDirectory->delete($tmpDir); |
| 147 | + |
| 148 | + $logDirectory = $this->filesystem->getDirectoryWrite(DirectoryList::LOG); |
| 149 | + $logDirectory->delete($tmpDir); |
| 150 | + } |
| 151 | + |
| 152 | + /** |
| 153 | + * Create uploader instance for testing purposes. |
| 154 | + * |
| 155 | + * @param string $fileName |
| 156 | + * |
| 157 | + * @return Uploader |
| 158 | + */ |
| 159 | + private function createUploader(string $fileName): Uploader |
| 160 | + { |
| 161 | + $tmpDirectory = $this->filesystem->getDirectoryWrite(DirectoryList::SYS_TMP); |
| 162 | + |
| 163 | + $filePath = $tmpDirectory->getAbsolutePath($fileName); |
| 164 | + |
| 165 | + $tmpDirectory->writeFile($fileName, 'just a text'); |
| 166 | + |
| 167 | + $type = [ |
| 168 | + 'tmp_name' => $filePath, |
| 169 | + 'name' => $fileName, |
| 170 | + ]; |
| 171 | + |
| 172 | + return $this->uploaderFactory->create(['fileId' => $type]); |
| 173 | + } |
| 174 | +} |
0 commit comments