Skip to content

#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

Conversation

jmonteros422
Copy link
Contributor

Description (*)

magento/adobe-stock-integration#1391

Related Pull Requests

Fixed Issues (if relevant)

  1. Fixes SaveAssetsKeywordsInterface to delete obsolete keywords adobe-stock-integration#1391

Manual testing scenarios (*)

  1. Edit Image details
  2. Add / remove new keyword tags
  3. Should delete old keyword asset links not specified on the parameters and insert the new ones.

Questions or comments

Contribution checklist (*)

  • Pull request has a meaningful description of its purpose
  • All commits are accompanied by meaningful commit messages
  • All new or changed code is covered with unit/integration tests (if applicable)
  • All automated tests passed successfully (all builds are green)

…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
Copy link
Member

@sivaschenko sivaschenko left a 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;
Copy link
Member

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

Suggested change
private $getAssetsKeywordsInterface;
private $getAssetsKeywords;

Comment on lines 162 to 170
if (!empty($currentKeywordsData)) {
$currentKeywords = $this->getKeywordIdsFromKeywordData(
$currentKeywordsData[$assetId]->getKeywords()
);

return $currentKeywords;
}

return [];
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
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
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
* @param array $keywordsData
* @param KeywordInterface[] $keywordsData

* Get current keyword data of an asset
*
* @param int $assetId
* @return array
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
* @return array
* @return int[]

* @param int $assetId
* @return array
*/
private function getCurrentKeywords(int $assetId): array
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
private function getCurrentKeywords(int $assetId): array
private function getCurrentKeywordIds(int $assetId): array

try {
if (!empty($keywordIds)) {
$values = [];
$keywordsToInsert = array_diff($keywordIds, $this->getCurrentKeywords($assetId));
Copy link
Member

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
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
* @param array $keywordIds
* @param int[] $keywordIds

…delete obsolete keywords - Code enhancement for SavedAssetLinks logic and updated AssetKeywordsTest
@jmonteros422
Copy link
Contributor Author

@magento run all tests

Copy link
Member

@sivaschenko sivaschenko left a 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) {
Copy link
Member

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'];
Copy link
Member

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
@jmonteros422
Copy link
Contributor Author

@magento run all tests

Copy link
Member

@sivaschenko sivaschenko left a 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)) {
Copy link
Member

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)) {
Copy link
Member

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
Copy link
Member

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)

Suggested change
* @param array|null $updatedKeywords
* @param string[] $updatedKeywords


$this->assertCount(1, $loadedAssetKeywords);
if (!empty($keywords)) {
Copy link
Member

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
@jmonteros422
Copy link
Contributor Author

@magento run all tests

…delete obsolete keywords - Revised try catch to resolve failure in unit tests
@jmonteros422
Copy link
Contributor Author

@magento run all tests

Copy link
Member

@sivaschenko sivaschenko left a 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)) {
Copy link
Member

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
Copy link
Member

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]);
Copy link
Member

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);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
$this->assertCount($expectedCount, $loadedAssetKeywords);
$this->assertCount(1, $loadedAssetKeywords);

* @param string[] $keywords
* @param string[] $currentKeywords
*/
private function updateAssetKeywords(int $assetId, array $keywords = [], array $currentKeywords = []): void
Copy link
Member

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
@jmonteros422
Copy link
Contributor Author

@magento run all tests

@sivaschenko sivaschenko changed the title [WIP] #1391: SaveAssetsKeywordsInterface to delete obsolete keywords #1391: SaveAssetsKeywordsInterface to delete obsolete keywords Jul 23, 2020
@sivaschenko
Copy link
Member

Does not require manual/functional testing - covered by integration tests

@sivaschenko sivaschenko added Auto-Tests: Covered All changes in Pull Request is covered by auto-tests Priority: P1 Once P0 defects have been fixed, a defect having this priority is the next candidate for fixing. Severity: S2 Major restrictions or short-term circumventions are required until a fix is available. labels Jul 23, 2020
@magento-engcom-team magento-engcom-team merged commit a1fa1c6 into magento:2.4-develop Jul 29, 2020
@m2-assistant
Copy link

m2-assistant bot commented Jul 29, 2020

Hi @jmonteros422, thank you for your contribution!
Please, complete Contribution Survey, it will take less than a minute.
Your feedback will help us to improve contribution process.

@lenaorobei lenaorobei added this to the 2.4.1 milestone Jul 29, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Auto-Tests: Covered All changes in Pull Request is covered by auto-tests Component: MediaGallery Priority: P1 Once P0 defects have been fixed, a defect having this priority is the next candidate for fixing. Progress: accept Release Line: 2.4 Severity: S2 Major restrictions or short-term circumventions are required until a fix is available.
Projects
Archived in project
Development

Successfully merging this pull request may close these issues.

SaveAssetsKeywordsInterface to delete obsolete keywords
4 participants