Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added metadata integration tests #1560

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 26 additions & 10 deletions MediaGalleryMetadata/Model/Jpeg/Segment/XmpWriter.php
Original file line number Diff line number Diff line change
Expand Up @@ -73,18 +73,22 @@ public function __construct(
public function execute(FileInterface $file, MetadataInterface $metadata): FileInterface
{
$segments = $file->getSegments();
$xmpSegment = [];
$xmpSegments = [];
foreach ($segments as $key => $segment) {
if ($this->isSegmentXmp($segment)) {
$xmpSegment[$key] = $segment;
$xmpSegments[$key] = $segment;
}
}
if (!empty($xmpSegment)) {
foreach ($xmpSegment as $key => $segment) {
$segments[$key] = $this->updateSegment($segment, $metadata);
}
} else {
$segments[] = $this->writeSegment($metadata);

if (empty($xmpSegments)) {
return $this->fileFactory->create([
'path' => $file->getPath(),
'segments' => $this->insertXmpSegment($segments, $this->createXmpSegment($metadata))
]);
}

foreach ($xmpSegments as $key => $segment) {
$segments[$key] = $this->updateSegment($segment, $metadata);
}

return $this->fileFactory->create([
Expand All @@ -93,21 +97,33 @@ public function execute(FileInterface $file, MetadataInterface $metadata): FileI
]);
}

/**
* Insert XMP segment to image segments (at position 1)
*
* @param SegmentInterface[] $segments
* @param SegmentInterface $xmpSegment
* @return SegmentInterface[]
*/
private function insertXmpSegment(array $segments, SegmentInterface $xmpSegment): array
{
return array_merge(array_slice($segments, 0, 2), [$xmpSegment], array_slice($segments, 2));
}

/**
* Write new segment metadata
*
* @param MetadataInterface $metadata
* @return SegmentInterface
*/
public function writeSegment(MetadataInterface $metadata): SegmentInterface
public function createXmpSegment(MetadataInterface $metadata): SegmentInterface
{
$xmpData = $this->xmpTemplate->get();
return $this->segmentFactory->create([
'name' => self::XMP_SEGMENT_NAME,
'data' => self::XMP_SEGMENT_START . $this->addXmpMetadata->execute($xmpData, $metadata)
]);
}

/**
* Add metadata to the segment
*
Expand Down
149 changes: 149 additions & 0 deletions MediaGalleryMetadata/Test/Integration/Model/AddMetadataTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,149 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);

namespace Magento\MediaGalleryMetadata\Test\Integration\Model;

use Magento\Framework\App\Filesystem\DirectoryList;
use Magento\Framework\Exception\LocalizedException;
use Magento\Framework\Filesystem;
use Magento\Framework\Filesystem\Directory\WriteInterface;
use Magento\Framework\Filesystem\DriverInterface;
use Magento\MediaGalleryMetadataApi\Api\AddMetadataInterface;
use Magento\MediaGalleryMetadataApi\Api\Data\MetadataInterfaceFactory;
use Magento\MediaGalleryMetadataApi\Api\ExtractMetadataInterface;
use Magento\TestFramework\Helper\Bootstrap;
use PHPUnit\Framework\TestCase;

/**
* ExtractMetadata test
*/
class AddMetadataTest extends TestCase
{
/**
* @var ExtractMetadataInterface
*/
private $addMetadata;

/**
* @var WriteInterface
*/
private $varDirectory;

/**
* @var DriverInterface
*/
private $driver;

/**
* @var MetadataInterfaceFactory
*/
private $metadataFactory;

/**
* @var ExtractMetadataInterface
*/
private $extractMetadata;

/**
* @inheritdoc
*/
protected function setUp(): void
{
$this->addMetadata = Bootstrap::getObjectManager()->get(AddMetadataInterface::class);
$this->varDirectory = Bootstrap::getObjectManager()->get(Filesystem::class)
->getDirectoryWrite(DirectoryList::VAR_DIR);
$this->driver = Bootstrap::getObjectManager()->get(DriverInterface::class);
$this->metadataFactory = Bootstrap::getObjectManager()->get(MetadataInterfaceFactory::class);
$this->extractMetadata = Bootstrap::getObjectManager()->get(ExtractMetadataInterface::class);
}

/**
* Test for ExtractMetadata::execute
*
* @dataProvider filesProvider
* @param string $fileName
* @param string $title
* @param string $description
* @param array $keywords
* @throws LocalizedException
*/
public function testExecute(
string $fileName,
string $title,
string $description,
array $keywords
): void {
$path = realpath(__DIR__ . '/../../_files/' . $fileName);
$modifiableFilePath = $this->varDirectory->getAbsolutePath($fileName);
$this->driver->copy(
$path,
$modifiableFilePath
);
$metadata = $this->metadataFactory->create([
'title' => $title,
'description' => $description,
'keywords' => $keywords
]);

$this->addMetadata->execute($modifiableFilePath, $metadata);

$updatedMetadata = $this->extractMetadata->execute($modifiableFilePath);

$this->assertEquals($title, $updatedMetadata->getTitle());
$this->assertEquals($description, $updatedMetadata->getDescription());
$this->assertEquals($keywords, $updatedMetadata->getKeywords());

$this->driver->deleteFile($modifiableFilePath);
}

/**
* Data provider for testExecute
*
* @return array[]
*/
public function filesProvider(): array
{
return [
[
'macos-photos.jpeg',
'Updated Title',
'Updated Description',
[
'magento2',
'mediagallery'
]
],
[
'iptc_only.jpeg',
'Updated Title',
'Updated Description',
[
'magento2',
'mediagallery'
]
],
[
'macos-preview.png',
'Title of the magento image 2',
'Description of the magento image 2',
[
'magento2',
'community'
]
],
[
'empty_xmp_image.jpeg',
'Title of the magento image',
'Description of the magento image 2',
[
'magento2',
'community'
]
]
];
}
}
103 changes: 103 additions & 0 deletions MediaGalleryMetadata/Test/Integration/Model/ExtractMetadataTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);

namespace Magento\MediaGalleryMetadata\Test\Integration\Model;

use Magento\Framework\Exception\LocalizedException;
use Magento\MediaGalleryMetadataApi\Api\ExtractMetadataInterface;
use Magento\TestFramework\Helper\Bootstrap;
use PHPUnit\Framework\TestCase;

/**
* Test for ExtractMetadata
*/
class ExtractMetadataTest extends TestCase
{
/**
* @var ExtractMetadataInterface
*/
private $extractMetadata;

/**
* @inheritdoc
*/
protected function setUp(): void
{
$this->extractMetadata = Bootstrap::getObjectManager()->get(ExtractMetadataInterface::class);
}

/**
* Test for ExtractMetadata::execute
*
* @dataProvider filesProvider
* @param string $fileName
* @param string $title
* @param string $description
* @param array $keywords
* @throws LocalizedException
*/
public function testExecute(
string $fileName,
string $title,
string $description,
array $keywords
): void {
$path = realpath(__DIR__ . '/../../_files/' . $fileName);
$metadata = $this->extractMetadata->execute($path);

$this->assertEquals($title, $metadata->getTitle());
$this->assertEquals($description, $metadata->getDescription());
$this->assertEquals($keywords, $metadata->getKeywords());
}

/**
* Data provider for testExecute
*
* @return array[]
*/
public function filesProvider(): array
{
return [
[
'macos-photos.jpeg',
'Title of the magento image',
'Description of the magento image',
[
'magento',
'mediagallerymetadata'
]
],
[
'macos-preview.png',
'Title of the magento image',
'Description of the magento image',
[
'magento',
'mediagallerymetadata'
]
],
[
'iptc_only.jpeg',
'Title of the magento image',
'Description of the magento image',
[
'magento',
'mediagallerymetadata'
]
],
[
'exiftool.gif',
'Title of the magento image',
'Description of the magento image',
[
'magento',
'mediagallerymetadata'
]
]
];
}
}
Loading