Skip to content

Commit 0c8234d

Browse files
committed
ACP2E-3493: Expired persistent quotes are not cleaned up by a cron job sales_clean_quotes
- Fixed the CR comments.
1 parent fa92239 commit 0c8234d

File tree

4 files changed

+24
-41
lines changed

4 files changed

+24
-41
lines changed

app/code/Magento/Persistent/Model/CleanExpiredPersistentQuotes.php

Lines changed: 1 addition & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111
use Magento\Store\Model\StoreManagerInterface;
1212
use Magento\Quote\Model\ResourceModel\Quote\Collection as QuoteCollection;
1313
use Magento\Persistent\Model\ResourceModel\ExpiredPersistentQuotesCollection;
14-
use Magento\Customer\Model\Logger as CustomerLogger;
1514
use Magento\Quote\Model\QuoteRepository;
1615
use Psr\Log\LoggerInterface;
1716
use Exception;
@@ -24,14 +23,12 @@ class CleanExpiredPersistentQuotes
2423
/**
2524
* @param StoreManagerInterface $storeManager
2625
* @param ExpiredPersistentQuotesCollection $expiredPersistentQuotesCollection
27-
* @param CustomerLogger $customerLogger
2826
* @param QuoteRepository $quoteRepository
2927
* @param LoggerInterface $logger
3028
*/
3129
public function __construct(
3230
private readonly StoreManagerInterface $storeManager,
3331
private readonly ExpiredPersistentQuotesCollection $expiredPersistentQuotesCollection,
34-
private readonly CustomerLogger $customerLogger,
3532
private readonly QuoteRepository $quoteRepository,
3633
private readonly LoggerInterface $logger
3734
) {
@@ -73,9 +70,7 @@ private function deletePersistentQuotes(QuoteCollection $quoteCollection): void
7370
{
7471
foreach ($quoteCollection as $quote) {
7572
try {
76-
if (!$this->isLoggedInCustomer((int) $quote->getCustomerId())) {
77-
$this->quoteRepository->delete($quote);
78-
}
73+
$this->quoteRepository->delete($quote);
7974
} catch (Exception $e) {
8075
$message = sprintf(
8176
'Unable to delete expired quote (ID: %s): %s',
@@ -88,21 +83,4 @@ private function deletePersistentQuotes(QuoteCollection $quoteCollection): void
8883

8984
$quoteCollection->clear();
9085
}
91-
92-
/**
93-
* Determine if the customer is currently logged in based on their last login and logout timestamps.
94-
*
95-
* @param int $customerId
96-
* @return bool
97-
*/
98-
private function isLoggedInCustomer(int $customerId): bool
99-
{
100-
$isLoggedIn = false;
101-
$customerLastLoginAt = strtotime($this->customerLogger->get($customerId)->getLastLoginAt());
102-
$customerLastLogoutAt = strtotime($this->customerLogger->get($customerId)->getLastLogoutAt());
103-
if ($customerLastLoginAt > $customerLastLogoutAt) {
104-
$isLoggedIn = true;
105-
}
106-
return $isLoggedIn;
107-
}
10886
}

app/code/Magento/Persistent/Model/ResourceModel/ExpiredPersistentQuotesCollection.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,11 @@ public function getExpiredPersistentQuotes(StoreInterface $store): AbstractColle
4848

4949
/** @var $quotes Collection */
5050
$quotes = $this->quoteCollectionFactory->create();
51+
$quotes->getSelect()->join(
52+
['cl' => $quotes->getTable('customer_log')],
53+
'cl.customer_id = main_table.customer_id',
54+
[]
55+
)->where('cl.last_logout_at > cl.last_login_at');
5156
$quotes->addFieldToFilter('main_table.store_id', $store->getId());
5257
$quotes->addFieldToFilter('main_table.updated_at', ['lt' => gmdate("Y-m-d H:i:s", time() - $lifetime)]);
5358
$quotes->addFieldToFilter('main_table.is_persistent', 1);

app/code/Magento/Persistent/Test/Unit/Model/CleanExpiredPersistentQuotesTest.php

Lines changed: 1 addition & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99

1010
use Magento\Persistent\Model\CleanExpiredPersistentQuotes;
1111
use Magento\Persistent\Model\ResourceModel\ExpiredPersistentQuotesCollection;
12-
use Magento\Customer\Model\Logger as CustomerLogger;
1312
use Magento\Quote\Model\Quote;
1413
use Magento\Quote\Model\QuoteRepository;
1514
use Magento\Store\Model\StoreManagerInterface;
@@ -32,11 +31,6 @@ class CleanExpiredPersistentQuotesTest extends TestCase
3231
*/
3332
private ExpiredPersistentQuotesCollection $expiredPersistentQuotesCollectionMock;
3433

35-
/**
36-
* @var CustomerLogger
37-
*/
38-
private CustomerLogger $customerLoggerMock;
39-
4034
/**
4135
* @var QuoteRepository
4236
*/
@@ -56,14 +50,12 @@ protected function setUp(): void
5650
{
5751
$this->storeManagerMock = $this->createMock(StoreManagerInterface::class);
5852
$this->expiredPersistentQuotesCollectionMock = $this->createMock(ExpiredPersistentQuotesCollection::class);
59-
$this->customerLoggerMock = $this->createMock(CustomerLogger::class);
6053
$this->quoteRepositoryMock = $this->createMock(QuoteRepository::class);
6154
$this->loggerMock = $this->createMock(LoggerInterface::class);
6255

6356
$this->cleanExpiredPersistentQuotes = new CleanExpiredPersistentQuotes(
6457
$this->storeManagerMock,
6558
$this->expiredPersistentQuotesCollectionMock,
66-
$this->customerLoggerMock,
6759
$this->quoteRepositoryMock,
6860
$this->loggerMock
6961
);
@@ -95,18 +87,9 @@ public function testExecuteDeletesExpiredQuotes(): void
9587
->with($storeMock)
9688
->willReturn($quoteCollectionMock);
9789

98-
$quoteMock = $this->getMockBuilder(Quote::class)
99-
->disableOriginalConstructor()
100-
->addMethods(['getCustomerId'])
101-
->getMock();
102-
$quoteMock->method('getCustomerId')->willReturn(1);
90+
$quoteMock = $this->createMock(Quote::class);
10391
$quoteCollectionMock->method('getIterator')->willReturn(new \ArrayIterator([$quoteMock]));
10492

105-
$logMock = $this->createMock(Log::class);
106-
$logMock->method('getLastLoginAt')->willReturn('2025-01-01 00:00:00');
107-
$logMock->method('getLastLogoutAt')->willReturn('2025-01-01 10:05:00');
108-
$this->customerLoggerMock->method('get')->willReturn($logMock);
109-
11093
$this->quoteRepositoryMock->expects($this->once())->method('delete');
11194

11295
$this->cleanExpiredPersistentQuotes->execute($websiteId);

app/code/Magento/Persistent/Test/Unit/Model/ResourceModel/ExpiredPersistentQuotesCollectionTest.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
use Magento\Store\Api\Data\StoreInterface;
1515
use Magento\Persistent\Helper\Data;
1616
use Magento\Store\Model\ScopeInterface;
17+
use Magento\Framework\DB\Select;
1718
use PHPUnit\Framework\TestCase;
1819

1920
class ExpiredPersistentQuotesCollectionTest extends TestCase
@@ -58,6 +59,22 @@ public function testGetExpiredPersistentQuotes(): void
5859

5960
$this->quoteCollectionFactoryMock->method('create')->willReturn($quoteCollectionMock);
6061

62+
$dbSelectMock = $this->createMock(Select::class);
63+
$quoteCollectionMock->method('getSelect')->willReturn($dbSelectMock);
64+
$quoteCollectionMock->method('getTable')->willReturn('customer_log');
65+
$dbSelectMock->expects($this->once())
66+
->method('join')
67+
->with(
68+
$this->equalTo(['cl' => 'customer_log']),
69+
$this->equalTo('cl.customer_id = main_table.customer_id'),
70+
$this->equalTo([])
71+
)
72+
->willReturnSelf();
73+
$dbSelectMock->expects($this->once())
74+
->method('where')
75+
->with($this->equalTo('cl.last_logout_at > cl.last_login_at'))
76+
->willReturnSelf();
77+
6178
$quoteCollectionMock->expects($this->exactly(3))
6279
->method('addFieldToFilter')
6380
->with(

0 commit comments

Comments
 (0)