|
| 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\MediaGalleryMetadata\Test\Integration\Model\Jpeg\Segment; |
| 9 | + |
| 10 | +use Magento\Framework\App\Filesystem\DirectoryList; |
| 11 | +use Magento\Framework\Exception\LocalizedException; |
| 12 | +use Magento\Framework\Filesystem; |
| 13 | +use Magento\Framework\Filesystem\DriverInterface; |
| 14 | +use Magento\TestFramework\Helper\Bootstrap; |
| 15 | +use PHPUnit\Framework\TestCase; |
| 16 | +use Magento\MediaGalleryMetadata\Model\Jpeg\ReadFile; |
| 17 | +use Magento\MediaGalleryMetadata\Model\MetadataFactory; |
| 18 | + |
| 19 | +/** |
| 20 | + * Test for EXIF reader |
| 21 | + */ |
| 22 | +class ExifTest extends TestCase |
| 23 | +{ |
| 24 | + /** |
| 25 | + * @var WriteIptc |
| 26 | + */ |
| 27 | + private $iptcWriter; |
| 28 | + |
| 29 | + /** |
| 30 | + * @var ReadIptc |
| 31 | + */ |
| 32 | + private $iptcReader; |
| 33 | + |
| 34 | + /** |
| 35 | + * @var DriverInterface |
| 36 | + */ |
| 37 | + private $driver; |
| 38 | + |
| 39 | + /** |
| 40 | + * @var ReadFile |
| 41 | + */ |
| 42 | + private $fileReader; |
| 43 | + |
| 44 | + /** |
| 45 | + * @var MetadataFactory |
| 46 | + */ |
| 47 | + private $metadataFactory; |
| 48 | + |
| 49 | + /** |
| 50 | + * @var WriteInterface |
| 51 | + */ |
| 52 | + private $varDirectory; |
| 53 | + |
| 54 | + /** |
| 55 | + * @inheritdoc |
| 56 | + */ |
| 57 | + protected function setUp(): void |
| 58 | + { |
| 59 | + $this->varDirectory = Bootstrap::getObjectManager()->get(Filesystem::class) |
| 60 | + ->getDirectoryWrite(DirectoryList::VAR_DIR); |
| 61 | + $this->iptcWriter = Bootstrap::getObjectManager()->get(WriteIptc::class); |
| 62 | + $this->iptcReader = Bootstrap::getObjectManager()->get(ReadIptc::class); |
| 63 | + $this->fileReader = Bootstrap::getObjectManager()->get(ReadFile::class); |
| 64 | + $this->driver = Bootstrap::getObjectManager()->get(DriverInterface::class); |
| 65 | + $this->metadataFactory = Bootstrap::getObjectManager()->get(MetadataFactory::class); |
| 66 | + } |
| 67 | + |
| 68 | + /** |
| 69 | + * Test for IPTC reader and writer |
| 70 | + * |
| 71 | + * @dataProvider filesProvider |
| 72 | + * @param string $fileName |
| 73 | + * @param string $title |
| 74 | + * @param string $description |
| 75 | + * @param array $keywords |
| 76 | + * @throws LocalizedException |
| 77 | + */ |
| 78 | + public function testWriteRead( |
| 79 | + string $fileName, |
| 80 | + string $title, |
| 81 | + string $description, |
| 82 | + array $keywords |
| 83 | + ): void { |
| 84 | + $path = realpath(__DIR__ . '/../../../../_files/' . $fileName); |
| 85 | + $modifiableFilePath = $this->varDirectory->getAbsolutePath($fileName); |
| 86 | + $this->driver->copy( |
| 87 | + $path, |
| 88 | + $modifiableFilePath |
| 89 | + ); |
| 90 | + $modifiableFilePath = $this->fileReader->execute($modifiableFilePath); |
| 91 | + $originalMetadata = $this->iptcReader->execute($modifiableFilePath); |
| 92 | + |
| 93 | + $this->assertEmpty($originalMetadata->getTitle()); |
| 94 | + $this->assertEmpty($originalMetadata->getDescription()); |
| 95 | + $this->assertEmpty($originalMetadata->getKeywords()); |
| 96 | + |
| 97 | + $updatedFile = $this->iptcWriter->execute( |
| 98 | + $modifiableFilePath, |
| 99 | + $this->metadataFactory->create([ |
| 100 | + 'title' => $title, |
| 101 | + 'description' => $description, |
| 102 | + 'keywords' => $keywords |
| 103 | + ]) |
| 104 | + ); |
| 105 | + |
| 106 | + $updatedMetadata = $this->iptcReader->execute($updatedFile); |
| 107 | + |
| 108 | + $this->assertEquals($title, $updatedMetadata->getTitle()); |
| 109 | + $this->assertEquals($description, $updatedMetadata->getDescription()); |
| 110 | + $this->assertEquals($keywords, $updatedMetadata->getKeywords()); |
| 111 | + } |
| 112 | + |
| 113 | + /** |
| 114 | + * Data provider for testExecute |
| 115 | + * |
| 116 | + * @return array[] |
| 117 | + */ |
| 118 | + public function filesProvider(): array |
| 119 | + { |
| 120 | + return [ |
| 121 | + [ |
| 122 | + 'empty_iptc.jpeg', |
| 123 | + 'Updated Title', |
| 124 | + 'Updated Description', |
| 125 | + [ |
| 126 | + 'magento2', |
| 127 | + 'mediagallery' |
| 128 | + ] |
| 129 | + ] |
| 130 | + ]; |
| 131 | + } |
| 132 | +} |
0 commit comments