Skip to content

Commit 5c6e78a

Browse files
MC-36372: Admin: edit customers billing and shipping addresses for order
1 parent 42311fa commit 5c6e78a

File tree

4 files changed

+430
-0
lines changed

4 files changed

+430
-0
lines changed
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
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\Sales\Block\Adminhtml\Order\Address;
9+
10+
use Magento\Framework\ObjectManagerInterface;
11+
use Magento\Framework\Registry;
12+
use Magento\Framework\View\LayoutInterface;
13+
use Magento\Sales\Api\Data\OrderInterfaceFactory;
14+
use Magento\TestFramework\Helper\Bootstrap;
15+
use PHPUnit\Framework\TestCase;
16+
17+
/**
18+
* Checks order address edit form block
19+
*
20+
* @see \Magento\Sales\Block\Adminhtml\Order\Address\Form
21+
*
22+
* @magentoAppArea adminhtml
23+
*/
24+
class FormTest extends TestCase
25+
{
26+
/** @var ObjectManagerInterface */
27+
private $objectManager;
28+
29+
/** @var Form */
30+
private $block;
31+
32+
/** @var Registry */
33+
private $registry;
34+
35+
/** @var OrderInterfaceFactory */
36+
private $orderFactory;
37+
38+
/**
39+
* @inheritdoc
40+
*/
41+
protected function setUp(): void
42+
{
43+
parent::setUp();
44+
45+
$this->objectManager = Bootstrap::getObjectManager();
46+
$this->block = $this->objectManager->get(LayoutInterface::class)->createBlock(Form::class);
47+
$this->orderFactory = $this->objectManager->get(OrderInterfaceFactory::class);
48+
$this->registry = $this->objectManager->get(Registry::class);
49+
}
50+
51+
/**
52+
* @return void
53+
*/
54+
protected function tearDown(): void
55+
{
56+
$this->registry->unregister('order_address');
57+
58+
parent::tearDown();
59+
}
60+
61+
/**
62+
* @magentoDataFixture Magento/Sales/_files/order.php
63+
*
64+
* @return void
65+
*/
66+
public function testGetFormValues(): void
67+
{
68+
$this->registry->unregister('order_address');
69+
$order = $this->orderFactory->create()->loadByIncrementId(100000001);
70+
$address = $order->getShippingAddress();
71+
$this->registry->register('order_address', $address);
72+
$formValues = $this->block->getFormValues();
73+
$this->assertEquals($address->getData(), $formValues);
74+
}
75+
}
Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
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\Sales\Block\Adminhtml\Order;
9+
10+
use Magento\Framework\ObjectManagerInterface;
11+
use Magento\Framework\Registry;
12+
use Magento\Framework\View\LayoutInterface;
13+
use Magento\Sales\Api\Data\OrderAddressInterface;
14+
use Magento\Sales\Api\Data\OrderInterface;
15+
use Magento\Sales\Model\Order\Address as AddressType;
16+
use Magento\Sales\Model\OrderFactory;
17+
use Magento\TestFramework\Helper\Bootstrap;
18+
use PHPUnit\Framework\TestCase;
19+
20+
/**
21+
* Checks order address edit block
22+
*
23+
* @see \Magento\Sales\Block\Adminhtml\Order\Address
24+
*
25+
* @magentoAppArea adminhtml
26+
*/
27+
class AddressTest extends TestCase
28+
{
29+
/** @var ObjectManagerInterface */
30+
private $objectManager;
31+
32+
/** @var Address */
33+
private $block;
34+
35+
/** @var Registry */
36+
private $registry;
37+
38+
/** @var OrderFactory */
39+
private $orderFactory;
40+
41+
/**
42+
* @inheritdoc
43+
*/
44+
protected function setUp(): void
45+
{
46+
parent::setUp();
47+
48+
$this->objectManager = Bootstrap::getObjectManager();
49+
$this->block = $this->objectManager->get(LayoutInterface::class)->createBlock(Address::class);
50+
$this->registry = $this->objectManager->get(Registry::class);
51+
$this->orderFactory = $this->objectManager->get(OrderFactory::class);
52+
}
53+
54+
/**
55+
* @inheritdoc
56+
*/
57+
protected function tearDown(): void
58+
{
59+
$this->registry->unregister('order_address');
60+
61+
parent::tearDown();
62+
}
63+
64+
/**
65+
* @dataProvider addressTypeProvider
66+
*
67+
* @magentoDataFixture Magento/Sales/_files/order.php
68+
*
69+
* @param string $type
70+
* @return void
71+
*/
72+
public function testGetHeaderText(string $type): void
73+
{
74+
$order = $this->orderFactory->create()->loadByIncrementId(100000001);
75+
$address = $this->getAddressByType($order, $type);
76+
$this->registry->unregister('order_address');
77+
$this->registry->register('order_address', $address);
78+
$text = $this->block->getHeaderText();
79+
$this->assertEquals(
80+
(string)__('Edit Order %1 %2 Address', $order->getIncrementId(), ucfirst($type)),
81+
(string)$text
82+
);
83+
}
84+
85+
/**
86+
* @return array
87+
*/
88+
public function addressTypeProvider(): array
89+
{
90+
return [
91+
'billing_address' => [
92+
AddressType::TYPE_BILLING,
93+
],
94+
'shipping_address' => [
95+
AddressType::TYPE_SHIPPING,
96+
]
97+
];
98+
}
99+
100+
/**
101+
* Get address by address type
102+
*
103+
* @param OrderInterface $order
104+
* @param string $type
105+
* @return OrderAddressInterface|null
106+
*/
107+
private function getAddressByType(OrderInterface $order, string $type): ?OrderAddressInterface
108+
{
109+
return $type === AddressType::TYPE_BILLING ? $order->getBillingAddress() : $order->getShippingAddress();
110+
}
111+
}
Lines changed: 168 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,168 @@
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\Sales\Controller\Adminhtml\Order;
9+
10+
use Magento\Framework\App\Request\Http as HttpRequest;
11+
use Magento\Sales\Api\Data\OrderAddressInterface;
12+
use Magento\Sales\Api\Data\OrderInterface;
13+
use Magento\Sales\Api\Data\OrderInterfaceFactory;
14+
use Magento\Sales\Api\OrderAddressRepositoryInterface;
15+
use Magento\Sales\Model\Order\Address as AddressType;
16+
use Magento\TestFramework\TestCase\AbstractBackendController;
17+
18+
/**
19+
* Class check address save action
20+
*
21+
* @see \Magento\Sales\Controller\Adminhtml\Order\AddressSave
22+
*
23+
* @magentoDbIsolation disabled
24+
* @magentoAppArea adminhtml
25+
*/
26+
class AddressSaveTest extends AbstractBackendController
27+
{
28+
/** @var OrderInterfaceFactory */
29+
private $orderFactory;
30+
31+
/** @var OrderAddressRepositoryInterface */
32+
private $orderAddressRepository;
33+
34+
/**
35+
* @inheritdoc
36+
*/
37+
protected function setUp(): void
38+
{
39+
parent::setUp();
40+
41+
$this->orderFactory = $this->_objectManager->get(OrderInterfaceFactory::class);
42+
$this->orderAddressRepository = $this->_objectManager->get(OrderAddressRepositoryInterface::class);
43+
}
44+
45+
/**
46+
* @dataProvider addressTypeProvider
47+
*
48+
* @magentoDataFixture Magento/Sales/_files/order.php
49+
*
50+
* @param string $type
51+
* @return void
52+
*/
53+
public function testSave(string $type): void
54+
{
55+
$data = [
56+
OrderAddressInterface::FIRSTNAME => 'New test name',
57+
OrderAddressInterface::LASTNAME => 'New test lastname',
58+
OrderAddressInterface::STREET => ['new test street'],
59+
OrderAddressInterface::CITY => 'New Test City',
60+
OrderAddressInterface::COUNTRY_ID => 'UA',
61+
OrderAddressInterface::REGION => '1111',
62+
OrderAddressInterface::POSTCODE => '97203',
63+
OrderAddressInterface::TELEPHONE => '5555555555',
64+
];
65+
$order = $this->orderFactory->create()->loadByIncrementId(100000001);
66+
$addressId = $this->getAddressIdByType($order, $type);
67+
$this->dispatchWithParams(
68+
['address_id' => $addressId],
69+
$data
70+
);
71+
$this->assertSessionMessages(
72+
$this->containsEqual((string)__('You updated the order address.'))
73+
);
74+
$this->assertRedirect(
75+
$this->stringContains(sprintf('sales/order/view/order_id/%s/', $order->getId()))
76+
);
77+
$this->assertAddressData($addressId, $data);
78+
}
79+
80+
/**
81+
* @return array
82+
*/
83+
public function addressTypeProvider(): array
84+
{
85+
return [
86+
'billing_address' => [
87+
AddressType::TYPE_BILLING,
88+
],
89+
'shipping_address' => [
90+
AddressType::TYPE_SHIPPING,
91+
]
92+
];
93+
}
94+
95+
/**
96+
* @dataProvider wrongRequestDataProvider
97+
*
98+
* @param array $params
99+
* @param array $post
100+
* @return void
101+
*/
102+
public function testInvalidRequest(array $params, array $post = []): void
103+
{
104+
$this->dispatchWithParams($params, $post);
105+
$this->assertRedirect($this->stringContains('backend/sales/order/index/'));
106+
}
107+
108+
/**
109+
* @return array
110+
*/
111+
public function wrongRequestDataProvider(): array
112+
{
113+
return [
114+
'empty_post' => [
115+
['address_id' => 1],
116+
],
117+
'wrong_address_id' => [
118+
['address_id' => 7852147],
119+
],
120+
];
121+
}
122+
123+
/**
124+
* Check updated address data
125+
*
126+
* @param int $addressId
127+
* @param array $expectedData
128+
* @return void
129+
*/
130+
private function assertAddressData(int $addressId, array $expectedData): void
131+
{
132+
$address = $this->orderAddressRepository->get($addressId);
133+
foreach ($expectedData as $key => $value) {
134+
$key === OrderAddressInterface::STREET
135+
? $this->assertEquals(reset($value), $address->getData($key))
136+
: $this->assertEquals($value, $address->getData($key));
137+
}
138+
}
139+
140+
/**
141+
* Get address id by address type
142+
*
143+
* @param OrderInterface $order
144+
* @param string $type
145+
* @return int
146+
*/
147+
private function getAddressIdByType(OrderInterface $order, string $type): int
148+
{
149+
return $type === AddressType::TYPE_BILLING
150+
? (int)$order->getBillingAddressId()
151+
: (int)$order->getShippingAddressId();
152+
}
153+
154+
/**
155+
* Dispatch with params
156+
*
157+
* @param array $params
158+
* @param array $post
159+
* @return void
160+
*/
161+
private function dispatchWithParams(array $params, array $post): void
162+
{
163+
$this->getRequest()->setMethod(HttpRequest::METHOD_POST);
164+
$this->getRequest()->setParams($params);
165+
$this->getRequest()->setPostValue($post);
166+
$this->dispatch('backend/sales/order/addressSave');
167+
}
168+
}

0 commit comments

Comments
 (0)