Skip to content

Commit 8f882b8

Browse files
committed
B2B-2037: [AWS S3] [Integration Tests]: Investigate Test Failures in MediaGallerySynchronization & MediaGallerySynchronizationMetadata modules
1 parent 2aa205d commit 8f882b8

File tree

9 files changed

+80
-22
lines changed

9 files changed

+80
-22
lines changed

app/code/Magento/MediaGalleryMetadata/Model/Png/ReadFile.php

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,6 @@ class ReadFile implements ReadFileInterface
5050
* @param FileInterfaceFactory $fileFactory
5151
* @param SegmentInterfaceFactory $segmentFactory
5252
* @param Filesystem $filesystem
53-
* @throws FileSystemException
5453
*/
5554
public function __construct(
5655
FileInterfaceFactory $fileFactory,
@@ -60,19 +59,18 @@ public function __construct(
6059
$this->fileFactory = $fileFactory;
6160
$this->segmentFactory = $segmentFactory;
6261
$this->filesystem = $filesystem;
63-
$this->driver = $this->filesystem->getDirectoryWrite(DirectoryList::MEDIA)->getDriver();
6462
}
6563

6664
/**
6765
* @inheritdoc
6866
*/
6967
public function execute(string $path): FileInterface
7068
{
71-
$resource = $this->driver->fileOpen($path, 'rb');
69+
$resource = $this->getDriver()->fileOpen($path, 'rb');
7270
$header = $this->readHeader($resource);
7371

7472
if ($header != self::PNG_FILE_START) {
75-
$this->driver->fileClose($resource);
73+
$this->getDriver()->fileClose($resource);
7674
throw new ValidatorException(__('Not a PNG image'));
7775
}
7876

@@ -92,10 +90,10 @@ public function execute(string $path): FileInterface
9290
}
9391
} while ($header
9492
&& $segmentHeader['type'] != self::PNG_MARKER_IMAGE_END
95-
&& !$this->driver->endOfFile($resource)
93+
&& !$this->getDriver()->endOfFile($resource)
9694
);
9795

98-
$this->driver->fileClose($resource);
96+
$this->getDriver()->fileClose($resource);
9997

10098
return $this->fileFactory->create([
10199
'path' => $path,
@@ -127,10 +125,24 @@ private function read($resource, int $length): string
127125
{
128126
$data = '';
129127

130-
while (!$this->driver->endOfFile($resource) && strlen($data) < $length) {
131-
$data .= $this->driver->fileRead($resource, $length - strlen($data));
128+
while (!$this->getDriver()->endOfFile($resource) && strlen($data) < $length) {
129+
$data .= $this->getDriver()->fileRead($resource, $length - strlen($data));
132130
}
133131

134132
return $data;
135133
}
134+
135+
136+
/**
137+
* @return DriverInterface
138+
* @throws FileSystemException
139+
*/
140+
private function getDriver(): DriverInterface
141+
{
142+
if ($this->driver === null) {
143+
$this->driver = $this->filesystem->getDirectoryWrite(DirectoryList::MEDIA)->getDriver();
144+
}
145+
146+
return $this->driver;
147+
}
136148
}

app/code/Magento/MediaGalleryMetadata/Test/Integration/Model/AddMetadataTest.php

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -48,10 +48,19 @@ class AddMetadataTest extends TestCase
4848
protected function setUp(): void
4949
{
5050
$this->addMetadata = Bootstrap::getObjectManager()->get(AddMetadataInterface::class);
51-
$this->directory = Bootstrap::getObjectManager()->get(FileSystem::class)
52-
->getDirectoryWrite(DirectoryList::MEDIA);
5351
$this->metadataFactory = Bootstrap::getObjectManager()->get(MetadataInterfaceFactory::class);
5452
$this->extractMetadata = Bootstrap::getObjectManager()->get(ExtractMetadataInterface::class);
53+
$this->directory = Bootstrap::getObjectManager()->get(FileSystem::class)
54+
->getDirectoryWrite(DirectoryList::MEDIA);
55+
$this->directory->create('testDir');
56+
}
57+
58+
/**
59+
* @inheritDoc
60+
*/
61+
protected function tearDown(): void
62+
{
63+
$this->directory->delete('testDir');
5564
}
5665

5766
/**
@@ -70,7 +79,7 @@ public function testExecute(
7079
?string $description,
7180
?array $keywords
7281
): void {
73-
$modifiableFilePath = $this->directory->getAbsolutePath($fileName);
82+
$modifiableFilePath = $this->directory->getAbsolutePath('testDir/' . $fileName);
7483
$driver = $this->directory->getDriver();
7584
$driver->filePutContents(
7685
$modifiableFilePath,

app/code/Magento/MediaGalleryMetadata/Test/Integration/Model/ExtractMetadataTest.php

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,10 @@
77

88
namespace Magento\MediaGalleryMetadata\Test\Integration\Model;
99

10+
use Magento\Framework\App\Filesystem\DirectoryList;
1011
use Magento\Framework\Exception\LocalizedException;
12+
use Magento\Framework\Filesystem;
13+
use Magento\Framework\Filesystem\Directory\WriteInterface;
1114
use Magento\MediaGalleryMetadataApi\Api\ExtractMetadataInterface;
1215
use Magento\TestFramework\Helper\Bootstrap;
1316
use PHPUnit\Framework\TestCase;
@@ -22,12 +25,28 @@ class ExtractMetadataTest extends TestCase
2225
*/
2326
private $extractMetadata;
2427

28+
/**
29+
* @var WriteInterface
30+
*/
31+
private $directory;
32+
2533
/**
2634
* @inheritdoc
2735
*/
2836
protected function setUp(): void
2937
{
3038
$this->extractMetadata = Bootstrap::getObjectManager()->get(ExtractMetadataInterface::class);
39+
$this->directory = Bootstrap::getObjectManager()->get(FileSystem::class)
40+
->getDirectoryWrite(DirectoryList::MEDIA);
41+
$this->directory->create('testDir');
42+
}
43+
44+
/**
45+
* @inheritDoc
46+
*/
47+
protected function tearDown(): void
48+
{
49+
$this->directory->delete('testDir');
3150
}
3251

3352
/**
@@ -46,7 +65,13 @@ public function testExecute(
4665
string $description,
4766
?array $keywords
4867
): void {
49-
$path = realpath(__DIR__ . '/../../_files/' . $fileName);
68+
$path = $this->directory->getAbsolutePath('testDir/' . $fileName);
69+
$driver = $this->directory->getDriver();
70+
$driver->filePutContents(
71+
$path,
72+
file_get_contents(__DIR__ . '/../../_files/' . $fileName)
73+
);
74+
5075
$metadata = $this->extractMetadata->execute($path);
5176

5277
$this->assertEquals($title, $metadata->getTitle());

app/code/Magento/MediaGalleryMetadata/Test/Integration/Model/Gif/Segment/XmpTest.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ protected function setUp(): void
5858
$this->directory = Bootstrap::getObjectManager()->get(FileSystem::class)
5959
->getDirectoryWrite(DirectoryList::MEDIA);
6060
$this->metadataFactory = Bootstrap::getObjectManager()->get(MetadataFactory::class);
61+
$this->directory->create('testDir');
6162
}
6263

6364
/**

app/code/Magento/MediaGalleryMetadata/Test/Integration/Model/Jpeg/Segment/IptcTest.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ protected function setUp(): void
5959
$this->iptcReader = Bootstrap::getObjectManager()->get(ReadIptc::class);
6060
$this->fileReader = Bootstrap::getObjectManager()->get(ReadFile::class);
6161
$this->metadataFactory = Bootstrap::getObjectManager()->get(MetadataFactory::class);
62+
$this->directory->create('testDir');
6263
}
6364

6465
/**

app/code/Magento/MediaGalleryMetadata/Test/Integration/Model/Jpeg/Segment/XmpTest.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ protected function setUp(): void
5959
$this->directory = Bootstrap::getObjectManager()->get(FileSystem::class)
6060
->getDirectoryWrite(DirectoryList::MEDIA);
6161
$this->metadataFactory = Bootstrap::getObjectManager()->get(MetadataFactory::class);
62+
$this->directory->create('testDir');
6263
}
6364

6465
/**

app/code/Magento/MediaGalleryMetadata/Test/Integration/Model/Png/Segment/IptcTest.php

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -51,20 +51,29 @@ class IptcTest extends TestCase
5151
/**
5252
* @var WriteInterface
5353
*/
54-
private $varDirectory;
54+
private $directory;
5555

5656
/**
5757
* @inheritdoc
5858
*/
5959
protected function setUp(): void
6060
{
61-
$this->varDirectory = Bootstrap::getObjectManager()->get(Filesystem::class)
62-
->getDirectoryWrite(DirectoryList::VAR_DIR);
61+
$this->directory = Bootstrap::getObjectManager()->get(FileSystem::class)
62+
->getDirectoryWrite(DirectoryList::MEDIA);
6363
$this->iptcWriter = Bootstrap::getObjectManager()->get(WriteIptc::class);
6464
$this->iptcReader = Bootstrap::getObjectManager()->get(ReadIptc::class);
6565
$this->fileReader = Bootstrap::getObjectManager()->get(ReadFile::class);
66-
$this->driver = Bootstrap::getObjectManager()->get(DriverInterface::class);
66+
$this->driver = $this->directory->getDriver();
6767
$this->metadataFactory = Bootstrap::getObjectManager()->get(MetadataFactory::class);
68+
$this->directory->create('testDir');
69+
}
70+
71+
/**
72+
* @inheritDoc
73+
*/
74+
protected function tearDown(): void
75+
{
76+
$this->directory->delete('testDir');
6877
}
6978

7079
/**
@@ -83,13 +92,12 @@ public function testWriteRead(
8392
string $description,
8493
array $keywords
8594
): void {
86-
$path = realpath(__DIR__ . '/../../../../_files/' . $fileName);
87-
$modifiableFilePath = $this->varDirectory->getAbsolutePath($fileName);
88-
$this->driver->copy(
95+
$path = $this->directory->getAbsolutePath('testDir/' . $fileName);
96+
$this->driver->filePutContents(
8997
$path,
90-
$modifiableFilePath
98+
file_get_contents(__DIR__ . '/../../../../_files/' . $fileName)
9199
);
92-
$modifiableFilePath = $this->fileReader->execute($modifiableFilePath);
100+
$modifiableFilePath = $this->fileReader->execute($path);
93101
$originalMetadata = $this->iptcReader->execute($modifiableFilePath);
94102

95103
$this->assertEmpty($originalMetadata->getTitle());

app/code/Magento/MediaGalleryMetadata/Test/Integration/Model/Png/Segment/XmpTest.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ protected function setUp(): void
6565
$this->xmpReader = Bootstrap::getObjectManager()->get(ReadXmp::class);
6666
$this->fileReader = Bootstrap::getObjectManager()->get(ReadFile::class);
6767
$this->metadataFactory = Bootstrap::getObjectManager()->get(MetadataFactory::class);
68+
$this->directory->create('testDir');
6869
}
6970

7071
/**

app/code/Magento/RemoteStorage/Driver/Adapter/Cache/Generic.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -194,7 +194,7 @@ public function getMetadata(string $path): ?array
194194
return null;
195195
}
196196
$meta = $this->serializer->unserialize($meta);
197-
if (!$meta[$path]) {
197+
if (!isset($meta[$path]) || !$meta[$path]) {
198198
return null;
199199
}
200200
$this->cacheData[$path] = $meta[$path];

0 commit comments

Comments
 (0)