Skip to content

Commit 946c6ba

Browse files
authored
Merge pull request #5145 from magento-tsg-csl3/2.4-develop-pr4
[TSG-CSL3] For 2.4 (pr4)
2 parents 4165276 + a00d28a commit 946c6ba

30 files changed

+789
-413
lines changed

app/code/Magento/CatalogRule/Observer/ProcessAdminFinalPriceObserver.php

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,10 @@
77

88
namespace Magento\CatalogRule\Observer;
99

10-
use Magento\Framework\Stdlib\DateTime\TimezoneInterface;
11-
use Magento\Framework\Registry;
1210
use Magento\Framework\Event\ObserverInterface;
11+
use Magento\Framework\Registry;
12+
use Magento\Framework\Stdlib\DateTime\TimezoneInterface;
13+
use Magento\Store\Model\StoreManagerInterface;
1314

1415
/**
1516
* Observer for applying catalog rules on product for admin area
@@ -23,6 +24,11 @@ class ProcessAdminFinalPriceObserver implements ObserverInterface
2324
*/
2425
protected $coreRegistry;
2526

27+
/**
28+
* @var StoreManagerInterface
29+
*/
30+
protected $storeManager;
31+
2632
/**
2733
* @var \Magento\Framework\Stdlib\DateTime\TimezoneInterface
2834
*/
@@ -41,17 +47,20 @@ class ProcessAdminFinalPriceObserver implements ObserverInterface
4147
/**
4248
* @param RulePricesStorage $rulePricesStorage
4349
* @param Registry $coreRegistry
50+
* @param StoreManagerInterface $storeManager
4451
* @param \Magento\CatalogRule\Model\ResourceModel\RuleFactory $resourceRuleFactory
4552
* @param TimezoneInterface $localeDate
4653
*/
4754
public function __construct(
4855
RulePricesStorage $rulePricesStorage,
4956
Registry $coreRegistry,
57+
StoreManagerInterface $storeManager,
5058
\Magento\CatalogRule\Model\ResourceModel\RuleFactory $resourceRuleFactory,
5159
TimezoneInterface $localeDate
5260
) {
5361
$this->rulePricesStorage = $rulePricesStorage;
5462
$this->coreRegistry = $coreRegistry;
63+
$this->storeManager = $storeManager;
5564
$this->resourceRuleFactory = $resourceRuleFactory;
5665
$this->localeDate = $localeDate;
5766
}
@@ -61,6 +70,7 @@ public function __construct(
6170
*
6271
* @param \Magento\Framework\Event\Observer $observer
6372
* @return $this
73+
* @throws \Magento\Framework\Exception\NoSuchEntityException
6474
*/
6575
public function execute(\Magento\Framework\Event\Observer $observer)
6676
{
@@ -74,13 +84,17 @@ public function execute(\Magento\Framework\Event\Observer $observer)
7484
$wId = $ruleData->getWebsiteId();
7585
$gId = $ruleData->getCustomerGroupId();
7686
$pId = $product->getId();
77-
7887
$key = "{$date->format('Y-m-d H:i:s')}|{$wId}|{$gId}|{$pId}";
7988
} elseif ($product->getWebsiteId() !== null && $product->getCustomerGroupId() !== null) {
8089
$wId = $product->getWebsiteId();
8190
$gId = $product->getCustomerGroupId();
8291
$pId = $product->getId();
8392
$key = "{$date->format('Y-m-d H:i:s')}|{$wId}|{$gId}|{$pId}";
93+
} elseif ($product->getWebsiteId() === null && $product->getCustomerGroupId() !== null) {
94+
$wId = $this->storeManager->getStore($storeId)->getWebsiteId();
95+
$gId = $product->getCustomerGroupId();
96+
$pId = $product->getId();
97+
$key = "{$date->format('Y-m-d H:i:s')}|{$wId}|{$gId}|{$pId}";
8498
}
8599

86100
if ($key) {
Lines changed: 191 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,191 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
namespace Magento\CatalogRule\Test\Unit\Observer;
7+
8+
use Magento\Catalog\Model\Product;
9+
use Magento\CatalogRule\Model\ResourceModel\RuleFactory;
10+
use Magento\CatalogRule\Observer\ProcessAdminFinalPriceObserver;
11+
use Magento\CatalogRule\Observer\RulePricesStorage;
12+
use Magento\Framework\Event;
13+
use Magento\Framework\Event\Observer;
14+
use Magento\Framework\Stdlib\DateTime\TimezoneInterface;
15+
use Magento\Framework\TestFramework\Unit\Helper\ObjectManager;
16+
use Magento\Store\Model\Store;
17+
use Magento\Store\Model\StoreManagerInterface;
18+
use Magento\Ui\Component\Form\Element\DataType\Date;
19+
use PHPUnit\Framework\TestCase;
20+
21+
/**
22+
* Class ProcessAdminFinalPriceObserverTest
23+
*
24+
* Test class for Observer for applying catalog rules on product for admin area
25+
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
26+
*/
27+
class ProcessAdminFinalPriceObserverTest extends TestCase
28+
{
29+
/**
30+
* @var ProcessAdminFinalPriceObserver
31+
*/
32+
private $observer;
33+
34+
/**
35+
* Store Manager mock
36+
*
37+
* @var StoreManagerInterface
38+
*/
39+
private $storeManagerMock;
40+
41+
/**
42+
* Locale Date mock
43+
*
44+
* @var TimezoneInterface
45+
*/
46+
private $localeDateMock;
47+
48+
/**
49+
* Resource Rule Factory mock
50+
*
51+
* @var RuleFactory
52+
*/
53+
private $resourceRuleFactoryMock;
54+
55+
/**
56+
* Rule Prices Storage mock
57+
*
58+
* @var RulePricesStorage
59+
*/
60+
private $rulePricesStorageMock;
61+
62+
/**
63+
* @var Event|\PHPUnit_Framework_MockObject_MockObject
64+
*/
65+
private $eventMock;
66+
67+
/**
68+
* @var Observer|\PHPUnit\Framework\MockObject\MockObject
69+
*/
70+
private $observerMock;
71+
72+
protected function setUp()
73+
{
74+
$this->observerMock = $this
75+
->getMockBuilder(Observer::class)
76+
->disableOriginalConstructor()
77+
->getMock();
78+
$this->eventMock = $this
79+
->getMockBuilder(Event::class)
80+
->setMethods(['getProduct'])
81+
->disableOriginalConstructor()
82+
->getMock();
83+
$this->rulePricesStorageMock = $this->getMockBuilder(RulePricesStorage::class)
84+
->setMethods(['getWebsiteId', 'getRulePrice', 'getCustomerGroupId', 'setRulePrice'])
85+
->disableOriginalConstructor()
86+
->getMock();
87+
$this->storeManagerMock = $this->getMockBuilder(StoreManagerInterface::class)
88+
->setMethods(['getStore'])
89+
->disableOriginalConstructor()
90+
->getMockForAbstractClass();
91+
$this->resourceRuleFactoryMock = $this->getMockBuilder(RuleFactory::class)
92+
->setMethods(['create'])
93+
->disableOriginalConstructor()
94+
->getMock();
95+
$this->localeDateMock = $this->getMockBuilder(TimezoneInterface::class)
96+
->setMethods(['scopeDate'])
97+
->disableOriginalConstructor()
98+
->getMockForAbstractClass();
99+
$objectManagerHelper = new ObjectManager($this);
100+
$this->observer = $objectManagerHelper->getObject(
101+
ProcessAdminFinalPriceObserver::class,
102+
[
103+
'rulePricesStorage' => $this->rulePricesStorageMock,
104+
'storeManager' => $this->storeManagerMock,
105+
'resourceRuleFactory' => $this->resourceRuleFactoryMock,
106+
'localeDate' => $this->localeDateMock
107+
]
108+
);
109+
}
110+
111+
public function testExecute()
112+
{
113+
$finalPrice = 20.00;
114+
$rulePrice = 10.00;
115+
$storeId = 2;
116+
$wId = 1;
117+
$gId = 4;
118+
$pId = 20;
119+
$localeDateFormat = 'Y-m-d H:i:s';
120+
$date = '2019-12-02 08:00:00';
121+
$storeMock = $this->createMock(Store::class);
122+
$this->observerMock
123+
->expects($this->atLeastOnce())
124+
->method('getEvent')
125+
->willReturn($this->eventMock);
126+
127+
$productMock = $this->getMockBuilder(Product::class)
128+
->setMethods(
129+
[
130+
'getStoreId',
131+
'getWebsiteId',
132+
'getId',
133+
'getData',
134+
'getCustomerGroupId',
135+
'setFinalPrice'
136+
]
137+
)
138+
->disableOriginalConstructor()
139+
->getMock();
140+
$dateMock = $this->getMockBuilder(Date::class)
141+
->setMethods(['format'])
142+
->disableOriginalConstructor()
143+
->getMock();
144+
145+
$this->localeDateMock->expects($this->once())
146+
->method('scopeDate')
147+
->with($storeId)
148+
->willReturn($dateMock);
149+
$dateMock->expects($this->once())
150+
->method('format')
151+
->with($localeDateFormat)
152+
->willReturn($date);
153+
$storeMock->expects($this->once())
154+
->method('getWebsiteId')
155+
->willReturn($wId);
156+
$this->storeManagerMock->expects($this->once())
157+
->method('getStore')
158+
->with($storeId)
159+
->willReturn($storeMock);
160+
$productMock->expects($this->once())
161+
->method('getStoreId')
162+
->willReturn($storeId);
163+
$productMock->expects($this->any())
164+
->method('getCustomerGroupId')
165+
->willReturn($gId);
166+
$productMock->expects($this->once())
167+
->method('getId')
168+
->willReturn($pId);
169+
$productMock->expects($this->once())
170+
->method('getData')
171+
->with('final_price')
172+
->willReturn($finalPrice);
173+
$this->rulePricesStorageMock->expects($this->any())
174+
->method('getCustomerGroupId')
175+
->willReturn($gId);
176+
$this->resourceRuleFactoryMock->expects($this->once())
177+
->method('create')
178+
->willReturn($this->rulePricesStorageMock);
179+
$this->rulePricesStorageMock->expects($this->any())
180+
->method('getRulePrice')
181+
->willReturn($rulePrice);
182+
$this->rulePricesStorageMock->expects($this->once())
183+
->method('setRulePrice')
184+
->willReturnSelf();
185+
$this->eventMock
186+
->expects($this->atLeastOnce())
187+
->method('getProduct')
188+
->willReturn($productMock);
189+
$this->assertEquals($this->observer, $this->observer->execute($this->observerMock));
190+
}
191+
}

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

Lines changed: 16 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -56,19 +56,13 @@ class GuestPaymentInformationManagement implements \Magento\Checkout\Api\GuestPa
5656
*/
5757
private $logger;
5858

59-
/**
60-
* @var ResourceConnection
61-
*/
62-
private $connectionPool;
63-
6459
/**
6560
* @param \Magento\Quote\Api\GuestBillingAddressManagementInterface $billingAddressManagement
6661
* @param \Magento\Quote\Api\GuestPaymentMethodManagementInterface $paymentMethodManagement
6762
* @param \Magento\Quote\Api\GuestCartManagementInterface $cartManagement
6863
* @param \Magento\Checkout\Api\PaymentInformationManagementInterface $paymentInformationManagement
6964
* @param \Magento\Quote\Model\QuoteIdMaskFactory $quoteIdMaskFactory
7065
* @param CartRepositoryInterface $cartRepository
71-
* @param ResourceConnection $connectionPool
7266
* @codeCoverageIgnore
7367
*/
7468
public function __construct(
@@ -77,16 +71,14 @@ public function __construct(
7771
\Magento\Quote\Api\GuestCartManagementInterface $cartManagement,
7872
\Magento\Checkout\Api\PaymentInformationManagementInterface $paymentInformationManagement,
7973
\Magento\Quote\Model\QuoteIdMaskFactory $quoteIdMaskFactory,
80-
CartRepositoryInterface $cartRepository,
81-
ResourceConnection $connectionPool = null
74+
CartRepositoryInterface $cartRepository
8275
) {
8376
$this->billingAddressManagement = $billingAddressManagement;
8477
$this->paymentMethodManagement = $paymentMethodManagement;
8578
$this->cartManagement = $cartManagement;
8679
$this->paymentInformationManagement = $paymentInformationManagement;
8780
$this->quoteIdMaskFactory = $quoteIdMaskFactory;
8881
$this->cartRepository = $cartRepository;
89-
$this->connectionPool = $connectionPool ?: ObjectManager::getInstance()->get(ResourceConnection::class);
9082
}
9183

9284
/**
@@ -98,33 +90,23 @@ public function savePaymentInformationAndPlaceOrder(
9890
\Magento\Quote\Api\Data\PaymentInterface $paymentMethod,
9991
\Magento\Quote\Api\Data\AddressInterface $billingAddress = null
10092
) {
101-
$salesConnection = $this->connectionPool->getConnection('sales');
102-
$checkoutConnection = $this->connectionPool->getConnection('checkout');
103-
$salesConnection->beginTransaction();
104-
$checkoutConnection->beginTransaction();
105-
93+
$this->savePaymentInformation($cartId, $email, $paymentMethod, $billingAddress);
10694
try {
107-
$this->savePaymentInformation($cartId, $email, $paymentMethod, $billingAddress);
108-
try {
109-
$orderId = $this->cartManagement->placeOrder($cartId);
110-
} catch (\Magento\Framework\Exception\LocalizedException $e) {
111-
throw new CouldNotSaveException(
112-
__($e->getMessage()),
113-
$e
114-
);
115-
} catch (\Exception $e) {
116-
$this->getLogger()->critical($e);
117-
throw new CouldNotSaveException(
118-
__('An error occurred on the server. Please try to place the order again.'),
119-
$e
120-
);
121-
}
122-
$salesConnection->commit();
123-
$checkoutConnection->commit();
95+
$orderId = $this->cartManagement->placeOrder($cartId);
96+
} catch (\Magento\Framework\Exception\LocalizedException $e) {
97+
$this->getLogger()->critical(
98+
'Placing an order with quote_id ' . $cartId . ' is failed: ' . $e->getMessage()
99+
);
100+
throw new CouldNotSaveException(
101+
__($e->getMessage()),
102+
$e
103+
);
124104
} catch (\Exception $e) {
125-
$salesConnection->rollBack();
126-
$checkoutConnection->rollBack();
127-
throw $e;
105+
$this->getLogger()->critical($e);
106+
throw new CouldNotSaveException(
107+
__('An error occurred on the server. Please try to place the order again.'),
108+
$e
109+
);
128110
}
129111

130112
return $orderId;

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

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
use Magento\Framework\Exception\CouldNotSaveException;
1010

1111
/**
12-
* Payment information management
12+
* Payment information management service.
1313
*
1414
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
1515
*/
@@ -85,6 +85,9 @@ public function savePaymentInformationAndPlaceOrder(
8585
try {
8686
$orderId = $this->cartManagement->placeOrder($cartId);
8787
} catch (\Magento\Framework\Exception\LocalizedException $e) {
88+
$this->getLogger()->critical(
89+
'Placing an order with quote_id ' . $cartId . ' is failed: ' . $e->getMessage()
90+
);
8891
throw new CouldNotSaveException(
8992
__($e->getMessage()),
9093
$e

0 commit comments

Comments
 (0)