|
| 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\MediaGallerySynchronization\Test\Integration\Model; |
| 9 | + |
| 10 | +use Magento\Framework\App\Filesystem\DirectoryList; |
| 11 | +use Magento\Framework\Exception\FileSystemException; |
| 12 | +use Magento\Framework\Filesystem; |
| 13 | +use Magento\Framework\Filesystem\Directory\WriteInterface; |
| 14 | +use Magento\Framework\Filesystem\DriverInterface; |
| 15 | +use Magento\MediaGalleryApi\Api\Data\AssetInterface; |
| 16 | +use Magento\MediaGalleryApi\Api\Data\KeywordInterface; |
| 17 | +use Magento\MediaGalleryApi\Api\GetAssetsByPathsInterface; |
| 18 | +use Magento\MediaGallerySynchronizationApi\Api\SynchronizeFilesInterface; |
| 19 | +use Magento\MediaGalleryApi\Api\GetAssetsKeywordsInterface; |
| 20 | +use Magento\TestFramework\Helper\Bootstrap; |
| 21 | +use PHPUnit\Framework\TestCase; |
| 22 | + |
| 23 | +/** |
| 24 | + * Test for SynchronizeFiles. |
| 25 | + */ |
| 26 | +class SynchronizeFilesTest extends TestCase |
| 27 | +{ |
| 28 | + /** |
| 29 | + * @var DriverInterface |
| 30 | + */ |
| 31 | + private $driver; |
| 32 | + |
| 33 | + /** |
| 34 | + * @var SynchronizeFilesInterface |
| 35 | + */ |
| 36 | + private $synchronizeFiles; |
| 37 | + |
| 38 | + /** |
| 39 | + * @var GetAssetsByPathsInterface |
| 40 | + */ |
| 41 | + private $getAssetsByPath; |
| 42 | + |
| 43 | + /** |
| 44 | + * @var WriteInterface |
| 45 | + */ |
| 46 | + private $mediaDirectory; |
| 47 | + |
| 48 | + /** |
| 49 | + * @var GetAssetsKeywordsInterface |
| 50 | + */ |
| 51 | + private $getAssetKeywords; |
| 52 | + |
| 53 | + /** |
| 54 | + * @inheritdoc |
| 55 | + */ |
| 56 | + protected function setUp(): void |
| 57 | + { |
| 58 | + $this->driver = Bootstrap::getObjectManager()->get(DriverInterface::class); |
| 59 | + $this->synchronizeFiles = Bootstrap::getObjectManager()->get(SynchronizeFilesInterface::class); |
| 60 | + $this->getAssetsByPath = Bootstrap::getObjectManager()->get(GetAssetsByPathsInterface::class); |
| 61 | + $this->getAssetKeywords = Bootstrap::getObjectManager()->get(GetAssetsKeywordsInterface::class); |
| 62 | + $this->mediaDirectory = Bootstrap::getObjectManager()->get(Filesystem::class) |
| 63 | + ->getDirectoryWrite(DirectoryList::MEDIA); |
| 64 | + } |
| 65 | + |
| 66 | + /** |
| 67 | + * Test for SynchronizeFiles::execute |
| 68 | + * |
| 69 | + * @dataProvider filesProvider |
| 70 | + * @param null|string $file |
| 71 | + * @param null|string $title |
| 72 | + * @param null|string $description |
| 73 | + * @param null|array $keywords |
| 74 | + * @throws FileSystemException |
| 75 | + * @throws \Magento\Framework\Exception\LocalizedException |
| 76 | + */ |
| 77 | + public function testExecute( |
| 78 | + ?string $file, |
| 79 | + ?string $title, |
| 80 | + ?string $description, |
| 81 | + ?array $keywords |
| 82 | + ): void { |
| 83 | + $path = realpath(__DIR__ . '/../_files/' . $file); |
| 84 | + $modifiableFilePath = $this->mediaDirectory->getAbsolutePath($file); |
| 85 | + $this->driver->copy( |
| 86 | + $path, |
| 87 | + $modifiableFilePath |
| 88 | + ); |
| 89 | + |
| 90 | + $this->synchronizeFiles->execute([$file]); |
| 91 | + |
| 92 | + $loadedAssets = $this->getAssetsByPath->execute([$file])[0]; |
| 93 | + $loadedKeywords = $this->getKeywords($loadedAssets) ?: null; |
| 94 | + |
| 95 | + $this->assertEquals($title, $loadedAssets->getTitle()); |
| 96 | + $this->assertEquals($description, $loadedAssets->getDescription()); |
| 97 | + $this->assertEquals($keywords, $loadedKeywords); |
| 98 | + |
| 99 | + $this->driver->deleteFile($modifiableFilePath); |
| 100 | + } |
| 101 | + |
| 102 | + /** |
| 103 | + * Data provider for testExecute |
| 104 | + * |
| 105 | + * @return array[] |
| 106 | + */ |
| 107 | + public function filesProvider(): array |
| 108 | + { |
| 109 | + return [ |
| 110 | + [ |
| 111 | + '/magento.jpg', |
| 112 | + 'magento', |
| 113 | + null, |
| 114 | + null |
| 115 | + ], |
| 116 | + [ |
| 117 | + '/magento_metadata.jpg', |
| 118 | + 'Title of the magento image', |
| 119 | + 'Description of the magento image', |
| 120 | + [ |
| 121 | + 'magento', |
| 122 | + 'mediagallerymetadata' |
| 123 | + ] |
| 124 | + ] |
| 125 | + ]; |
| 126 | + } |
| 127 | + |
| 128 | + /** |
| 129 | + * Key asset keywords |
| 130 | + * |
| 131 | + * @param AssetInterface $asset |
| 132 | + * @return string[] |
| 133 | + */ |
| 134 | + private function getKeywords(AssetInterface $asset): array |
| 135 | + { |
| 136 | + $assetKeywords = $this->getAssetKeywords->execute([$asset->getId()]); |
| 137 | + |
| 138 | + if (empty($assetKeywords)) { |
| 139 | + return []; |
| 140 | + } |
| 141 | + |
| 142 | + $keywords = current($assetKeywords)->getKeywords(); |
| 143 | + |
| 144 | + return array_map( |
| 145 | + function (KeywordInterface $keyword) { |
| 146 | + return $keyword->getKeyword(); |
| 147 | + }, |
| 148 | + $keywords |
| 149 | + ); |
| 150 | + } |
| 151 | +} |
0 commit comments