9
9
use Magento \Customer \Api \CustomerRepositoryInterface ;
10
10
use Magento \Framework \Api \SearchCriteriaBuilder ;
11
11
use Magento \Framework \App \Request \Http ;
12
+ use Magento \Framework \Data \Form \FormKey ;
12
13
use Magento \Framework \Exception \LocalizedException ;
13
14
use Magento \Framework \Message \MessageInterface ;
14
15
use Magento \Quote \Api \CartRepositoryInterface ;
16
+ use Magento \Sales \Api \Data \OrderInterface ;
17
+ use Magento \Sales \Model \OrderRepository ;
15
18
use Magento \Sales \Model \Service \OrderService ;
19
+ use Magento \TestFramework \Mail \Template \TransportBuilderMock ;
16
20
use Magento \TestFramework \TestCase \AbstractBackendController ;
21
+ use PHPUnit \Framework \Constraint \StringContains ;
17
22
use PHPUnit_Framework_MockObject_MockObject as MockObject ;
18
23
24
+ /**
25
+ * Class test backend order save.
26
+ *
27
+ * @magentoAppArea adminhtml
28
+ * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
29
+ */
19
30
class SaveTest extends AbstractBackendController
20
31
{
32
+ /**
33
+ * @var TransportBuilderMock
34
+ */
35
+ private $ transportBuilder ;
36
+
37
+ /**
38
+ * @var FormKey
39
+ */
40
+ private $ formKey ;
41
+
42
+ /**
43
+ * @var string
44
+ */
45
+ protected $ resource = 'Magento_Sales::create ' ;
46
+
47
+ /**
48
+ * @var string
49
+ */
50
+ protected $ uri = 'backend/sales/order_create/save ' ;
51
+
52
+ /**
53
+ * @inheritdoc
54
+ */
55
+ protected function setUp ()
56
+ {
57
+ parent ::setUp ();
58
+ $ this ->transportBuilder = $ this ->_objectManager ->get (TransportBuilderMock::class);
59
+ $ this ->formKey = $ this ->_objectManager ->get (FormKey::class);
60
+ }
61
+
21
62
/**
22
63
* Checks a case when order creation is failed on payment method processing but new customer already created
23
64
* in the database and after new controller dispatching the customer should be already loaded in session
24
65
* to prevent invalid validation.
25
66
*
26
- * @magentoAppArea adminhtml
27
67
* @magentoDataFixture Magento/Sales/_files/quote_with_new_customer.php
28
68
*/
29
69
public function testExecuteWithPaymentOperation ()
@@ -36,7 +76,7 @@ public function testExecuteWithPaymentOperation()
36
76
$ email = 'john.doe001@test.com ' ;
37
77
$ data = [
38
78
'account ' => [
39
- 'email ' => $ email
79
+ 'email ' => $ email,
40
80
]
41
81
];
42
82
$ this ->getRequest ()->setMethod (Http::METHOD_POST );
@@ -66,13 +106,52 @@ public function testExecuteWithPaymentOperation()
66
106
$ this ->_objectManager ->removeSharedInstance (OrderService::class);
67
107
}
68
108
109
+ /**
110
+ * @magentoDbIsolation enabled
111
+ * @magentoDataFixture Magento/Sales/_files/guest_quote_with_addresses.php
112
+ *
113
+ * @return void
114
+ */
115
+ public function testSendEmailOnOrderSave (): void
116
+ {
117
+ $ this ->prepareRequest (['send_confirmation ' => true ]);
118
+ $ this ->dispatch ('backend/sales/order_create/save ' );
119
+ $ this ->assertSessionMessages (
120
+ $ this ->equalTo ([(string )__ ('You created the order. ' )]),
121
+ MessageInterface::TYPE_SUCCESS
122
+ );
123
+
124
+ $ this ->assertRedirect ($ this ->stringContains ('sales/order/view/ ' ));
125
+
126
+ $ orderId = $ this ->getOrderId ();
127
+ if ($ orderId === false ) {
128
+ $ this ->fail ('Order is not created. ' );
129
+ }
130
+ $ order = $ this ->getOrder ($ orderId );
131
+
132
+ $ message = $ this ->transportBuilder ->getSentMessage ();
133
+ $ subject = __ ('Your %1 order confirmation ' , $ order ->getStore ()->getFrontendName ())->render ();
134
+ $ assert = $ this ->logicalAnd (
135
+ new StringContains ($ order ->getBillingAddress ()->getName ()),
136
+ new StringContains (
137
+ 'Thank you for your order from ' . $ order ->getStore ()->getFrontendName ()
138
+ ),
139
+ new StringContains (
140
+ "Your Order <span class= \"no-link \"># {$ order ->getIncrementId ()}</span> "
141
+ )
142
+ );
143
+
144
+ $ this ->assertEquals ($ message ->getSubject (), $ subject );
145
+ $ this ->assertThat ($ message ->getRawMessage (), $ assert );
146
+ }
147
+
69
148
/**
70
149
* Gets quote by reserved order id.
71
150
*
72
151
* @param string $reservedOrderId
73
152
* @return \Magento\Quote\Api\Data\CartInterface
74
153
*/
75
- private function getQuote ($ reservedOrderId )
154
+ private function getQuote (string $ reservedOrderId ): \ Magento \ Quote \ Api \ Data \ CartInterface
76
155
{
77
156
/** @var SearchCriteriaBuilder $searchCriteriaBuilder */
78
157
$ searchCriteriaBuilder = $ this ->_objectManager ->get (SearchCriteriaBuilder::class);
@@ -82,6 +161,81 @@ private function getQuote($reservedOrderId)
82
161
/** @var CartRepositoryInterface $quoteRepository */
83
162
$ quoteRepository = $ this ->_objectManager ->get (CartRepositoryInterface::class);
84
163
$ items = $ quoteRepository ->getList ($ searchCriteria )->getItems ();
164
+
85
165
return array_pop ($ items );
86
166
}
167
+
168
+ /**
169
+ * @inheritdoc
170
+ * @magentoDbIsolation enabled
171
+ * @magentoDataFixture Magento/Sales/_files/guest_quote_with_addresses.php
172
+ */
173
+ public function testAclHasAccess ()
174
+ {
175
+ $ this ->prepareRequest ();
176
+
177
+ parent ::testAclHasAccess ();
178
+ }
179
+
180
+ /**
181
+ * @inheritdoc
182
+ * @magentoDbIsolation enabled
183
+ * @magentoDataFixture Magento/Sales/_files/guest_quote_with_addresses.php
184
+ */
185
+ public function testAclNoAccess ()
186
+ {
187
+ $ this ->prepareRequest ();
188
+
189
+ parent ::testAclNoAccess ();
190
+ }
191
+
192
+ /**
193
+ * @param int $orderId
194
+ * @return OrderInterface
195
+ */
196
+ private function getOrder (int $ orderId ): OrderInterface
197
+ {
198
+ return $ this ->_objectManager ->get (OrderRepository::class)->get ($ orderId );
199
+ }
200
+
201
+ /**
202
+ * @param array $params
203
+ * @return void
204
+ */
205
+ private function prepareRequest (array $ params = []): void
206
+ {
207
+ $ quote = $ this ->getQuote ('guest_quote ' );
208
+ $ session = $ this ->_objectManager ->get (Quote::class);
209
+ $ session ->setQuoteId ($ quote ->getId ());
210
+ $ session ->setCustomerId (0 );
211
+
212
+ $ email = 'john.doe001@test.com ' ;
213
+ $ data = [
214
+ 'account ' => [
215
+ 'email ' => $ email ,
216
+ ],
217
+ ];
218
+
219
+ $ data = array_replace_recursive ($ data , $ params );
220
+
221
+ $ this ->getRequest ()
222
+ ->setMethod ('POST ' )
223
+ ->setParams (['form_key ' => $ this ->formKey ->getFormKey ()])
224
+ ->setPostValue (['order ' => $ data ]);
225
+ }
226
+
227
+ /**
228
+ * @return string|bool
229
+ */
230
+ protected function getOrderId ()
231
+ {
232
+ $ currentUrl = $ this ->getResponse ()->getHeader ('Location ' );
233
+ $ orderId = false ;
234
+
235
+ if (preg_match ('/order_id\/(?<order_id>\d+)/ ' , $ currentUrl , $ matches )) {
236
+ $ orderId = $ matches ['order_id ' ] ?? '' ;
237
+ }
238
+
239
+ return $ orderId ;
240
+ }
87
241
}
0 commit comments