Skip to content

Commit 30cd67d

Browse files
committed
MC-37196: Admin: invoice order for customer
1 parent 849b6f0 commit 30cd67d

File tree

8 files changed

+707
-97
lines changed

8 files changed

+707
-97
lines changed
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\Sales\Block\Adminhtml\Order\Invoice\Create;
9+
10+
use Magento\Framework\ObjectManagerInterface;
11+
use Magento\Framework\Registry;
12+
use Magento\Framework\View\LayoutInterface;
13+
use Magento\Sales\Model\OrderFactory;
14+
use Magento\Sales\Model\ResourceModel\Order\Invoice\CollectionFactory;
15+
use Magento\TestFramework\Helper\Bootstrap;
16+
use PHPUnit\Framework\TestCase;
17+
18+
/**
19+
* Checks invoiced items grid appearance
20+
*
21+
* @see \Magento\Sales\Block\Adminhtml\Order\Invoice\Create\Items
22+
*
23+
* @magentoAppArea adminhtml
24+
* @magentoDbIsolation enabled
25+
*/
26+
class ItemsTest extends TestCase
27+
{
28+
/** @var ObjectManagerInterface */
29+
private $objectManager;
30+
31+
/** @var Items */
32+
private $block;
33+
34+
/** @var OrderFactory */
35+
private $orderFactory;
36+
37+
/** @var Registry */
38+
private $registry;
39+
40+
/** @var CollectionFactory */
41+
private $invoiceCollectionFactory;
42+
43+
/**
44+
* @inheritdoc
45+
*/
46+
protected function setUp(): void
47+
{
48+
parent::setUp();
49+
50+
$this->objectManager = Bootstrap::getObjectManager();
51+
$this->block = $this->objectManager->get(LayoutInterface::class)->createBlock(Items::class);
52+
$this->orderFactory = $this->objectManager->get(OrderFactory::class);
53+
$this->registry = $this->objectManager->get(Registry::class);
54+
$this->invoiceCollectionFactory = $this->objectManager->get(CollectionFactory::class);
55+
}
56+
57+
/**
58+
* @inheritdoc
59+
*/
60+
protected function tearDown(): void
61+
{
62+
$this->registry->unregister('current_invoice');
63+
64+
parent::tearDown();
65+
}
66+
67+
/**
68+
* @magentoDataFixture Magento/Sales/_files/invoice.php
69+
*
70+
* @return void
71+
*/
72+
public function testGetUpdateButtonHtml(): void
73+
{
74+
$order = $this->orderFactory->create()->loadByIncrementId('100000001');
75+
$invoice = $this->invoiceCollectionFactory->create()->setOrderFilter($order)->setPageSize(1)->getFirstItem();
76+
$this->registry->unregister('current_invoice');
77+
$this->registry->register('current_invoice', $invoice);
78+
$this->block->toHtml();
79+
$button = $this->block->getChildBlock('update_button');
80+
$this->assertEquals((string)__('Update Qty\'s'), (string)$button->getLabel());
81+
$this->assertStringContainsString(
82+
sprintf('sales/index/updateQty/order_id/%u/', (int)$order->getEntityId()),
83+
$button->getOnClick()
84+
);
85+
}
86+
}
Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
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\Backend\Model\Search\AuthorizationMock;
11+
use Magento\Framework\Authorization;
12+
use Magento\Framework\ObjectManagerInterface;
13+
use Magento\Framework\Registry;
14+
use Magento\Framework\View\LayoutInterface;
15+
use Magento\Sales\Api\Data\OrderInterface;
16+
use Magento\Sales\Model\OrderFactory;
17+
use Magento\TestFramework\Helper\Bootstrap;
18+
use Magento\TestFramework\Helper\Xpath;
19+
use PHPUnit\Framework\TestCase;
20+
21+
/**
22+
* Checks order create block
23+
*
24+
* @see \Magento\Sales\Block\Adminhtml\Order\View
25+
*
26+
* @magentoAppArea adminhtml
27+
* @magentoDbIsolation enabled
28+
*/
29+
class ViewTest extends TestCase
30+
{
31+
/** @var ObjectManagerInterface */
32+
private $objectManager;
33+
34+
/** @var LayoutInterface */
35+
private $layout;
36+
37+
/** @var Registry */
38+
private $registry;
39+
40+
/** @var OrderFactory */
41+
private $orderFactory;
42+
43+
/**
44+
* @inheritdoc
45+
*/
46+
protected function setUp(): void
47+
{
48+
parent::setUp();
49+
50+
$this->objectManager = Bootstrap::getObjectManager();
51+
$this->objectManager->addSharedInstance(
52+
$this->objectManager->get(AuthorizationMock::class),
53+
Authorization::class
54+
);
55+
$this->registry = $this->objectManager->get(Registry::class);
56+
$this->orderFactory = $this->objectManager->get(OrderFactory::class);
57+
$this->layout = $this->objectManager->get(LayoutInterface::class);
58+
}
59+
60+
/**
61+
* @inheritdoc
62+
*/
63+
protected function tearDown(): void
64+
{
65+
$this->registry->unregister('sales_order');
66+
67+
parent::tearDown();
68+
}
69+
70+
/**
71+
* @magentoDataFixture Magento/Sales/_files/order.php
72+
*
73+
* @return void
74+
*/
75+
public function testInvoiceButton(): void
76+
{
77+
$this->registerOrder('100000001');
78+
$this->assertEquals(
79+
1,
80+
Xpath::getElementsCountForXpath(
81+
'//button[@id=\'order_invoice\']',
82+
$this->layout->createBlock(View::class)->getButtonsHtml()
83+
)
84+
);
85+
}
86+
87+
/**
88+
* @magentoDataFixture Magento/Sales/_files/order_with_bundle_and_invoiced.php
89+
*
90+
* @return void
91+
*/
92+
public function testInvoiceButtonIsNotVisible(): void
93+
{
94+
$this->registerOrder('100000001');
95+
$this->assertEmpty(
96+
Xpath::getElementsCountForXpath(
97+
'//button[@id=\'order_invoice\']',
98+
$this->layout->createBlock(View::class)->getButtonsHtml()
99+
)
100+
);
101+
}
102+
103+
/**
104+
* Register order
105+
*
106+
* @param OrderInterface $order
107+
* @return void
108+
*/
109+
private function registerOrder(string $orderIncrementId): void
110+
{
111+
$order = $this->orderFactory->create()->loadByIncrementId($orderIncrementId);
112+
$this->registry->unregister('sales_order');
113+
$this->registry->register('sales_order', $order);
114+
}
115+
}

dev/tests/integration/testsuite/Magento/Sales/Controller/Adminhtml/Order/Invoice/AbstractInvoiceControllerTest.php

Lines changed: 59 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -7,86 +7,106 @@
77

88
namespace Magento\Sales\Controller\Adminhtml\Order\Invoice;
99

10-
use Magento\Framework\Api\SearchCriteria;
1110
use Magento\Framework\Api\SearchCriteriaBuilder;
12-
use Magento\Framework\Data\Form\FormKey;
11+
use Magento\Framework\App\Request\Http;
1312
use Magento\Sales\Api\Data\InvoiceInterface;
1413
use Magento\Sales\Api\Data\OrderInterface;
1514
use Magento\Sales\Model\OrderRepository;
15+
use Magento\Sales\Model\ResourceModel\Order\Invoice\CollectionFactory;
1616
use Magento\TestFramework\Mail\Template\TransportBuilderMock;
1717
use Magento\TestFramework\TestCase\AbstractBackendController;
1818

1919
/**
2020
* Abstract backend invoice test.
2121
*/
22-
class AbstractInvoiceControllerTest extends AbstractBackendController
22+
abstract class AbstractInvoiceControllerTest extends AbstractBackendController
2323
{
24-
/**
25-
* @var TransportBuilderMock
26-
*/
24+
/** @var TransportBuilderMock */
2725
protected $transportBuilder;
2826

29-
/**
30-
* @var OrderRepository
31-
*/
32-
protected $orderRepository;
27+
/** @var string */
28+
protected $resource = 'Magento_Sales::sales_invoice';
3329

34-
/**
35-
* @var FormKey
36-
*/
37-
protected $formKey;
30+
/** @var OrderRepository */
31+
private $orderRepository;
3832

39-
/**
40-
* @var string
41-
*/
42-
protected $resource = 'Magento_Sales::sales_invoice';
33+
/** @var SearchCriteriaBuilder */
34+
private $searchCriteriaBuilder;
35+
36+
/** @var CollectionFactory */
37+
private $invoiceCollectionFactory;
4338

4439
/**
4540
* @inheritdoc
4641
*/
4742
protected function setUp(): void
4843
{
4944
parent::setUp();
45+
5046
$this->transportBuilder = $this->_objectManager->get(TransportBuilderMock::class);
5147
$this->orderRepository = $this->_objectManager->get(OrderRepository::class);
52-
$this->formKey = $this->_objectManager->get(FormKey::class);
48+
$this->searchCriteriaBuilder = $this->_objectManager->get(SearchCriteriaBuilder::class);
49+
$this->invoiceCollectionFactory = $this->_objectManager->get(CollectionFactory::class);
5350
}
5451

5552
/**
53+
* Retrieve order
54+
*
5655
* @param string $incrementalId
5756
* @return OrderInterface|null
5857
*/
59-
protected function getOrder(string $incrementalId)
58+
protected function getOrder(string $incrementalId): ?OrderInterface
6059
{
61-
/** @var SearchCriteria $searchCriteria */
62-
$searchCriteria = $this->_objectManager->create(SearchCriteriaBuilder::class)
63-
->addFilter(OrderInterface::INCREMENT_ID, $incrementalId)
60+
$searchCriteria = $this->searchCriteriaBuilder->addFilter(OrderInterface::INCREMENT_ID, $incrementalId)
6461
->create();
65-
6662
$orders = $this->orderRepository->getList($searchCriteria)->getItems();
67-
/** @var OrderInterface $order */
68-
$order = reset($orders);
6963

70-
return $order;
64+
return reset($orders);
7165
}
7266

7367
/**
74-
* @param OrderInterface $order
68+
* Get firs order invoice
69+
*
70+
* @param OrderInterface|int $order
7571
* @return InvoiceInterface
7672
*/
77-
protected function getInvoiceByOrder(OrderInterface $order): InvoiceInterface
73+
protected function getInvoiceByOrder($order): InvoiceInterface
7874
{
79-
/** @var \Magento\Sales\Model\ResourceModel\Order\Invoice\Collection $invoiceCollection */
80-
$invoiceCollection = $this->_objectManager->create(
81-
\Magento\Sales\Model\ResourceModel\Order\Invoice\CollectionFactory::class
82-
)->create();
75+
$invoiceCollection = $this->invoiceCollectionFactory->create();
8376

84-
/** @var InvoiceInterface $invoice */
85-
$invoice = $invoiceCollection
86-
->setOrderFilter($order)
87-
->setPageSize(1)
88-
->getFirstItem();
77+
return $invoiceCollection->setOrderFilter($order)->setPageSize(1)->getFirstItem();
78+
}
8979

90-
return $invoice;
80+
/**
81+
* Prepare request
82+
*
83+
* @param array $postParams
84+
* @param array $params
85+
* @return void
86+
*/
87+
protected function prepareRequest(array $postParams = [], array $params = []): void
88+
{
89+
$this->getRequest()->setMethod(Http::METHOD_POST);
90+
$this->getRequest()->setParams($params);
91+
$this->getRequest()->setPostValue($postParams);
92+
}
93+
94+
/**
95+
* Normalize post parameters
96+
*
97+
* @param array $items
98+
* @param string $commentText
99+
* @param bool $doShipment
100+
* @return array
101+
*/
102+
protected function hydratePost(array $items, string $commentText = '', $doShipment = false): array
103+
{
104+
return [
105+
'invoice' => [
106+
'items' => $items,
107+
'comment_text' => $commentText,
108+
'do_shipment' => $doShipment
109+
],
110+
];
91111
}
92112
}

0 commit comments

Comments
 (0)