Skip to content

Commit 6104113

Browse files
authored
Merge pull request #7167 from magento-arcticfoxes/B2B-2028
B2B-2028: [AWS S3] [Integration Tests]: Investigate Test Failures in CustomerCustomAttributes module
2 parents 227cf92 + 3932620 commit 6104113

File tree

5 files changed

+313
-125
lines changed

5 files changed

+313
-125
lines changed
Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
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\RemoteStorage\Model\File;
9+
10+
use Magento\Framework\App\Filesystem\DirectoryList;
11+
use Magento\Framework\App\ObjectManager;
12+
use Magento\Framework\Exception\FileSystemException;
13+
use Magento\Framework\File\Mime;
14+
use Magento\Framework\Filesystem;
15+
use Magento\Framework\Filesystem\Directory\TargetDirectory;
16+
use Magento\Framework\Filesystem\DriverPool;
17+
use Magento\RemoteStorage\Model\Config;
18+
19+
/**
20+
* Uploader class for cases when remote storage is enabled and the uploaded file is located on remote storage
21+
*/
22+
class Uploader extends \Magento\Framework\File\Uploader
23+
{
24+
/**
25+
* @var Filesystem\Directory\WriteInterface
26+
*/
27+
private $tmpDirectoryWrite;
28+
29+
/**
30+
* @var Filesystem\Directory\WriteInterface
31+
*/
32+
private $remoteDirectoryWrite;
33+
34+
/**
35+
* Copies file to the tmp directory if remote storage is enabled and tmp file is located on remote storage
36+
*
37+
* @param string|array $fileId
38+
* @param Mime|null $fileMime
39+
* @param DirectoryList|null $directoryList
40+
* @param DriverPool|null $driverPool
41+
* @param TargetDirectory|null $targetDirectory
42+
* @param Filesystem|null $filesystem
43+
* @param Config|null $config
44+
* @throws \DomainException
45+
*/
46+
public function __construct(
47+
$fileId,
48+
Mime $fileMime = null,
49+
DirectoryList $directoryList = null,
50+
DriverPool $driverPool = null,
51+
TargetDirectory $targetDirectory = null,
52+
Filesystem $filesystem = null,
53+
Config $config = null
54+
) {
55+
$targetDirectory = $targetDirectory ?: ObjectManager::getInstance()->get(TargetDirectory::class);
56+
$filesystem = $filesystem ?: ObjectManager::getInstance()->get(FileSystem::class);
57+
$config = $config ?: ObjectManager::getInstance()->get(Config::class);
58+
59+
if ($config->isEnabled() && isset($fileId['tmp_name'])) {
60+
$this->tmpDirectoryWrite = $filesystem->getDirectoryWrite(DirectoryList::TMP);
61+
$this->remoteDirectoryWrite = $targetDirectory->getDirectoryWrite(DirectoryList::ROOT);
62+
63+
$fileId['tmp_name'] = $this->copyFileToTmp($fileId['tmp_name']);
64+
}
65+
66+
parent::__construct($fileId, $fileMime, $directoryList, $driverPool, $targetDirectory, $filesystem);
67+
}
68+
69+
/**
70+
* Moves file from the remote storage to the tmp folder
71+
*
72+
* @param string $filePath
73+
* @return string
74+
* @throws FileSystemException
75+
*/
76+
private function copyFileToTmp(string $filePath): string
77+
{
78+
$absolutePath = $this->remoteDirectoryWrite->getAbsolutePath($filePath);
79+
if ($this->remoteDirectoryWrite->isFile($absolutePath)) {
80+
$this->tmpDirectoryWrite->create();
81+
// phpcs:ignore Magento2.Functions.DiscouragedFunction
82+
$tmpPath = $this->tmpDirectoryWrite->getAbsolutePath() . basename($filePath);
83+
$content = $this->remoteDirectoryWrite->getDriver()->fileGetContents($filePath);
84+
$this->tmpDirectoryWrite->getDriver()->filePutContents($tmpPath, $content);
85+
86+
return $tmpPath;
87+
}
88+
89+
return $filePath;
90+
}
91+
}

app/code/Magento/RemoteStorage/etc/di.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
<preference for="Magento\RemoteStorage\Driver\Adapter\MetadataProviderInterface" type="Magento\RemoteStorage\Driver\Adapter\MetadataProvider"/>
1212
<preference for="Magento\RemoteStorage\Driver\Adapter\MetadataProviderFactoryInterface" type="Magento\RemoteStorage\Driver\Adapter\MetadataProviderFactory"/>
1313
<preference for="Magento\Framework\Filesystem\DriverPool" type="Magento\RemoteStorage\Driver\DriverPool"/>
14+
<preference for="Magento\Framework\File\Uploader" type="Magento\RemoteStorage\Model\File\Uploader"/>
1415
<virtualType name="remoteWriteFactory" type="Magento\Framework\Filesystem\Directory\WriteFactory">
1516
<arguments>
1617
<argument name="driverPool" xsi:type="object">Magento\RemoteStorage\Driver\DriverPool</argument>
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+
}

0 commit comments

Comments
 (0)