Skip to content

Commit 63cdbb2

Browse files
author
Anna Bukatar
committed
ACP2E-78: Wrong label is saved by scheduled update for shopping cart price rule
1 parent 3f9f487 commit 63cdbb2

File tree

5 files changed

+88
-22
lines changed

5 files changed

+88
-22
lines changed

app/code/Magento/SalesRule/Block/Adminhtml/Promo/Quote/Edit/Tab/Labels.php

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ public function __construct(
3939
protected $_nameInLayout = 'store_view_labels';
4040

4141
/**
42-
* {@inheritdoc}
42+
* @inheritdoc
4343
* @codeCoverageIgnore
4444
*/
4545
public function getTabClass()
@@ -48,7 +48,7 @@ public function getTabClass()
4848
}
4949

5050
/**
51-
* {@inheritdoc}
51+
* @inheritdoc
5252
* @codeCoverageIgnore
5353
*/
5454
public function getTabUrl()
@@ -57,7 +57,7 @@ public function getTabUrl()
5757
}
5858

5959
/**
60-
* {@inheritdoc}
60+
* @inheritdoc
6161
* @codeCoverageIgnore
6262
*/
6363
public function isAjaxLoaded()
@@ -66,7 +66,7 @@ public function isAjaxLoaded()
6666
}
6767

6868
/**
69-
* {@inheritdoc}
69+
* @inheritdoc
7070
* @codeCoverageIgnore
7171
*/
7272
public function getTabLabel()
@@ -75,7 +75,7 @@ public function getTabLabel()
7575
}
7676

7777
/**
78-
* {@inheritdoc}
78+
* @inheritdoc
7979
* @codeCoverageIgnore
8080
*/
8181
public function getTabTitle()
@@ -84,7 +84,7 @@ public function getTabTitle()
8484
}
8585

8686
/**
87-
* {@inheritdoc}
87+
* @inheritdoc
8888
* @codeCoverageIgnore
8989
*/
9090
public function canShowTab()
@@ -93,7 +93,7 @@ public function canShowTab()
9393
}
9494

9595
/**
96-
* {@inheritdoc}
96+
* @inheritdoc
9797
* @codeCoverageIgnore
9898
*/
9999
public function isHidden()
@@ -180,7 +180,7 @@ protected function _createStoreSpecificFieldset($form, $labels)
180180
'required' => false,
181181
'value' => isset($labels[$store->getId()]) ? $labels[$store->getId()] : '',
182182
'fieldset_html_class' => 'store',
183-
'data-form-part' => 'sales_rule_form'
183+
'data-form-part' => $this->getData('target_form')
184184
]
185185
);
186186
}

app/code/Magento/SalesRule/Model/ResourceModel/Rule.php

Lines changed: 39 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,11 @@ class Rule extends AbstractResource
5858
*/
5959
private $metadataPool;
6060

61+
/**
62+
* @var string
63+
*/
64+
private $linkedField;
65+
6166
/**
6267
* @param \Magento\Framework\Model\ResourceModel\Db\Context $context
6368
* @param \Magento\Framework\Stdlib\StringUtils $string
@@ -79,6 +84,7 @@ public function __construct(
7984
$this->string = $string;
8085
$this->_resourceCoupon = $resourceCoupon;
8186
$associatedEntitiesMapInstance = $associatedEntityMapInstance ?: ObjectManager::getInstance()->get(
87+
// phpstan:ignore "Class Magento\SalesRule\Model\ResourceModel\Rule\AssociatedEntityMap not found."
8288
\Magento\SalesRule\Model\ResourceModel\Rule\AssociatedEntityMap::class
8389
);
8490
$this->_associatedEntitiesMap = $associatedEntitiesMapInstance->getData();
@@ -98,9 +104,10 @@ protected function _construct()
98104
}
99105

100106
/**
107+
* Load customer group IDs for a rule
108+
*
101109
* @param AbstractModel $object
102110
* @return void
103-
* @deprecated 100.1.0
104111
*/
105112
public function loadCustomerGroupIds(AbstractModel $object)
106113
{
@@ -111,9 +118,10 @@ public function loadCustomerGroupIds(AbstractModel $object)
111118
}
112119

113120
/**
121+
* Load website IDs for a rule
122+
*
114123
* @param AbstractModel $object
115124
* @return void
116-
* @deprecated 100.1.0
117125
*/
118126
public function loadWebsiteIds(AbstractModel $object)
119127
{
@@ -166,7 +174,7 @@ public function load(AbstractModel $object, $value, $field = null)
166174
protected function _afterSave(AbstractModel $object)
167175
{
168176
if ($object->hasStoreLabels()) {
169-
$this->saveStoreLabels($object->getId(), $object->getStoreLabels());
177+
$this->saveStoreLabels($object->getData($this->getLinkField()), $object->getStoreLabels());
170178
}
171179

172180
// Save product attributes used in rule
@@ -223,7 +231,7 @@ public function saveStoreLabels($ruleId, $labels)
223231
$data = [];
224232
foreach ($labels as $storeId => $label) {
225233
if ($this->string->strlen($label)) {
226-
$data[] = ['rule_id' => $ruleId, 'store_id' => $storeId, 'label' => $label];
234+
$data[] = [$this->getLinkField() => $ruleId, 'store_id' => $storeId, 'label' => $label];
227235
} else {
228236
$deleteByStoreIds[] = $storeId;
229237
}
@@ -236,7 +244,10 @@ public function saveStoreLabels($ruleId, $labels)
236244
}
237245

238246
if (!empty($deleteByStoreIds)) {
239-
$connection->delete($table, ['rule_id=?' => $ruleId, 'store_id IN (?)' => $deleteByStoreIds]);
247+
$connection->delete(
248+
$table,
249+
[$this->getLinkField() . '=?' => $ruleId, 'store_id IN (?)' => $deleteByStoreIds]
250+
);
240251
}
241252
} catch (\Exception $e) {
242253
$connection->rollBack();
@@ -259,7 +270,7 @@ public function getStoreLabels($ruleId)
259270
$this->getTable('salesrule_label'),
260271
['store_id', 'label']
261272
)->where(
262-
'rule_id = :rule_id'
273+
$this->getLinkField() . ' = :rule_id'
263274
);
264275
return $this->getConnection()->fetchPairs($select, [':rule_id' => $ruleId]);
265276
}
@@ -277,7 +288,7 @@ public function getStoreLabel($ruleId, $storeId)
277288
$this->getTable('salesrule_label'),
278289
'label'
279290
)->where(
280-
'rule_id = :rule_id'
291+
$this->getLinkField() . ' = :rule_id'
281292
)->where(
282293
'store_id IN(0, :store_id)'
283294
)->order(
@@ -315,10 +326,9 @@ public function getActiveAttributes()
315326
public function setActualProductAttributes($rule, $attributes)
316327
{
317328
$connection = $this->getConnection();
318-
$metadata = $this->metadataPool->getMetadata(RuleInterface::class);
319329
$connection->delete(
320330
$this->getTable('salesrule_product_attribute'),
321-
[$metadata->getLinkField() . '=?' => $rule->getData($metadata->getLinkField())]
331+
[$this->getLinkField() . '=?' => $rule->getData($this->getLinkField())]
322332
);
323333

324334
//Getting attribute IDs for attribute codes
@@ -341,7 +351,7 @@ public function setActualProductAttributes($rule, $attributes)
341351
foreach ($rule->getWebsiteIds() as $websiteId) {
342352
foreach ($attributeIds as $attribute) {
343353
$data[] = [
344-
$metadata->getLinkField() => $rule->getData($metadata->getLinkField()),
354+
$this->getLinkField() => $rule->getData($this->getLinkField()),
345355
'website_id' => $websiteId,
346356
'customer_group_id' => $customerGroupId,
347357
'attribute_id' => $attribute,
@@ -374,6 +384,8 @@ public function getProductAttributes($serializedString)
374384
}
375385

376386
/**
387+
* Save cart rule
388+
*
377389
* @param \Magento\Framework\Model\AbstractModel $object
378390
* @return $this
379391
*/
@@ -396,8 +408,9 @@ public function delete(AbstractModel $object)
396408
}
397409

398410
/**
411+
* Init EntityManager
412+
*
399413
* @return \Magento\Framework\EntityManager\EntityManager
400-
* @deprecated 100.1.0
401414
*/
402415
private function getEntityManager()
403416
{
@@ -407,4 +420,19 @@ private function getEntityManager()
407420
}
408421
return $this->entityManager;
409422
}
423+
424+
/**
425+
* Get lined field for Rule entity
426+
*
427+
* @return string
428+
*/
429+
public function getLinkField() :string
430+
{
431+
if ($this->linkedField === null) {
432+
$metadata = $this->metadataPool->getMetadata(RuleInterface::class);
433+
$this->linkedField = $metadata->getLinkField();
434+
}
435+
436+
return $this->linkedField;
437+
}
410438
}

app/code/Magento/SalesRule/Model/Rule.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -423,7 +423,8 @@ public function getStoreLabel($store = null)
423423
public function getStoreLabels()
424424
{
425425
if (!$this->hasStoreLabels()) {
426-
$labels = $this->_getResource()->getStoreLabels($this->getId());
426+
$linkedField = $this->_getResource()->getLinkField();
427+
$labels = $this->_getResource()->getStoreLabels($this->getData($linkedField));
427428
$this->setStoreLabels($labels);
428429
}
429430

app/code/Magento/SalesRule/Test/Unit/Model/ResourceModel/RuleTest.php

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,13 @@
1212
use Magento\Framework\DB\Adapter\AdapterInterface;
1313
use Magento\Framework\DB\Select;
1414
use Magento\Framework\EntityManager\EntityManager;
15+
use Magento\Framework\EntityManager\EntityMetadataInterface;
16+
use Magento\Framework\EntityManager\MetadataPool;
1517
use Magento\Framework\Model\AbstractModel;
1618
use Magento\Framework\Model\ResourceModel\Db\Context;
1719
use Magento\Framework\Model\ResourceModel\Db\ObjectRelationProcessor;
1820
use Magento\Framework\Model\ResourceModel\Db\TransactionManagerInterface;
21+
use Magento\Framework\Serialize\Serializer\Json;
1922
use Magento\Framework\TestFramework\Unit\Helper\ObjectManager;
2023
use Magento\SalesRule\Model\ResourceModel\Rule;
2124
use Magento\SalesRule\Model\Rule\Condition\Product;
@@ -78,6 +81,11 @@ class RuleTest extends TestCase
7881
*/
7982
protected $relationProcessorMock;
8083

84+
/**
85+
* @var MockObject
86+
*/
87+
private $metadataPoolMock;
88+
8189
protected function setUp(): void
8290
{
8391
$this->objectManager = new ObjectManager($this);
@@ -154,14 +162,22 @@ protected function setUp(): void
154162
],
155163
]
156164
);
165+
$serializerMock = $this->getMockBuilder(Json::class)
166+
->disableOriginalConstructor()
167+
->getMockForAbstractClass();
168+
$this->metadataPoolMock = $this->getMockBuilder(MetadataPool::class)
169+
->disableOriginalConstructor()
170+
->getMock();
157171

158172
$this->model = $this->objectManager->getObject(
159173
Rule::class,
160174
[
161175
'context' => $context,
162176
'connectionName' => $connectionName,
163177
'entityManager' => $this->entityManager,
164-
'associatedEntityMapInstance' => $associatedEntitiesMap
178+
'associatedEntityMapInstance' => $associatedEntitiesMap,
179+
'serializer' => $serializerMock,
180+
'metadataPool' => $this->metadataPoolMock
165181
]
166182
);
167183
}
@@ -212,6 +228,23 @@ public function testGetProductAttributes($testString, $expects)
212228
$this->assertEquals($expects, $result);
213229
}
214230

231+
/**
232+
* Checks that linked field is used for rule labels
233+
*/
234+
public function testSaveStoreLabels()
235+
{
236+
$entityMetadataInterfaceMock = $this->getMockBuilder(EntityMetadataInterface::class)
237+
->disableOriginalConstructor()
238+
->getMockForAbstractClass();
239+
$entityMetadataInterfaceMock->expects($this->once())
240+
->method('getLinkField')
241+
->willReturn('fieldName');
242+
$this->metadataPoolMock->expects($this->once())
243+
->method('getMetadata')
244+
->willReturn($entityMetadataInterfaceMock);
245+
$this->model->saveStoreLabels(1, ['test']);
246+
}
247+
215248
/**
216249
* @return array
217250
*/

app/code/Magento/SalesRule/view/adminhtml/ui_component/sales_rule_form.xml

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -538,7 +538,11 @@
538538
</field>
539539
<container name="store_view_labels" sortOrder="40">
540540
<htmlContent name="html_content">
541-
<block name="store_view_labels" class="Magento\SalesRule\Block\Adminhtml\Promo\Quote\Edit\Tab\Labels"/>
541+
<block name="store_view_labels" class="Magento\SalesRule\Block\Adminhtml\Promo\Quote\Edit\Tab\Labels">
542+
<arguments>
543+
<argument name="target_form" xsi:type="string">sales_rule_form</argument>
544+
</arguments>
545+
</block>
542546
</htmlContent>
543547
</container>
544548
</fieldset>

0 commit comments

Comments
 (0)