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