Skip to content

Commit 06a6bbb

Browse files
Merge remote-tracking branch 'origin/MC-35123' into 2.4-develop-pr34
2 parents 1ee7e1f + 092f5a9 commit 06a6bbb

File tree

10 files changed

+163
-40
lines changed

10 files changed

+163
-40
lines changed

app/code/Magento/Bundle/Test/Unit/Model/Product/TypeTest.php

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
use Magento\Bundle\Model\Product\Type;
1212
use Magento\Bundle\Model\ResourceModel\BundleFactory;
1313
use Magento\Bundle\Model\ResourceModel\Option\Collection;
14+
use Magento\CatalogRule\Model\ResourceModel\Product\CollectionProcessor;
1415
use Magento\Bundle\Model\ResourceModel\Selection\Collection as SelectionCollection;
1516
use Magento\Bundle\Model\ResourceModel\Selection\CollectionFactory;
1617
use Magento\Bundle\Model\Selection;
@@ -42,6 +43,8 @@
4243
use PHPUnit\Framework\TestCase;
4344

4445
/**
46+
* Test for bundle product type
47+
*
4548
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
4649
*/
4750
class TypeTest extends TestCase
@@ -116,6 +119,11 @@ class TypeTest extends TestCase
116119
*/
117120
private $arrayUtility;
118121

122+
/**
123+
* @var CollectionProcessor|MockObject
124+
*/
125+
private $catalogRuleProcessor;
126+
119127
/**
120128
* @return void
121129
*/
@@ -172,20 +180,20 @@ protected function setUp(): void
172180
->setMethods(['create'])
173181
->disableOriginalConstructor()
174182
->getMock();
175-
176183
$this->serializer = $this->getMockBuilder(Json::class)
177184
->setMethods(null)
178185
->disableOriginalConstructor()
179186
->getMock();
180-
181187
$this->metadataPool = $this->getMockBuilder(MetadataPool::class)
182188
->disableOriginalConstructor()
183189
->getMock();
184-
185190
$this->arrayUtility = $this->getMockBuilder(ArrayUtils::class)
186191
->setMethods(['flatten'])
187192
->disableOriginalConstructor()
188193
->getMock();
194+
$this->catalogRuleProcessor = $this->getMockBuilder(CollectionProcessor::class)
195+
->disableOriginalConstructor()
196+
->getMock();
189197

190198
$objectHelper = new ObjectManager($this);
191199
$this->model = $objectHelper->getObject(
@@ -1542,7 +1550,7 @@ public function testPrepareForCartAdvancedSpecifyProductOptions()
15421550

15431551
$this->parentClass($group, $option, $buyRequest, $product);
15441552

1545-
$product->expects($this->once())
1553+
$product->expects($this->any())
15461554
->method('getSkipCheckRequiredOption')
15471555
->willReturn(true);
15481556
$buyRequest->expects($this->once())
@@ -2424,9 +2432,6 @@ protected function parentClass($group, $option, $buyRequest, $product)
24242432
$group->expects($this->once())
24252433
->method('setProcessMode')
24262434
->willReturnSelf();
2427-
$group->expects($this->once())
2428-
->method('validateUserValue')
2429-
->willReturnSelf();
24302435
$group->expects($this->once())
24312436
->method('prepareForCart')
24322437
->willReturn('someString');

app/code/Magento/Catalog/Model/Product/Option/Type/File.php

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,9 @@
1616
/**
1717
* Catalog product option file type
1818
*
19-
* @author Magento Core Team <core@magentocommerce.com>
19+
* @SuppressWarnings(PHPMD.ExcessiveClassComplexity)
2020
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
21+
* @SuppressWarnings(PHPMD.CookieAndSessionMisuse)
2122
*/
2223
class File extends \Magento\Catalog\Model\Product\Option\Type\DefaultType
2324
{
@@ -181,6 +182,7 @@ protected function _getProcessingParams()
181182

182183
/**
183184
* Returns file info array if we need to get file from already existing file.
185+
*
184186
* Or returns null, if we need to get file from uploaded array.
185187
*
186188
* @return null|array
@@ -262,7 +264,6 @@ public function validateUserValue($values)
262264
. "Make sure the options are entered and try again."
263265
)
264266
);
265-
break;
266267
default:
267268
$this->setUserValue(null);
268269
break;
@@ -330,7 +331,11 @@ public function prepareForCart()
330331
public function getFormattedOptionValue($optionValue)
331332
{
332333
if ($this->_formattedOptionValue === null) {
333-
$value = $this->serializer->unserialize($optionValue);
334+
try {
335+
$value = $this->serializer->unserialize($optionValue);
336+
} catch (\InvalidArgumentException $e) {
337+
return $optionValue;
338+
}
334339
if ($value === null) {
335340
return $optionValue;
336341
}
@@ -476,13 +481,13 @@ public function copyQuoteToOrder()
476481
try {
477482
$value = $this->serializer->unserialize($quoteOption->getValue());
478483
if (!isset($value['quote_path'])) {
479-
throw new \Exception();
484+
return $this;
480485
}
481486
$quotePath = $value['quote_path'];
482487
$orderPath = $value['order_path'];
483488

484489
if (!$this->mediaDirectory->isFile($quotePath) || !$this->mediaDirectory->isReadable($quotePath)) {
485-
throw new \Exception();
490+
return $this;
486491
}
487492

488493
if ($this->_coreFileStorageDatabase->checkDbUsage()) {
@@ -524,6 +529,8 @@ protected function _getOptionDownloadUrl($route, $params)
524529
}
525530

526531
/**
532+
* Prepare size
533+
*
527534
* @param array $value
528535
* @return string
529536
*/

app/code/Magento/Catalog/Model/Product/Type/AbstractType.php

Lines changed: 30 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,15 +9,18 @@
99
use Magento\Catalog\Api\ProductRepositoryInterface;
1010
use Magento\Framework\App\Filesystem\DirectoryList;
1111
use Magento\Framework\Exception\LocalizedException;
12+
use Magento\Framework\App\ObjectManager;
1213

1314
/**
14-
* @api
1515
* Abstract model for product type implementation
16+
*
17+
* phpcs:disable Magento2.Classes.AbstractApi
18+
* @api
19+
* @since 100.0.2
1620
* @SuppressWarnings(PHPMD.ExcessivePublicCount)
1721
* @SuppressWarnings(PHPMD.TooManyFields)
1822
* @SuppressWarnings(PHPMD.ExcessiveClassComplexity)
1923
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
20-
* @since 100.0.2
2124
*/
2225
abstract class AbstractType
2326
{
@@ -207,7 +210,7 @@ public function __construct(
207210
$this->_filesystem = $filesystem;
208211
$this->_logger = $logger;
209212
$this->productRepository = $productRepository;
210-
$this->serializer = $serializer ?: \Magento\Framework\App\ObjectManager::getInstance()
213+
$this->serializer = $serializer ?: ObjectManager::getInstance()
211214
->get(\Magento\Framework\Serialize\Serializer\Json::class);
212215
}
213216

@@ -355,6 +358,7 @@ public function isSalable($product)
355358

356359
/**
357360
* Prepare product and its configuration to be added to some products list.
361+
*
358362
* Perform standard preparation process and then prepare options belonging to specific product type.
359363
*
360364
* @param \Magento\Framework\DataObject $buyRequest
@@ -440,6 +444,7 @@ public function processConfiguration(
440444

441445
/**
442446
* Initialize product(s) for add to cart process.
447+
*
443448
* Advanced version of func to prepare product for cart - processMode can be specified there.
444449
*
445450
* @param \Magento\Framework\DataObject $buyRequest
@@ -476,6 +481,7 @@ public function prepareForCart(\Magento\Framework\DataObject $buyRequest, $produ
476481
* @throws \Magento\Framework\Exception\LocalizedException
477482
* @SuppressWarnings(PHPMD.CyclomaticComplexity)
478483
* @SuppressWarnings(PHPMD.NPathComplexity)
484+
* phpcs:disable Generic.Metrics.NestingLevel
479485
*/
480486
public function processFileQueue()
481487
{
@@ -492,6 +498,7 @@ public function processFileQueue()
492498
/** @var $uploader \Zend_File_Transfer_Adapter_Http */
493499
$uploader = isset($queueOptions['uploader']) ? $queueOptions['uploader'] : null;
494500

501+
// phpcs:ignore Magento2.Functions.DiscouragedFunction
495502
$path = dirname($dst);
496503

497504
try {
@@ -529,9 +536,11 @@ public function processFileQueue()
529536

530537
return $this;
531538
}
539+
//phpcs:enable
532540

533541
/**
534542
* Add file to File Queue
543+
*
535544
* @param array $queueOptions Array of File Queue
536545
* (eg. ['operation'=>'move',
537546
* 'src_name'=>'filename',
@@ -572,6 +581,7 @@ public function getSpecifyOptionMessage()
572581
* @param string $processMode
573582
* @return array
574583
* @throws LocalizedException
584+
* @SuppressWarnings(PHPMD.CyclomaticComplexity)
575585
*/
576586
protected function _prepareOptions(\Magento\Framework\DataObject $buyRequest, $product, $processMode)
577587
{
@@ -583,15 +593,22 @@ protected function _prepareOptions(\Magento\Framework\DataObject $buyRequest, $p
583593
}
584594
if ($options !== null) {
585595
$results = [];
596+
$optionsFromRequest = $buyRequest->getOptions();
586597
foreach ($options as $option) {
587598
/* @var $option \Magento\Catalog\Model\Product\Option */
588599
try {
589600
$group = $option->groupFactory($option->getType())
590601
->setOption($option)
591602
->setProduct($product)
592603
->setRequest($buyRequest)
593-
->setProcessMode($processMode)
594-
->validateUserValue($buyRequest->getOptions());
604+
->setProcessMode($processMode);
605+
606+
if ($product->getSkipCheckRequiredOption() !== true) {
607+
$group->validateUserValue($optionsFromRequest);
608+
} elseif ($optionsFromRequest !== null && isset($optionsFromRequest[$option->getId()])) {
609+
$transport->options[$option->getId()] = $optionsFromRequest[$option->getId()];
610+
}
611+
595612
} catch (LocalizedException $e) {
596613
$results[] = $e->getMessage();
597614
continue;
@@ -643,8 +660,7 @@ public function checkProductBuyState($product)
643660
}
644661

645662
/**
646-
* Prepare additional options/information for order item which will be
647-
* created from this product
663+
* Prepare additional options/information for order item which will be created from this product
648664
*
649665
* @param \Magento\Catalog\Model\Product $product
650666
* @return array
@@ -900,7 +916,7 @@ public function getStoreFilter($product)
900916
/**
901917
* Set store filter for associated products
902918
*
903-
* @param $store int|\Magento\Store\Model\Store
919+
* @param int|\Magento\Store\Model\Store $store
904920
* @param \Magento\Catalog\Model\Product $product
905921
* @return $this
906922
*/
@@ -913,6 +929,7 @@ public function setStoreFilter($store, $product)
913929

914930
/**
915931
* Allow for updates of children qty's
932+
*
916933
* (applicable for complicated product types. As default returns false)
917934
*
918935
* @param \Magento\Catalog\Model\Product $product
@@ -940,6 +957,7 @@ public function prepareQuoteItemQty($qty, $product)
940957

941958
/**
942959
* Implementation of product specify logic of which product needs to be assigned to option.
960+
*
943961
* For example if product which was added to option already removed from catalog.
944962
*
945963
* @param \Magento\Catalog\Model\Product $optionProduct
@@ -979,6 +997,7 @@ public function setConfig($config)
979997

980998
/**
981999
* Retrieve additional searchable data from type instance
1000+
*
9821001
* Using based on product id and store_id data
9831002
*
9841003
* @param \Magento\Catalog\Model\Product $product
@@ -999,6 +1018,7 @@ public function getSearchableData($product)
9991018

10001019
/**
10011020
* Retrieve products divided into groups required to purchase
1021+
*
10021022
* At least one product in each group has to be purchased
10031023
*
10041024
* @param \Magento\Catalog\Model\Product $product
@@ -1092,6 +1112,8 @@ public function getIdentities(\Magento\Catalog\Model\Product $product)
10921112
}
10931113

10941114
/**
1115+
* Get Associated Products
1116+
*
10951117
* @param \Magento\Catalog\Model\Product\Type\AbstractType $product
10961118
* @return array
10971119
* @SuppressWarnings(PHPMD.UnusedFormalParameter)

app/code/Magento/Catalog/Test/Unit/Model/Product/Option/Type/FileTest.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@
2424
use PHPUnit\Framework\TestCase;
2525

2626
/**
27+
* Test file option type
28+
*
2729
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
2830
*/
2931
class FileTest extends TestCase
@@ -142,6 +144,14 @@ protected function getFileObject()
142144
);
143145
}
144146

147+
public function testGetFormattedOptionValueWithUnserializedValue()
148+
{
149+
$fileObject = $this->getFileObject();
150+
151+
$value = 'some unserialized value, 1, 2.test';
152+
$this->assertEquals($value, $fileObject->getFormattedOptionValue($value));
153+
}
154+
145155
public function testGetCustomizedView()
146156
{
147157
$fileObject = $this->getFileObject();

app/code/Magento/Checkout/Model/Cart.php

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
* @api
1919
* @SuppressWarnings(PHPMD.CookieAndSessionMisuse)
2020
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
21+
* @SuppressWarnings(PHPMD.ExcessiveClassComplexity)
2122
* @deprecated 100.1.0 Use \Magento\Quote\Model\Quote instead
2223
* @see \Magento\Quote\Api\Data\CartInterface
2324
*/
@@ -272,6 +273,10 @@ public function addOrderItem($orderItem, $qtyFlag = null)
272273
* with the same id may have different sets of order attributes.
273274
*/
274275
$product = $this->productRepository->getById($orderItem->getProductId(), false, $storeId, true);
276+
if ($orderItem->getOrderId() !== null) {
277+
//reorder existing order
278+
$product->setSkipCheckRequiredOption(true);
279+
}
275280
} catch (NoSuchEntityException $e) {
276281
return $this;
277282
}
@@ -282,7 +287,14 @@ public function addOrderItem($orderItem, $qtyFlag = null)
282287
} else {
283288
$info->setQty(1);
284289
}
285-
290+
$productOptions = $orderItem->getProductOptions();
291+
if ($productOptions !== null && !empty($productOptions['options'])) {
292+
$formattedOptions = [];
293+
foreach ($productOptions['options'] as $option) {
294+
$formattedOptions[$option['option_id']] = $option['option_value'];
295+
}
296+
$info->setData('options', $formattedOptions);
297+
}
286298
$this->addProduct($product, $info);
287299
}
288300
return $this;
@@ -291,8 +303,8 @@ public function addOrderItem($orderItem, $qtyFlag = null)
291303
/**
292304
* Get product object based on requested product information
293305
*
294-
* @param Product|int|string $productInfo
295-
* @return Product
306+
* @param Product|int|string $productInfo
307+
* @return Product
296308
* @throws \Magento\Framework\Exception\LocalizedException
297309
*/
298310
protected function _getProduct($productInfo)
@@ -332,8 +344,8 @@ protected function _getProduct($productInfo)
332344
/**
333345
* Get request for product add to cart procedure
334346
*
335-
* @param \Magento\Framework\DataObject|int|array $requestInfo
336-
* @return \Magento\Framework\DataObject
347+
* @param \Magento\Framework\DataObject|int|array $requestInfo
348+
* @return \Magento\Framework\DataObject
337349
* @throws \Magento\Framework\Exception\LocalizedException
338350
*/
339351
protected function _getProductRequest($requestInfo)

0 commit comments

Comments
 (0)