Skip to content

Commit

Permalink
Merge branch '2.4-develop' into 2.4-develop
Browse files Browse the repository at this point in the history
  • Loading branch information
engcom-Foxtrot authored Mar 19, 2021
2 parents 5188dcf + 4559910 commit 22643ae
Show file tree
Hide file tree
Showing 271 changed files with 13,160 additions and 1,005 deletions.
58 changes: 22 additions & 36 deletions app/code/Magento/Authorization/Model/Role.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,28 +5,32 @@
*/
namespace Magento\Authorization\Model;

use Magento\Authorization\Model\ResourceModel\Role\Collection;
use Magento\Framework\App\ObjectManager;
use Magento\Framework\Model\AbstractModel;

/**
* Admin Role Model
*
* @api
* @method int getParentId()
* @method \Magento\Authorization\Model\Role setParentId(int $value)
* @method Role setParentId(int $value)
* @method int getTreeLevel()
* @method \Magento\Authorization\Model\Role setTreeLevel(int $value)
* @method Role setTreeLevel(int $value)
* @method int getSortOrder()
* @method \Magento\Authorization\Model\Role setSortOrder(int $value)
* @method Role setSortOrder(int $value)
* @method string getRoleType()
* @method \Magento\Authorization\Model\Role setRoleType(string $value)
* @method Role setRoleType(string $value)
* @method int getUserId()
* @method \Magento\Authorization\Model\Role setUserId(int $value)
* @method Role setUserId(int $value)
* @method string getUserType()
* @method \Magento\Authorization\Model\Role setUserType(string $value)
* @method Role setUserType(string $value)
* @method string getRoleName()
* @method \Magento\Authorization\Model\Role setRoleName(string $value)
* @method Role setRoleName(string $value)
* @api
* @since 100.0.2
*/
class Role extends \Magento\Framework\Model\AbstractModel
class Role extends AbstractModel
{
/**
* @var string
Expand All @@ -38,23 +42,6 @@ class Role extends \Magento\Framework\Model\AbstractModel
*/
protected $_cacheTag = 'user_assigned_role';

/**
* @param \Magento\Framework\Model\Context $context
* @param \Magento\Framework\Registry $registry
* @param \Magento\Authorization\Model\ResourceModel\Role $resource
* @param \Magento\Authorization\Model\ResourceModel\Role\Collection $resourceCollection
* @param array $data
*/
public function __construct( //phpcs:ignore Generic.CodeAnalysis.UselessOverridingMethod
\Magento\Framework\Model\Context $context,
\Magento\Framework\Registry $registry,
\Magento\Authorization\Model\ResourceModel\Role $resource,
\Magento\Authorization\Model\ResourceModel\Role\Collection $resourceCollection,
array $data = []
) {
parent::__construct($context, $registry, $resource, $resourceCollection, $data);
}

/**
* @inheritDoc
*/
Expand All @@ -70,31 +57,30 @@ public function __sleep()
public function __wakeup()
{
parent::__wakeup();
$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$this->_resource = $objectManager->get(\Magento\Authorization\Model\ResourceModel\Role::class);
$this->_resourceCollection = $objectManager->get(
\Magento\Authorization\Model\ResourceModel\Role\Collection::class
);
$objectManager = ObjectManager::getInstance();
$this->_resource = $objectManager->get(ResourceModel\Role::class);
$this->_resourceCollection = $objectManager->get(Collection::class);
}

/**
* Class constructor
*
* @return void
* @inheritdoc
*/
protected function _construct()
{
$this->_init(\Magento\Authorization\Model\ResourceModel\Role::class);
$this->_init(ResourceModel\Role::class);
}

/**
* Update object into database
* Obsolete method of update
*
* @return $this
* @deprecated Method was never implemented and used.
*/
public function update()
{
$this->getResource()->update($this);
// phpcs:disable Magento2.Functions.DiscouragedFunction
trigger_error('Method was never implemented and used.', E_USER_DEPRECATED);

return $this;
}

Expand Down
33 changes: 33 additions & 0 deletions app/code/Magento/AwsS3/Test/Mftf/Helper/S3FileAssertions.php
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,21 @@ public function assertFileExists($filePath, $message = ''): void
$this->assertTrue($this->driver->isExists($filePath), $message);
}

/**
* Asserts that a file with the given glob pattern exists in the given path on the remote storage system
*
* @param string $path
* @param string $pattern
* @param string $message
*
* @throws \Magento\Framework\Exception\FileSystemException
*/
public function assertGlobbedFileExists($path, $pattern, $message = ""): void
{
$files = $this->driver->search($pattern, $path);
$this->assertNotEmpty($files, $message);
}

/**
* Assert a file does not exist on the remote storage system
*
Expand Down Expand Up @@ -206,6 +221,24 @@ public function assertFileContainsString($filePath, $text, $message = ""): void
$this->assertStringContainsString($text, $this->driver->fileGetContents($filePath), $message);
}

/**
* Asserts that a file with the given glob pattern at the given path on the remote storage system contains a given string
*
* @param string $path
* @param string $pattern
* @param string $text
* @param int $fileIndex
* @param string $message
* @return void
*
* @throws \Magento\Framework\Exception\FileSystemException
*/
public function assertGlobbedFileContainsString($path, $pattern, $text, $fileIndex = 0, $message = ""): void
{
$files = $this->driver->search($pattern, $path);
$this->assertStringContainsString($text, $this->driver->fileGetContents($files[$fileIndex] ?? ''), $message);
}

/**
* Assert a file on the remote storage system does not contain a given string
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@
</createData>

<actionGroup ref="CliIndexerReindexActionGroup" stepKey="reindex">
<argument name="indices" value="cataloginventory_stock catalog_product_price"/>
<argument name="indices" value=""/>
</actionGroup>
</before>
<after>
Expand Down
2 changes: 1 addition & 1 deletion app/code/Magento/Catalog/Block/Product/ListProduct.php
Original file line number Diff line number Diff line change
Expand Up @@ -461,7 +461,7 @@ private function initializeProductCollection()
// if the product is associated with any category
if ($categories->count()) {
// show products from this category
$this->setCategoryId(current($categories->getIterator())->getId());
$this->setCategoryId($categories->getIterator()->current()->getId());
}
}

Expand Down
1 change: 1 addition & 0 deletions app/code/Magento/Catalog/Block/Ui/ProductViewCounter.php
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,7 @@ public function getCurrentProductData()
$this->productRenderCollectorComposite
->collect($product, $productRender);
$data = $this->hydrator->extract($productRender);
$data['is_available'] = $product->isAvailable();

$currentProductData = [
'items' => [
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,12 @@
namespace Magento\Catalog\Controller\Adminhtml\Product;

use Magento\Backend\App\Action\Context;
use Magento\Backend\Model\View\Result\Redirect;
use Magento\Catalog\Api\ProductRepositoryInterface;
use Magento\Catalog\Controller\Adminhtml\Product;
use Magento\Catalog\Model\ResourceModel\Product\CollectionFactory;
use Magento\Framework\App\Action\HttpPostActionInterface as HttpPostActionInterface;
use Magento\Framework\App\ObjectManager;
use Magento\Framework\Controller\ResultFactory;
use Magento\Framework\Exception\LocalizedException;
use Magento\Ui\Component\MassAction\Filter;
Expand All @@ -20,7 +23,7 @@
/**
* Class \Magento\Catalog\Controller\Adminhtml\Product\MassDelete
*/
class MassDelete extends \Magento\Catalog\Controller\Adminhtml\Product implements HttpPostActionInterface
class MassDelete extends Product implements HttpPostActionInterface
{
/**
* Massactions filter
Expand Down Expand Up @@ -49,8 +52,8 @@ class MassDelete extends \Magento\Catalog\Controller\Adminhtml\Product implement
* @param Builder $productBuilder
* @param Filter $filter
* @param CollectionFactory $collectionFactory
* @param ProductRepositoryInterface $productRepository
* @param LoggerInterface $logger
* @param ProductRepositoryInterface|null $productRepository
* @param LoggerInterface|null $logger
*/
public function __construct(
Context $context,
Expand All @@ -63,20 +66,23 @@ public function __construct(
$this->filter = $filter;
$this->collectionFactory = $collectionFactory;
$this->productRepository = $productRepository ?:
\Magento\Framework\App\ObjectManager::getInstance()->create(ProductRepositoryInterface::class);
ObjectManager::getInstance()->create(ProductRepositoryInterface::class);
$this->logger = $logger ?:
\Magento\Framework\App\ObjectManager::getInstance()->create(LoggerInterface::class);
ObjectManager::getInstance()->create(LoggerInterface::class);
parent::__construct($context, $productBuilder);
}

/**
* Mass Delete Action
*
* @return \Magento\Backend\Model\View\Result\Redirect
* @return Redirect
* @throws LocalizedException
*/
public function execute()
{
$collection = $this->filter->getCollection($this->collectionFactory->create());
$collection->addMediaGalleryData();

$productDeleted = 0;
$productDeletedError = 0;
/** @var \Magento\Catalog\Model\Product $product */
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,146 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);

namespace Magento\Catalog\Model\ResourceModel;

use Exception;
use Magento\Catalog\Api\Data\ProductInterface;
use Magento\Catalog\Model\Product\Gallery\Processor;
use Magento\Catalog\Model\Product\Media\ConfigInterface as MediaConfig;
use Magento\Catalog\Model\ResourceModel\Product\Gallery;
use Magento\Framework\App\Filesystem\DirectoryList;
use Magento\Framework\DataObject;
use Magento\Framework\Exception\FileSystemException;
use Magento\Framework\Filesystem;
use Psr\Log\LoggerInterface;

/**
* Process media gallery and delete media image after product delete
*/
class MediaImageDeleteProcessor
{
/**
* @var MediaConfig
*/
private $imageConfig;

/**
* @var Filesystem
*/
private $mediaDirectory;

/**
* @var Processor
*/
private $imageProcessor;

/**
* @var Gallery
*/
private $productGallery;

/**
* @var LoggerInterface
*/
private $logger;

/**
* Product constructor.
*
* @param MediaConfig $imageConfig
* @param Filesystem $filesystem
* @param Processor $imageProcessor
* @param Gallery $productGallery
* @param LoggerInterface $logger
* @throws FileSystemException
*/
public function __construct(
MediaConfig $imageConfig,
Filesystem $filesystem,
Processor $imageProcessor,
Gallery $productGallery,
LoggerInterface $logger
) {
$this->imageConfig = $imageConfig;
$this->mediaDirectory = $filesystem->getDirectoryWrite(DirectoryList::MEDIA);
$this->imageProcessor = $imageProcessor;
$this->productGallery = $productGallery;
$this->logger = $logger;
}

/**
* Process $product data and remove image from gallery after product delete
*
* @param DataObject $product
* @return void
*/
public function execute(DataObject $product): void
{
try {
$productImages = $product->getMediaGalleryImages();
foreach ($productImages as $image) {
$imageFile = $image->getFile();
if ($imageFile) {
$this->deleteProductImage($image, $product, $imageFile);
}
}
} catch (Exception $e) {
$this->logger->critical($e);
}
}

/**
* Check if image exists and is not used by any other products
*
* @param string $file
* @return bool
*/
private function canDeleteImage(string $file): bool
{
return $this->productGallery->countImageUses($file) <= 1;
}

/**
* Delete the physical image if it's existed and not used by other products
*
* @param string $imageFile
* @param string $filePath
* @throws FileSystemException
*/
private function deletePhysicalImage(string $imageFile, string $filePath): void
{
if ($this->canDeleteImage($imageFile)) {
$this->mediaDirectory->delete($filePath);
}
}

/**
* Remove product image
*
* @param DataObject $image
* @param ProductInterface $product
* @param string $imageFile
*/
private function deleteProductImage(
DataObject $image,
ProductInterface $product,
string $imageFile
): void {
$catalogPath = $this->imageConfig->getBaseMediaPath();
$filePath = $catalogPath . $imageFile;

if ($this->mediaDirectory->isFile($filePath)) {
try {
$this->productGallery->deleteGallery($image->getValueId());
$this->imageProcessor->removeImage($product, $imageFile);
$this->deletePhysicalImage($imageFile, $filePath);
} catch (Exception $e) {
$this->logger->critical($e);
}
}
}
}
Loading

0 comments on commit 22643ae

Please sign in to comment.