Skip to content

Commit 6818648

Browse files
authored
Merge pull request #7193 from magento-arcticfoxes/B2B-2024
B2B-2024: [AWS S3] [Integration Tests]: Investigate Test Failures in cms module
2 parents a0a1cd3 + ab23bde commit 6818648

File tree

12 files changed

+172
-88
lines changed

12 files changed

+172
-88
lines changed

app/code/Magento/Cms/Model/Wysiwyg/Images/Storage.php

Lines changed: 4 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -77,8 +77,6 @@ class Storage extends \Magento\Framework\DataObject
7777
protected $_coreFileStorageDb = null;
7878

7979
/**
80-
* Cms wysiwyg images
81-
*
8280
* @var \Magento\Cms\Helper\Wysiwyg\Images
8381
*/
8482
protected $_cmsWysiwygImages = null;
@@ -109,36 +107,26 @@ class Storage extends \Magento\Framework\DataObject
109107
protected $_session;
110108

111109
/**
112-
* Directory database factory
113-
*
114110
* @var \Magento\MediaStorage\Model\File\Storage\Directory\DatabaseFactory
115111
*/
116112
protected $_directoryDatabaseFactory;
117113

118114
/**
119-
* Storage database factory
120-
*
121115
* @var \Magento\MediaStorage\Model\File\Storage\DatabaseFactory
122116
*/
123117
protected $_storageDatabaseFactory;
124118

125119
/**
126-
* Storage file factory
127-
*
128120
* @var \Magento\MediaStorage\Model\File\Storage\FileFactory
129121
*/
130122
protected $_storageFileFactory;
131123

132124
/**
133-
* Storage collection factory
134-
*
135125
* @var \Magento\Cms\Model\Wysiwyg\Images\Storage\CollectionFactory
136126
*/
137127
protected $_storageCollectionFactory;
138128

139129
/**
140-
* Uploader factory
141-
*
142130
* @var \Magento\MediaStorage\Model\File\UploaderFactory
143131
*/
144132
protected $_uploaderFactory;
@@ -745,8 +733,8 @@ private function getResizedParams(string $source): array
745733
$configWidth = $this->_resizeParameters['width'];
746734
$configHeight = $this->_resizeParameters['height'];
747735

748-
//phpcs:ignore Generic.PHP.NoSilencedErrors
749-
[$imageWidth, $imageHeight] = @getimagesize($source);
736+
$driver = $this->_directory->getDriver();
737+
[$imageWidth, $imageHeight] = getimagesizefromstring($driver->fileGetContents($source));
750738

751739
if ($imageWidth && $imageHeight) {
752740
$imageWidth = $configWidth > $imageWidth ? $imageWidth : $configWidth;
@@ -999,7 +987,7 @@ private function getAllowedPathPattern()
999987
);
1000988
$regExp = '/^(';
1001989
$or = '';
1002-
foreach($mediaGalleryImageFolders as $folder) {
990+
foreach ($mediaGalleryImageFolders as $folder) {
1003991
$folderPattern = str_replace('/', '[\/]+', $folder);
1004992
$regExp .= $or . $folderPattern . '\b(?!-)(?:\/?[a-zA-Z0-9\-\_]+)*\/?$';
1005993
$or = '|';
@@ -1013,7 +1001,7 @@ private function getAllowedPathPattern()
10131001
/**
10141002
* Get allowed media gallery image folders
10151003
*
1016-
* example:
1004+
* Example:
10171005
* [
10181006
* [0 => 'wysiwyg'],
10191007
* [0 => 'catalog', 1 => 'category']

app/code/Magento/Cms/Test/Unit/Model/Wysiwyg/Images/StorageTest.php

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,9 @@ class StorageTest extends TestCase
141141
*/
142142
private $fileMock;
143143

144+
/**
145+
* @var array
146+
*/
144147
private $allowedImageExtensions = [
145148
'jpg' => 'image/jpg',
146149
'jpeg' => 'image/jpeg',
@@ -366,13 +369,13 @@ public function testGetDirsCollectionCreateSubDirectories()
366369
}
367370

368371
/**
369-
* @param array $exclude
370-
* @param array $include
371-
* @param array $fileNames
372-
* @param array $expectedRemoveKeys
372+
* @param $path
373+
* @param $callNum
374+
* @param string $dirsFilter
375+
* @throws \Exception
373376
* @dataProvider dirsCollectionDataProvider
374377
*/
375-
public function testGetDirsCollection($path, $callNum, $dirsFilter='')
378+
public function testGetDirsCollection($path, $callNum, $dirsFilter = '')
376379
{
377380
$this->generalTestGetDirsCollection($path, $callNum, $dirsFilter);
378381
}
@@ -517,6 +520,9 @@ public function testUploadFile()
517520
[$thumbnailTargetPath, true],
518521
]
519522
);
523+
$this->driverMock->expects(self::once())
524+
->method('fileGetContents')
525+
->willReturn('some content');
520526

521527
$image = $this->getMockBuilder(Image::class)
522528
->disableOriginalConstructor()

app/code/Magento/RemoteStorage/Model/Filesystem/Directory/WriteFactory.php

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,6 @@
1919
class WriteFactory extends BaseWriteFactory
2020
{
2121
/**
22-
* Object Manager
23-
*
2422
* @var ObjectManagerInterface
2523
*/
2624
private $objectManager;
@@ -32,19 +30,29 @@ class WriteFactory extends BaseWriteFactory
3230
*/
3331
private $driverPool;
3432

33+
/**
34+
* Deny List Validator
35+
*
36+
* @var DenyListPathValidator
37+
*/
38+
private $denyListPathValidator;
39+
3540
/**
3641
* WriteFactory constructor.
3742
*
3843
* @param ObjectManagerInterface $objectManager
3944
* @param BaseDriverPool $driverPool
45+
* @param DenyListPathValidator|null $denyListPathValidator
4046
*/
4147
public function __construct(
4248
ObjectManagerInterface $objectManager,
43-
BaseDriverPool $driverPool
49+
BaseDriverPool $driverPool,
50+
?DenyListPathValidator $denyListPathValidator = null
4451
) {
4552
$this->objectManager = $objectManager;
4653
$this->driverPool = $driverPool;
47-
parent::__construct($driverPool);
54+
$this->denyListPathValidator = $denyListPathValidator;
55+
parent::__construct($driverPool, $denyListPathValidator);
4856
}
4957

5058
/**
@@ -64,7 +72,7 @@ public function create(
6472

6573
$validators = [
6674
'pathValidator' => new PathValidator($driver),
67-
'denyListPathValidator' => new DenyListPathValidator($driver)
75+
'denyListPathValidator' => $this->denyListPathValidator ?: new DenyListPathValidator($driver)
6876
];
6977

7078
$pathValidator = new CompositePathValidator($validators);
@@ -82,6 +90,5 @@ public function create(
8290
} else {
8391
return parent::create($path, $driverCode, $createPermissions);
8492
}
85-
8693
}
8794
}

dev/tests/integration/testsuite/Magento/AdvancedPricingImportExport/Model/Export/AdvancedPricingTest.php

Lines changed: 43 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -6,19 +6,19 @@
66

77
namespace Magento\AdvancedPricingImportExport\Model\Export;
88

9-
use Magento\Framework\App\Filesystem\DirectoryList;
10-
use Magento\Framework\Exception\NoSuchEntityException;
11-
use Magento\Framework\File\Csv;
12-
use Magento\TestFramework\Indexer\TestCase;
13-
use Magento\TestFramework\Helper\Bootstrap;
14-
use Magento\Framework\Filesystem;
159
use Magento\AdvancedPricingImportExport\Model\Export\AdvancedPricing as ExportAdvancedPricing;
10+
use Magento\AdvancedPricingImportExport\Model\Import\AdvancedPricing as ImportAdvancedPricing;
1611
use Magento\Catalog\Api\ProductRepositoryInterface;
1712
use Magento\Catalog\Model\Product;
13+
use Magento\Framework\App\Filesystem\DirectoryList;
14+
use Magento\Framework\Exception\NoSuchEntityException;
15+
use Magento\Framework\Filesystem;
16+
use Magento\Framework\Filesystem\Directory\Write;
1817
use Magento\ImportExport\Model\Export\Adapter\Csv as ExportAdapterCsv;
19-
use Magento\AdvancedPricingImportExport\Model\Import\AdvancedPricing as ImportAdvancedPricing;
20-
use Magento\ImportExport\Model\Import\Source\Csv as ImportSourceCsv;
2118
use Magento\ImportExport\Model\Import;
19+
use Magento\ImportExport\Model\Import\Source\Csv as ImportSourceCsv;
20+
use Magento\TestFramework\Helper\Bootstrap;
21+
use Magento\TestFramework\Indexer\TestCase;
2222

2323
/**
2424
* Test for \Magento\AdvancedPricingImportExport\Model\Export\AdvancedPricing
@@ -41,6 +41,11 @@ class AdvancedPricingTest extends TestCase
4141
*/
4242
protected $fileSystem;
4343

44+
/**
45+
* @var Write
46+
*/
47+
private $directory;
48+
4449
// @codingStandardsIgnoreStart
4550
public static function setUpBeforeClass(): void
4651
{
@@ -64,6 +69,7 @@ protected function setUp(): void
6469

6570
$this->objectManager = Bootstrap::getObjectManager();
6671
$this->fileSystem = $this->objectManager->get(Filesystem::class);
72+
$this->directory = $this->fileSystem->getDirectoryWrite(DirectoryList::VAR_IMPORT_EXPORT);
6773
$this->model = $this->objectManager->create(ExportAdvancedPricing::class);
6874
}
6975

@@ -179,7 +185,7 @@ public function testExportImportOfAdvancedPricing(): void
179185
{
180186
$simpleSku = 'simple';
181187
$secondSimpleSku = 'second_simple';
182-
$csvfile = uniqid('importexport_') . '.csv';
188+
$csvfile = $this->directory->getAbsolutePath(uniqid('importexport_') . '.csv');
183189
$exportContent = $this->exportData($csvfile);
184190
$this->assertStringContainsString(
185191
\sprintf('%s,"All Websites [USD]","ALL GROUPS",10.0000,3.00,Discount', $secondSimpleSku),
@@ -269,10 +275,7 @@ private function updateTierPriceDataInCsv(string $csvfile): void
269275
],
270276
];
271277

272-
/** @var Csv $csv */
273-
$csv = $this->objectManager->get(Csv::class);
274-
$varDirectory = $this->fileSystem->getDirectoryWrite(DirectoryList::VAR_DIR);
275-
$csv->appendData($varDirectory->getAbsolutePath($csvfile), $csvNewData);
278+
$this->updateCsvFile($csvfile, $csvNewData);
276279
}
277280

278281
/**
@@ -281,16 +284,15 @@ private function updateTierPriceDataInCsv(string $csvfile): void
281284
*/
282285
private function exportData($csvFile)
283286
{
284-
$this->model->setWriter(
285-
Bootstrap::getObjectManager()
286-
->create(
287-
ExportAdapterCsv::class,
288-
['fileSystem' => $this->fileSystem, 'destination' => $csvFile]
289-
)
290-
);
287+
$writer = Bootstrap::getObjectManager()->create(ExportAdapterCsv::class, ['fileSystem' => $this->fileSystem]);
288+
289+
$this->model->setWriter($writer);
291290
$exportContent = $this->model->export();
292291
$this->assertNotEmpty($exportContent);
293292

293+
$driver = $this->directory->getDriver();
294+
$driver->filePutContents($this->directory->getAbsolutePath($csvFile), $exportContent);
295+
294296
return $exportContent;
295297
}
296298

@@ -301,12 +303,11 @@ private function importData($csvFile)
301303
{
302304
/** @var ImportAdvancedPricing $importModel */
303305
$importModel = $this->objectManager->create(ImportAdvancedPricing::class);
304-
$directory = $this->fileSystem->getDirectoryWrite(DirectoryList::VAR_DIR);
305306
$source = $this->objectManager->create(
306307
ImportSourceCsv::class,
307308
[
308309
'file' => $csvFile,
309-
'directory' => $directory
310+
'directory' => $this->directory
310311
]
311312
);
312313
$errors = $importModel->setParameters(
@@ -366,4 +367,24 @@ private function removeImportedProducts(array $skus): void
366367
$registry->unregister('isSecureArea');
367368
$registry->register('isSecureArea', false);
368369
}
370+
371+
/**
372+
* Appends csv data to the file
373+
*
374+
* @param string $filePath
375+
* @param array $csv
376+
* @return void
377+
*/
378+
private function updateCsvFile(string $filePath, array $csv): void
379+
{
380+
$driver = $this->directory->getDriver();
381+
$driver->deleteFile($filePath);
382+
$fileResource = $driver->fileOpen($filePath, 'w');
383+
384+
foreach ($csv as $dataRow) {
385+
$driver->filePutCsv($fileResource, $dataRow);
386+
}
387+
388+
$driver->fileClose($fileResource);
389+
}
369390
}

dev/tests/integration/testsuite/Magento/AdvancedPricingImportExport/Model/Import/AdvancedPricingTest.php

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -175,6 +175,7 @@ public function testImportDelete()
175175
$index = 0;
176176
$ids = [];
177177
$origPricingData = [];
178+
$skus = ['simple'];
178179
while (isset($skus[$index])) {
179180
$ids[$index] = $productRepository->get($skus[$index])->getId();
180181
$origPricingData[$index] = $this->objectManager->create(\Magento\Catalog\Model\Product::class)
@@ -192,12 +193,16 @@ public function testImportDelete()
192193
$exportModel->setWriter(
193194
\Magento\TestFramework\Helper\Bootstrap::getObjectManager()->create(
194195
\Magento\ImportExport\Model\Export\Adapter\Csv::class,
195-
['fileSystem' => $this->fileSystem, 'destination' => $csvfile]
196+
['fileSystem' => $this->fileSystem]
196197
)
197198
);
198-
$this->assertNotEmpty($exportModel->export());
199+
$exportContent = $exportModel->export();
200+
$this->assertNotEmpty($exportContent);
199201

200-
$errors = $this->doImport($csvfile, DirectoryList::VAR_DIR, Import::BEHAVIOR_DELETE, true);
202+
$directory = $this->fileSystem->getDirectoryWrite(DirectoryList::VAR_IMPORT_EXPORT);
203+
$directory->getDriver()->filePutContents($directory->getAbsolutePath($csvfile), $exportContent);
204+
205+
$errors = $this->doImport($csvfile, DirectoryList::VAR_IMPORT_EXPORT, Import::BEHAVIOR_DELETE, true);
201206

202207
$this->assertTrue(
203208
$errors->getErrorsCount() == 0,

0 commit comments

Comments
 (0)