Skip to content

Commit 5a6e229

Browse files
authored
Merge pull request magento#1847 from engcom-Golf/asi-1846
Fix error when saving image from stock that does not have Category
2 parents f4f8e94 + cdce16e commit 5a6e229

File tree

4 files changed

+99
-34
lines changed

4 files changed

+99
-34
lines changed

AdobeStockAsset/Model/Asset.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,9 +56,11 @@ public function getId(): ?int
5656
/**
5757
* @inheritdoc
5858
*/
59-
public function getCategoryId(): int
59+
public function getCategoryId(): ?int
6060
{
61-
return (int) $this->getData(self::CATEGORY_ID);
61+
$categoryId = $this->getData(self::CATEGORY_ID);
62+
63+
return $categoryId !== null ? (int) $categoryId : null;
6264
}
6365

6466
/**

AdobeStockAsset/Model/SaveAsset.php

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -80,11 +80,13 @@ public function execute(AssetInterface $asset): void
8080

8181
$category = $asset->getCategory();
8282
if ($category !== null) {
83-
$category = $this->categoryRepository->save($category);
83+
$categoryId = $category->getId();
84+
if ($categoryId !== null) {
85+
$category = $this->categoryRepository->save($category);
86+
}
87+
$data[self::CATEGORY_ID] = $categoryId;
88+
$data[self::CATEGORY] = $category;
8489
}
85-
$data[self::CATEGORY_ID] = $category->getId();
86-
$data[self::CATEGORY] = $category;
87-
8890
$creator = $asset->getCreator();
8991
if ($creator !== null) {
9092
$creator = $this->creatorRepository->save($creator);

AdobeStockAsset/Test/Integration/Model/SaveAssetTest.php

Lines changed: 86 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
use Magento\AdobeStockAssetApi\Api\CreatorRepositoryInterface;
1414
use Magento\AdobeStockAssetApi\Api\Data\AssetInterface;
1515
use Magento\AdobeStockAssetApi\Api\Data\AssetInterfaceFactory;
16+
use Magento\AdobeStockAssetApi\Api\Data\CategoryInterfaceFactory;
1617
use Magento\AdobeStockAssetApi\Api\SaveAssetInterface;
1718
use Magento\MediaGalleryApi\Api\GetAssetsByPathsInterface;
1819
use Magento\MediaGalleryApi\Api\SaveAssetsInterface;
@@ -35,6 +36,29 @@ class SaveAssetTest extends TestCase
3536
*/
3637
private $saveAsset;
3738

39+
/**
40+
* @return array
41+
*/
42+
public function getAssetData(): array
43+
{
44+
return [
45+
'asset_save' => [
46+
'data' => [
47+
'media_gallery_path' => ['some/path.jpg'],
48+
'category_id' => 42,
49+
'creator_id' => 42,
50+
]
51+
],
52+
'without_category' => [
53+
'data' => [
54+
'media_gallery_path' => ['some/path.jpg'],
55+
'category_id' => null,
56+
'creator_id' => 42,
57+
]
58+
]
59+
];
60+
}
61+
3862
/**
3963
* @inheritdoc
4064
*/
@@ -46,13 +70,17 @@ protected function setUp(): void
4670

4771
/**
4872
* Test save an Adobe Stock asset.
73+
*
74+
* @param array $caseData
75+
*
76+
* @dataProvider getAssetData
4977
* @magentoDataFixture ../../../../app/code/Magento/AdobeStockAsset/Test/_files/media_asset.php
5078
* @magentoDataFixture ../../../../app/code/Magento/AdobeStockAsset/Test/_files/category.php
5179
* @magentoDataFixture ../../../../app/code/Magento/AdobeStockAsset/Test/_files/creator.php
5280
*/
53-
public function testExecute(): void
81+
public function testExecute(array $caseData): void
5482
{
55-
$asset = $this->prepareAsset();
83+
$asset = $this->prepareAsset($caseData);
5684
$this->saveAsset->execute($asset);
5785
$expectedAsset = $this->assetRepository->getById($asset->getId());
5886

@@ -66,35 +94,24 @@ public function testExecute(): void
6694

6795
/**
6896
* Prepare an Adobe Stock asset test object.
97+
*
98+
* @param array $caseData
99+
* @return AssetInterface
100+
* @throws \Magento\Framework\Exception\LocalizedException
69101
*/
70-
public function prepareAsset(): AssetInterface
102+
public function prepareAsset(array $caseData): AssetInterface
71103
{
72-
/** @var GetAssetsByPathsInterface $mediaGetByPath */
73-
$mediaGetByPath = Bootstrap::getObjectManager()->get(GetAssetsByPathsInterface::class);
74-
$mediaAssetId = $mediaGetByPath->execute(['some/path.jpg'])[0]->getId();
75-
76-
/** @var CategoryRepositoryInterface $categoryRepository */
77-
$categoryRepository = Bootstrap::getObjectManager()->get(CategoryRepositoryInterface::class);
78-
$category= $categoryRepository->getById(42);
79-
80-
/** @var CreatorRepositoryInterface $creatorRepository */
81-
$creatorRepository = Bootstrap::getObjectManager()->get(CreatorRepositoryInterface::class);
82-
$creator = $creatorRepository->getById(42);
83-
104+
$assetData['data'] = [
105+
'id' => 1,
106+
'is_licensed' => 1,
107+
];
108+
$assetData['data']['media_gallery_id'] = $this->getMediaAssetId($caseData['media_gallery_path']);
109+
$assetData['data']['category'] = $this->getCategory($caseData['category_id']);
110+
$assetData['data']['creator'] = $this->getCreator($caseData['creator_id']);
84111
/** @var AssetInterfaceFactory $assetFactory */
85112
$assetFactory = Bootstrap::getObjectManager()->get(AssetInterfaceFactory::class);
86113
/** @var AssetInterface $asset */
87-
$asset = $assetFactory->create(
88-
[
89-
'data' => [
90-
'id' => 1,
91-
'is_licensed' => 1,
92-
'media_gallery_id' => $mediaAssetId,
93-
'category' => $category,
94-
'creator' => $creator
95-
]
96-
]
97-
);
114+
$asset = $assetFactory->create($assetData);
98115

99116
return $asset;
100117
}
@@ -109,4 +126,47 @@ private function cleanUpEntries(AssetInterface $asset): void
109126
{
110127
$this->assetRepository->deleteById($asset->getId());
111128
}
129+
130+
/**
131+
* @param $paths
132+
* @return int|null
133+
* @throws \Magento\Framework\Exception\LocalizedException
134+
*/
135+
protected function getMediaAssetId($paths): int
136+
{
137+
/** @var GetAssetsByPathsInterface $mediaGetByPath */
138+
$mediaGetByPath = Bootstrap::getObjectManager()->get(GetAssetsByPathsInterface::class);
139+
$mediaAssetId = $mediaGetByPath->execute($paths)[0]->getId();
140+
141+
return $mediaAssetId;
142+
}
143+
144+
/**
145+
* @param int|null $categoryId
146+
* @return \Magento\AdobeStockAssetApi\Api\Data\CategoryInterface|null
147+
* @throws \Magento\Framework\Exception\NoSuchEntityException
148+
*/
149+
protected function getCategory(?int $categoryId): ?\Magento\AdobeStockAssetApi\Api\Data\CategoryInterface
150+
{
151+
/** @var CategoryRepositoryInterface $categoryRepository */
152+
$categoryRepository = Bootstrap::getObjectManager()->get(CategoryRepositoryInterface::class);
153+
/** @var CategoryInterfaceFactory $categoryRepository */
154+
$categoryFactory = Bootstrap::getObjectManager()->get(CategoryInterfaceFactory::class);
155+
156+
return $categoryId !== null ? $categoryRepository->getById($categoryId) : $categoryFactory->create();
157+
}
158+
159+
/**
160+
* @param int $creatorId
161+
* @return \Magento\AdobeStockAssetApi\Api\Data\CreatorInterface
162+
* @throws \Magento\Framework\Exception\NoSuchEntityException
163+
*/
164+
protected function getCreator(int $creatorId): \Magento\AdobeStockAssetApi\Api\Data\CreatorInterface
165+
{
166+
/** @var CreatorRepositoryInterface $creatorRepository */
167+
$creatorRepository = Bootstrap::getObjectManager()->get(CreatorRepositoryInterface::class);
168+
$creator = $creatorRepository->getById($creatorId);
169+
170+
return $creator;
171+
}
112172
}

AdobeStockImageAdminUi/Plugin/AddAdobeStockImageDetailsPlugin.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,8 @@ public function afterExecute(
128128
*/
129129
private function loadAssetsInfo(AssetInterface $asset): array
130130
{
131-
$assetCategory = $this->categoryRepository->getById($asset->getCategoryId());
131+
$hasCategory = $asset->getCategoryId() !== null;
132+
$assetCategory = $hasCategory ? $this->categoryRepository->getById($asset->getCategoryId()) : null;
132133
$assetCreator = $this->creatorRepository->getById($asset->getCreatorId());
133134

134135
return [
@@ -142,7 +143,7 @@ private function loadAssetsInfo(AssetInterface $asset): array
142143
],
143144
[
144145
'title' => __('Category'),
145-
'value' => $assetCategory->getName(),
146+
'value' => $assetCategory !== null ? $assetCategory->getName() : __('None'),
146147
],
147148
[
148149
'title' => __('Author'),

0 commit comments

Comments
 (0)