Skip to content

Commit a92bc25

Browse files
authored
Merge pull request #6497 from magento-l3/2.5-bugfixes-010521
[L3] [2.5.0] Bugfixes
2 parents afd9589 + 4867c17 commit a92bc25

File tree

5 files changed

+209
-18
lines changed

5 files changed

+209
-18
lines changed

app/code/Magento/CustomerImportExport/Test/Unit/Model/ResourceModel/Import/CustomerComposite/DataTest.php

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
*/
1111
namespace Magento\CustomerImportExport\Test\Unit\Model\ResourceModel\Import\CustomerComposite;
1212

13+
use Magento\Backend\Model\Auth\Session;
1314
use Magento\CustomerImportExport\Model\Import\Address;
1415
use Magento\CustomerImportExport\Model\Import\CustomerComposite;
1516
use Magento\Framework\App\ResourceConnection;
@@ -20,13 +21,44 @@
2021
use Magento\Framework\Json\Helper\Data;
2122
use Magento\Framework\Model\ResourceModel\Db\Context;
2223
use Magento\Framework\TestFramework\Unit\Helper\ObjectManager;
24+
use PHPUnit\Framework\MockObject\MockBuilder;
2325
use PHPUnit\Framework\TestCase;
2426

2527
/**
2628
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
2729
*/
2830
class DataTest extends TestCase
2931
{
32+
/**
33+
* @var string[]
34+
*/
35+
private $mockBuilderCallbacks = [
36+
Session::class => 'getSessionMock'
37+
];
38+
39+
/**
40+
* @inheritDoc
41+
*/
42+
public function getMockBuilder(string $className): MockBuilder
43+
{
44+
$mockBuilder = parent::getMockBuilder($className);
45+
if (isset($this->mockBuilderCallbacks[$className])) {
46+
$mockBuilder = call_user_func_array([$this, $this->mockBuilderCallbacks[$className]], [$mockBuilder]);
47+
}
48+
49+
return $mockBuilder;
50+
}
51+
52+
/**
53+
* @param MockBuilder $mockBuilder
54+
* @return MockBuilder
55+
* @SuppressWarnings(PHPMD.UnusedPrivateMethod) Used in callback.
56+
*/
57+
private function getSessionMock(MockBuilder $mockBuilder): MockBuilder
58+
{
59+
return $mockBuilder->onlyMethods(['isLoggedIn']);
60+
}
61+
3062
/**
3163
* Array of customer attributes
3264
*
@@ -57,9 +89,10 @@ protected function _getDependencies($entityType, $bunchData)
5789
);
5890

5991
/** @var $selectMock \Magento\Framework\DB\Select */
60-
$selectMock = $this->createPartialMock(Select::class, ['from', 'order']);
92+
$selectMock = $this->createPartialMock(Select::class, ['from', 'order', 'where']);
6193
$selectMock->expects($this->any())->method('from')->willReturnSelf();
6294
$selectMock->expects($this->any())->method('order')->willReturnSelf();
95+
$selectMock->expects($this->any())->method('where')->willReturnSelf();
6396

6497
/** @var AdapterInterface $connectionMock */
6598
$connectionMock = $this->getMockBuilder(\Magento\Framework\DB\Adapter\Pdo\Mysql::class)

app/code/Magento/ImportExport/Model/ResourceModel/Import/Data.php

Lines changed: 84 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,23 @@
55
*/
66
namespace Magento\ImportExport\Model\ResourceModel\Import;
77

8+
use Magento\Backend\Model\Auth\Session;
9+
use Magento\Framework\App\ObjectManager;
10+
use Magento\Framework\DB\Select;
11+
812
/**
913
* ImportExport import data resource model
1014
*
1115
* @api
1216
* @since 100.0.2
17+
* @SuppressWarnings(PHPMD.CookieAndSessionMisuse) Necessary to get current logged in user without modifying methods
1318
*/
1419
class Data extends \Magento\Framework\Model\ResourceModel\Db\AbstractDb implements \IteratorAggregate
1520
{
21+
/**
22+
* Offline import user ID
23+
*/
24+
private const DEFAULT_USER_ID = 0;
1625
/**
1726
* @var \Iterator
1827
*/
@@ -24,21 +33,28 @@ class Data extends \Magento\Framework\Model\ResourceModel\Db\AbstractDb implemen
2433
* @var \Magento\Framework\Json\Helper\Data
2534
*/
2635
protected $jsonHelper;
36+
/**
37+
* @var Session
38+
*/
39+
private $authSession;
2740

2841
/**
2942
* Class constructor
3043
*
3144
* @param \Magento\Framework\Model\ResourceModel\Db\Context $context
3245
* @param \Magento\Framework\Json\Helper\Data $jsonHelper
33-
* @param string $connectionName
46+
* @param string|null $connectionName
47+
* @param Session|null $authSession
3448
*/
3549
public function __construct(
3650
\Magento\Framework\Model\ResourceModel\Db\Context $context,
3751
\Magento\Framework\Json\Helper\Data $jsonHelper,
38-
$connectionName = null
52+
$connectionName = null,
53+
?Session $authSession = null
3954
) {
4055
parent::__construct($context, $connectionName);
4156
$this->jsonHelper = $jsonHelper;
57+
$this->authSession = $authSession ?? ObjectManager::getInstance()->get(Session::class);
4258
}
4359

4460
/**
@@ -60,6 +76,7 @@ public function getIterator()
6076
{
6177
$connection = $this->getConnection();
6278
$select = $connection->select()->from($this->getMainTable(), ['data'])->order('id ASC');
79+
$select = $this->prepareSelect($select);
6380
$stmt = $connection->query($select);
6481

6582
$stmt->setFetchMode(\Zend_Db::FETCH_NUM);
@@ -81,7 +98,7 @@ public function getIterator()
8198
*/
8299
public function cleanBunches()
83100
{
84-
return $this->getConnection()->delete($this->getMainTable());
101+
return $this->getConnection()->delete($this->getMainTable(), $this->prepareDelete([]));
85102
}
86103

87104
/**
@@ -114,7 +131,9 @@ public function getEntityTypeCode()
114131
public function getUniqueColumnData($code)
115132
{
116133
$connection = $this->getConnection();
117-
$values = array_unique($connection->fetchCol($connection->select()->from($this->getMainTable(), [$code])));
134+
$select = $connection->select()->from($this->getMainTable(), [$code]);
135+
$select = $this->prepareSelect($select);
136+
$values = array_unique($connection->fetchCol($select));
118137

119138
if (count($values) != 1) {
120139
throw new \Magento\Framework\Exception\LocalizedException(
@@ -169,7 +188,67 @@ public function saveBunch($entity, $behavior, array $data)
169188

170189
return $this->getConnection()->insert(
171190
$this->getMainTable(),
172-
['behavior' => $behavior, 'entity' => $entity, 'data' => $encodedData]
191+
$this->prepareInsert(['behavior' => $behavior, 'entity' => $entity, 'data' => $encodedData])
173192
);
174193
}
194+
195+
/**
196+
* Prepare select for query
197+
*
198+
* @param Select $select
199+
* @return Select
200+
*/
201+
private function prepareSelect(Select $select): Select
202+
{
203+
// check if the table has not been overridden for backward compatibility
204+
if ($this->getMainTable() === $this->getTable('importexport_importdata')) {
205+
// user_id is NULL part is for backward compatibility
206+
$select->where('user_id=? OR user_id is NULL', $this->getUserId());
207+
}
208+
209+
return $select;
210+
}
211+
212+
/**
213+
* Prepare data for insert
214+
*
215+
* @param array $data
216+
* @return array
217+
*/
218+
private function prepareInsert(array $data): array
219+
{
220+
// check if the table has not been overridden for backward compatibility
221+
if ($this->getMainTable() === $this->getTable('importexport_importdata')) {
222+
$data['user_id'] = $this->getUserId();
223+
}
224+
225+
return $data;
226+
}
227+
228+
/**
229+
* Prepare delete constraints
230+
*
231+
* @param array $where
232+
* @return array
233+
*/
234+
private function prepareDelete(array $where): array
235+
{
236+
// check if the table has not been overridden for backward compatibility
237+
if ($this->getMainTable() === $this->getTable('importexport_importdata')) {
238+
// user_id is NULL part is for backward compatibility
239+
$where['user_id=? OR user_id is NULL'] = $this->getUserId();
240+
}
241+
242+
return $where;
243+
}
244+
245+
/**
246+
* Get current user ID
247+
*
248+
* @return int
249+
*/
250+
private function getUserId(): int
251+
{
252+
return $this->authSession->isLoggedIn() ? $this->authSession->getUser()->getId() : self::DEFAULT_USER_ID;
253+
}
175254
}

app/code/Magento/ImportExport/etc/db_schema.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
<column xsi:type="varchar" name="entity" nullable="false" length="50" comment="Entity"/>
1313
<column xsi:type="varchar" name="behavior" nullable="false" length="10" default="append" comment="Behavior"/>
1414
<column xsi:type="longtext" name="data" nullable="true" comment="Data"/>
15+
<column xsi:type="int" name="user_id" unsigned="true" nullable="true" identity="false" comment="User ID"/>
1516
<constraint xsi:type="primary" referenceId="PRIMARY">
1617
<column name="id"/>
1718
</constraint>

app/code/Magento/ImportExport/etc/db_schema_whitelist.json

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@
44
"id": true,
55
"entity": true,
66
"behavior": true,
7-
"data": true
7+
"data": true,
8+
"user_id": true
89
},
910
"constraint": {
1011
"PRIMARY": true
@@ -24,4 +25,4 @@
2425
"PRIMARY": true
2526
}
2627
}
27-
}
28+
}

0 commit comments

Comments
 (0)