Skip to content

Commit 2c43b82

Browse files
ENGCOM-6185: Fixes issue 23411 - Remove misplaced critical log 'No such entity with customerId = xx' #25307
- Merge Pull Request #25307 from hostep/magento2:fixes-issue-23411 - Merged commits: 1. bd96456 2. 8920923 3. 96b969d 4. 36fb3b7 5. 31412e0 6. 3459162
2 parents 2e3073d + 3459162 commit 2c43b82

File tree

2 files changed

+92
-16
lines changed

2 files changed

+92
-16
lines changed

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

Lines changed: 28 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77

88
use Magento\Customer\Api\Data\CustomerInterface;
99
use Magento\Framework\App\ObjectManager;
10+
use Magento\Framework\Exception\NoSuchEntityException;
11+
use Magento\Quote\Api\Data\CartInterface;
1012
use Magento\Quote\Model\Quote;
1113
use Magento\Quote\Model\QuoteIdMaskFactory;
1214
use Psr\Log\LoggerInterface;
@@ -21,9 +23,6 @@
2123
*/
2224
class Session extends \Magento\Framework\Session\SessionManager
2325
{
24-
/**
25-
* Checkout state begin
26-
*/
2726
const CHECKOUT_STATE_BEGIN = 'begin';
2827

2928
/**
@@ -228,7 +227,7 @@ public function setLoadInactive($load = true)
228227
*
229228
* @return Quote
230229
* @throws \Magento\Framework\Exception\LocalizedException
231-
* @throws \Magento\Framework\Exception\NoSuchEntityException
230+
* @throws NoSuchEntityException
232231
* @SuppressWarnings(PHPMD.CyclomaticComplexity)
233232
* @SuppressWarnings(PHPMD.NPathComplexity)
234233
*/
@@ -273,21 +272,17 @@ public function getQuote()
273272
*/
274273
$quote = $this->quoteRepository->get($this->getQuoteId());
275274
}
276-
} catch (\Magento\Framework\Exception\NoSuchEntityException $e) {
275+
} catch (NoSuchEntityException $e) {
277276
$this->setQuoteId(null);
278277
}
279278
}
280279

281280
if (!$this->getQuoteId()) {
282281
if ($this->_customerSession->isLoggedIn() || $this->_customer) {
283-
$customerId = $this->_customer
284-
? $this->_customer->getId()
285-
: $this->_customerSession->getCustomerId();
286-
try {
287-
$quote = $this->quoteRepository->getActiveForCustomer($customerId);
288-
$this->setQuoteId($quote->getId());
289-
} catch (\Magento\Framework\Exception\NoSuchEntityException $e) {
290-
$this->logger->critical($e);
282+
$quoteByCustomer = $this->getQuoteByCustomer();
283+
if ($quoteByCustomer !== null) {
284+
$this->setQuoteId($quoteByCustomer->getId());
285+
$quote = $quoteByCustomer;
291286
}
292287
} else {
293288
$quote->setIsCheckoutCart(true);
@@ -375,7 +370,7 @@ public function loadCustomerQuote()
375370

376371
try {
377372
$customerQuote = $this->quoteRepository->getForCustomer($this->_customerSession->getCustomerId());
378-
} catch (\Magento\Framework\Exception\NoSuchEntityException $e) {
373+
} catch (NoSuchEntityException $e) {
379374
$customerQuote = $this->quoteFactory->create();
380375
}
381376
$customerQuote->setStoreId($this->_storeManager->getStore()->getId());
@@ -558,7 +553,7 @@ public function restoreQuote()
558553
$this->replaceQuote($quote)->unsLastRealOrderId();
559554
$this->_eventManager->dispatch('restore_quote', ['order' => $order, 'quote' => $quote]);
560555
return true;
561-
} catch (\Magento\Framework\Exception\NoSuchEntityException $e) {
556+
} catch (NoSuchEntityException $e) {
562557
$this->logger->critical($e);
563558
}
564559
}
@@ -588,4 +583,22 @@ protected function isQuoteMasked()
588583
{
589584
return $this->isQuoteMasked;
590585
}
586+
587+
/**
588+
* Returns quote for customer if there is any
589+
*/
590+
private function getQuoteByCustomer(): ?CartInterface
591+
{
592+
$customerId = $this->_customer
593+
? $this->_customer->getId()
594+
: $this->_customerSession->getCustomerId();
595+
596+
try {
597+
$quote = $this->quoteRepository->getActiveForCustomer($customerId);
598+
} catch (NoSuchEntityException $e) {
599+
$quote = null;
600+
}
601+
602+
return $quote;
603+
}
591604
}

app/code/Magento/Checkout/Test/Unit/Model/SessionTest.php

Lines changed: 64 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,8 @@
99
*/
1010
namespace Magento\Checkout\Test\Unit\Model;
1111

12-
use \Magento\Checkout\Model\Session;
12+
use Magento\Checkout\Model\Session;
13+
use Magento\Framework\Exception\NoSuchEntityException;
1314

1415
/**
1516
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
@@ -374,6 +375,68 @@ public function testGetStepData()
374375
$this->assertEquals($stepData['complex']['key'], $session->getStepData('complex', 'key'));
375376
}
376377

378+
/**
379+
* Ensure that if quote not exist for customer quote will be null
380+
*
381+
* @return void
382+
*/
383+
public function testGetQuote(): void
384+
{
385+
$storeManager = $this->getMockForAbstractClass(\Magento\Store\Model\StoreManagerInterface::class);
386+
$customerSession = $this->createMock(\Magento\Customer\Model\Session::class);
387+
$quoteRepository = $this->createMock(\Magento\Quote\Api\CartRepositoryInterface::class);
388+
$quoteFactory = $this->createMock(\Magento\Quote\Model\QuoteFactory::class);
389+
$quote = $this->createMock(\Magento\Quote\Model\Quote::class);
390+
$logger = $this->createMock(\Psr\Log\LoggerInterface::class);
391+
$loggerMethods = get_class_methods(\Psr\Log\LoggerInterface::class);
392+
393+
$quoteFactory->expects($this->once())
394+
->method('create')
395+
->willReturn($quote);
396+
$customerSession->expects($this->exactly(3))
397+
->method('isLoggedIn')
398+
->willReturn(true);
399+
$store = $this->getMockBuilder(\Magento\Store\Model\Store::class)
400+
->disableOriginalConstructor()
401+
->setMethods(['getWebsiteId', '__wakeup'])
402+
->getMock();
403+
$storeManager->expects($this->any())
404+
->method('getStore')
405+
->will($this->returnValue($store));
406+
$storage = $this->getMockBuilder(\Magento\Framework\Session\Storage::class)
407+
->disableOriginalConstructor()
408+
->setMethods(['setData', 'getData'])
409+
->getMock();
410+
$storage->expects($this->at(0))
411+
->method('getData')
412+
->willReturn(1);
413+
$quoteRepository->expects($this->once())
414+
->method('getActiveForCustomer')
415+
->willThrowException(new NoSuchEntityException());
416+
417+
foreach ($loggerMethods as $method) {
418+
$logger->expects($this->never())->method($method);
419+
}
420+
421+
$quote->expects($this->once())
422+
->method('setCustomer')
423+
->with(null);
424+
425+
$constructArguments = $this->_helper->getConstructArguments(
426+
\Magento\Checkout\Model\Session::class,
427+
[
428+
'storeManager' => $storeManager,
429+
'quoteRepository' => $quoteRepository,
430+
'customerSession' => $customerSession,
431+
'storage' => $storage,
432+
'quoteFactory' => $quoteFactory,
433+
'logger' => $logger
434+
]
435+
);
436+
$this->_session = $this->_helper->getObject(\Magento\Checkout\Model\Session::class, $constructArguments);
437+
$this->_session->getQuote();
438+
}
439+
377440
public function testSetStepData()
378441
{
379442
$stepData = [

0 commit comments

Comments
 (0)