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

Updated MediaGallery modules and marked as API #27499

Merged
merged 44 commits into from
Apr 19, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
44 commits
Select commit Hold shift + click to select a range
c12201e
Updated MediaGallery modules and marked as API
sivaschenko Mar 30, 2020
abb4503
Fixed static tests
sivaschenko Mar 30, 2020
be2e695
magento/magento2#27499: Code review changes
sivaschenko Apr 1, 2020
e8eb3a9
magento/magento2#27499: Corrected DI configuration
sivaschenko Apr 1, 2020
e671db3
- Rename interface
VladimirZaets Apr 3, 2020
83e7e37
Test coverage
VladimirZaets Apr 3, 2020
df912e4
Merge branch '2.4-develop' of github.com:magento/magento2 into media-…
sivaschenko Apr 3, 2020
3784f92
magento/magento2#27499: Removed DataExtractor in favour to DataObject…
sivaschenko Apr 3, 2020
b77e66d
magento/magento2#27499: Code review changes
sivaschenko Apr 3, 2020
909ea3f
agento/magento2#27499: Updated blacklist patterns
sivaschenko Apr 4, 2020
0a57486
Add configuration for MediaGallery/Directory
VladimirZaets Apr 7, 2020
7a82782
Merge - Add configuration for MediaGallery/Directory
VladimirZaets Apr 7, 2020
90bf91d
Corrected blacklist patterns
sivaschenko Apr 7, 2020
7fd26a9
Added comments and strict types enabling
sivaschenko Apr 7, 2020
e1bea53
- Fix unit test
VladimirZaets Apr 8, 2020
2bd8609
- Fix unit test
VladimirZaets Apr 8, 2020
e69ee88
- Remove optional example attribute from xsd
VladimirZaets Apr 8, 2020
5aceeab
- Add rollback for media_asset fixture.
VladimirZaets Apr 8, 2020
a8ba203
magento/magento2#27499: Moved configuration xsd to MediaGalleryApi mo…
sivaschenko Apr 8, 2020
9f4bafc
magento/magento2#27499: MediaGallery bulk interfaces introduced
sivaschenko Apr 8, 2020
9106468
magento/magento2#27499: Extracted MediaGalleryCatalog module
sivaschenko Apr 9, 2020
96d3862
magento/magento2#27499: Corrected services implementation
sivaschenko Apr 9, 2020
e7a78fa
magento/magento2#27499: Fixed directory integration tests
sivaschenko Apr 9, 2020
f4d980a
magento/magento2#27499: Integration tests
sivaschenko Apr 9, 2020
ce08d34
magento/magento2#27499: Corrected comments and unit tests
sivaschenko Apr 9, 2020
6dfb2cc
magento/magento2#27499: Filtering data for insert
sivaschenko Apr 9, 2020
8e2337f
Merge remote-tracking branch 'mainline/2.4-develop' into sivaschenko/…
Apr 10, 2020
c13a5fb
magento/magento#227499: Intorduced ConfigInterface
sivaschenko Apr 14, 2020
30ce748
magento/magento2#27499: Code review fixes
Apr 14, 2020
9eecf12
Merge branch 'media-gallery-api' of github.com:sivaschenko/magento2 i…
Apr 14, 2020
6a94b59
magento/magento2#27499: Code review fixes
Apr 14, 2020
a77e990
Merge remote-tracking branch 'mainline/2.4-develop' into sivaschenko/…
Apr 14, 2020
e3ea27d
magento/magento2#27499: Code review fixes.
Apr 15, 2020
2e52006
Merge branch '2.4-develop' into media-gallery-api
lenaorobei Apr 16, 2020
024300a
Updated MediaGallery modules and marked as API
Apr 16, 2020
6e077c3
Merge branch 'media-gallery-api' of github.com:sivaschenko/magento2 i…
Apr 16, 2020
01ac38b
Updated MediaGallery modules and marked as API
Apr 16, 2020
1c7fd0a
Eliminate AbstractExtensibleModel usage
Apr 16, 2020
8919c70
Merge branch '2.4-develop' into media-gallery-api
sivaschenko Apr 17, 2020
c6c49b9
Merge branch '2.4-develop' of github.com:magento/magento2 into media-…
sivaschenko Apr 18, 2020
9a7a1c5
Fixed unit tests
sivaschenko Apr 18, 2020
21955c5
Fixed integration tests
sivaschenko Apr 18, 2020
c006590
Added blacklist to directory operations
sivaschenko Apr 18, 2020
8b3c9b1
Added MediaGalleryCatalog to composer.json
sivaschenko Apr 19, 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
47 changes: 13 additions & 34 deletions app/code/Magento/MediaGallery/Model/Asset/Command/Save.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,6 @@ class Save implements SaveInterface
*/
private $resourceConnection;

/**
* @var DataObjectProcessor
*/
private $objectProcessor;

/**
* @var LoggerInterface
*/
Expand All @@ -42,16 +37,13 @@ class Save implements SaveInterface
* Save constructor.
*
* @param ResourceConnection $resourceConnection
* @param DataObjectProcessor $objectProcessor
* @param LoggerInterface $logger
*/
public function __construct(
ResourceConnection $resourceConnection,
DataObjectProcessor $objectProcessor,
LoggerInterface $logger
) {
$this->resourceConnection = $resourceConnection;
$this->objectProcessor = $objectProcessor;
$this->logger = $logger;
}

Expand All @@ -72,37 +64,24 @@ public function execute(AssetInterface $mediaAsset): int

$connection->insertOnDuplicate(
sivaschenko marked this conversation as resolved.
Show resolved Hide resolved
$tableName,
$this->filterData($this->objectProcessor->buildOutputDataArray($mediaAsset, AssetInterface::class))
[
'id' => $mediaAsset->getId(),
'path' => $mediaAsset->getPath(),
'title' => $mediaAsset->getTitle(),
'source' => $mediaAsset->getSource(),
'content_type' => $mediaAsset->getContentType(),
'width' => $mediaAsset->getWidth(),
'height' => $mediaAsset->getHeight(),
'size' => $mediaAsset->getSize(),
'created_at' => $mediaAsset->getCreatedAt(),
'updated_at' => $mediaAsset->getUpdatedAt(),
sidolov marked this conversation as resolved.
Show resolved Hide resolved
]
);
return (int) $connection->lastInsertId($tableName);
return (int)$connection->lastInsertId($tableName);
} catch (\Exception $exception) {
$this->logger->critical($exception);
$message = __('An error occurred during media asset save: %1', $exception->getMessage());
throw new CouldNotSaveException($message, $exception);
}
}

/**
* Filter data to get flat array without null values
*
* @param array $data
* @return array
*/
private function filterData(array $data): array
{
$filteredData = [];
foreach ($data as $key => $value) {
if ($value === null) {
continue;
}
if (is_array($value)) {
continue;
}
if (is_object($value)) {
continue;
}
$filteredData[$key] = $value;
}
return $filteredData;
}
}
19 changes: 4 additions & 15 deletions app/code/Magento/MediaGallery/Model/Directory/Config.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,12 @@
namespace Magento\MediaGallery\Model\Directory;

use Magento\Framework\Config\DataInterface;
use Magento\MediaGalleryApi\Model\BlacklistPatternsConfigInterface;

/**
* Media gallery directory config
*/
class Config
class Config implements BlacklistPatternsConfigInterface
{
private const XML_PATH_BLACKLIST_PATTERNS = 'blacklist/patterns';
sidolov marked this conversation as resolved.
Show resolved Hide resolved

Expand All @@ -29,25 +30,13 @@ public function __construct(DataInterface $data)
$this->data = $data;
}

/**
* Get config value by key.
*
* @param string|null $key
* @param string|null $default
* @return array
*/
public function get($key = null, $default = null)
{
return $this->data->get($key, $default);
}

/**
* Returns list of blacklist regexp patterns
*
* @return array
*/
public function getBlacklistPatterns() : array
public function get() : array
{
return $this->get(self::XML_PATH_BLACKLIST_PATTERNS);
return $this->data->get(self::XML_PATH_BLACKLIST_PATTERNS);
}
}
11 changes: 6 additions & 5 deletions app/code/Magento/MediaGallery/Model/Directory/IsBlacklisted.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,21 +8,22 @@
namespace Magento\MediaGallery\Model\Directory;

use Magento\MediaGalleryApi\Api\IsPathBlacklistedInterface;
use Magento\MediaGalleryApi\Model\BlacklistPatternsConfigInterface;

/**
* Check if the path is blacklisted for media gallery. Directory path may be blacklisted if it's reserved by the system
*/
class IsBlacklisted implements IsPathBlacklistedInterface
sidolov marked this conversation as resolved.
Show resolved Hide resolved
{
/**
* @var Config
* @var BlacklistPatternsConfigInterface
*/
private $config;

/**
* @param Config $config
/*
* @param BlacklistPatternsConfigInterface $config
*/
public function __construct(Config $config)
public function __construct(BlacklistPatternsConfigInterface $config)
{
$this->config = $config;
}
Expand All @@ -35,7 +36,7 @@ public function __construct(Config $config)
*/
public function execute(string $path): bool
{
foreach ($this->config->getBlacklistPatterns() as $pattern) {
foreach ($this->config->get() as $pattern) {
if (empty($pattern)) {
continue;
}
Expand Down
45 changes: 12 additions & 33 deletions app/code/Magento/MediaGallery/Model/ResourceModel/SaveAssets.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,6 @@ class SaveAssets implements SaveAssetsInterface
*/
private $resourceConnection;

/**
* @var DataObjectProcessor
*/
private $objectProcessor;

/**
* @var LoggerInterface
*/
Expand All @@ -40,16 +35,13 @@ class SaveAssets implements SaveAssetsInterface
* Save constructor.
*
* @param ResourceConnection $resourceConnection
* @param DataObjectProcessor $objectProcessor
* @param LoggerInterface $logger
*/
public function __construct(
ResourceConnection $resourceConnection,
DataObjectProcessor $objectProcessor,
LoggerInterface $logger
) {
$this->resourceConnection = $resourceConnection;
$this->objectProcessor = $objectProcessor;
$this->logger = $logger;
}

Expand All @@ -66,7 +58,18 @@ public function execute(array $assets): void
try {
$connection->insertOnDuplicate(
$tableName,
$this->filterData($this->objectProcessor->buildOutputDataArray($asset, AssetInterface::class))
[
'id' => $asset->getId(),
'path' => $asset->getPath(),
'title' => $asset->getTitle(),
'source' => $asset->getSource(),
'content_type' => $asset->getContentType(),
'width' => $asset->getWidth(),
'height' => $asset->getHeight(),
'size' => $asset->getSize(),
'created_at' => $asset->getCreatedAt(),
'updated_at' => $asset->getUpdatedAt(),
sidolov marked this conversation as resolved.
Show resolved Hide resolved
]
);
} catch (\Exception $exception) {
$this->logger->critical($exception);
Expand All @@ -85,28 +88,4 @@ public function execute(array $assets): void
);
}
}

/**
* Filter data to get flat array without null values
*
* @param array $data
* @return array
*/
private function filterData(array $data): array
{
$filteredData = [];
foreach ($data as $key => $value) {
if ($value === null) {
continue;
}
if (is_array($value)) {
continue;
}
if (is_object($value)) {
continue;
}
$filteredData[$key] = $value;
}
return $filteredData;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
use Magento\Framework\TestFramework\Unit\Helper\ObjectManager;
use Magento\MediaGallery\Model\Directory\IsBlacklisted;
use Magento\MediaGallery\Model\Directory\Config;
use Magento\MediaGalleryApi\Model\BlacklistPatternsConfigInterface;
use PHPUnit\Framework\MockObject\MockObject;
use PHPUnit\Framework\TestCase;

Expand All @@ -24,7 +25,7 @@ class IsBlacklistedTest extends TestCase
private $object;

/**
* @var Config|MockObject
* @var BlacklistPatternsConfigInterface|MockObject
*/
private $config;

Expand All @@ -33,8 +34,8 @@ class IsBlacklistedTest extends TestCase
*/
protected function setUp(): void
{
$this->config = $this->getMockBuilder(Config::class)->disableOriginalConstructor()->getMock();
$this->config->expects($this->at(0))->method('getBlacklistPatterns')->willReturn([
$this->config = $this->getMockBuilder(BlacklistPatternsConfigInterface::class)->disableOriginalConstructor()->getMock();
$this->config->expects($this->at(0))->method('get')->willReturn([
'tmp' => '/pub\/media\/tmp/',
'captcha' => '/pub\/media\/captcha/'
]);
Expand Down
2 changes: 2 additions & 0 deletions app/code/Magento/MediaGallery/etc/di.xml
Original file line number Diff line number Diff line change
Expand Up @@ -56,4 +56,6 @@
<argument name="data" xsi:type="object">Magento\MediaGallery\Model\Directory\Config\Data</argument>
</arguments>
</type>

<preference for="Magento\MediaGalleryApi\Model\BlacklistPatternsConfigInterface" type="Magento\MediaGallery\Model\Directory\Config"/>
</config>
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ interface CreateDirectoriesByPathsInterface
* Create new directories by provided paths
*
* @param string[] $paths
* @return void
* @throws \Magento\Framework\Exception\CouldNotSaveException
*/
public function execute(array $paths): void;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ interface DeleteDirectoriesByPathsInterface
* Deletes the existing folders
*
* @param string[] $paths
* @return void
* @throws \Magento\Framework\Exception\CouldNotDeleteException
*/
public function execute(array $paths): void;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@
namespace Magento\MediaGalleryApi\Api;

/**
* Directory paths that are reserved by system and not be included in the media gallery
* Check if the path is blacklisted for media gallery.
*
* Directory path may be blacklisted if it's reserved by the system.
* @api
*/
interface IsPathBlacklistedInterface
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ interface SaveAssetsInterface
* Save media asset. The saved asset can later be retrieved by path
*
* @param \Magento\MediaGalleryApi\Api\Data\AssetInterface[] $assets
* @return void
* @throws \Magento\Framework\Exception\CouldNotSaveException
*/
public function execute(array $assets): void;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ interface SaveAssetsKeywordsInterface
* Save assets keywords
*
* @param \Magento\MediaGalleryApi\Api\Data\AssetKeywordsInterface[] $assetKeywords
* @return void
* @throws \Magento\Framework\Exception\CouldNotSaveException
*/
public function execute(array $assetKeywords): void;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/

namespace Magento\MediaGalleryApi\Model;

/**
* Returns list of blacklist regexp patterns
*/
interface BlacklistPatternsConfigInterface
{
/**
* Get regexp patterns
*
* @return array
*/
public function get(): array;
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
/**
* Ensures that metadata is removed from the database when an image has been deleted (from legacy media gallery)
*/
class Processor
class RemoveAssetAfterRemoveImage
{
/**
* @var DeleteAssetsByPathsInterface
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
use Magento\Catalog\Model\Product;
use Magento\Catalog\Model\Product\Gallery\Processor as ProcessorSubject;
use Magento\Framework\TestFramework\Unit\Helper\ObjectManager as ObjectManagerHelper;
use Magento\MediaGalleryCatalog\Plugin\Product\Gallery\Processor;
use Magento\MediaGalleryCatalog\Plugin\Product\Gallery\RemoveAssetAfterRemoveImage;
use Magento\MediaGalleryApi\Api\DeleteAssetsByPathsInterface;
use PHPUnit\Framework\MockObject\MockObject;
use PHPUnit\Framework\TestCase;
Expand All @@ -19,7 +19,7 @@
/**
* Unit test for \Magento\MediaGalleryCatalog\Plugin\Product\Gallery\Processor
*/
class ProcessorTest extends TestCase
class RemoveAssetAfterRemoveImageTest extends TestCase
{
private const STUB_FILE_NAME = 'file';

Expand All @@ -44,7 +44,7 @@ class ProcessorTest extends TestCase
private $productMock;

/**
* @var Processor
* @var RemoveAssetAfterRemoveImage
*/
private $plugin;

Expand All @@ -60,7 +60,7 @@ protected function setUp()
$this->loggerMock = $this->createMock(LoggerInterface::class);

$this->plugin = (new ObjectManagerHelper($this))->getObject(
Processor::class,
RemoveAssetAfterRemoveImage::class,
[
'deleteByPaths' => $this->deleteMediaAssetByPathMock,
'logger' => $this->loggerMock
Expand Down
3 changes: 1 addition & 2 deletions app/code/Magento/MediaGalleryCatalog/etc/di.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
-->
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
<type name="Magento\Catalog\Model\Product\Gallery\Processor">
<plugin name="media_gallery_image_remove_metadata" type="Magento\MediaGallery\Plugin\Product\Gallery\Processor"
sortOrder="10" disabled="false"/>
<plugin name="media_gallery_image_remove_metadata" type="Magento\MediaGallery\Plugin\Product\Gallery\RemoveAssetAfterRemoveImage"/>
</type>
</config>