Skip to content
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

Polpulate asset metadata on uploading the image to magento && Populate media asset description when saving image from adobe stock #1596

Merged
merged 37 commits into from
Jul 26, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
37 commits
Select commit Hold shift + click to select a range
6a4ddd3
Polpulate asset metadata on uploading the image to magento &&
Nazar65 Jul 14, 2020
cb5afbc
Fix static tests, correct dependencies
Nazar65 Jul 14, 2020
febf2a7
Cover changes with mftf tests, fix unit tests
Nazar65 Jul 15, 2020
16ea39c
Refactor MFTF tests
Nazar65 Jul 15, 2020
9400ee3
Add more assertion
Nazar65 Jul 15, 2020
bd2a80d
Merge branch '2.0-develop' into ASI-1497
Nazar65 Jul 15, 2020
8d76d53
Add Magento\Framework\Filesystem\Driver\File to the di configuration
Nazar65 Jul 15, 2020
da42f2d
Merge branch 'ASI-1497' of https://github.com/Nazar65/adobe-stock-int…
Nazar65 Jul 15, 2020
3725bcf
Fix mftf tests
Nazar65 Jul 15, 2020
b2794dc
Refactore per code review, save ,etadata keywords on synchronization …
Nazar65 Jul 17, 2020
84d5cd3
Remove unused dependencies
Nazar65 Jul 17, 2020
8996f46
Rwfactor save image file to be more extensible
Nazar65 Jul 17, 2020
c206111
fix typos
Nazar65 Jul 17, 2020
fa84f0b
Revert plugin deletion due to CLI DI issues
Nazar65 Jul 17, 2020
725f8d4
Static tests fixes
Nazar65 Jul 17, 2020
c9fbcac
Remove unused dependencies fix unit test
Nazar65 Jul 17, 2020
7892652
Remove resize as it already done by plugin, sort importers
Nazar65 Jul 17, 2020
6653a3a
Apply correct sorting
Nazar65 Jul 17, 2020
60529bf
Add missing property
Nazar65 Jul 17, 2020
e57171d
Fix static tests && mftf test fixes
Nazar65 Jul 17, 2020
467f64c
Correct dependencies
Nazar65 Jul 18, 2020
d43b292
Merge branch '2.0-develop' into ASI-1497
sivaschenko Jul 20, 2020
b6119f5
Fix mftf tests
Nazar65 Jul 21, 2020
c45e813
Fix mftf tests
Nazar65 Jul 21, 2020
0f5570d
Mftf fixes
Nazar65 Jul 21, 2020
14e6ac8
Fix image deletion
Nazar65 Jul 21, 2020
d8d9d7d
Fix typos
Nazar65 Jul 21, 2020
173adab
Merge branch '2.0-develop' into ASI-1497
Nazar65 Jul 21, 2020
d55fbfb
Fix mftf ActionGroups
Nazar65 Jul 21, 2020
dc2daf6
Merge branch '2.0-develop' into ASI-1497
Nazar65 Jul 21, 2020
5ec12b9
Fix mftf tests
Nazar65 Jul 21, 2020
fa973ad
Use actionGroup
Nazar65 Jul 21, 2020
97ae46a
Merge branch 'ASI-1497' of https://github.com/Nazar65/adobe-stock-int…
Nazar65 Jul 21, 2020
0e57347
Fix ActionGroupArgument
Nazar65 Jul 21, 2020
87438e2
Correct dependencies
Nazar65 Jul 22, 2020
1171908
Fix subject type
Nazar65 Jul 22, 2020
9126ed2
Merge branch '2.0-develop' into ASI-1497
sivaschenko Jul 23, 2020
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions AdobeStockImage/Model/Extract/MediaGalleryAsset.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ public function convert(Document $document, array $additionalData = []): AssetIn
return $this->assetFactory->create([
'path' => $assetData['path'],
'title' => $assetData['title'],
'description' => $assetData['description'],
'width' => $assetData['width'],
'height' => $assetData['height'],
'size' => $assetData['size'],
Expand Down
42 changes: 41 additions & 1 deletion AdobeStockImage/Model/SaveMediaGalleryAsset.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
use Magento\MediaGalleryApi\Api\SaveAssetsInterface;
use Magento\MediaGallerySynchronizationApi\Model\GetContentHashInterface;
use Magento\Framework\Exception\FileSystemException;
use Magento\MediaGalleryMetadataApi\Api\ExtractMetadataInterface;
use Magento\Framework\Api\AttributeValueFactory;

/**
* Process save action of the media gallery asset.
Expand All @@ -31,6 +33,11 @@ class SaveMediaGalleryAsset
*/
private $documentToMediaGalleryAsset;

/**
* @var AttributeValueFactory
*/
private $attributeValueFactory;

/**
* @var GetContentHashInterface
*/
Expand All @@ -41,22 +48,33 @@ class SaveMediaGalleryAsset
*/
private $fileSystem;

/**
* @var ExtractMetadataInterface
*/
private $extractMetadata;

/**
* @param SaveAssetsInterface $saveMediaAsset
* @param DocumentToMediaGalleryAsset $documentToMediaGalleryAsset
* @param GetContentHashInterface $getContentHash
* @param Filesystem $fileSystem
* @param ExtractMetadataInterface $extractMetadata
* @param AttributeValueFactory $attributeValueFactory
*/
public function __construct(
SaveAssetsInterface $saveMediaAsset,
DocumentToMediaGalleryAsset $documentToMediaGalleryAsset,
GetContentHashInterface $getContentHash,
Filesystem $fileSystem
Filesystem $fileSystem,
ExtractMetadataInterface $extractMetadata,
AttributeValueFactory $attributeValueFactory
) {
$this->saveMediaAsset = $saveMediaAsset;
$this->documentToMediaGalleryAsset = $documentToMediaGalleryAsset;
$this->getContentHash = $getContentHash;
$this->fileSystem = $fileSystem;
$this->extractMetadata = $extractMetadata;
$this->attributeValueFactory = $attributeValueFactory;
}

/**
Expand All @@ -79,13 +97,35 @@ public function execute(Document $document, string $destinationPath): void
'hash' => $this->hashImageContent($destinationPath)
];

$document = $this->setDescriptionField($document, $destinationPath);
$mediaGalleryAsset = $this->documentToMediaGalleryAsset->convert($document, $additionalData);
$this->saveMediaAsset->execute([$mediaGalleryAsset]);
} catch (\Exception $exception) {
throw new CouldNotSaveException(__('Could not save media gallery asset.'), $exception);
}
}

/**
* Set description from file metadata
*
* @param Document $document
* @param string $destinationPath
*/
private function setDescriptionField(Document $document, string $destinationPath): Document
{
$customAttributes = $document->getCustomAttributes();
$mediaDirectory = $this->fileSystem->getDirectoryRead(DirectoryList::MEDIA);
$metadata = $this->extractMetadata->execute($mediaDirectory->getAbsolutePath($destinationPath));
$attribute = $this->attributeValueFactory->create();

$attribute->setAttributeCode('description');
$attribute->setValue($metadata->getDescription());
$customAttributes['description'] = $attribute;
$document->setCustomAttributes($customAttributes);

return $document;
}

/**
* Calculates saved image file size.
*
Expand Down
40 changes: 39 additions & 1 deletion AdobeStockImage/Test/Unit/Model/SaveMediaGalleryAssetTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@
use Magento\MediaGallerySynchronizationApi\Model\GetContentHashInterface;
use PHPUnit\Framework\MockObject\MockObject;
use PHPUnit\Framework\TestCase;
use Magento\MediaGalleryMetadataApi\Api\ExtractMetadataInterface;
use Magento\Framework\Api\AttributeValueFactory;
use Magento\Framework\Api\AttributeValue;
use Magento\MediaGalleryMetadataApi\Api\Data\MetadataInterface;

/**
* Test saving a media gallery asset and return its id.
Expand Down Expand Up @@ -57,6 +61,16 @@ class SaveMediaGalleryAssetTest extends TestCase
*/
private $getContentHash;

/**
* @var ExtractMetadataInterface|MockObject
*/
private $extractMetadata;

/**
* @var AttributeValueFactory|MockObject
*/
private $attributeValueFactory;

/**
* @inheritdoc
*/
Expand All @@ -67,13 +81,17 @@ protected function setUp(): void
$this->converter = $this->createMock(DocumentToMediaGalleryAsset::class);
$this->filesystem = $this->createMock(Filesystem::class);
$this->mediaDirectory = $this->createMock(Read::class);
$this->extractMetadata = $this->createMock(ExtractMetadataInterface::class);
$this->attributeValueFactory = $this->createMock(AttributeValueFactory::class);

$this->saveMediaAsset = (new ObjectManager($this))->getObject(
SaveMediaGalleryAsset::class,
[
'saveMediaAsset' => $this->saveAssets,
'documentToMediaGalleryAsset' => $this->converter,
'fileSystem' => $this->filesystem
'fileSystem' => $this->filesystem,
'extractMetadata' => $this->extractMetadata,
'attributeValueFactory' => $this->attributeValueFactory
]
);
$reflection = new \ReflectionClass(get_class($this->saveMediaAsset));
Expand Down Expand Up @@ -119,11 +137,31 @@ public function testExecute(): void
'size' => $fileSize,
'hash' => $this->getContentHash->execute($hash)
];
$attributeMock = $this->createMock(AttributeValue::class);
$metadataMock = $this->createMock(MetadataInterface::class);
$document->expects($this->once())
->method('getCustomAttributes')
->willReturn([]);
$this->extractMetadata->expects($this->once())
->method('execute')
->willReturn($metadataMock);
$this->attributeValueFactory->expects($this->once())
->method('create')
->willReturn($attributeMock);

$mediaGalleryAssetMock = $this->createMock(Asset::class);
$this->converter->expects($this->once())
->method('convert')
->with($document, $additionalData)
->willReturn($mediaGalleryAssetMock);
$attributeMock->expects($this->once())
->method('setAttributeCode');
$attributeMock->expects($this->once())
->method('setValue');
$metadataMock->expects($this->once())
->method('getDescription');
$document->expects($this->once())
->method('setCustomAttributes');

$this->saveAssets->expects($this->once())
->method('execute')
Expand Down
3 changes: 2 additions & 1 deletion AdobeStockImage/composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@
"magento/module-adobe-stock-image-api": "*",
"magento/module-media-gallery-api": "*",
"magento/module-media-gallery-synchronization-api": "*",
"magento/module-cms": "*"
"magento/module-cms": "*",
"magento/module-media-gallery-metadata-api": "*"
},
"suggest": {
"magento/module-catalog": "*"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,7 @@
<actionGroup ref="NavigateToCreatedCMSPageActionGroup" stepKey="navigateToCreatedCMSPage">
<argument name="CMSPage" value="$$page$$"/>
</actionGroup>
<click selector="{{CmsNewPagePageContentSection.header}}" stepKey="clickExpandContent"/>
<click selector="{{CmsWYSIWYGSection.InsertImageBtn}}" stepKey="clickInsertImageIcon" />
<actionGroup ref="AdminOpenMediaGalleryFromPageNoEditorActionGroup" stepKey="openMediaGalleryForPage"/>
<waitForPageLoad stepKey="waitForPageLoad" />
<actionGroup ref="AdminEnhancedMediaGalleryUploadImageActionGroup" stepKey="uploadImage">
<argument name="image" value="ImageUpload"/>
Expand Down
46 changes: 13 additions & 33 deletions MediaGalleryIntegration/Plugin/SaveBaseCategoryImageInformation.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,29 +7,17 @@

namespace Magento\MediaGalleryIntegration\Plugin;

use Magento\MediaGalleryApi\Api\SaveAssetsInterface;
use Magento\MediaGalleryApi\Api\GetAssetsByPathsInterface;
use Magento\MediaGalleryApi\Api\DeleteAssetsByPathsInterface;
use Magento\Catalog\Model\ImageUploader;
use Magento\MediaGallerySynchronization\Model\CreateAssetFromFile;
use Magento\Cms\Model\Wysiwyg\Images\Storage;
use Magento\MediaGallerySynchronization\Model\Filesystem\SplFileInfoFactory;
use Magento\MediaGallerySynchronizationApi\Api\SynchronizeFilesInterface;

/**
* Save base category image by SaveAssetsInterface.
*/
class SaveBaseCategoryImageInformation
{
/**
* @var SplFileInfoFactory
*/
private $splFileInfoFactory;

/**
* @var SaveAssetsInterface
*/
private $saveAsset;

/**
* @var DeleteAssetsByPathsInterface
*/
Expand All @@ -39,39 +27,33 @@ class SaveBaseCategoryImageInformation
* @var GetAssetsByPathsInterface
*/
private $getAssetsByPaths;

/**
* @var CreateAssetFromFile
*/
private $createAssetFromFile;

/**
* @var Storage
*/
private $storage;

/**
* @var SynchronizeFilesInterface
*/
private $synchronizeFiles;

/**
* @param DeleteAssetsByPathsInterface $deleteAssetsByPath
* @param GetAssetsByPathsInterface $getAssetsByPaths
* @param SplFileInfoFactory $splFileInfoFactory
* @param CreateAssetFromFile $createAssetFromFile
* @param SaveAssetsInterface $saveAsset
* @param Storage $storage
* @param SynchronizeFilesInterface $synchronizeFiles
*/
public function __construct(
DeleteAssetsByPathsInterface $deleteAssetsByPath,
GetAssetsByPathsInterface $getAssetsByPaths,
SplFileInfoFactory $splFileInfoFactory,
CreateAssetFromFile $createAssetFromFile,
SaveAssetsInterface $saveAsset,
Storage $storage
Storage $storage,
SynchronizeFilesInterface $synchronizeFiles
) {
$this->deleteAssetsByPaths = $deleteAssetsByPath;
$this->getAssetsByPaths = $getAssetsByPaths;
$this->splFileInfoFactory = $splFileInfoFactory;
$this->createAssetFromFile = $createAssetFromFile;
$this->saveAsset = $saveAsset;
$this->storage = $storage;
$this->synchronizeFiles = $synchronizeFiles;
}

/**
Expand All @@ -83,16 +65,14 @@ public function __construct(
public function afterMoveFileFromTmp(ImageUploader $subject, string $imagePath): string
{
$absolutePath = $this->storage->getCmsWysiwygImages()->getStorageRoot() . $imagePath;
$file = $this->splFileInfoFactory->create($absolutePath);
$tmpPath = $subject->getBaseTmpPath() . '/' . $file->getFileName();
$tmpPath = $subject->getBaseTmpPath() . '/' . substr(strrchr($imagePath, "/"), 1);
$tmpAssets = $this->getAssetsByPaths->execute([$tmpPath]);

if (!empty($tmpAssets)) {
$this->deleteAssetsByPaths->execute([$tmpAssets[0]->getPath()]);
}

$this->saveAsset->execute([$this->createAssetFromFile->execute($file)]);
$this->storage->resizeFile($absolutePath);

$this->synchronizeFiles->execute([$absolutePath]);

return $imagePath;
}
Expand Down
Loading