Skip to content

Commit 8a644eb

Browse files
committed
ACP2E-3493: small refactoring
1 parent b68fa5d commit 8a644eb

File tree

2 files changed

+97
-49
lines changed

2 files changed

+97
-49
lines changed

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

Lines changed: 44 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77

88
namespace Magento\Persistent\Model\ResourceModel;
99

10+
use Magento\Framework\DB\Select;
1011
use Magento\Framework\Model\ResourceModel\Db\Collection\AbstractCollection;
1112
use Magento\Persistent\Helper\Data;
1213
use Magento\Framework\App\Config\ScopeConfigInterface;
@@ -53,27 +54,50 @@ public function getExpiredPersistentQuotes(StoreInterface $store, int $lastId, i
5354
/** @var $quotes Collection */
5455
$quotes = $this->quoteCollectionFactory->create();
5556

56-
$select = $quotes->getSelect();
57-
$select->joinLeft(
58-
['cl1' => $quotes->getTable('customer_log')],
59-
'cl1.customer_id = main_table.customer_id
60-
AND cl1.last_login_at < cl1.last_logout_at
61-
AND cl1.last_logout_at IS NOT NULL',
62-
[]
63-
)->joinLeft(
64-
['cl2' => $quotes->getTable('customer_log')],
65-
'cl2.customer_id = main_table.customer_id
66-
AND cl2.last_login_at < "' . $lastLoginCondition . '"
67-
AND (cl2.last_logout_at IS NULL OR cl2.last_login_at > cl2.last_logout_at)',
68-
[]
69-
);
57+
$additionalQuotes = clone $quotes;
58+
$additionalQuotes->addFieldToFilter('main_table.store_id', (int)$store->getId());
59+
$additionalQuotes->addFieldToFilter('main_table.updated_at', ['lt' => $lastLoginCondition]);
60+
$additionalQuotes->addFieldToFilter('main_table.is_persistent', 1);
61+
$additionalQuotes->addFieldToFilter('main_table.entity_id', ['gt' => $lastId]);
62+
$additionalQuotes->setOrder('entity_id', Collection::SORT_ORDER_ASC);
63+
$additionalQuotes->setPageSize($batchSize);
64+
65+
$select1 = clone $additionalQuotes->getSelect();
66+
$select2 = clone $additionalQuotes->getSelect();
67+
68+
//case 1 - customer logged in and logged out
69+
$select1->reset(Select::COLUMNS)
70+
->columns('main_table.entity_id')
71+
->joinLeft(
72+
['cl1' => $additionalQuotes->getTable('customer_log')],
73+
'cl1.customer_id = main_table.customer_id',
74+
[]
75+
)->where('cl1.last_login_at < cl1.last_logout_at
76+
AND cl1.last_logout_at IS NOT NULL');
77+
78+
//case 2 - customer logged in and not logged out but session expired
79+
//case 3 - customer logged in, logged out, logged in and then session expired
80+
$select2->reset(Select::COLUMNS)
81+
->columns('main_table.entity_id')
82+
->joinLeft(
83+
['cl2' => $additionalQuotes->getTable('customer_log')],
84+
'cl2.customer_id = main_table.customer_id',
85+
[]
86+
)->where('cl2.last_login_at < "' . $lastLoginCondition . '"
87+
AND (cl2.last_logout_at IS NULL OR cl2.last_login_at > cl2.last_logout_at)');
88+
89+
$selectQuoteIds = $additionalQuotes
90+
->getConnection()
91+
->select()
92+
->union(
93+
[
94+
$select1,
95+
$select2
96+
],
97+
Select::SQL_UNION_ALL
98+
);
7099

71-
$quotes->addFieldToFilter('main_table.store_id', $store->getId());
72-
$quotes->addFieldToFilter('main_table.updated_at', ['lt' => $lastLoginCondition]);
73-
$quotes->addFieldToFilter('main_table.is_persistent', 1);
74-
$quotes->addFieldToFilter('main_table.entity_id', ['gt' => $lastId]);
75-
$quotes->setOrder('entity_id', Collection::SORT_ORDER_ASC);
76-
$quotes->setPageSize($batchSize);
100+
$quotes->getSelect()->where('main_table.entity_id IN (' . $selectQuoteIds . ')');
77101

78102
return $quotes;
79103
}

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

Lines changed: 53 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77

88
namespace Magento\Persistent\Test\Unit\Model\ResourceModel;
99

10+
use Magento\Framework\DB\Adapter\AdapterInterface;
1011
use Magento\Persistent\Model\ResourceModel\ExpiredPersistentQuotesCollection;
1112
use Magento\Quote\Model\ResourceModel\Quote\CollectionFactory;
1213
use Magento\Framework\App\Config\ScopeConfigInterface;
@@ -65,35 +66,6 @@ public function testGetExpiredPersistentQuotes(): void
6566
$quoteCollectionMock = $this->createMock(Collection::class);
6667
$this->quoteCollectionFactoryMock->method('create')->willReturn($quoteCollectionMock);
6768

68-
$dbSelectMock = $this->createMock(Select::class);
69-
$quoteCollectionMock->method('getSelect')->willReturn($dbSelectMock);
70-
$quoteCollectionMock->method('getTable')->willReturn('customer_log');
71-
72-
$dbSelectMock->method('joinLeft')
73-
->willReturnCallback(function ($table, $condition) use ($dbSelectMock) {
74-
static $callCount = 0;
75-
$callCount++;
76-
77-
if ($callCount === 1) {
78-
$this->assertEquals(['cl1' => 'customer_log'], $table);
79-
$this->assertStringContainsString('cl1.customer_id = main_table.customer_id', $condition);
80-
$this->assertStringContainsString('cl1.last_login_at < cl1.last_logout_at', $condition);
81-
$this->assertStringContainsString('cl1.last_logout_at IS NOT NULL', $condition);
82-
}
83-
84-
if ($callCount === 2) {
85-
$this->assertEquals(['cl2' => 'customer_log'], $table);
86-
$this->assertStringContainsString('cl2.customer_id = main_table.customer_id', $condition);
87-
$this->assertStringContainsString('cl2.last_login_at <', $condition);
88-
$this->assertStringContainsString(
89-
'cl2.last_logout_at IS NULL OR cl2.last_login_at > cl2.last_logout_at',
90-
$condition
91-
);
92-
}
93-
94-
return $dbSelectMock;
95-
});
96-
9769
$quoteCollectionMock->method('addFieldToFilter')
9870
->willReturnCallback(function ($field) use ($quoteCollectionMock) {
9971
static $filterCallCount = 0;
@@ -117,6 +89,58 @@ public function testGetExpiredPersistentQuotes(): void
11789
->with($this->isType('integer'))
11890
->willReturnSelf();
11991

92+
$dbSelectMock1 = $this->createMock(Select::class);
93+
$dbSelectMock2 = $this->createMock(Select::class);
94+
$dbSelectMock3 = $this->createMock(Select::class);
95+
$quoteCollectionMock->method('getSelect')
96+
->willReturnOnConsecutiveCalls($dbSelectMock1, $dbSelectMock2, $dbSelectMock3);
97+
$quoteCollectionMock->method('getTable')
98+
->willReturn('customer_log');
99+
100+
$dbSelectMock1->method('reset')
101+
->with(Select::COLUMNS)
102+
->willReturn($dbSelectMock1);
103+
$dbSelectMock1->method('columns')->willReturnSelf();
104+
$dbSelectMock1->method('joinLeft')
105+
->with(
106+
['cl1' => 'customer_log'],
107+
'cl1.customer_id = main_table.customer_id',
108+
[]
109+
)
110+
->willReturnSelf();
111+
$dbSelectMock1->method('where')
112+
->with('cl1.last_login_at < cl1.last_logout_at
113+
AND cl1.last_logout_at IS NOT NULL')
114+
->willReturnSelf();
115+
116+
$dbSelectMock2->method('reset')
117+
->with(Select::COLUMNS)
118+
->willReturn($dbSelectMock2);
119+
$dbSelectMock2->method('columns')->willReturnSelf();
120+
$dbSelectMock2->method('joinLeft')
121+
->with(
122+
['cl2' => 'customer_log'],
123+
'cl2.customer_id = main_table.customer_id',
124+
[]
125+
)
126+
->willReturnSelf();
127+
$dbSelectMock2->method('where')
128+
->with('cl2.last_login_at < "' . gmdate("Y-m-d H:i:s", time() - 60) . '"
129+
AND (cl2.last_logout_at IS NULL OR cl2.last_login_at > cl2.last_logout_at)')
130+
->willReturnSelf();
131+
132+
$dbSelectMockUnion = $this->createMock(Select::class);
133+
$connectionMock = $this->createMock(AdapterInterface::class);
134+
$quoteCollectionMock->method('getConnection')->willReturn($connectionMock);
135+
$connectionMock->method('select')->willReturn($dbSelectMockUnion);
136+
$dbSelectMockUnion->method('union')
137+
->with([$dbSelectMock1, $dbSelectMock2], Select::SQL_UNION_ALL)
138+
->willReturn($dbSelectMockUnion);
139+
140+
$dbSelectMockUnion->method('where')
141+
->with($this->stringContains('main_table.entity_id IN ('))
142+
->willReturnSelf();
143+
120144
$result = $this->model->getExpiredPersistentQuotes($storeMock, 0, 100);
121145
$this->assertSame($quoteCollectionMock, $result);
122146
}

0 commit comments

Comments
 (0)