Skip to content

Commit 6159dc8

Browse files
authored
Merge pull request #5161 from magento-tsg-csl3/2.4-develop-pr5
[TSG-CSL3] Forwardport for 2.4 (pr5)
2 parents fa00ff7 + 7502475 commit 6159dc8

File tree

17 files changed

+759
-176
lines changed

17 files changed

+759
-176
lines changed

app/code/Magento/Bundle/Test/Unit/Ui/DataProvider/Product/Form/Modifier/AbstractModifierTest.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ protected function setUp()
4545
$this->locatorMock = $this->getMockBuilder(LocatorInterface::class)
4646
->getMockForAbstractClass();
4747
$this->productMock = $this->getMockBuilder(ProductInterface::class)
48+
->setMethods(['getPriceType'])
4849
->getMockForAbstractClass();
4950

5051
$this->locatorMock->expects($this->any())
Lines changed: 173 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,173 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
declare(strict_types=1);
7+
8+
namespace Magento\Bundle\Test\Unit\Ui\DataProvider\Product\Form\Modifier;
9+
10+
use Magento\Bundle\Ui\DataProvider\Product\Form\Modifier\BundlePrice;
11+
use Magento\Catalog\Api\Data\ProductAttributeInterface;
12+
use Magento\Framework\Stdlib\ArrayManager;
13+
14+
class BundlePriceTest extends AbstractModifierTest
15+
{
16+
/**
17+
* @return BundlePrice
18+
*/
19+
protected function createModel()
20+
{
21+
return $this->objectManager->getObject(
22+
BundlePrice::class,
23+
[
24+
'locator' => $this->locatorMock,
25+
'arrayManager' => $this->arrayManagerMock
26+
]
27+
);
28+
}
29+
30+
/**
31+
* @SuppressWarnings(PHPMD.ExcessiveMethodLength)
32+
*/
33+
public function testModifyMeta()
34+
{
35+
$this->productMock->expects($this->any())
36+
->method('getId')
37+
->willReturn(true);
38+
$this->productMock->expects($this->any())
39+
->method('getPriceType')
40+
->willReturn(0);
41+
$priceTypePath = 'bundle-items/children/' . BundlePrice::CODE_PRICE_TYPE;
42+
$priceTypeConfigPath = $priceTypePath . BundlePrice::META_CONFIG_PATH;
43+
$pricePath = 'product-details/children/' . ProductAttributeInterface::CODE_PRICE;
44+
$priceConfigPath = $pricePath . BundlePrice::META_CONFIG_PATH;
45+
$sourceMeta = [
46+
'bundle-items' => [
47+
'children' => [
48+
BundlePrice::CODE_PRICE_TYPE => []
49+
]
50+
]
51+
];
52+
$priceTypeParams = [
53+
'disabled' => true,
54+
'valueMap' => [
55+
'false' => '1',
56+
'true' => '0'
57+
],
58+
'validation' => [
59+
'required-entry' => false
60+
]
61+
];
62+
$priceTypeMeta = [
63+
'bundle-items' => [
64+
'children' => [
65+
BundlePrice::CODE_PRICE_TYPE => $priceTypeParams
66+
]
67+
]
68+
];
69+
$priceParams = [
70+
'imports' => [
71+
'disabled' => 'ns = ${ $.ns }, index = ' . BundlePrice::CODE_PRICE_TYPE . ':checked'
72+
]
73+
];
74+
$priceMeta = [
75+
'product-details' => [
76+
'children' => [
77+
BundlePrice::CODE_PRICE_TYPE => []
78+
]
79+
],
80+
'bundle-items' => [
81+
'children' => [
82+
ProductAttributeInterface::CODE_PRICE => $priceParams
83+
]
84+
]
85+
];
86+
$taxParams = [
87+
'service' => [
88+
'template' => ''
89+
]
90+
];
91+
92+
$this->arrayManagerMock->expects($this->any())
93+
->method('findPath')
94+
->willReturnMap(
95+
[
96+
[
97+
BundlePrice::CODE_PRICE_TYPE,
98+
$sourceMeta,
99+
null,
100+
'children',
101+
ArrayManager::DEFAULT_PATH_DELIMITER,
102+
$priceTypePath
103+
],
104+
[
105+
ProductAttributeInterface::CODE_PRICE,
106+
$priceTypeMeta,
107+
BundlePrice::DEFAULT_GENERAL_PANEL . '/children',
108+
'children',
109+
ArrayManager::DEFAULT_PATH_DELIMITER,
110+
$pricePath
111+
],
112+
[
113+
BundlePrice::CODE_TAX_CLASS_ID,
114+
$priceMeta,
115+
null,
116+
'children',
117+
ArrayManager::DEFAULT_PATH_DELIMITER,
118+
$pricePath
119+
],
120+
[
121+
BundlePrice::CODE_TAX_CLASS_ID,
122+
$priceMeta,
123+
null,
124+
'children',
125+
ArrayManager::DEFAULT_PATH_DELIMITER,
126+
$pricePath
127+
]
128+
]
129+
);
130+
$this->arrayManagerMock->expects($this->exactly(4))
131+
->method('merge')
132+
->willReturnMap(
133+
[
134+
[
135+
$priceTypeConfigPath,
136+
$sourceMeta,
137+
$priceTypeParams,
138+
ArrayManager::DEFAULT_PATH_DELIMITER,
139+
$priceTypeMeta
140+
],
141+
[
142+
$priceConfigPath,
143+
$priceTypeMeta,
144+
$priceParams,
145+
ArrayManager::DEFAULT_PATH_DELIMITER,
146+
$priceMeta
147+
],
148+
[
149+
$priceConfigPath,
150+
$priceMeta,
151+
$priceParams,
152+
ArrayManager::DEFAULT_PATH_DELIMITER,
153+
$priceMeta
154+
],
155+
[
156+
$priceConfigPath,
157+
$priceMeta,
158+
$taxParams,
159+
ArrayManager::DEFAULT_PATH_DELIMITER,
160+
$priceMeta
161+
]
162+
]
163+
);
164+
165+
$this->assertSame($priceMeta, $this->getModel()->modifyMeta($sourceMeta));
166+
}
167+
168+
public function testModifyData()
169+
{
170+
$expectedData = [];
171+
$this->assertEquals($expectedData, $this->getModel()->modifyData($expectedData));
172+
}
173+
}

app/code/Magento/Bundle/Ui/DataProvider/Product/Form/Modifier/BundlePrice.php

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
*/
66
namespace Magento\Bundle\Ui\DataProvider\Product\Form\Modifier;
77

8+
use Magento\Bundle\Model\Product\Price;
89
use Magento\Catalog\Ui\DataProvider\Product\Form\Modifier\AbstractModifier;
910
use Magento\Catalog\Api\Data\ProductAttributeInterface;
1011
use Magento\Framework\Stdlib\ArrayManager;
@@ -39,7 +40,7 @@ public function __construct(
3940
$this->locator = $locator;
4041
$this->arrayManager = $arrayManager;
4142
}
42-
43+
4344
/**
4445
* @inheritdoc
4546
*/
@@ -89,7 +90,22 @@ public function modifyMeta(array $meta)
8990
]
9091
]
9192
);
92-
93+
if ($this->locator->getProduct()->getPriceType() == Price::PRICE_TYPE_DYNAMIC) {
94+
$meta = $this->arrayManager->merge(
95+
$this->arrayManager->findPath(
96+
static::CODE_TAX_CLASS_ID,
97+
$meta,
98+
null,
99+
'children'
100+
) . static::META_CONFIG_PATH,
101+
$meta,
102+
[
103+
'service' => [
104+
'template' => ''
105+
]
106+
]
107+
);
108+
}
93109
return $meta;
94110
}
95111

app/code/Magento/Catalog/Model/Category/Attribute/Backend/Image.php

Lines changed: 35 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,12 @@
55
*/
66
namespace Magento\Catalog\Model\Category\Attribute\Backend;
77

8+
use Magento\Catalog\Model\ImageUploader;
89
use Magento\Framework\App\Filesystem\DirectoryList;
10+
use Magento\Framework\App\ObjectManager;
911
use Magento\Framework\File\Uploader;
12+
use Magento\Store\Api\Data\StoreInterface;
13+
use Magento\Store\Model\StoreManagerInterface;
1014

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

4751
/**
48-
* @var \Magento\Catalog\Model\ImageUploader
52+
* @var ImageUploader
4953
*/
5054
private $imageUploader;
5155

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

61+
/**
62+
* @var StoreManagerInterface
63+
*/
64+
private $storeManager;
65+
5766
/**
5867
* @param \Psr\Log\LoggerInterface $logger
5968
* @param \Magento\Framework\Filesystem $filesystem
6069
* @param \Magento\MediaStorage\Model\File\UploaderFactory $fileUploaderFactory
70+
* @param StoreManagerInterface $storeManager
71+
* @param ImageUploader $imageUploader
6172
*/
6273
public function __construct(
6374
\Psr\Log\LoggerInterface $logger,
6475
\Magento\Framework\Filesystem $filesystem,
65-
\Magento\MediaStorage\Model\File\UploaderFactory $fileUploaderFactory
76+
\Magento\MediaStorage\Model\File\UploaderFactory $fileUploaderFactory,
77+
StoreManagerInterface $storeManager = null,
78+
ImageUploader $imageUploader = null
6679
) {
6780
$this->_filesystem = $filesystem;
6881
$this->_fileUploaderFactory = $fileUploaderFactory;
6982
$this->_logger = $logger;
83+
$this->storeManager = $storeManager ??
84+
ObjectManager::getInstance()->get(StoreManagerInterface::class);
85+
$this->imageUploader = $imageUploader ??
86+
ObjectManager::getInstance()->get(ImageUploader::class);
7087
}
7188

7289
/**
@@ -94,13 +111,13 @@ private function getUploadedImageName($value)
94111
*/
95112
private function checkUniqueImageName(string $imageName): string
96113
{
97-
$imageUploader = $this->getImageUploader();
98114
$mediaDirectory = $this->_filesystem->getDirectoryWrite(DirectoryList::MEDIA);
99115
$imageAbsolutePath = $mediaDirectory->getAbsolutePath(
100-
$imageUploader->getBasePath() . DIRECTORY_SEPARATOR . $imageName
116+
$this->imageUploader->getBasePath() . DIRECTORY_SEPARATOR . $imageName
101117
);
102118

103-
$imageName = Uploader::getNewFilename($imageAbsolutePath);
119+
// phpcs:ignore Magento2.Functions.DiscouragedFunction
120+
$imageName = call_user_func([Uploader::class, 'getNewFilename'], $imageAbsolutePath);
104121

105122
return $imageName;
106123
}
@@ -119,7 +136,18 @@ public function beforeSave($object)
119136
$attributeName = $this->getAttribute()->getName();
120137
$value = $object->getData($attributeName);
121138

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

142-
/**
143-
* Get Instance of Category Image Uploader.
144-
*
145-
* @return \Magento\Catalog\Model\ImageUploader
146-
*
147-
* @deprecated 101.0.0
148-
*/
149-
private function getImageUploader()
150-
{
151-
if ($this->imageUploader === null) {
152-
$this->imageUploader = \Magento\Framework\App\ObjectManager::getInstance()
153-
->get(\Magento\Catalog\CategoryImageUpload::class);
154-
}
155-
156-
return $this->imageUploader;
157-
}
158-
159170
/**
160171
* Check if temporary file is available for new image upload.
161172
*
@@ -194,19 +205,10 @@ private function fileResidesOutsideCategoryDir($value)
194205
*
195206
* @param \Magento\Framework\DataObject $object
196207
* @return \Magento\Catalog\Model\Category\Attribute\Backend\Image
208+
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
197209
*/
198210
public function afterSave($object)
199211
{
200-
$value = $object->getData($this->additionalData . $this->getAttribute()->getName());
201-
202-
if ($this->isTmpFileAvailable($value) && $imageName = $this->getUploadedImageName($value)) {
203-
try {
204-
$this->getImageUploader()->moveFileFromTmp($imageName);
205-
} catch (\Exception $e) {
206-
$this->_logger->critical($e);
207-
}
208-
}
209-
210212
return $this;
211213
}
212214
}

app/code/Magento/Catalog/Model/ImageUploader.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -191,12 +191,12 @@ public function getFilePath($path, $imageName)
191191
* Checking file for moving and move it
192192
*
193193
* @param string $imageName
194-
*
194+
* @param bool $returnRelativePath
195195
* @return string
196196
*
197197
* @throws \Magento\Framework\Exception\LocalizedException
198198
*/
199-
public function moveFileFromTmp($imageName)
199+
public function moveFileFromTmp($imageName, $returnRelativePath = false)
200200
{
201201
$baseTmpPath = $this->getBaseTmpPath();
202202
$basePath = $this->getBasePath();
@@ -226,7 +226,7 @@ public function moveFileFromTmp($imageName)
226226
);
227227
}
228228

229-
return $imageName;
229+
return $returnRelativePath ? $baseImagePath : $imageName;
230230
}
231231

232232
/**

0 commit comments

Comments
 (0)