Skip to content

Commit 371a472

Browse files
committed
B2B-2028: [AWS S3] [Integration Tests]: Investigate Test Failures in CustomerCustomAttributes module
1 parent b6bad89 commit 371a472

File tree

3 files changed

+221
-125
lines changed

3 files changed

+221
-125
lines changed
Lines changed: 174 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,174 @@
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+
}

dev/tests/integration/testsuite/Magento/Framework/File/UploaderTest.php

Lines changed: 47 additions & 125 deletions
Original file line numberDiff line numberDiff line change
@@ -7,169 +7,91 @@
77

88
namespace Magento\Framework\File;
99

10-
use Magento\Customer\Model\FileProcessor;
1110
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;
1314

1415
/**
1516
* Test for \Magento\Framework\File\Uploader
1617
*/
17-
class UploaderTest extends \PHPUnit\Framework\TestCase
18+
class UploaderTest extends TestCase
1819
{
1920
/**
20-
* @var \Magento\MediaStorage\Model\File\UploaderFactory
21+
* @var UploaderFactory
2122
*/
2223
private $uploaderFactory;
2324

2425
/**
25-
* @var \Magento\Framework\Filesystem
26+
* @var Filesystem\File\WriteInterface
2627
*/
27-
private $filesystem;
28+
private $mediaDirectory;
2829

2930
/**
30-
* @inheritdoc
31+
* @inheritDoc
3132
*/
3233
protected function setUp(): void
3334
{
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);
3939
}
4040

4141
/**
42-
* @return void
42+
* @inheritDoc
4343
*/
44-
public function testUploadFileFromAllowedFolder(): void
44+
public function tearDown(): void
4545
{
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');
5347
}
5448

5549
/**
56-
* @return void
50+
* @dataProvider uploadDataProvider
51+
* @throws \Magento\Framework\Exception\FileSystemException
5752
*/
58-
public function testUploadFileFromNotAllowedFolder(): void
53+
public function testUpload(string $expectedFile, ?string $newImageName = null): void
5954
{
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,
7369
];
7470

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);
8574

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);
8976

90-
$uploader->save($longDirectoryFolderName);
77+
self::assertEquals($newImageName ?? 'magento.jpg', $uploader->getUploadedFileName());
78+
self::assertTrue($this->mediaDirectory->isExist($expectedFile));
9179
}
9280

9381
/**
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-
*
12882
* @return array
12983
*/
130-
public function dirCodeDataProvider(): array
84+
public function uploadDataProvider(): array
13185
{
13286
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+
]
13595
];
13696
}
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-
}
17597
}
Loading

0 commit comments

Comments
 (0)