Skip to content

Commit 49635e5

Browse files
committed
Merge branch 'ACP2E-3343' of https://github.com/adobe-commerce-tier-4/magento2ce into PR-14-10-2024
2 parents eec9c29 + b30350c commit 49635e5

File tree

4 files changed

+74
-17
lines changed

4 files changed

+74
-17
lines changed

app/code/Magento/Quote/Model/QuoteValidator.php

Lines changed: 3 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<?php
22
/**
3-
* Copyright © Magento, Inc. All rights reserved.
4-
* See COPYING.txt for license details.
3+
* Copyright 2014 Adobe
4+
* All Rights Reserved.
55
*/
66

77
namespace Magento\Quote\Model;
@@ -14,7 +14,6 @@
1414
use Magento\Quote\Model\Quote as QuoteEntity;
1515
use Magento\Quote\Model\Quote\Validator\MinimumOrderAmount\ValidationMessage as OrderAmountValidationMessage;
1616
use Magento\Quote\Model\ValidationRules\QuoteValidationRuleInterface;
17-
use Magento\Framework\Webapi\Rest\Response as RestResponse;
1817

1918
/**
2019
* Class to validate the quote
@@ -44,32 +43,24 @@ class QuoteValidator
4443
*/
4544
private $quoteValidationRule;
4645

47-
/**
48-
* @var RestResponse
49-
*/
50-
private $_response;
51-
5246
/**
5347
* QuoteValidator constructor.
5448
*
5549
* @param AllowedCountries|null $allowedCountryReader
5650
* @param OrderAmountValidationMessage|null $minimumAmountMessage
5751
* @param QuoteValidationRuleInterface|null $quoteValidationRule
58-
* @param RestResponse|null $response
5952
*/
6053
public function __construct(
6154
AllowedCountries $allowedCountryReader = null,
6255
OrderAmountValidationMessage $minimumAmountMessage = null,
63-
QuoteValidationRuleInterface $quoteValidationRule = null,
64-
RestResponse $response = null
56+
QuoteValidationRuleInterface $quoteValidationRule = null
6557
) {
6658
$this->allowedCountryReader = $allowedCountryReader ?: ObjectManager::getInstance()
6759
->get(AllowedCountries::class);
6860
$this->minimumAmountMessage = $minimumAmountMessage ?: ObjectManager::getInstance()
6961
->get(OrderAmountValidationMessage::class);
7062
$this->quoteValidationRule = $quoteValidationRule ?: ObjectManager::getInstance()
7163
->get(QuoteValidationRuleInterface::class);
72-
$this->_response = $response ?: ObjectManager::getInstance()->get(RestResponse::class);
7364
}
7465

7566
/**
@@ -115,7 +106,6 @@ public function validateBeforeSubmit(QuoteEntity $quote)
115106
$defaultMessage .= ' %1';
116107
}
117108
if ($defaultMessage) {
118-
$this->_response->setHeader('errorRedirectAction', '#shipping');
119109
throw new ValidatorException(__($defaultMessage, implode(' ', $messages)));
120110
}
121111
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
<?php
2+
/**
3+
* Copyright 2024 Adobe
4+
* All Rights Reserved.
5+
*/
6+
declare(strict_types=1);
7+
8+
namespace Magento\Quote\Plugin\Webapi\Model;
9+
10+
use Magento\Framework\Validator\Exception as ValidatorException;
11+
use Magento\Framework\Webapi\Rest\Response as RestResponse;
12+
use Magento\Quote\Model\Quote;
13+
use Magento\Quote\Model\QuoteValidator;
14+
15+
class ErrorRedirectProcessor
16+
{
17+
/**
18+
* @param RestResponse $restResponse
19+
*/
20+
public function __construct(
21+
private readonly RestResponse $restResponse
22+
) {
23+
}
24+
25+
/**
26+
* Set errorRedirectAction in case of exception.
27+
*
28+
* @param QuoteValidator $subject
29+
* @param callable $proceed
30+
* @param Quote $quote
31+
*/
32+
public function aroundValidateBeforeSubmit(QuoteValidator $subject, callable $proceed, Quote $quote)
33+
{
34+
try {
35+
$result = $proceed($quote);
36+
} catch (ValidatorException $e) {
37+
$this->restResponse->setHeader('errorRedirectAction', '#shipping');
38+
throw $e;
39+
}
40+
41+
return $result;
42+
}
43+
}

app/code/Magento/Quote/etc/webapi_rest/di.xml

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
<?xml version="1.0"?>
22
<!--
33
/**
4-
* Copyright © Magento, Inc. All rights reserved.
5-
* See COPYING.txt for license details.
4+
* Copyright 2014 Adobe
5+
* All Rights Reserved.
66
*/
77
-->
88
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
@@ -23,4 +23,7 @@
2323
<type name="Magento\Quote\Api\CartRepositoryInterface">
2424
<plugin name="quoteValidateOrderId" type="Magento\Quote\Plugin\ValidateQuoteOrigOrder"/>
2525
</type>
26+
<type name="Magento\Quote\Model\QuoteValidator">
27+
<plugin name="error_redirect_processor" type="Magento\Quote\Plugin\Webapi\Model\ErrorRedirectProcessor" />
28+
</type>
2629
</config>

dev/tests/integration/testsuite/Magento/Framework/App/FrontControllerTest.php

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<?php
22
/**
3-
* Copyright © Magento, Inc. All rights reserved.
4-
* See COPYING.txt for license details.
3+
* Copyright 2013 Adobe
4+
* All Rights Reserved.
55
*/
66
namespace Magento\Framework\App;
77

@@ -18,6 +18,7 @@
1818
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
1919
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
2020
* @magentoAppArea frontend
21+
* @magentoAppIsolation enabled
2122
*/
2223
class FrontControllerTest extends TestCase
2324
{
@@ -118,4 +119,24 @@ public function testInvalidRequest()
118119
$this->_model->dispatch($request)
119120
);
120121
}
122+
123+
public function testDispatchWithAcceptHeader(): void
124+
{
125+
if (!Bootstrap::canTestHeaders()) {
126+
$this->markTestSkipped('Can\'t test dispatch process without sending headers');
127+
}
128+
129+
$this->fakeRequestValidator->valid = true;
130+
$_SERVER['HTTP_HOST'] = 'localhost';
131+
$this->_objectManager->get(State::class)->setAreaCode('frontend');
132+
$request = $this->_objectManager->get(HttpRequest::class);
133+
$request->setRequestUri('/checkout');
134+
$headers = $request->getHeaders();
135+
$headers->addHeaderLine('Accept', 'text/html');
136+
$request->setHeaders($headers);
137+
$this->assertInstanceOf(
138+
ResultInterface::class,
139+
$this->_model->dispatch($request)
140+
);
141+
}
121142
}

0 commit comments

Comments
 (0)