-
Notifications
You must be signed in to change notification settings - Fork 9.4k
#1391: SaveAssetsKeywordsInterface to delete obsolete keywords #29207
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
#1391: SaveAssetsKeywordsInterface to delete obsolete keywords #29207
Conversation
…delete obsolete keywords - Add method to delete keywords which has no relation to assets
…delete obsolete keywords - Revised behaviour to delete obsolete asset keyword link based on updated arguments
…delete obsolete keywords - Move and improve logic for saving and deleting obsolete asset keyword links
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for the pull request @jmonteros422 ! Please see my comments
/** | ||
* @var GetAssetsKeywordsInterface | ||
*/ | ||
private $getAssetsKeywordsInterface; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I propose to use a shorter and more precise property naming
private $getAssetsKeywordsInterface; | |
private $getAssetsKeywords; |
if (!empty($currentKeywordsData)) { | ||
$currentKeywords = $this->getKeywordIdsFromKeywordData( | ||
$currentKeywordsData[$assetId]->getKeywords() | ||
); | ||
|
||
return $currentKeywords; | ||
} | ||
|
||
return []; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
if (!empty($currentKeywordsData)) { | |
$currentKeywords = $this->getKeywordIdsFromKeywordData( | |
$currentKeywordsData[$assetId]->getKeywords() | |
); | |
return $currentKeywords; | |
} | |
return []; | |
if (empty($currentKeywordsData)) { | |
return []; | |
} | |
return $this->getKeywordIdsFromKeywordData( | |
$currentKeywordsData[$assetId]->getKeywords() | |
); |
/** | ||
* Get keyword ids from keyword data | ||
* | ||
* @param array $keywordsData |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
* @param array $keywordsData | |
* @param KeywordInterface[] $keywordsData |
* Get current keyword data of an asset | ||
* | ||
* @param int $assetId | ||
* @return array |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
* @return array | |
* @return int[] |
* @param int $assetId | ||
* @return array | ||
*/ | ||
private function getCurrentKeywords(int $assetId): array |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
private function getCurrentKeywords(int $assetId): array | |
private function getCurrentKeywordIds(int $assetId): array |
try { | ||
if (!empty($keywordIds)) { | ||
$values = []; | ||
$keywordsToInsert = array_diff($keywordIds, $this->getCurrentKeywords($assetId)); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What do you think about moving current keywords retrieving to the execute
method to retrieve them only once.
insertAssetKeywords
and deleteAssetKeywords
methods can accept only the needed keywordIds (the result of array_diff
)
* Save new asset keyword links | ||
* | ||
* @param int $assetId | ||
* @param array $keywordIds |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
* @param array $keywordIds | |
* @param int[] $keywordIds |
…delete obsolete keywords - Code enhancement for SavedAssetLinks logic and updated AssetKeywordsTest
@magento run all tests |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for the updates @jmonteros422 ! Looks good. See my comments regarding the integration test
$this->assertEquals(count($updatedKeywords), count($updatedLoadedKeywords)); | ||
|
||
$updatedLoadedKeywordStrings = []; | ||
foreach ($updatedLoadedKeywords as $updatedLoadedKeywordObject) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please extract the reusable code to a private method
@@ -72,6 +72,7 @@ protected function setUp(): void | |||
public function testSaveAndGetKeywords(array $keywords): void | |||
{ | |||
$keywords = ['pear', 'plum']; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please remove this variable for the dataprovider items to be used
…delete obsolete keywords - integration test refactor
@magento run all tests |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for updates @jmonteros422 ! Please see my comments
private function insertAssetKeywords(int $assetId, array $keywordIds): void | ||
{ | ||
try { | ||
if (!empty($keywordIds)) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'd revert the conditions to decrease the nesting level (this can be applied to other such conditions in this PR as well). Also this check can be moved out of try-catch block
if (empty($keywordIds)) {
return;
}
$values[] = [$assetId, $keywordId]; | ||
} | ||
|
||
if (!empty($values)) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this is an unnecessary check. the previous check should be enough in my opinion
@@ -66,45 +66,57 @@ protected function setUp(): void | |||
* | |||
* @magentoDataFixture Magento/MediaGallery/_files/media_asset.php | |||
* @dataProvider keywordsProvider | |||
* @param array $keywords | |||
* @param array|null $keywords | |||
* @param array|null $updatedKeywords |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
These parameters cannot be null, also detailed array type declaration is welcome)
* @param array|null $updatedKeywords | |
* @param string[] $updatedKeywords |
|
||
$this->assertCount(1, $loadedAssetKeywords); | ||
if (!empty($keywords)) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'd still perform a verification of empty keywords, can we remove this condition?
…delete obsolete keywords - Enhancements, Revision on integration test and fix unit test failure
@magento run all tests |
…delete obsolete keywords - Revised try catch to resolve failure in unit tests
@magento run all tests |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hi @jmonteros422 , added a couple of comments regarding the integration test
|
||
$loadedKeywordStrings = []; | ||
foreach ($loadedKeywords as $loadedKeywordObject) { | ||
$loadedKeywordStrings[] = $loadedKeywordObject->getKeyword(); | ||
if (!empty($loadedKeywords)) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
foreach loop will not be executed if $loadedKeywords
is an empty array, the condition is not necessary
* @throws \Magento\Framework\Exception\LocalizedException | ||
*/ | ||
public function testSaveAndGetKeywords(array $keywords): void | ||
public function testSaveAndGetKeywords(array $keywords = [], array $updatedKeywords = []): void |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't think these arguments should be optional
'keywords' => $this->getKeywords($keywords) | ||
] | ||
); | ||
|
||
$this->saveAssetsKeywords->execute([$assetKeywords]); | ||
$loadedAssetKeywords = $this->getAssetsKeywords->execute([$loadedAsset->getId()]); | ||
$loadedAssetKeywords = $this->getAssetsKeywords->execute([$assetId]); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
For empty keywords - let's ensure empty result is returned and quit. To avoid the following code complication
if (empty($keywords)) {
$this->assertEmpty($loadedAssetKeywords);
return;
}
|
||
$this->assertCount($expectedCount, $loadedAssetKeywords); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
$this->assertCount($expectedCount, $loadedAssetKeywords); | |
$this->assertCount(1, $loadedAssetKeywords); |
* @param string[] $keywords | ||
* @param string[] $currentKeywords | ||
*/ | ||
private function updateAssetKeywords(int $assetId, array $keywords = [], array $currentKeywords = []): void |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't think we need the $currentKeywords
parameter for this function at all, only the actual (new) keywords should be used for update and verification
…delete obsolete keywords - Allow empty keywords to execute saveAssetLinks, added condition to integration test for empty keywords
@magento run all tests |
Does not require manual/functional testing - covered by integration tests |
Hi @jmonteros422, thank you for your contribution! |
Description (*)
magento/adobe-stock-integration#1391
Related Pull Requests
Fixed Issues (if relevant)
Manual testing scenarios (*)
Questions or comments
Contribution checklist (*)