|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Copyright © Magento, Inc. All rights reserved. |
| 4 | + * See COPYING.txt for license details. |
| 5 | + */ |
| 6 | + |
| 7 | +declare(strict_types=1); |
| 8 | + |
| 9 | +namespace Magento\AdobeStockImage\Test\Integration\Model; |
| 10 | + |
| 11 | +use AdobeStock\Api\Models\StockFile; |
| 12 | +use Magento\AdobeStockAssetApi\Api\AssetRepositoryInterface; |
| 13 | +use Magento\AdobeStockClient\Model\StockFileToDocument; |
| 14 | +use Magento\AdobeStockImage\Model\SaveImageFile; |
| 15 | +use Magento\AdobeStockImage\Model\Storage\Save; |
| 16 | +use Magento\AdobeStockImageApi\Api\SaveImageInterface; |
| 17 | +use Magento\Framework\Api\AttributeValueFactory; |
| 18 | +use Magento\Framework\Api\Search\Document; |
| 19 | +use Magento\Framework\Api\SearchCriteriaBuilder; |
| 20 | +use Magento\Framework\App\Filesystem\DirectoryList; |
| 21 | +use Magento\Framework\Exception\IntegrationException; |
| 22 | +use Magento\Framework\Exception\LocalizedException; |
| 23 | +use Magento\Framework\Filesystem; |
| 24 | +use Magento\Framework\Filesystem\Driver\Https; |
| 25 | +use Magento\Framework\Filesystem\DriverInterface; |
| 26 | +use Magento\TestFramework\Helper\Bootstrap; |
| 27 | +use PHPUnit\Framework\TestCase; |
| 28 | + |
| 29 | + |
| 30 | +/** |
| 31 | + * Test client for communication to Adobe Stock API. |
| 32 | + */ |
| 33 | +class SaveImageTest extends TestCase |
| 34 | +{ |
| 35 | + const URL_FIELD = 'thumbnail_240_url'; |
| 36 | + |
| 37 | + /** |
| 38 | + * @var SaveImageInterface |
| 39 | + */ |
| 40 | + private $saveImage; |
| 41 | + |
| 42 | + /** |
| 43 | + * @var string |
| 44 | + */ |
| 45 | + private $saveDestination = 'catalog/category/tmp.png'; |
| 46 | + |
| 47 | + /** |
| 48 | + * @var DriverInterface |
| 49 | + */ |
| 50 | + private $driver; |
| 51 | + |
| 52 | + /** |
| 53 | + * @var Filesystem |
| 54 | + */ |
| 55 | + private $fileSystem; |
| 56 | + |
| 57 | + /** |
| 58 | + * @var AssetRepositoryInterface |
| 59 | + */ |
| 60 | + private $assetRepository; |
| 61 | + |
| 62 | + /** |
| 63 | + * @var SearchCriteriaBuilder |
| 64 | + */ |
| 65 | + private $searchCriteriaBuilder; |
| 66 | + |
| 67 | + /** |
| 68 | + * @inheritdoc |
| 69 | + */ |
| 70 | + protected function setUp(): void |
| 71 | + { |
| 72 | + $this->driver = Bootstrap::getObjectManager()->get(DriverInterface::class); |
| 73 | + $this->fileSystem = Bootstrap::getObjectManager()->get(Filesystem::class); |
| 74 | + $this->assetRepository = Bootstrap::getObjectManager()->get(AssetRepositoryInterface::class); |
| 75 | + $this->searchCriteriaBuilder = Bootstrap::getObjectManager()->get(SearchCriteriaBuilder::class); |
| 76 | + |
| 77 | + $this->deleteImage(); |
| 78 | + $https = $this->createMock(Https::class); |
| 79 | + $https->expects($this->once()) |
| 80 | + ->method('fileGetContents') |
| 81 | + ->willReturnCallback(function ($filePath) { |
| 82 | + return file_get_contents($filePath); |
| 83 | + }); |
| 84 | + $storageSave = Bootstrap::getObjectManager()->create(Save::class, ['driver' => $https,]); |
| 85 | + $saveImageFile = Bootstrap::getObjectManager()->create(SaveImageFile::class, ['storageSave' => $storageSave,]); |
| 86 | + $this->saveImage = Bootstrap::getObjectManager()->create( |
| 87 | + SaveImageInterface::class, |
| 88 | + ['saveImageFile' => $saveImageFile] |
| 89 | + ); |
| 90 | + } |
| 91 | + |
| 92 | + /** |
| 93 | + * @inheridoc |
| 94 | + */ |
| 95 | + protected function tearDown(): void |
| 96 | + { |
| 97 | + $this->deleteImage(); |
| 98 | + parent::tearDown(); |
| 99 | + } |
| 100 | + |
| 101 | + /** |
| 102 | + * Test with image. |
| 103 | + * |
| 104 | + * @return void |
| 105 | + */ |
| 106 | + public function testSave(): void |
| 107 | + { |
| 108 | + $document = $this->getDocument(); |
| 109 | + $this->saveImage->execute( |
| 110 | + $document, |
| 111 | + $document->getCustomAttribute(self::URL_FIELD)->getValue(), |
| 112 | + $this->fileSystem->getDirectoryWrite(DirectoryList::MEDIA)->getAbsolutePath($this->saveDestination) |
| 113 | + ); |
| 114 | + self::assertTrue( |
| 115 | + $this->fileSystem->getDirectoryRead(DirectoryList::MEDIA)->isExist($this->saveDestination), |
| 116 | + 'File was not saved by destination' |
| 117 | + ); |
| 118 | + $searchCriteria = $this->searchCriteriaBuilder |
| 119 | + ->addFilter('creator_id', $document->getCustomAttribute('creator_id')->getValue(), 'eq') |
| 120 | + ->create(); |
| 121 | + self::assertNotEmpty( |
| 122 | + $this->assetRepository->getList($searchCriteria), |
| 123 | + 'Image asset was not saved' |
| 124 | + ); |
| 125 | + } |
| 126 | + |
| 127 | + /** |
| 128 | + * Document for save. |
| 129 | + * |
| 130 | + * @return Document |
| 131 | + * @throws IntegrationException |
| 132 | + */ |
| 133 | + private function getDocument(): Document |
| 134 | + { |
| 135 | + $stockFileData = [ |
| 136 | + 'id' => 1, |
| 137 | + 'comp_url' => 'https://test.url/1.png', |
| 138 | + 'width' => 110, |
| 139 | + 'title' => 'test', |
| 140 | + 'content_type' => 'image/png', |
| 141 | + 'height' => 210, |
| 142 | + 'some_bool_param' => false, |
| 143 | + 'some_nullable_param' => null, |
| 144 | + 'category' => [ |
| 145 | + 'id' => 1, |
| 146 | + 'name' => 'Test' |
| 147 | + ], |
| 148 | + ]; |
| 149 | + |
| 150 | + $stockFile = new StockFile($stockFileData); |
| 151 | + /** @var StockFileToDocument $stockFileToDocument */ |
| 152 | + $stockFileToDocument = Bootstrap::getObjectManager()->create(StockFileToDocument::class); |
| 153 | + $document = $stockFileToDocument->convert($stockFile); |
| 154 | + $this->addAttributes($document, [ |
| 155 | + 'is_downloaded' => 0, |
| 156 | + 'path' => '', |
| 157 | + 'is_licensed_locally' => 0, |
| 158 | + self::URL_FIELD => $this->getImageFilePath('magento-logo.png'), |
| 159 | + 'creator_id' => 1122, |
| 160 | + 'creator_name' => 'Test' |
| 161 | + ]); |
| 162 | + return $document; |
| 163 | + } |
| 164 | + |
| 165 | + /** |
| 166 | + * Add attributes to document |
| 167 | + * |
| 168 | + * @param Document $document |
| 169 | + * @param array $attributes [code => value] |
| 170 | + * @return Document |
| 171 | + */ |
| 172 | + private function addAttributes(Document $document, array $attributes): Document |
| 173 | + { |
| 174 | + $customAttributes = $document->getCustomAttributes(); |
| 175 | + $attributeValueFactory = Bootstrap::getObjectManager()->create( |
| 176 | + AttributeValueFactory::class |
| 177 | + ); |
| 178 | + foreach ($attributes as $code => $value) { |
| 179 | + $attribute = $attributeValueFactory->create(); |
| 180 | + $attribute->setAttributeCode($code); |
| 181 | + $attribute->setValue($value); |
| 182 | + $customAttributes[$code] = $attribute; |
| 183 | + } |
| 184 | + |
| 185 | + $document->setCustomAttributes($customAttributes); |
| 186 | + |
| 187 | + return $document; |
| 188 | + } |
| 189 | + |
| 190 | + /** |
| 191 | + * Return image file path |
| 192 | + * |
| 193 | + * @param string $filename |
| 194 | + * @return string |
| 195 | + */ |
| 196 | + private function getImageFilePath(string $filename): string |
| 197 | + { |
| 198 | + return dirname(__DIR__, 1) |
| 199 | + . DIRECTORY_SEPARATOR |
| 200 | + . implode( |
| 201 | + DIRECTORY_SEPARATOR, |
| 202 | + [ |
| 203 | + '_files', |
| 204 | + $filename |
| 205 | + ] |
| 206 | + ); |
| 207 | + } |
| 208 | + |
| 209 | + /** |
| 210 | + * Delete test image if exists |
| 211 | + * |
| 212 | + * @return void |
| 213 | + */ |
| 214 | + private function deleteImage(): void |
| 215 | + { |
| 216 | + $mediaDir = $this->fileSystem->getDirectoryWrite(DirectoryList::MEDIA); |
| 217 | + if ($mediaDir->isExist($this->saveDestination)) { |
| 218 | + |
| 219 | + $this->driver->deleteFile($mediaDir->getAbsolutePath($this->saveDestination)); |
| 220 | + } |
| 221 | + } |
| 222 | +} |
0 commit comments