Skip to content

Commit 120836e

Browse files
author
Alexander Akimov
authored
Merge pull request #2800 from magento-tsg/2.3-develop-pr27
[TSG] Upporting for 2.3 (pr27) (2.3.0)
2 parents d7aafe9 + 5d4f74d commit 120836e

File tree

8 files changed

+312
-31
lines changed

8 files changed

+312
-31
lines changed

app/code/Magento/Catalog/Controller/Adminhtml/Category/RefreshPath.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,11 @@ public function execute()
3838

3939
/** @var \Magento\Framework\Controller\Result\Json $resultJson */
4040
$resultJson = $this->resultJsonFactory->create();
41-
return $resultJson->setData(['id' => $categoryId, 'path' => $category->getPath()]);
41+
return $resultJson->setData([
42+
'id' => $categoryId,
43+
'path' => $category->getPath(),
44+
'parentId' => $category->getParentId(),
45+
]);
4246
}
4347
}
4448
}

app/code/Magento/Catalog/Controller/Adminhtml/Category/Save.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,9 @@ public function execute()
139139
$parentId = isset($categoryPostData['parent']) ? $categoryPostData['parent'] : null;
140140
if ($categoryPostData) {
141141
$category->addData($categoryPostData);
142+
if ($parentId) {
143+
$category->setParentId($parentId);
144+
}
142145
if ($isNewCategory) {
143146
$parentCategory = $this->getParentCategory($parentId, $storeId);
144147
$category->setPath($parentCategory->getPath());
Lines changed: 147 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,147 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
7+
declare(strict_types=1);
8+
9+
namespace Magento\Catalog\Test\Unit\Controller\Adminhtml\Category;
10+
11+
use Magento\Framework\Controller\Result\JsonFactory;
12+
use Magento\Catalog\Controller\Adminhtml\Category\RefreshPath;
13+
use Magento\Backend\App\Action\Context;
14+
use Magento\Framework\TestFramework\Unit\Helper\ObjectManager;
15+
16+
/**
17+
* Test for class Magento\Catalog\Controller\Adminhtml\Category\RefreshPath.
18+
*/
19+
class RefreshPathTest extends \PHPUnit\Framework\TestCase
20+
{
21+
/**
22+
* @var JsonFactory|\PHPUnit_Framework_MockObject_MockObject
23+
*/
24+
private $resultJsonFactoryMock;
25+
26+
/**
27+
* @var Context|\PHPUnit_Framework_MockObject_MockObject
28+
*/
29+
private $contextMock;
30+
31+
/**
32+
* @inheritDoc
33+
*/
34+
protected function setUp()
35+
{
36+
$this->resultJsonFactoryMock = $this->getMockBuilder(JsonFactory::class)
37+
->disableOriginalConstructor()
38+
->setMethods(['create', 'setData'])
39+
->getMock();
40+
41+
$this->contextMock = $this->getMockBuilder(Context::class)
42+
->disableOriginalConstructor()
43+
->setMethods(['getRequest'])
44+
->getMock();
45+
}
46+
47+
/**
48+
* Sets object non-public property.
49+
*
50+
* @param mixed $object
51+
* @param string $propertyName
52+
* @param mixed $value
53+
*
54+
* @return void
55+
*/
56+
private function setObjectProperty($object, string $propertyName, $value) : void
57+
{
58+
$reflectionClass = new \ReflectionClass($object);
59+
$reflectionProperty = $reflectionClass->getProperty($propertyName);
60+
$reflectionProperty->setAccessible(true);
61+
$reflectionProperty->setValue($object, $value);
62+
}
63+
64+
/**
65+
* @return void
66+
*/
67+
public function testExecute() : void
68+
{
69+
$value = ['id' => 3, 'path' => '1/2/3', 'parentId' => 2];
70+
$result = '{"id":3,"path":"1/2/3","parentId":"2"}';
71+
72+
$requestMock = $this->getMockForAbstractClass(\Magento\Framework\App\RequestInterface::class);
73+
74+
$refreshPath = $this->getMockBuilder(RefreshPath::class)
75+
->setMethods(['getRequest', 'create'])
76+
->setConstructorArgs([
77+
$this->contextMock,
78+
$this->resultJsonFactoryMock,
79+
])->getMock();
80+
81+
$refreshPath->expects($this->any())->method('getRequest')->willReturn($requestMock);
82+
$requestMock->expects($this->any())->method('getParam')->with('id')->willReturn($value['id']);
83+
84+
$categoryMock = $this->getMockBuilder(\Magento\Catalog\Model\Category::class)
85+
->disableOriginalConstructor()
86+
->setMethods(['getPath', 'getParentId', 'getResource'])
87+
->getMock();
88+
89+
$categoryMock->expects($this->any())->method('getPath')->willReturn($value['path']);
90+
$categoryMock->expects($this->any())->method('getParentId')->willReturn($value['parentId']);
91+
92+
$categoryResource = $this->createMock(\Magento\Catalog\Model\ResourceModel\Category::class);
93+
94+
$objectManagerMock = $this->getMockBuilder(ObjectManager::class)
95+
->disableOriginalConstructor()
96+
->setMethods(['create'])
97+
->getMock();
98+
99+
$this->setObjectProperty($refreshPath, '_objectManager', $objectManagerMock);
100+
$this->setObjectProperty($categoryMock, '_resource', $categoryResource);
101+
102+
$objectManagerMock->expects($this->once())
103+
->method('create')
104+
->with(\Magento\Catalog\Model\Category::class)
105+
->willReturn($categoryMock);
106+
107+
$this->resultJsonFactoryMock->expects($this->any())->method('create')->willReturnSelf();
108+
$this->resultJsonFactoryMock->expects($this->any())
109+
->method('setData')
110+
->with($value)
111+
->willReturn($result);
112+
113+
$this->assertEquals($result, $refreshPath->execute());
114+
}
115+
116+
/**
117+
* @return void
118+
*/
119+
public function testExecuteWithoutCategoryId() : void
120+
{
121+
$requestMock = $this->getMockForAbstractClass(\Magento\Framework\App\RequestInterface::class);
122+
123+
$refreshPath = $this->getMockBuilder(RefreshPath::class)
124+
->setMethods(['getRequest', 'create'])
125+
->setConstructorArgs([
126+
$this->contextMock,
127+
$this->resultJsonFactoryMock,
128+
])->getMock();
129+
130+
$refreshPath->expects($this->any())->method('getRequest')->willReturn($requestMock);
131+
$requestMock->expects($this->any())->method('getParam')->with('id')->willReturn(null);
132+
133+
$objectManagerMock = $this->getMockBuilder(ObjectManager::class)
134+
->disableOriginalConstructor()
135+
->setMethods(['create'])
136+
->getMock();
137+
138+
$this->setObjectProperty($refreshPath, '_objectManager', $objectManagerMock);
139+
140+
$objectManagerMock->expects($this->never())
141+
->method('create')
142+
->with(\Magento\Catalog\Model\Category::class)
143+
->willReturnSelf();
144+
145+
$refreshPath->execute();
146+
}
147+
}

app/code/Magento/Catalog/view/adminhtml/ui_component/category_form.xml

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -251,7 +251,6 @@
251251
<item name="type" xsi:type="string">group</item>
252252
<item name="config" xsi:type="array">
253253
<item name="breakLine" xsi:type="boolean">true</item>
254-
<item name="required" xsi:type="boolean">true</item>
255254
</item>
256255
</argument>
257256
<field name="available_sort_by" formElement="multiselect">
@@ -292,7 +291,6 @@
292291
<item name="type" xsi:type="string">group</item>
293292
<item name="config" xsi:type="array">
294293
<item name="breakLine" xsi:type="boolean">true</item>
295-
<item name="required" xsi:type="boolean">true</item>
296294
</item>
297295
</argument>
298296
<field name="default_sort_by" formElement="select">

app/code/Magento/Catalog/view/adminhtml/web/catalog/category/form.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ define([
1414
options: {
1515
categoryIdSelector: 'input[name="id"]',
1616
categoryPathSelector: 'input[name="path"]',
17+
categoryParentSelector: 'input[name="parent"]',
1718
refreshUrl: config.refreshUrl
1819
},
1920

@@ -45,6 +46,7 @@ define([
4546
} else {
4647
$(this.options.categoryIdSelector).val(data.id).change();
4748
$(this.options.categoryPathSelector).val(data.path).change();
49+
$(this.options.categoryParentSelector).val(data.parentId).change();
4850
}
4951
}
5052
};

app/code/Magento/Sales/Model/Order/OrderCustomerExtractor.php

Lines changed: 37 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,12 @@
1111
use Magento\Customer\Api\Data\CustomerInterface;
1212
use Magento\Sales\Api\OrderRepositoryInterface;
1313
use Magento\Framework\DataObject\Copy as CopyService;
14-
use Magento\Customer\Api\Data\AddressInterface;
1514
use Magento\Customer\Api\Data\RegionInterface;
1615
use Magento\Customer\Api\Data\AddressInterfaceFactory as AddressFactory;
17-
use Magento\Quote\Model\Quote\Address as QuoteAddress;
1816
use Magento\Customer\Api\Data\RegionInterfaceFactory as RegionFactory;
1917
use Magento\Customer\Api\Data\CustomerInterfaceFactory as CustomerFactory;
2018
use Magento\Quote\Api\Data\AddressInterfaceFactory as QuoteAddressFactory;
19+
use Magento\Sales\Model\Order\Address as OrderAddress;
2120

2221
/**
2322
* Extract customer data from an order.
@@ -87,8 +86,9 @@ public function __construct(
8786
}
8887

8988
/**
90-
* @param int $orderId
89+
* Extract customer data from order.
9190
*
91+
* @param int $orderId
9292
* @return CustomerInterface
9393
*/
9494
public function extract(int $orderId): CustomerInterface
@@ -107,36 +107,45 @@ public function extract(int $orderId): CustomerInterface
107107
$order->getBillingAddress(),
108108
[]
109109
);
110-
$addresses = $order->getAddresses();
111-
foreach ($addresses as $address) {
112-
$addressData = $this->objectCopyService->copyFieldsetToTarget(
113-
'order_address',
114-
'to_customer_address',
115-
$address,
116-
[]
117-
);
118-
/** @var AddressInterface $customerAddress */
119-
$customerAddress = $this->addressFactory->create(['data' => $addressData]);
120-
switch ($address->getAddressType()) {
121-
case QuoteAddress::ADDRESS_TYPE_BILLING:
122-
$customerAddress->setIsDefaultBilling(true);
123-
break;
124-
case QuoteAddress::ADDRESS_TYPE_SHIPPING:
125-
$customerAddress->setIsDefaultShipping(true);
126-
break;
110+
111+
$processedAddressData = [];
112+
$customerAddresses = [];
113+
foreach ($order->getAddresses() as $orderAddress) {
114+
$addressData = $this->objectCopyService
115+
->copyFieldsetToTarget('order_address', 'to_customer_address', $orderAddress, []);
116+
117+
$index = array_search($addressData, $processedAddressData);
118+
if ($index === false) {
119+
// create new customer address only if it is unique
120+
$customerAddress = $this->addressFactory->create(['data' => $addressData]);
121+
$customerAddress->setIsDefaultBilling(false);
122+
$customerAddress->setIsDefaultShipping(false);
123+
if (is_string($orderAddress->getRegion())) {
124+
/** @var RegionInterface $region */
125+
$region = $this->regionFactory->create();
126+
$region->setRegion($orderAddress->getRegion());
127+
$region->setRegionCode($orderAddress->getRegionCode());
128+
$region->setRegionId($orderAddress->getRegionId());
129+
$customerAddress->setRegion($region);
130+
}
131+
132+
$processedAddressData[] = $addressData;
133+
$customerAddresses[] = $customerAddress;
134+
$index = count($processedAddressData) - 1;
127135
}
128136

129-
if (is_string($address->getRegion())) {
130-
/** @var RegionInterface $region */
131-
$region = $this->regionFactory->create();
132-
$region->setRegion($address->getRegion());
133-
$region->setRegionCode($address->getRegionCode());
134-
$region->setRegionId($address->getRegionId());
135-
$customerAddress->setRegion($region);
137+
$customerAddress = $customerAddresses[$index];
138+
// make sure that address type flags from equal addresses are stored in one resulted address
139+
if ($orderAddress->getAddressType() == OrderAddress::TYPE_BILLING) {
140+
$customerAddress->setIsDefaultBilling(true);
141+
}
142+
if ($orderAddress->getAddressType() == OrderAddress::TYPE_SHIPPING) {
143+
$customerAddress->setIsDefaultShipping(true);
136144
}
137-
$customerData['addresses'][] = $customerAddress;
138145
}
139146

147+
$customerData['addresses'] = $customerAddresses;
148+
140149
return $this->customerFactory->create(['data' => $customerData]);
141150
}
142151
}

dev/tests/integration/testsuite/Magento/Sales/Api/OrderCustomerDelegateInterfaceTest.php

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,61 @@ public function testDelegateNew(): void
159159
'12345abcD'
160160
);
161161

162+
//Testing that addresses from order and the order itself are assigned
163+
//to customer.
164+
$order = $this->orderRepository->get($orderId);
165+
$this->assertCount(1, $createdCustomer->getAddresses());
166+
$this->assertNotNull($createdCustomer->getDefaultBilling());
167+
$this->assertNotNull($createdCustomer->getDefaultShipping());
168+
foreach ($createdCustomer->getAddresses() as $address) {
169+
$this->assertTrue(
170+
$address->isDefaultBilling() || $address->isDefaultShipping()
171+
);
172+
if ($address->isDefaultBilling()) {
173+
$this->compareAddresses($order->getBillingAddress(), $address);
174+
} elseif ($address->isDefaultShipping()) {
175+
$this->compareAddresses($order->getShippingAddress(), $address);
176+
}
177+
}
178+
$this->assertEquals($order->getCustomerId(), $createdCustomer->getId());
179+
}
180+
181+
/**
182+
* @magentoDbIsolation enabled
183+
* @magentoAppIsolation enabled
184+
* @magentoDataFixture Magento/Sales/_files/order_different_addresses.php
185+
* @return void
186+
*/
187+
public function testDelegateNewDifferentAddresses(): void
188+
{
189+
$orderAutoincrementId = '100000001';
190+
/** @var Order $orderModel */
191+
$orderModel = $this->orderFactory->create();
192+
$orderModel->loadByIncrementId($orderAutoincrementId);
193+
$orderId = (int)$orderModel->getId();
194+
unset($orderModel);
195+
196+
$this->delegate->delegateNew($orderId);
197+
198+
//Saving new customer with prepared data from order.
199+
/** @var CustomerInterface $customer */
200+
$customer = $this->customerFactory->create();
201+
$customer->setWebsiteId(1)
202+
->setEmail('[email protected]')
203+
->setGroupId(1)
204+
->setStoreId(1)
205+
->setPrefix('Mr.')
206+
->setFirstname('John')
207+
->setMiddlename('A')
208+
->setLastname('Smith')
209+
->setSuffix('Esq.')
210+
->setTaxvat('12')
211+
->setGender(0);
212+
$createdCustomer = $this->accountManagement->createAccount(
213+
$customer,
214+
'12345abcD'
215+
);
216+
162217
//Testing that addresses from order and the order itself are assigned
163218
//to customer.
164219
$order = $this->orderRepository->get($orderId);

0 commit comments

Comments
 (0)