Skip to content

Commit

Permalink
Merge pull request #1844 from magento-engcom/2.2-develop-prs
Browse files Browse the repository at this point in the history
Merge pull request #1014 from magento-engcom/2.2-develop-prs

[EngCom] Public Pull Requests - 2.2-develop

Public Pull Requests
 - #1012 10814: Attribute repository resets sourceModel for new attributes. by @nmalevanec
- #1006 #12285: The option false for mobile device don't work in product -
view page gallery by @p-bystritsky
- #983 #12259: Save and Duplicated product not working by @p-bystritsky
- #1000 8204: catalog:images:resize = getimagesize(): Read error! by @RomaKis
#12633 Magento Connect no longer exist by @miguelbalparda

Fixed Public Issues
 - #10814 Attribute repository resets sourceModel for new attributes
 - #12285 The option false for mobile device don't work in product view page gallery
 - #12490 I can't disable full screen gallery on mobile on magento 2.2.1
 - #12259 Save and Duplicated product not working
 - #8204 catalog:images:resize = getimagesize(): Read error! in vendor/magento/module-catalog/Model/Product/Image.php on line 410 if an image is 0 bytes
 - #12632 Magento Connect no longer exist
  • Loading branch information
Oleksii Korshenko authored Dec 13, 2017
2 parents 506d500 + 407884c commit 7d00d28
Show file tree
Hide file tree
Showing 14 changed files with 408 additions and 44 deletions.
28 changes: 20 additions & 8 deletions app/code/Magento/Catalog/Console/Command/ImagesResizeCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@
*/
namespace Magento\Catalog\Console\Command;

use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;

class ImagesResizeCommand extends \Symfony\Component\Console\Command\Command
{
/**
Expand Down Expand Up @@ -58,10 +61,8 @@ protected function configure()
/**
* {@inheritdoc}
*/
protected function execute(
\Symfony\Component\Console\Input\InputInterface $input,
\Symfony\Component\Console\Output\OutputInterface $output
) {
protected function execute(InputInterface $input, OutputInterface $output)
{
$this->appState->setAreaCode(\Magento\Framework\App\Area::AREA_GLOBAL);

/** @var \Magento\Catalog\Model\ResourceModel\Product\Collection $productCollection */
Expand All @@ -73,6 +74,7 @@ protected function execute(
return \Magento\Framework\Console\Cli::RETURN_SUCCESS;
}

$errorMessage = '';
try {
foreach ($productIds as $productId) {
try {
Expand All @@ -82,9 +84,13 @@ protected function execute(
continue;
}

/** @var \Magento\Catalog\Model\Product\Image\Cache $imageCache */
$imageCache = $this->imageCacheFactory->create();
$imageCache->generate($product);
try {
/** @var \Magento\Catalog\Model\Product\Image\Cache $imageCache */
$imageCache = $this->imageCacheFactory->create();
$imageCache->generate($product);
} catch (\Magento\Framework\Exception\RuntimeException $e) {
$errorMessage = $e->getMessage();
}

$output->write(".");
}
Expand All @@ -95,6 +101,12 @@ protected function execute(
}

$output->write("\n");
$output->writeln("<info>Product images resized successfully</info>");
$output->writeln("<info>Product images resized successfully.</info>");

if ($errorMessage !== '') {
$output->writeln("<comment>{$errorMessage}</comment>");
}

return 0;
}
}
32 changes: 23 additions & 9 deletions app/code/Magento/Catalog/Model/Product/Attribute/Repository.php
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,9 @@ public function save(\Magento\Catalog\Api\Data\ProductAttributeInterface $attrib
$attribute->setAttributeId($existingModel->getAttributeId());
$attribute->setIsUserDefined($existingModel->getIsUserDefined());
$attribute->setFrontendInput($existingModel->getFrontendInput());
if ($attribute->getIsUserDefined()) {
$this->processAttributeData($attribute);
}

if (is_array($attribute->getFrontendLabels())) {
$defaultFrontendLabel = $attribute->getDefaultFrontendLabel();
Expand Down Expand Up @@ -156,15 +159,7 @@ public function save(\Magento\Catalog\Api\Data\ProductAttributeInterface $attrib
$this->validateCode($attribute->getAttributeCode());
$this->validateFrontendInput($attribute->getFrontendInput());

$attribute->setBackendType(
$attribute->getBackendTypeByInput($attribute->getFrontendInput())
);
$attribute->setSourceModel(
$this->productHelper->getAttributeSourceModelByInputType($attribute->getFrontendInput())
);
$attribute->setBackendModel(
$this->productHelper->getAttributeBackendModelByInputType($attribute->getFrontendInput())
);
$this->processAttributeData($attribute);
$attribute->setEntityTypeId(
$this->eavConfig
->getEntityType(\Magento\Catalog\Api\Data\ProductAttributeInterface::ENTITY_TYPE_CODE)
Expand Down Expand Up @@ -275,4 +270,23 @@ protected function validateFrontendInput($frontendInput)
throw InputException::invalidFieldValue('frontend_input', $frontendInput);
}
}

/**
* Process attribute data based on attribute frontend input type.
*
* @param \Magento\Catalog\Api\Data\ProductAttributeInterface $attribute
* @return void
*/
private function processAttributeData(\Magento\Catalog\Api\Data\ProductAttributeInterface $attribute)
{
$attribute->setBackendType(
$attribute->getBackendTypeByInput($attribute->getFrontendInput())
);
$attribute->setSourceModel(
$this->productHelper->getAttributeSourceModelByInputType($attribute->getFrontendInput())
);
$attribute->setBackendModel(
$this->productHelper->getAttributeBackendModelByInputType($attribute->getFrontendInput())
);
}
}
39 changes: 21 additions & 18 deletions app/code/Magento/Catalog/Model/Product/Copier.php
Original file line number Diff line number Diff line change
@@ -1,18 +1,24 @@
<?php
/**
* Catalog product copier. Creates product duplicate
*
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
namespace Magento\Catalog\Model\Product;

use Magento\Catalog\Api\Data\ProductInterface;
use Magento\Catalog\Model\Product;
use Magento\Framework\App\ObjectManager;
use Magento\Framework\EntityManager\MetadataPool;
use Magento\Catalog\Model\ProductFactory;
use Magento\Catalog\Model\Product\Option\Repository as OptionRepository;

/**
* Catalog product copier. Creates product duplicate
*/
class Copier
{
/**
* @var Option\Repository
* @var OptionRepository
*/
protected $optionRepository;

Expand All @@ -22,22 +28,22 @@ class Copier
protected $copyConstructor;

/**
* @var \Magento\Catalog\Model\ProductFactory
* @var ProductFactory
*/
protected $productFactory;

/**
* @var \Magento\Framework\EntityManager\MetadataPool
* @var MetadataPool
*/
protected $metadataPool;

/**
* @param CopyConstructorInterface $copyConstructor
* @param \Magento\Catalog\Model\ProductFactory $productFactory
* @param ProductFactory $productFactory
*/
public function __construct(
CopyConstructorInterface $copyConstructor,
\Magento\Catalog\Model\ProductFactory $productFactory
ProductFactory $productFactory
) {
$this->productFactory = $productFactory;
$this->copyConstructor = $copyConstructor;
Expand All @@ -46,18 +52,16 @@ public function __construct(
/**
* Create product duplicate
*
* @param \Magento\Catalog\Model\Product $product
* @return \Magento\Catalog\Model\Product
* @param Product $product
* @return Product
*/
public function copy(\Magento\Catalog\Model\Product $product)
public function copy(Product $product)
{
$product->getWebsiteIds();
$product->getCategoryIds();

/** @var \Magento\Framework\EntityManager\EntityMetadataInterface $metadata */
$metadata = $this->getMetadataPool()->getMetadata(ProductInterface::class);

/** @var \Magento\Catalog\Model\Product $duplicate */
$duplicate = $this->productFactory->create();
$productData = $product->getData();
$productData = $this->removeStockItem($productData);
Expand All @@ -83,6 +87,7 @@ public function copy(\Magento\Catalog\Model\Product $product)
$duplicate->save();
$isDuplicateSaved = true;
} catch (\Magento\Framework\Exception\AlreadyExistsException $e) {
} catch (\Magento\UrlRewrite\Model\Exception\UrlAlreadyExistsException $e) {
}
} while (!$isDuplicateSaved);
$this->getOptionRepository()->duplicate($product, $duplicate);
Expand All @@ -94,27 +99,25 @@ public function copy(\Magento\Catalog\Model\Product $product)
}

/**
* @return Option\Repository
* @return OptionRepository
* @deprecated 101.0.0
*/
private function getOptionRepository()
{
if (null === $this->optionRepository) {
$this->optionRepository = \Magento\Framework\App\ObjectManager::getInstance()
->get(\Magento\Catalog\Model\Product\Option\Repository::class);
$this->optionRepository = ObjectManager::getInstance()->get(OptionRepository::class);
}
return $this->optionRepository;
}

/**
* @return \Magento\Framework\EntityManager\MetadataPool
* @return MetadataPool
* @deprecated 101.0.0
*/
private function getMetadataPool()
{
if (null === $this->metadataPool) {
$this->metadataPool = \Magento\Framework\App\ObjectManager::getInstance()
->get(\Magento\Framework\EntityManager\MetadataPool::class);
$this->metadataPool = ObjectManager::getInstance()->get(MetadataPool::class);
}
return $this->metadataPool;
}
Expand Down
33 changes: 30 additions & 3 deletions app/code/Magento/Catalog/Model/Product/Image/Cache.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
use Magento\Theme\Model\ResourceModel\Theme\Collection as ThemeCollection;
use Magento\Framework\App\Area;
use Magento\Framework\View\ConfigInterface;
use Psr\Log\LoggerInterface;

class Cache
{
Expand All @@ -33,19 +34,29 @@ class Cache
*/
protected $data = [];

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

/**
* @param ConfigInterface $viewConfig
* @param ThemeCollection $themeCollection
* @param ImageHelper $imageHelper
* @param LoggerInterface $logger
*/
public function __construct(
ConfigInterface $viewConfig,
ThemeCollection $themeCollection,
ImageHelper $imageHelper
ImageHelper $imageHelper,
LoggerInterface $logger = null
) {
$this->viewConfig = $viewConfig;
$this->themeCollection = $themeCollection;
$this->imageHelper = $imageHelper;
$this->logger = $logger ?: \Magento\Framework\App\ObjectManager::getInstance()->get(LoggerInterface::class);
}

/**
Expand Down Expand Up @@ -74,21 +85,37 @@ protected function getData()
}

/**
* Resize product images and save results to image cache
* Resize product images and save results to image cache.
*
* @param Product $product
*
* @return $this
* @throws \Exception
*/
public function generate(Product $product)
{
$isException = false;
$galleryImages = $product->getMediaGalleryImages();
if ($galleryImages) {
foreach ($galleryImages as $image) {
foreach ($this->getData() as $imageData) {
$this->processImageData($product, $imageData, $image->getFile());
try {
$this->processImageData($product, $imageData, $image->getFile());
} catch (\Exception $e) {
$isException = true;
$this->logger->error($e->getMessage());
$this->logger->error(__('The image could not be resized: ') . $image->getPath());
}
}
}
}

if ($isException === true) {
throw new \Magento\Framework\Exception\RuntimeException(
__('Some images could not be resized. See log file for details.')
);
}

return $this;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -208,4 +208,44 @@ protected function prepareImageCache()
->method('create')
->willReturn($this->imageCache);
}

public function testExecuteWithExceptionInGenerate()
{
$productsIds = [1, 2];

$this->appState->expects($this->once())
->method('setAreaCode')
->with(Area::AREA_GLOBAL)
->willReturnSelf();

$this->productCollection->expects($this->once())
->method('getAllIds')
->willReturn($productsIds);

$productMock = $this->getMockBuilder(\Magento\Catalog\Model\Product::class)
->disableOriginalConstructor()
->getMock();

$this->productRepository->expects($this->at(0))
->method('getById')
->with($productsIds[0])
->willReturn($productMock);
$this->productRepository->expects($this->at(1))
->method('getById')
->with($productsIds[1])
->willThrowException(new NoSuchEntityException());

$this->imageCache->expects($this->exactly(count($productsIds) - 1))
->method('generate')
->with($productMock)
->willThrowException(new \Magento\Framework\Exception\RuntimeException(__('Some text is here.')));

$commandTester = new CommandTester($this->command);
$commandTester->execute([]);

$this->assertContains(
'Some text is here.',
$commandTester->getDisplay()
);
}
}
Loading

0 comments on commit 7d00d28

Please sign in to comment.