Skip to content

Commit dbcaec9

Browse files
committed
ACP2E-3493: Expired persistent quotes are not cleaned up by a cron job sales_clean_quotes
- Added the test coverage.
1 parent a1c3d98 commit dbcaec9

File tree

1 file changed

+173
-0
lines changed

1 file changed

+173
-0
lines changed
Lines changed: 173 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,173 @@
1+
<?php
2+
/**
3+
* Copyright 2024 Adobe
4+
* All Rights Reserved.
5+
*/
6+
declare(strict_types=1);
7+
8+
namespace Magento\Persistent\Test\Unit\Model;
9+
10+
use Magento\Framework\App\Config\ScopeConfigInterface;
11+
use Magento\Framework\App\ResourceConnection;
12+
use Magento\Persistent\Helper\Data;
13+
use Magento\Persistent\Model\DeleteExpiredQuote;
14+
use Magento\Store\Model\StoreManagerInterface;
15+
use Magento\Store\Model\Website;
16+
use Magento\Store\Api\Data\StoreInterface;
17+
use PHPUnit\Framework\TestCase;
18+
use PHPUnit\Framework\MockObject\MockObject;
19+
20+
class DeleteExpiredQuoteTest extends TestCase
21+
{
22+
/**
23+
* @var DeleteExpiredQuote
24+
*/
25+
private DeleteExpiredQuote $model;
26+
27+
/**
28+
* @var MockObject|StoreManagerInterface
29+
*/
30+
private StoreManagerInterface|MockObject $storeManagerMock;
31+
32+
/**
33+
* @var MockObject|ScopeConfigInterface
34+
*/
35+
private ScopeConfigInterface|MockObject $scopeConfigMock;
36+
37+
/**
38+
* @var MockObject|ResourceConnection
39+
*/
40+
private ResourceConnection|MockObject $resourceConnectionMock;
41+
42+
/**
43+
* @var MockObject|StoreInterface
44+
*/
45+
private StoreInterface|MockObject $storeMock;
46+
47+
protected function setUp(): void
48+
{
49+
$this->storeManagerMock = $this->createMock(StoreManagerInterface::class);
50+
$this->scopeConfigMock = $this->createMock(ScopeConfigInterface::class);
51+
$this->resourceConnectionMock = $this->createMock(ResourceConnection::class);
52+
$this->storeMock = $this->createMock(StoreInterface::class);
53+
54+
$this->model = new DeleteExpiredQuote(
55+
$this->storeManagerMock,
56+
$this->scopeConfigMock,
57+
$this->resourceConnectionMock
58+
);
59+
}
60+
61+
/**
62+
* Test deleting expired quotes.
63+
*/
64+
public function testDeleteExpiredQuote(): void
65+
{
66+
$websiteId = 1;
67+
$storeIds = [1, 2];
68+
$websiteMock = $this->createMock(Website::class);
69+
$websiteMock->method('getStoreIds')->willReturn($storeIds);
70+
$this->storeManagerMock->method('getWebsite')->with($websiteId)->willReturn($websiteMock);
71+
$storeMock = $this->createMock(\Magento\Store\Model\Store::class);
72+
$storeMock->method('getWebsiteId')->willReturn($websiteId);
73+
$this->storeManagerMock->method('getStore')->willReturn($storeMock);
74+
75+
// Mock the scope config to return a specific lifetime
76+
$lifetime = 60; // 1 hour in seconds
77+
$this->scopeConfigMock->method('getValue')
78+
->with(Data::XML_PATH_LIFE_TIME, 'website', $websiteId)
79+
->willReturn($lifetime);
80+
81+
// Mock the database connection
82+
$connectionMock = $this->createMock(\Magento\Framework\DB\Adapter\AdapterInterface::class);
83+
$this->resourceConnectionMock->method('getConnection')->willReturn($connectionMock);
84+
$connectionMock->expects($this->once())
85+
->method('delete')
86+
->with(
87+
$this->resourceConnectionMock->getTableName('quote'),
88+
$this->callback(function ($condition) use ($storeIds, $lifetime) {
89+
// Validate the delete condition
90+
$expiredBefore = gmdate('Y-m-d H:i:s', time() - $lifetime);
91+
return isset($condition['store_id in (?)']) &&
92+
$condition['store_id in (?)'] === $storeIds &&
93+
isset($condition['updated_at < ?']) &&
94+
$condition['updated_at < ?'] === $expiredBefore &&
95+
isset($condition['is_persistent']) &&
96+
$condition['is_persistent'] === 1;
97+
})
98+
);
99+
100+
// Call the method to test
101+
$this->model->deleteExpiredQuote($websiteId);
102+
}
103+
104+
/**
105+
* Test deleting expired quotes when no websiteId is provided.
106+
*/
107+
public function testDeleteExpiredQuoteWithNullWebsiteId(): void
108+
{
109+
// Mock the store manager and store data
110+
$websiteId = 1;
111+
$storeIds = [1, 2];
112+
$websiteMock = $this->createMock(Website::class);
113+
$websiteMock->method('getStoreIds')->willReturn($storeIds);
114+
$this->storeManagerMock->method('getWebsite')->with($websiteId)->willReturn($websiteMock);
115+
$this->storeManagerMock->method('getStore')->willReturn($this->storeMock);
116+
$this->storeMock->method('getWebsiteId')->willReturn($websiteId);
117+
118+
// Mock the scope config to return a specific lifetime
119+
$lifetime = 60; // 1 hour in seconds
120+
$this->scopeConfigMock->method('getValue')
121+
->with(Data::XML_PATH_LIFE_TIME, 'website', $websiteId)
122+
->willReturn($lifetime);
123+
124+
// Mock the database connection
125+
$connectionMock = $this->createMock(\Magento\Framework\DB\Adapter\AdapterInterface::class);
126+
$this->resourceConnectionMock->method('getConnection')->willReturn($connectionMock);
127+
$connectionMock->expects($this->once())
128+
->method('delete')
129+
->with(
130+
$this->resourceConnectionMock->getTableName('quote'),
131+
$this->callback(function ($condition) use ($storeIds, $lifetime) {
132+
// Validate the delete condition
133+
$expiredBefore = gmdate('Y-m-d H:i:s', time() - $lifetime);
134+
return isset($condition['store_id in (?)']) &&
135+
$condition['store_id in (?)'] === $storeIds &&
136+
isset($condition['updated_at < ?']) &&
137+
$condition['updated_at < ?'] === $expiredBefore &&
138+
isset($condition['is_persistent']) &&
139+
$condition['is_persistent'] === 1;
140+
})
141+
);
142+
143+
// Call the method to test with null websiteId
144+
$this->model->deleteExpiredQuote();
145+
}
146+
147+
/**
148+
* Test when there is no lifetime configured.
149+
*/
150+
public function testDeleteExpiredQuoteWithNoLifetime(): void
151+
{
152+
// Mock the store manager and store data
153+
$websiteId = 1;
154+
$storeIds = [1, 2];
155+
$websiteMock = $this->createMock(Website::class);
156+
$websiteMock->method('getStoreIds')->willReturn($storeIds);
157+
$this->storeManagerMock->method('getWebsite')->with($websiteId)->willReturn($websiteMock);
158+
159+
// Mock the scope config to return no lifetime
160+
$this->scopeConfigMock->method('getValue')
161+
->with(Data::XML_PATH_LIFE_TIME, 'website', $websiteId)
162+
->willReturn(0); // No lifetime
163+
164+
// Mock the database connection
165+
$connectionMock = $this->createMock(\Magento\Framework\DB\Adapter\AdapterInterface::class);
166+
$this->resourceConnectionMock->method('getConnection')->willReturn($connectionMock);
167+
$connectionMock->expects($this->never()) // No delete should be called
168+
->method('delete');
169+
170+
// Call the method to test with no lifetime
171+
$this->model->deleteExpiredQuote($websiteId);
172+
}
173+
}

0 commit comments

Comments
 (0)