Skip to content

Commit 3422a25

Browse files
committed
MC-35960: Admin: Create Order from customer edit page
1 parent 2219289 commit 3422a25

File tree

7 files changed

+452
-0
lines changed

7 files changed

+452
-0
lines changed
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
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+
use Magento\Catalog\Api\ProductRepositoryInterface;
9+
use Magento\Checkout\Model\Type\Onepage;
10+
use Magento\Customer\Api\CustomerRepositoryInterface;
11+
use Magento\Quote\Api\CartRepositoryInterface;
12+
use Magento\Quote\Api\Data\CartInterface;
13+
use Magento\Quote\Api\Data\CartInterfaceFactory;
14+
use Magento\TestFramework\Helper\Bootstrap;
15+
16+
require __DIR__ . '/../../Customer/_files/customer.php';
17+
require __DIR__ . '/../../../Magento/Catalog/_files/second_product_simple.php';
18+
19+
$objectManager = Bootstrap::getObjectManager();
20+
/** @var ProductRepositoryInterface $productRepository */
21+
$productRepository = $objectManager->get(ProductRepositoryInterface::class);
22+
$productRepository->cleanCache();
23+
/** @var CartRepositoryInterface $quoteRepository */
24+
$quoteRepository = $objectManager->get(CartRepositoryInterface::class);
25+
/** @var CustomerRepositoryInterface $customerRepository */
26+
$customerRepository = $objectManager->get(CustomerRepositoryInterface::class);
27+
$customer = $customerRepository->get('[email protected]');
28+
29+
/** @var CartInterface $quote */
30+
$quote = $objectManager->get(CartInterfaceFactory::class)->create();
31+
$quote->setStoreId(1)
32+
->setIsActive(true)
33+
->setIsMultiShipping(0)
34+
->setCustomer($customer)
35+
->setCheckoutMethod(Onepage::METHOD_CUSTOMER)
36+
->setReservedOrderId('test_order_with_customer_without_address')
37+
->addProduct($productRepository->get('simple2'), 1);
38+
$quoteRepository->save($quote);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
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+
use Magento\Quote\Api\CartRepositoryInterface;
9+
use Magento\TestFramework\Helper\Bootstrap;
10+
use Magento\TestFramework\Quote\Model\GetQuoteByReservedOrderId;
11+
12+
$objectManager = Bootstrap::getObjectManager();
13+
/** @var CartRepositoryInterface $quoteRepository */
14+
$quoteRepository = $objectManager->get(CartRepositoryInterface::class);
15+
/** @var GetQuoteByReservedOrderId $getQuoteByReservedOrderId */
16+
$getQuoteByReservedOrderId = $objectManager->get(GetQuoteByReservedOrderId::class);
17+
$quote = $getQuoteByReservedOrderId->execute('test_order_with_customer_without_address');
18+
if ($quote !== null) {
19+
$quoteRepository->delete($quote);
20+
}
21+
22+
require __DIR__ . '/../../../Magento/Catalog/_files/second_product_simple_rollback.php';
23+
require __DIR__ . '/../../Customer/_files/customer_rollback.php';
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
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\Customer\Block\Adminhtml\Edit;
9+
10+
use Magento\Backend\Model\Search\AuthorizationMock;
11+
use Magento\Customer\Controller\RegistryConstants;
12+
use Magento\Framework\Authorization;
13+
use Magento\Framework\ObjectManagerInterface;
14+
use Magento\Framework\Registry;
15+
use Magento\TestFramework\Helper\Bootstrap;
16+
use PHPUnit\Framework\TestCase;
17+
18+
/**
19+
* Class checks Create Order button visibility
20+
*
21+
* @magentoAppArea adminhtml
22+
* @magentoDbIsolation enabled
23+
*/
24+
class OrderButtonTest extends TestCase
25+
{
26+
/** @var ObjectManagerInterface */
27+
private $objectManager;
28+
29+
/** @var OrderButton */
30+
private $button;
31+
32+
/** @var Registry */
33+
private $registry;
34+
35+
/**
36+
* @inheritdoc
37+
*/
38+
protected function setUp(): void
39+
{
40+
parent::setUp();
41+
42+
$this->objectManager = Bootstrap::getObjectManager();
43+
$this->objectManager->addSharedInstance(
44+
$this->objectManager->get(AuthorizationMock::class),
45+
Authorization::class
46+
);
47+
$this->button = $this->objectManager->get(OrderButton::class);
48+
$this->registry = $this->objectManager->get(Registry::class);
49+
}
50+
51+
/**
52+
* @inheritdoc
53+
*/
54+
protected function tearDown(): void
55+
{
56+
$this->registry->unregister(RegistryConstants::CURRENT_CUSTOMER_ID);
57+
58+
parent::tearDown();
59+
}
60+
61+
/**
62+
* @return void
63+
*/
64+
public function testGetButtonDataWithoutCustomer(): void
65+
{
66+
$this->assertEmpty($this->button->getButtonData());
67+
}
68+
69+
/**
70+
* @magentoDataFixture Magento/Customer/_files/customer.php
71+
*
72+
* @return void
73+
*/
74+
public function testGetButtonDataWithCustomer(): void
75+
{
76+
$this->registry->unregister(RegistryConstants::CURRENT_CUSTOMER_ID);
77+
$this->registry->register(RegistryConstants::CURRENT_CUSTOMER_ID, 1);
78+
$data = $this->button->getButtonData();
79+
$this->assertNotEmpty($data);
80+
$this->assertEquals(__('Create Order'), $data['label']);
81+
$this->assertStringContainsString('sales/order_create/start/customer_id/1/', $data['on_click']);
82+
}
83+
}
Lines changed: 125 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,125 @@
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\Create;
9+
10+
use Magento\Config\Model\Config\Backend\Admin\Custom;
11+
use Magento\Customer\Api\Data\AddressInterface;
12+
use Magento\Customer\Model\Address\AddressModelInterface;
13+
use Magento\Customer\Model\CustomerRegistry;
14+
use Magento\Customer\Model\Metadata\FormFactory;
15+
use Magento\Framework\App\Config\ScopeConfigInterface;
16+
use Magento\Framework\Data\Form;
17+
use Magento\Framework\View\Element\BlockInterface;
18+
use Magento\Framework\View\LayoutInterface;
19+
use Magento\Quote\Api\CartRepositoryInterface;
20+
use Magento\TestFramework\Helper\Bootstrap;
21+
use PHPUnit\Framework\TestCase;
22+
23+
/**
24+
* Class consist of basic logic to check address form
25+
*/
26+
abstract class AbstractAddressFormTest extends TestCase
27+
{
28+
/** @var LayoutInterface */
29+
protected $layout;
30+
31+
/** @var CustomerRegistry */
32+
protected $customerRegistry;
33+
34+
/** @var CartRepositoryInterface */
35+
private $quoteRepository;
36+
37+
/** @var BlockInterface */
38+
private $form;
39+
40+
/** @var ScopeConfigInterface */
41+
private $config;
42+
43+
/** @var array */
44+
private $formAttributes;
45+
46+
/**
47+
* @inheritdoc
48+
*/
49+
protected function setUp(): void
50+
{
51+
parent::setUp();
52+
53+
$objectManager = Bootstrap::getObjectManager();
54+
$this->layout = $objectManager->get(LayoutInterface::class);
55+
$this->form = $this->getFormBlock();
56+
$this->customerRegistry = $objectManager->get(CustomerRegistry::class);
57+
$this->quoteRepository = $objectManager->get(CartRepositoryInterface::class);
58+
$this->config = $objectManager->get(ScopeConfigInterface::class);
59+
$this->formAttributes = array_keys($objectManager->get(FormFactory::class)
60+
->create('customer_address', 'adminhtml_customer_address')->getAttributes());
61+
}
62+
63+
/**
64+
* Check that all form values are filled according to address attributes values
65+
*
66+
* @param int $customerId
67+
* @return void
68+
*/
69+
protected function checkFormValuesExist(int $customerId): void
70+
{
71+
$address = $this->getAddress($customerId);
72+
$form = $this->prepareForm($customerId);
73+
foreach ($this->formAttributes as $attribute) {
74+
$this->assertEquals($address->getData($attribute), $form->getElement($attribute)->getValue());
75+
}
76+
}
77+
78+
/**
79+
* Check that form values is empty
80+
*
81+
* @param int $customerId
82+
* @return void
83+
*/
84+
protected function checkFormValuesAreEmpty(int $customerId): void
85+
{
86+
$defaultCountryCode = $this->config->getValue(Custom::XML_PATH_GENERAL_COUNTRY_DEFAULT);
87+
$form = $this->prepareForm($customerId);
88+
foreach ($this->formAttributes as $attribute) {
89+
if ($attribute === AddressInterface::COUNTRY_ID) {
90+
$this->assertEquals($defaultCountryCode, $form->getElement($attribute)->getValue());
91+
continue;
92+
}
93+
$this->assertNull($form->getElement($attribute)->getValue());
94+
}
95+
}
96+
97+
/**
98+
* Prepare form
99+
*
100+
* @param int $customerId
101+
* @return Form
102+
*/
103+
private function prepareForm(int $customerId): Form
104+
{
105+
$quote = $this->quoteRepository->getForCustomer($customerId);
106+
$this->form->getCreateOrderModel()->setQuote($quote);
107+
108+
return $this->form->getForm();
109+
}
110+
111+
/**
112+
* Get form block
113+
*
114+
* @return BlockInterface
115+
*/
116+
abstract protected function getFormBlock(): BlockInterface;
117+
118+
/**
119+
* Get appropriate customer address
120+
*
121+
* @param int $customerId
122+
* @return AddressModelInterface
123+
*/
124+
abstract protected function getAddress(int $customerId): AddressModelInterface;
125+
}
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
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\Create\Billing;
9+
10+
use Magento\Customer\Model\Address\AddressModelInterface;
11+
use Magento\Framework\View\Element\BlockInterface;
12+
use Magento\Sales\Block\Adminhtml\Order\Create\AbstractAddressFormTest;
13+
use Magento\Sales\Block\Adminhtml\Order\Create\Shipping\Address;
14+
15+
/**
16+
* Class checks billing address form behaviour
17+
*
18+
* @magentoAppArea adminhtml
19+
* @magentoDbIsolation enabled
20+
*
21+
*/
22+
class FormTest extends AbstractAddressFormTest
23+
{
24+
/**
25+
* @magentoDataFixture Magento/Checkout/_files/quote_with_address_saved.php
26+
*
27+
* @return void
28+
*/
29+
public function testFormValuesExist(): void
30+
{
31+
$this->checkFormValuesExist(1);
32+
}
33+
34+
/**
35+
* @magentoDataFixture Magento/Checkout/_files/quote_with_customer_without_address.php
36+
*
37+
* @return void
38+
*/
39+
public function testFormValuesAreEmpty(): void
40+
{
41+
$this->checkFormValuesAreEmpty(1);
42+
}
43+
44+
/**
45+
* @inheritdoc
46+
*/
47+
protected function getFormBlock(): BlockInterface
48+
{
49+
return $this->layout->createBlock(Address::class);
50+
}
51+
52+
/**
53+
* @inheritdoc
54+
*/
55+
protected function getAddress(int $customerId): AddressModelInterface
56+
{
57+
return $this->customerRegistry->retrieve($customerId)->getDefaultBillingAddress();
58+
}
59+
}
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\Sales\Block\Adminhtml\Order\Create\Shipping;
9+
10+
use Magento\Customer\Model\Address\AddressModelInterface;
11+
use Magento\Framework\View\Element\BlockInterface;
12+
use Magento\Sales\Block\Adminhtml\Order\Create\AbstractAddressFormTest;
13+
14+
/**
15+
* Class checks shipping address form behaviour
16+
*
17+
* @magentoAppArea adminhtml
18+
* @magentoDbIsolation enabled
19+
*/
20+
class FormTest extends AbstractAddressFormTest
21+
{
22+
/**
23+
* @magentoDataFixture Magento/Checkout/_files/quote_with_address_saved.php
24+
*
25+
* @return void
26+
*/
27+
public function testFormValuesExist(): void
28+
{
29+
$this->checkFormValuesExist(1);
30+
}
31+
32+
/**
33+
* @magentoDataFixture Magento/Checkout/_files/quote_with_customer_without_address.php
34+
*
35+
* @return void
36+
*/
37+
public function testFormValuesAreEmpty(): void
38+
{
39+
$this->checkFormValuesAreEmpty(1);
40+
}
41+
42+
/**
43+
* @inheritdoc
44+
*/
45+
protected function getFormBlock(): BlockInterface
46+
{
47+
return $this->layout->createBlock(Address::class);
48+
}
49+
50+
/**
51+
* @inheritdoc
52+
*/
53+
protected function getAddress(int $customerId): AddressModelInterface
54+
{
55+
return $this->customerRegistry->retrieve($customerId)->getDefaultShippingAddress();
56+
}
57+
}

0 commit comments

Comments
 (0)