Skip to content

Commit bb1c882

Browse files
authored
Merge pull request #7963 from magento-l3/ACP2E-1132
ACP2E-1132: Fix name uniqueness when Tax Rule is created
2 parents f06b479 + ff754d6 commit bb1c882

File tree

5 files changed

+89
-181
lines changed

5 files changed

+89
-181
lines changed

app/code/Magento/Customer/Test/Fixture/Customer.php

Lines changed: 40 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,10 @@
88

99
namespace Magento\Customer\Test\Fixture;
1010

11+
use Magento\Customer\Api\AccountManagementInterface;
1112
use Magento\Customer\Api\CustomerRepositoryInterface;
1213
use Magento\Customer\Api\Data\AddressInterface;
1314
use Magento\Customer\Api\Data\CustomerInterface;
14-
use Magento\Customer\Model\Customer as CustomerModel;
15-
use Magento\Customer\Model\CustomerFactory;
1615
use Magento\Customer\Model\CustomerRegistry;
1716
use Magento\Framework\DataObject;
1817
use Magento\Framework\Exception\LocalizedException;
@@ -36,10 +35,10 @@ class Customer implements RevertibleDataFixtureInterface
3635
CustomerInterface::CREATED_IN => null,
3736
CustomerInterface::DOB => null,
3837
CustomerInterface::EMAIL => 'customer%uniqid%@mail.com',
39-
CustomerInterface::FIRSTNAME => 'Firstname %uniqid%',
38+
CustomerInterface::FIRSTNAME => 'Firstname%uniqid%',
4039
CustomerInterface::GENDER => null,
4140
CustomerInterface::GROUP_ID => null,
42-
CustomerInterface::LASTNAME => 'Lastname %uniqid%',
41+
CustomerInterface::LASTNAME => 'Lastname%uniqid%',
4342
CustomerInterface::MIDDLENAME => null,
4443
CustomerInterface::PREFIX => null,
4544
CustomerInterface::STORE_ID => null,
@@ -49,7 +48,9 @@ class Customer implements RevertibleDataFixtureInterface
4948
CustomerInterface::DEFAULT_BILLING => null,
5049
CustomerInterface::DEFAULT_SHIPPING => null,
5150
CustomerInterface::KEY_ADDRESSES => [],
52-
CustomerInterface::DISABLE_AUTO_GROUP_CHANGE => null
51+
CustomerInterface::DISABLE_AUTO_GROUP_CHANGE => null,
52+
CustomerInterface::CUSTOM_ATTRIBUTES => [],
53+
CustomerInterface::EXTENSION_ATTRIBUTES_KEY => [],
5354
];
5455

5556
private const DEFAULT_DATA_ADDRESS = [
@@ -58,80 +59,68 @@ class Customer implements RevertibleDataFixtureInterface
5859
AddressInterface::REGION => 'Massachusetts',
5960
AddressInterface::REGION_ID => '32',
6061
AddressInterface::COUNTRY_ID => 'US',
61-
AddressInterface::STREET => '123 Test Street',
62+
AddressInterface::STREET => ['%street_number% Test Street%uniqid%'],
6263
AddressInterface::COMPANY => null,
6364
AddressInterface::TELEPHONE => '1234567890',
6465
AddressInterface::FAX => null,
6566
AddressInterface::POSTCODE => '02108',
6667
AddressInterface::CITY => 'Boston',
67-
AddressInterface::FIRSTNAME => 'Firstname %uniqid%',
68-
AddressInterface::LASTNAME => 'Lastname %uniqid%',
68+
AddressInterface::FIRSTNAME => 'Firstname%uniqid%',
69+
AddressInterface::LASTNAME => 'Lastname%uniqid%',
6970
AddressInterface::MIDDLENAME => null,
7071
AddressInterface::PREFIX => null,
7172
AddressInterface::SUFFIX => null,
7273
AddressInterface::VAT_ID => null,
7374
AddressInterface::DEFAULT_BILLING => true,
74-
AddressInterface::DEFAULT_SHIPPING => true
75+
AddressInterface::DEFAULT_SHIPPING => true,
76+
AddressInterface::CUSTOM_ATTRIBUTES => [],
77+
AddressInterface::EXTENSION_ATTRIBUTES_KEY => [],
7578
];
7679

7780
/**
7881
* @var ServiceFactory
7982
*/
80-
private $serviceFactory;
83+
private ServiceFactory $serviceFactory;
8184

8285
/**
83-
* @var CustomerRepositoryInterface
86+
* @var AccountManagementInterface
8487
*/
85-
private $customerRepository;
86-
87-
/**
88-
* @var CustomerFactory
89-
*/
90-
private $customerFactory;
88+
private AccountManagementInterface $accountManagement;
9189

9290
/**
9391
* @var CustomerRegistry
9492
*/
95-
private $customerRegistry;
93+
private CustomerRegistry $customerRegistry;
9694

9795
/**
9896
* @var ProcessorInterface
9997
*/
100-
private $dataProcessor;
98+
private ProcessorInterface $dataProcessor;
10199

102100
/**
103101
* @var DataMerger
104102
*/
105-
private $dataMerger;
106-
107-
/**
108-
* @var CustomerModel|null
109-
*/
110-
private $customer;
103+
private DataMerger $dataMerger;
111104

112105
/**
113106
* @param ServiceFactory $serviceFactory
114-
* @param CustomerRepositoryInterface $customerRepository
115-
* @param CustomerFactory $customerFactory
107+
* @param AccountManagementInterface $accountManagement
116108
* @param CustomerRegistry $customerRegistry
117109
* @param ProcessorInterface $dataProcessor
118110
* @param DataMerger $dataMerger
119111
*/
120112
public function __construct(
121113
ServiceFactory $serviceFactory,
122-
CustomerRepositoryInterface $customerRepository,
123-
CustomerFactory $customerFactory,
114+
AccountManagementInterface $accountManagement,
124115
CustomerRegistry $customerRegistry,
125116
ProcessorInterface $dataProcessor,
126-
DataMerger $dataMerger,
117+
DataMerger $dataMerger
127118
) {
128119
$this->serviceFactory = $serviceFactory;
129-
$this->customerRepository = $customerRepository;
130-
$this->customerFactory = $customerFactory;
120+
$this->accountManagement = $accountManagement;
131121
$this->customerRegistry = $customerRegistry;
132122
$this->dataProcessor = $dataProcessor;
133123
$this->dataMerger = $dataMerger;
134-
$this->customer = null;
135124
}
136125

137126
/**
@@ -145,14 +134,12 @@ public function apply(array $data = []): ?DataObject
145134
{
146135
$customerSaveService = $this->serviceFactory->create(CustomerRepositoryInterface::class, 'save');
147136
$data = $this->prepareData($data);
148-
if (count($data[CustomerInterface::KEY_ADDRESSES])) {
149-
$addresses = $this->prepareCustomerAddress($data[CustomerInterface::KEY_ADDRESSES]);
150-
$data[CustomerInterface::KEY_ADDRESSES] = $addresses;
151-
}
137+
$passwordHash = $this->accountManagement->getPasswordHash($data['password']);
138+
unset($data['password']);
152139
$customerSaveService->execute(
153140
[
154141
'customer' => $data,
155-
'passwordHash' => $this->customer->getPasswordHash()
142+
'passwordHash' => $passwordHash
156143
]
157144
);
158145
return $this->customerRegistry->retrieveByEmail($data['email'], $data['website_id']);
@@ -180,29 +167,33 @@ public function revert(DataObject $data): void
180167
*/
181168
private function prepareData(array $data): array
182169
{
183-
$data = $this->dataMerger->merge(self::DEFAULT_DATA, $data, false);
184-
185-
$this->customer = $this->customerFactory->create(['data' => $data]);
186-
$this->customer->setPassword($data['password']);
187-
if (isset($data['password'])) {
188-
unset($data['password']);
189-
}
170+
$data = $this->dataMerger->merge(self::DEFAULT_DATA, $data);
171+
$data[CustomerInterface::KEY_ADDRESSES] = $this->prepareAddresses($data[CustomerInterface::KEY_ADDRESSES]);
190172

191173
return $this->dataProcessor->process($this, $data);
192174
}
193175

194176
/**
195-
* Prepare customer address
177+
* Prepare customer addresses
196178
*
197179
* @param array $data
198180
* @return array
199181
*/
200-
private function prepareCustomerAddress(array $data): array
182+
private function prepareAddresses(array $data): array
201183
{
202184
$addresses = [];
185+
$default = self::DEFAULT_DATA_ADDRESS;
186+
$streetNumber = 123;
203187
foreach ($data as $dataAddress) {
204-
$dataAddress = $this->dataMerger->merge(self::DEFAULT_DATA_ADDRESS, $dataAddress, false);
205-
$addresses[] = $this->dataProcessor->process($this, $dataAddress);
188+
$dataAddress = $this->dataMerger->merge($default, $dataAddress);
189+
$placeholders = ['%street_number%' => $streetNumber++];
190+
$dataAddress[AddressInterface::STREET] = array_map(
191+
fn ($str) => strtr($str, $placeholders),
192+
$dataAddress[AddressInterface::STREET]
193+
);
194+
$addresses[] = $dataAddress;
195+
$default[AddressInterface::DEFAULT_BILLING] = false;
196+
$default[AddressInterface::DEFAULT_SHIPPING] = false;
206197
}
207198

208199
return $addresses;

app/code/Magento/Quote/Test/Fixture/CustomerCart.php

Lines changed: 13 additions & 82 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,6 @@
1010
use Magento\Framework\DataObject;
1111
use Magento\Quote\Api\CartManagementInterface;
1212
use Magento\Quote\Api\CartRepositoryInterface;
13-
use Magento\Quote\Model\QuoteFactory;
14-
use Magento\Quote\Model\ResourceModel\Quote as QuoteResource;
15-
use Magento\Store\Model\StoreManagerInterface;
16-
use Magento\TestFramework\Fixture\Api\DataMerger;
17-
use Magento\TestFramework\Fixture\Api\ServiceFactory;
18-
use Magento\TestFramework\Fixture\Data\ProcessorInterface;
1913
use Magento\TestFramework\Fixture\RevertibleDataFixtureInterface;
2014

2115
class CustomerCart implements RevertibleDataFixtureInterface
@@ -24,11 +18,6 @@ class CustomerCart implements RevertibleDataFixtureInterface
2418
'customer_id' => null
2519
];
2620

27-
/**
28-
* @var ServiceFactory
29-
*/
30-
private $serviceFactory;
31-
3221
/**
3322
* @var CartRepositoryInterface
3423
*/
@@ -40,97 +29,39 @@ class CustomerCart implements RevertibleDataFixtureInterface
4029
private $cartManagement;
4130

4231
/**
43-
* @var QuoteResource
44-
*/
45-
private $quoteResource;
46-
47-
/**
48-
* @var QuoteFactory
49-
*/
50-
private $quoteFactory;
51-
52-
/**
53-
* @var ProcessorInterface
54-
*/
55-
private $dataProcessor;
56-
57-
/**
58-
* @var DataMerger
59-
*/
60-
private $dataMerger;
61-
62-
/**
63-
* @var StoreManagerInterface
64-
*/
65-
private $storeManager;
66-
67-
/**
68-
* @param ServiceFactory $serviceFactory
69-
* @param StoreManagerInterface $storeManager
7032
* @param CartRepositoryInterface $cartRepository
7133
* @param CartManagementInterface $cartManagement
72-
* @param QuoteResource $quoteResource
73-
* @param QuoteFactory $quoteFactory
74-
* @param ProcessorInterface $dataProcessor
75-
* @param DataMerger $dataMerger
7634
*/
7735
public function __construct(
78-
ServiceFactory $serviceFactory,
79-
StoreManagerInterface $storeManager,
8036
CartRepositoryInterface $cartRepository,
81-
CartManagementInterface $cartManagement,
82-
QuoteResource $quoteResource,
83-
QuoteFactory $quoteFactory,
84-
ProcessorInterface $dataProcessor,
85-
DataMerger $dataMerger,
37+
CartManagementInterface $cartManagement
8638
) {
87-
$this->serviceFactory = $serviceFactory;
88-
$this->storeManager = $storeManager;
8939
$this->cartRepository = $cartRepository;
9040
$this->cartManagement = $cartManagement;
91-
$this->quoteResource = $quoteResource;
92-
$this->quoteFactory = $quoteFactory;
93-
$this->dataProcessor = $dataProcessor;
94-
$this->dataMerger = $dataMerger;
9541
}
9642

9743
/**
98-
* @inheritdoc
44+
* {@inheritdoc}
45+
* @param array $data Parameters
46+
* <pre>
47+
* $data = [
48+
* 'customer_id' => (int) Customer ID. Required.
49+
* ]
50+
* </pre>
9951
*/
10052
public function apply(array $data = []): ?DataObject
10153
{
102-
$data = $this->prepareData($data);
103-
$customerId = $data['customer_id'] ?? null;
104-
$storeId = $data['store_id'] ?? null;
105-
if ($storeId) {
106-
$setCurrentStoreService = $this->serviceFactory->create(StoreManagerInterface::class, 'setCurrentStore');
107-
$setCurrentStoreService->execute(['store' => $storeId]);
108-
}
109-
$cartService = $this->serviceFactory->create(CartManagementInterface::class, 'createEmptyCartForCustomer');
110-
$cartId = $cartService->execute(['customerId' => $customerId]);
111-
$cartRepositoryService = $this->serviceFactory->create(CartRepositoryInterface::class, 'get');
112-
$cart = $cartRepositoryService->execute(['cartId' => $cartId]);
113-
return $cart;
54+
$data = array_merge(self::DEFAULT_DATA, $data);
55+
$cartId = $this->cartManagement->createEmptyCartForCustomer($data['customer_id']);
56+
57+
return $this->cartRepository->get($cartId);
11458
}
11559

11660
/**
11761
* @inheritdoc
11862
*/
11963
public function revert(DataObject $data): void
12064
{
121-
$cartRepositoryService = $this->serviceFactory->create(CartRepositoryInterface::class, 'delete');
122-
$cartRepositoryService->execute(['quote' => $data]);
123-
}
124-
125-
/**
126-
* Prepare quote data
127-
*
128-
* @param array $data
129-
* @return array
130-
*/
131-
private function prepareData(array $data): array
132-
{
133-
$data = $this->dataMerger->merge(self::DEFAULT_DATA, $data, false);
134-
return $this->dataProcessor->process($this, $data);
65+
$this->cartRepository->delete($data);
13566
}
13667
}

app/code/Magento/Tax/Test/Fixture/TaxClass.php

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,13 @@
1010
use Magento\Framework\DataObject;
1111
use Magento\Tax\Api\TaxClassRepositoryInterface;
1212
use Magento\TestFramework\Fixture\Api\ServiceFactory;
13+
use Magento\TestFramework\Fixture\Data\ProcessorInterface;
1314
use Magento\TestFramework\Fixture\RevertibleDataFixtureInterface;
1415

1516
class TaxClass implements RevertibleDataFixtureInterface
1617
{
1718
private const DEFAULT_DATA = [
18-
'class_name' => '%uniqid%',
19+
'class_name' => 'taxclass%uniqid%',
1920
'class_type' => null,
2021
];
2122

@@ -29,16 +30,24 @@ class TaxClass implements RevertibleDataFixtureInterface
2930
*/
3031
private TaxClassRepositoryInterface $taxClassRepository;
3132

33+
/**
34+
* @var ProcessorInterface
35+
*/
36+
private ProcessorInterface $dataProcessor;
37+
3238
/**
3339
* @param ServiceFactory $serviceFactory
3440
* @param TaxClassRepositoryInterface $taxClassRepository
41+
* @param ProcessorInterface $dataProcessor
3542
*/
3643
public function __construct(
3744
ServiceFactory $serviceFactory,
38-
TaxClassRepositoryInterface $taxClassRepository
45+
TaxClassRepositoryInterface $taxClassRepository,
46+
ProcessorInterface $dataProcessor
3947
) {
4048
$this->serviceFactory = $serviceFactory;
4149
$this->taxClassRepository = $taxClassRepository;
50+
$this->dataProcessor = $dataProcessor;
4251
}
4352

4453
/**
@@ -47,7 +56,11 @@ public function __construct(
4756
public function apply(array $data = []): ?DataObject
4857
{
4958
$service = $this->serviceFactory->create(TaxClassRepositoryInterface::class, 'save');
50-
$taxClassId = $service->execute(['taxClass' => array_merge(self::DEFAULT_DATA, $data)]);
59+
$taxClassId = $service->execute(
60+
[
61+
'taxClass' => $this->dataProcessor->process($this, array_merge(self::DEFAULT_DATA, $data))
62+
]
63+
);
5164

5265
return $this->taxClassRepository->get($taxClassId);
5366
}

0 commit comments

Comments
 (0)