Skip to content

Commit f6bc039

Browse files
committed
ACP2E-493: Staging preview does not show expected dynamic block
1 parent 0117201 commit f6bc039

File tree

3 files changed

+307
-0
lines changed

3 files changed

+307
-0
lines changed
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
declare(strict_types=1);
7+
8+
namespace Magento\CatalogRule\Test\Fixture\Data;
9+
10+
use Magento\CatalogRule\Model\Rule\Action\Collection;
11+
use Magento\Framework\Serialize\Serializer\Json;
12+
13+
class ActionsSerializer
14+
{
15+
/**
16+
* @var Json
17+
*/
18+
private Json $json;
19+
20+
/**
21+
* @param Json $json
22+
*/
23+
public function __construct(
24+
Json $json
25+
) {
26+
$this->json = $json;
27+
}
28+
29+
/**
30+
* Normalizes and serializes actions data
31+
*
32+
* @param array $data
33+
* @return string
34+
*/
35+
public function serialize(array $data): string
36+
{
37+
return $this->json->serialize($this->normalize($data));
38+
}
39+
40+
/**
41+
* Normalizes actions data
42+
*
43+
* @param array $data
44+
* @return array
45+
*/
46+
private function normalize(array $data) : array
47+
{
48+
$actions = $data;
49+
$actions += [
50+
'type' => Collection::class,
51+
'attribute' => null,
52+
'value' => null,
53+
'operator' => '=',
54+
];
55+
return $actions;
56+
}
57+
}
Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
declare(strict_types=1);
7+
8+
namespace Magento\CatalogRule\Test\Fixture\Data;
9+
10+
use Magento\CatalogRule\Model\Rule\Condition\Combine;
11+
use Magento\CatalogRule\Model\Rule\Condition\Product;
12+
use Magento\Framework\Serialize\Serializer\Json;
13+
14+
class ConditionsSerializer
15+
{
16+
/**
17+
* @var Json
18+
*/
19+
private Json $json;
20+
21+
/**
22+
* @param Json $json
23+
*/
24+
public function __construct(
25+
Json $json
26+
) {
27+
$this->json = $json;
28+
}
29+
30+
/**
31+
* Normalizes and serializes conditions data
32+
*
33+
* @param array $data
34+
* @return string
35+
*/
36+
public function serialize(array $data): string
37+
{
38+
return $this->json->serialize($this->normalize($data));
39+
}
40+
41+
/**
42+
* Normalizes conditions data
43+
*
44+
* @param array $data
45+
* @return array
46+
*/
47+
private function normalize(array $data) : array
48+
{
49+
$conditions = $data;
50+
if (array_is_list($conditions)) {
51+
$conditions = [
52+
'conditions' => $conditions,
53+
];
54+
}
55+
$conditions += [
56+
'type' => Combine::class,
57+
'attribute' => null,
58+
'value' => true,
59+
'operator' => null,
60+
'aggregator' => 'all',
61+
'is_value_processed' => null,
62+
'conditions' => [
63+
64+
],
65+
];
66+
$subConditions = $conditions['conditions'];
67+
$conditions['conditions'] = [];
68+
69+
foreach ($subConditions as $condition) {
70+
if (isset($condition['conditions']) && array_is_list($condition)) {
71+
$condition = $this->normalize($condition);
72+
} else {
73+
$condition += [
74+
'type' => Product::class,
75+
'attribute' => null,
76+
'value' => null,
77+
'operator' => '==',
78+
'is_value_processed' => false,
79+
];
80+
}
81+
82+
$conditions['conditions'][] = $condition;
83+
}
84+
return $conditions;
85+
}
86+
}
Lines changed: 164 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,164 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
declare(strict_types=1);
7+
8+
namespace Magento\CatalogRule\Test\Fixture;
9+
10+
use Magento\CatalogRule\Test\Fixture\Data\ActionsSerializer;
11+
use Magento\CatalogRule\Test\Fixture\Data\ConditionsSerializer;
12+
use Magento\Customer\Model\ResourceModel\Group\CollectionFactory;
13+
use Magento\Framework\DataObject;
14+
use Magento\CatalogRule\Model\ResourceModel\Rule as ResourceModel;
15+
use Magento\CatalogRule\Model\RuleFactory;
16+
use Magento\Store\Model\StoreManagerInterface;
17+
use Magento\TestFramework\Fixture\Data\ProcessorInterface;
18+
use Magento\TestFramework\Fixture\RevertibleDataFixtureInterface;
19+
20+
class Rule implements RevertibleDataFixtureInterface
21+
{
22+
private const DEFAULT_DATA = [
23+
'name' => 'catalogrule%uniqid%',
24+
'sort_order' => 0,
25+
'is_active' => 1,
26+
'description' => null,
27+
'website_ids' => [],
28+
'customer_group_ids' => [],
29+
'stop_rules_processing' => true,
30+
'simple_action' => 'by_percent',
31+
'discount_amount' => 0,
32+
'conditions' => [],
33+
'actions' => [],
34+
];
35+
36+
/**
37+
* @var ProcessorInterface
38+
*/
39+
private ProcessorInterface $dataProcessor;
40+
41+
/**
42+
* @var ResourceModel
43+
*/
44+
private ResourceModel $catalogRuleResourceModel;
45+
46+
/**
47+
* @var RuleFactory
48+
*/
49+
private RuleFactory $catalogRuleFactory;
50+
51+
/**
52+
* @var CollectionFactory
53+
*/
54+
private CollectionFactory $customerGroupCollectionFactory;
55+
56+
/**
57+
* @var StoreManagerInterface
58+
*/
59+
private StoreManagerInterface $storeManager;
60+
61+
/**
62+
* @var ConditionsSerializer
63+
*/
64+
private ConditionsSerializer $conditionsSerializer;
65+
66+
/**
67+
* @var ActionsSerializer
68+
*/
69+
private ActionsSerializer $actionsSerializer;
70+
71+
/**
72+
* @param ProcessorInterface $dataProcessor
73+
* @param ResourceModel $catalogRuleResourceModel
74+
* @param RuleFactory $catalogRuleFactory
75+
* @param CollectionFactory $customerGroupCollectionFactory
76+
* @param StoreManagerInterface $storeManager
77+
* @param ConditionsSerializer $conditionsSerializer
78+
* @param ActionsSerializer $actionsSerializer
79+
*/
80+
public function __construct(
81+
ProcessorInterface $dataProcessor,
82+
ResourceModel $catalogRuleResourceModel,
83+
RuleFactory $catalogRuleFactory,
84+
CollectionFactory $customerGroupCollectionFactory,
85+
StoreManagerInterface $storeManager,
86+
ConditionsSerializer $conditionsSerializer,
87+
ActionsSerializer $actionsSerializer
88+
) {
89+
$this->dataProcessor = $dataProcessor;
90+
$this->catalogRuleResourceModel = $catalogRuleResourceModel;
91+
$this->catalogRuleFactory = $catalogRuleFactory;
92+
$this->customerGroupCollectionFactory = $customerGroupCollectionFactory;
93+
$this->storeManager = $storeManager;
94+
$this->conditionsSerializer = $conditionsSerializer;
95+
$this->actionsSerializer = $actionsSerializer;
96+
}
97+
98+
/**
99+
* @inheritdoc
100+
*/
101+
public function apply(array $data = []): ?DataObject
102+
{
103+
/** @var \Magento\CatalogRule\Model\Rule $model */
104+
$model = $this->catalogRuleFactory->create();
105+
$data = $this->prepareData($data);
106+
$model->setData($data);
107+
$this->catalogRuleResourceModel->save($model);
108+
109+
return $model;
110+
}
111+
112+
/**
113+
* {@inheritdoc}
114+
* @param array $data Parameters. Same format as Rule::DEFAULT_DATA.
115+
* - $data['conditions']: can be supplied in following formats:
116+
* - [['attribute'=>'..','value'=>'..'],['attribute'=>'..','value'=>'..','operator'=>'..'], [..]]
117+
* - ['aggregator'=>'any', 'conditions' => [[..],[..]]]
118+
*/
119+
public function revert(DataObject $data): void
120+
{
121+
/** @var \Magento\CatalogRule\Model\Rule $model */
122+
$model = $this->catalogRuleFactory->create();
123+
$this->catalogRuleResourceModel->load($model, $data->getId());
124+
if ($model->getId()) {
125+
$this->catalogRuleResourceModel->delete($model);
126+
}
127+
}
128+
129+
/**
130+
* Prepares rule default data
131+
*
132+
* @return array
133+
*/
134+
private function prepareDefaultData(): array
135+
{
136+
$data = self::DEFAULT_DATA;
137+
$customerGroupCollection = $this->customerGroupCollectionFactory->create();
138+
foreach ($customerGroupCollection->getAllIds() as $customerGroupId) {
139+
$data['customer_group_ids'][] = $customerGroupId;
140+
}
141+
142+
foreach ($this->storeManager->getWebsites() as $website) {
143+
$data['website_ids'][] = $website->getId();
144+
}
145+
146+
return $data;
147+
}
148+
149+
/**
150+
* Prepare CatalogRule data
151+
*
152+
* @param array $data
153+
* @return array
154+
*/
155+
private function prepareData(array $data): array
156+
{
157+
$data = array_merge($this->prepareDefaultData(), $data);
158+
$data['conditions_serialized'] = $this->conditionsSerializer->serialize($data['conditions']);
159+
$data['actions_serialized'] = $this->actionsSerializer->serialize($data['actions']);
160+
unset($data['conditions'], $data['actions']);
161+
162+
return $this->dataProcessor->process($this, $data);
163+
}
164+
}

0 commit comments

Comments
 (0)