Skip to content

Commit

Permalink
Merge pull request #5161 from magento-tsg-csl3/2.4-develop-pr5
Browse files Browse the repository at this point in the history
[TSG-CSL3] Forwardport for 2.4 (pr5)
  • Loading branch information
zakdma authored Dec 26, 2019
2 parents fa00ff7 + 7502475 commit 6159dc8
Show file tree
Hide file tree
Showing 17 changed files with 759 additions and 176 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ protected function setUp()
$this->locatorMock = $this->getMockBuilder(LocatorInterface::class)
->getMockForAbstractClass();
$this->productMock = $this->getMockBuilder(ProductInterface::class)
->setMethods(['getPriceType'])
->getMockForAbstractClass();

$this->locatorMock->expects($this->any())
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,173 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);

namespace Magento\Bundle\Test\Unit\Ui\DataProvider\Product\Form\Modifier;

use Magento\Bundle\Ui\DataProvider\Product\Form\Modifier\BundlePrice;
use Magento\Catalog\Api\Data\ProductAttributeInterface;
use Magento\Framework\Stdlib\ArrayManager;

class BundlePriceTest extends AbstractModifierTest
{
/**
* @return BundlePrice
*/
protected function createModel()
{
return $this->objectManager->getObject(
BundlePrice::class,
[
'locator' => $this->locatorMock,
'arrayManager' => $this->arrayManagerMock
]
);
}

/**
* @SuppressWarnings(PHPMD.ExcessiveMethodLength)
*/
public function testModifyMeta()
{
$this->productMock->expects($this->any())
->method('getId')
->willReturn(true);
$this->productMock->expects($this->any())
->method('getPriceType')
->willReturn(0);
$priceTypePath = 'bundle-items/children/' . BundlePrice::CODE_PRICE_TYPE;
$priceTypeConfigPath = $priceTypePath . BundlePrice::META_CONFIG_PATH;
$pricePath = 'product-details/children/' . ProductAttributeInterface::CODE_PRICE;
$priceConfigPath = $pricePath . BundlePrice::META_CONFIG_PATH;
$sourceMeta = [
'bundle-items' => [
'children' => [
BundlePrice::CODE_PRICE_TYPE => []
]
]
];
$priceTypeParams = [
'disabled' => true,
'valueMap' => [
'false' => '1',
'true' => '0'
],
'validation' => [
'required-entry' => false
]
];
$priceTypeMeta = [
'bundle-items' => [
'children' => [
BundlePrice::CODE_PRICE_TYPE => $priceTypeParams
]
]
];
$priceParams = [
'imports' => [
'disabled' => 'ns = ${ $.ns }, index = ' . BundlePrice::CODE_PRICE_TYPE . ':checked'
]
];
$priceMeta = [
'product-details' => [
'children' => [
BundlePrice::CODE_PRICE_TYPE => []
]
],
'bundle-items' => [
'children' => [
ProductAttributeInterface::CODE_PRICE => $priceParams
]
]
];
$taxParams = [
'service' => [
'template' => ''
]
];

$this->arrayManagerMock->expects($this->any())
->method('findPath')
->willReturnMap(
[
[
BundlePrice::CODE_PRICE_TYPE,
$sourceMeta,
null,
'children',
ArrayManager::DEFAULT_PATH_DELIMITER,
$priceTypePath
],
[
ProductAttributeInterface::CODE_PRICE,
$priceTypeMeta,
BundlePrice::DEFAULT_GENERAL_PANEL . '/children',
'children',
ArrayManager::DEFAULT_PATH_DELIMITER,
$pricePath
],
[
BundlePrice::CODE_TAX_CLASS_ID,
$priceMeta,
null,
'children',
ArrayManager::DEFAULT_PATH_DELIMITER,
$pricePath
],
[
BundlePrice::CODE_TAX_CLASS_ID,
$priceMeta,
null,
'children',
ArrayManager::DEFAULT_PATH_DELIMITER,
$pricePath
]
]
);
$this->arrayManagerMock->expects($this->exactly(4))
->method('merge')
->willReturnMap(
[
[
$priceTypeConfigPath,
$sourceMeta,
$priceTypeParams,
ArrayManager::DEFAULT_PATH_DELIMITER,
$priceTypeMeta
],
[
$priceConfigPath,
$priceTypeMeta,
$priceParams,
ArrayManager::DEFAULT_PATH_DELIMITER,
$priceMeta
],
[
$priceConfigPath,
$priceMeta,
$priceParams,
ArrayManager::DEFAULT_PATH_DELIMITER,
$priceMeta
],
[
$priceConfigPath,
$priceMeta,
$taxParams,
ArrayManager::DEFAULT_PATH_DELIMITER,
$priceMeta
]
]
);

$this->assertSame($priceMeta, $this->getModel()->modifyMeta($sourceMeta));
}

public function testModifyData()
{
$expectedData = [];
$this->assertEquals($expectedData, $this->getModel()->modifyData($expectedData));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
*/
namespace Magento\Bundle\Ui\DataProvider\Product\Form\Modifier;

use Magento\Bundle\Model\Product\Price;
use Magento\Catalog\Ui\DataProvider\Product\Form\Modifier\AbstractModifier;
use Magento\Catalog\Api\Data\ProductAttributeInterface;
use Magento\Framework\Stdlib\ArrayManager;
Expand Down Expand Up @@ -39,7 +40,7 @@ public function __construct(
$this->locator = $locator;
$this->arrayManager = $arrayManager;
}

/**
* @inheritdoc
*/
Expand Down Expand Up @@ -89,7 +90,22 @@ public function modifyMeta(array $meta)
]
]
);

if ($this->locator->getProduct()->getPriceType() == Price::PRICE_TYPE_DYNAMIC) {
$meta = $this->arrayManager->merge(
$this->arrayManager->findPath(
static::CODE_TAX_CLASS_ID,
$meta,
null,
'children'
) . static::META_CONFIG_PATH,
$meta,
[
'service' => [
'template' => ''
]
]
);
}
return $meta;
}

Expand Down
68 changes: 35 additions & 33 deletions app/code/Magento/Catalog/Model/Category/Attribute/Backend/Image.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,12 @@
*/
namespace Magento\Catalog\Model\Category\Attribute\Backend;

use Magento\Catalog\Model\ImageUploader;
use Magento\Framework\App\Filesystem\DirectoryList;
use Magento\Framework\App\ObjectManager;
use Magento\Framework\File\Uploader;
use Magento\Store\Api\Data\StoreInterface;
use Magento\Store\Model\StoreManagerInterface;

/**
* Catalog category image attribute backend model
Expand Down Expand Up @@ -45,7 +49,7 @@ class Image extends \Magento\Eav\Model\Entity\Attribute\Backend\AbstractBackend
protected $_logger;

/**
* @var \Magento\Catalog\Model\ImageUploader
* @var ImageUploader
*/
private $imageUploader;

Expand All @@ -54,19 +58,32 @@ class Image extends \Magento\Eav\Model\Entity\Attribute\Backend\AbstractBackend
*/
private $additionalData = '_additional_data_';

/**
* @var StoreManagerInterface
*/
private $storeManager;

/**
* @param \Psr\Log\LoggerInterface $logger
* @param \Magento\Framework\Filesystem $filesystem
* @param \Magento\MediaStorage\Model\File\UploaderFactory $fileUploaderFactory
* @param StoreManagerInterface $storeManager
* @param ImageUploader $imageUploader
*/
public function __construct(
\Psr\Log\LoggerInterface $logger,
\Magento\Framework\Filesystem $filesystem,
\Magento\MediaStorage\Model\File\UploaderFactory $fileUploaderFactory
\Magento\MediaStorage\Model\File\UploaderFactory $fileUploaderFactory,
StoreManagerInterface $storeManager = null,
ImageUploader $imageUploader = null
) {
$this->_filesystem = $filesystem;
$this->_fileUploaderFactory = $fileUploaderFactory;
$this->_logger = $logger;
$this->storeManager = $storeManager ??
ObjectManager::getInstance()->get(StoreManagerInterface::class);
$this->imageUploader = $imageUploader ??
ObjectManager::getInstance()->get(ImageUploader::class);
}

/**
Expand Down Expand Up @@ -94,13 +111,13 @@ private function getUploadedImageName($value)
*/
private function checkUniqueImageName(string $imageName): string
{
$imageUploader = $this->getImageUploader();
$mediaDirectory = $this->_filesystem->getDirectoryWrite(DirectoryList::MEDIA);
$imageAbsolutePath = $mediaDirectory->getAbsolutePath(
$imageUploader->getBasePath() . DIRECTORY_SEPARATOR . $imageName
$this->imageUploader->getBasePath() . DIRECTORY_SEPARATOR . $imageName
);

$imageName = Uploader::getNewFilename($imageAbsolutePath);
// phpcs:ignore Magento2.Functions.DiscouragedFunction
$imageName = call_user_func([Uploader::class, 'getNewFilename'], $imageAbsolutePath);

return $imageName;
}
Expand All @@ -119,7 +136,18 @@ public function beforeSave($object)
$attributeName = $this->getAttribute()->getName();
$value = $object->getData($attributeName);

if ($this->fileResidesOutsideCategoryDir($value)) {
if ($this->isTmpFileAvailable($value) && $imageName = $this->getUploadedImageName($value)) {
try {
/** @var StoreInterface $store */
$store = $this->storeManager->getStore();
$baseMediaDir = $store->getBaseMediaDir();
$newImgRelativePath = $this->imageUploader->moveFileFromTmp($imageName, true);
$value[0]['url'] = '/' . $baseMediaDir . '/' . $newImgRelativePath;
$value[0]['name'] = $value[0]['url'];
} catch (\Exception $e) {
$this->_logger->critical($e);
}
} elseif ($this->fileResidesOutsideCategoryDir($value)) {
// use relative path for image attribute so we know it's outside of category dir when we fetch it
// phpcs:ignore Magento2.Functions.DiscouragedFunction
$value[0]['url'] = parse_url($value[0]['url'], PHP_URL_PATH);
Expand All @@ -139,23 +167,6 @@ public function beforeSave($object)
return parent::beforeSave($object);
}

/**
* Get Instance of Category Image Uploader.
*
* @return \Magento\Catalog\Model\ImageUploader
*
* @deprecated 101.0.0
*/
private function getImageUploader()
{
if ($this->imageUploader === null) {
$this->imageUploader = \Magento\Framework\App\ObjectManager::getInstance()
->get(\Magento\Catalog\CategoryImageUpload::class);
}

return $this->imageUploader;
}

/**
* Check if temporary file is available for new image upload.
*
Expand Down Expand Up @@ -194,19 +205,10 @@ private function fileResidesOutsideCategoryDir($value)
*
* @param \Magento\Framework\DataObject $object
* @return \Magento\Catalog\Model\Category\Attribute\Backend\Image
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
*/
public function afterSave($object)
{
$value = $object->getData($this->additionalData . $this->getAttribute()->getName());

if ($this->isTmpFileAvailable($value) && $imageName = $this->getUploadedImageName($value)) {
try {
$this->getImageUploader()->moveFileFromTmp($imageName);
} catch (\Exception $e) {
$this->_logger->critical($e);
}
}

return $this;
}
}
6 changes: 3 additions & 3 deletions app/code/Magento/Catalog/Model/ImageUploader.php
Original file line number Diff line number Diff line change
Expand Up @@ -191,12 +191,12 @@ public function getFilePath($path, $imageName)
* Checking file for moving and move it
*
* @param string $imageName
*
* @param bool $returnRelativePath
* @return string
*
* @throws \Magento\Framework\Exception\LocalizedException
*/
public function moveFileFromTmp($imageName)
public function moveFileFromTmp($imageName, $returnRelativePath = false)
{
$baseTmpPath = $this->getBaseTmpPath();
$basePath = $this->getBasePath();
Expand Down Expand Up @@ -226,7 +226,7 @@ public function moveFileFromTmp($imageName)
);
}

return $imageName;
return $returnRelativePath ? $baseImagePath : $imageName;
}

/**
Expand Down
Loading

0 comments on commit 6159dc8

Please sign in to comment.