8
8
namespace Magento \Sales \Block \Order \Item \Renderer ;
9
9
10
10
use Magento \Framework \ObjectManagerInterface ;
11
+ use Magento \Framework \Registry ;
12
+ use Magento \Framework \View \Element \AbstractBlock ;
11
13
use Magento \Framework \View \LayoutInterface ;
14
+ use Magento \Framework \View \Result \PageFactory ;
15
+ use Magento \Sales \Api \Data \OrderInterface ;
12
16
use Magento \Sales \Api \Data \OrderInterfaceFactory ;
13
17
use Magento \TestFramework \Helper \Bootstrap ;
14
18
use Magento \TestFramework \Helper \Xpath ;
19
23
*
20
24
* @magentoAppArea frontend
21
25
* @magentoDbIsolation enabled
26
+ * @magentoAppIsolation enabled
22
27
*/
23
28
class DefaultRendererTest extends TestCase
24
29
{
@@ -31,14 +36,43 @@ class DefaultRendererTest extends TestCase
31
36
/** @var OrderInterfaceFactory */
32
37
private $ orderFactory ;
33
38
39
+ /** @var PageFactory */
40
+ private $ pageFactory ;
41
+
42
+ /** @var Registry */
43
+ private $ registry ;
44
+
45
+ /**
46
+ * @var array
47
+ */
48
+ private $ defaultFieldsToCheck = [
49
+ 'name ' => "//td[contains(@class, 'name')]/strong[contains(text(), '%s')] " ,
50
+ 'sku ' => "//td[contains(@class, 'sku') and contains(text(), '%s')] " ,
51
+ 'qty ' => "//td[contains(@class, 'qty') and contains(text(), '%d')] " ,
52
+ ];
53
+
34
54
/**
35
55
* @inheritdoc
36
56
*/
37
57
protected function setUp (): void
38
58
{
59
+ parent ::setUp ();
60
+
39
61
$ this ->objectManager = Bootstrap::getObjectManager ();
40
62
$ this ->block = $ this ->objectManager ->get (LayoutInterface::class)->createBlock (DefaultRenderer::class);
41
63
$ this ->orderFactory = $ this ->objectManager ->get (OrderInterfaceFactory::class);
64
+ $ this ->pageFactory = $ this ->objectManager ->get (PageFactory::class);
65
+ $ this ->registry = $ this ->objectManager ->get (Registry::class);
66
+ }
67
+
68
+ /**
69
+ * @inheritdoc
70
+ */
71
+ protected function tearDown (): void
72
+ {
73
+ $ this ->registry ->unregister ('current_order ' );
74
+
75
+ parent ::tearDown ();
42
76
}
43
77
44
78
/**
@@ -55,43 +89,13 @@ public function testDisplayingShipmentItem(): void
55
89
$ this ->assertNotNull ($ item );
56
90
$ blockHtml = $ this ->block ->setTemplate ('Magento_Sales::order/shipment/items/renderer/default.phtml ' )
57
91
->setItem ($ item )->toHtml ();
58
- $ this ->assertEquals (
59
- 1 ,
60
- Xpath::getElementsCountForXpath (
61
- sprintf (
62
- "//td[contains(@class, 'name')]/strong[contains(text(), '%s')] " ,
63
- $ item ->getName ()
64
- ),
65
- $ blockHtml
66
- ),
67
- sprintf ('Item with name %s wasn \'t found. ' , $ item ->getName ())
68
- );
69
- $ this ->assertEquals (
70
- 1 ,
71
- Xpath::getElementsCountForXpath (
72
- sprintf (
73
- "//td[contains(@class, 'sku') and contains(text(), '%s')] " ,
74
- $ item ->getSku ()
75
- ),
76
- $ blockHtml
77
- ),
78
- sprintf ('Item with sku %s wasn \'t found. ' , $ item ->getSku ())
79
- );
80
- $ this ->assertEquals (
81
- 1 ,
82
- Xpath::getElementsCountForXpath (
83
- sprintf (
84
- "//td[contains(@class, 'qty') and contains(text(), '%d')] " ,
85
- $ item ->getQty ()
86
- ),
87
- $ blockHtml
88
- ),
89
- sprintf (
90
- 'Qty for item %s wasn \'t found or not equals to %s. ' ,
91
- $ item ->getName (),
92
- $ item ->getQty ()
93
- )
94
- );
92
+ foreach ($ this ->defaultFieldsToCheck as $ key => $ xpath ) {
93
+ $ this ->assertEquals (
94
+ 1 ,
95
+ Xpath::getElementsCountForXpath (sprintf ($ xpath , $ item ->getData ($ key )), $ blockHtml ),
96
+ sprintf ('Item %s wasn \'t found or not equals to %s. ' , $ key , $ item ->getData ($ key ))
97
+ );
98
+ }
95
99
}
96
100
97
101
/**
@@ -108,4 +112,161 @@ public function testCreditmemoItemTotalAmount(): void
108
112
$ this ->assertNotNull ($ item ->getId ());
109
113
$ this ->assertEquals (10.00 , $ this ->block ->getTotalAmount ($ item ));
110
114
}
115
+
116
+ /**
117
+ * @magentoDataFixture Magento/Sales/_files/customer_order_with_two_items.php
118
+ *
119
+ * @return void
120
+ */
121
+ public function testPrintOrderItem (): void
122
+ {
123
+ $ order = $ this ->orderFactory ->create ()->loadByIncrementId ('100000555 ' );
124
+ $ this ->registerOrder ($ order );
125
+ $ item = $ order ->getItemsCollection ()->getFirstItem ();
126
+ $ this ->assertNotNull ($ item ->getId ());
127
+ $ block = $ this ->getBlock ('sales_order_print ' , 'sales.order.print.renderers.default ' );
128
+ $ this ->assertNotFalse ($ block );
129
+ $ blockHtml = $ block ->setItem ($ item )->toHtml ();
130
+ $ fieldsToCheck = [
131
+ 'name ' => "//td[contains(@class, 'name')]/strong[contains(text(), '%s')] " ,
132
+ 'sku ' => "//td[contains(@class, 'sku') and contains(text(), '%s')] " ,
133
+ 'price ' => "//td[contains(@class, 'price')]//span[contains(text(), '%01.2f')] " ,
134
+ 'qty_ordered ' => "//td[contains(@class, 'qty')]//span[contains(text(), ' " . __ ('Ordered ' )
135
+ . "')]/following-sibling::span[contains(text(), '%d')] " ,
136
+ 'row_total ' => "//td[contains(@class, 'subtotal')]//span[contains(text(), '%01.2f')] " ,
137
+ ];
138
+ foreach ($ fieldsToCheck as $ key => $ xpath ) {
139
+ $ this ->assertEquals (
140
+ 1 ,
141
+ Xpath::getElementsCountForXpath (sprintf ($ xpath , $ item ->getData ($ key )), $ blockHtml ),
142
+ sprintf ('Item %s wasn \'t found or not equals to %s. ' , $ key , $ item ->getData ($ key ))
143
+ );
144
+ }
145
+ }
146
+
147
+ /**
148
+ * @magentoDataFixture Magento/Sales/_files/invoices_for_items.php
149
+ *
150
+ * @return void
151
+ */
152
+ public function testPrintInvoiceItem (): void
153
+ {
154
+ $ order = $ this ->orderFactory ->create ()->loadByIncrementId ('100000555 ' );
155
+ $ this ->registerOrder ($ order );
156
+ $ invoice = $ order ->getInvoiceCollection ()->getFirstItem ();
157
+ $ this ->assertNotNull ($ invoice ->getId ());
158
+ $ item = $ invoice ->getItemsCollection ()->getFirstItem ();
159
+ $ this ->assertNotNull ($ item ->getId ());
160
+ $ block = $ this ->getBlock ('sales_order_printinvoice ' , 'sales.order.print.invoice.renderers.default ' );
161
+ $ this ->assertNotFalse ($ block );
162
+ $ blockHtml = $ block ->setItem ($ item )->toHtml ();
163
+ $ additionalFields = [
164
+ 'price ' => "//td[contains(@class, 'price')]//span[contains(text(), '%01.2f')] " ,
165
+ 'qty ' => "//td[contains(@class, 'qty')]/span[contains(text(), '%d')] " ,
166
+ 'row_total ' => "//td[contains(@class, 'subtotal')]//span[contains(text(), '%01.2f')] " ,
167
+ ];
168
+ $ this ->defaultFieldsToCheck = array_merge ($ this ->defaultFieldsToCheck , $ additionalFields );
169
+ foreach ($ this ->defaultFieldsToCheck as $ key => $ xpath ) {
170
+ $ this ->assertEquals (
171
+ 1 ,
172
+ Xpath::getElementsCountForXpath (sprintf ($ xpath , $ item ->getData ($ key )), $ blockHtml ),
173
+ sprintf ('Item %s wasn \'t found or not equals to %s. ' , $ key , $ item ->getData ($ key ))
174
+ );
175
+ }
176
+ }
177
+
178
+ /**
179
+ * @magentoDataFixture Magento/Sales/_files/shipment_for_order_with_customer.php
180
+ *
181
+ * @return void
182
+ */
183
+ public function testPrintShipmentItem (): void
184
+ {
185
+ $ order = $ this ->orderFactory ->create ()->loadByIncrementId ('100000001 ' );
186
+ $ this ->registerOrder ($ order );
187
+ $ shipment = $ order ->getShipmentsCollection ()->getFirstItem ();
188
+ $ this ->assertNotNull ($ shipment ->getId ());
189
+ $ item = $ shipment ->getAllItems ()[0 ] ?? null ;
190
+ $ this ->assertNotNull ($ item );
191
+ $ block = $ this ->getBlock ('sales_order_printshipment ' , 'sales.order.print.shipment.renderers.default ' );
192
+ $ this ->assertNotFalse ($ block );
193
+ $ blockHtml = $ block ->setItem ($ item )->toHtml ();
194
+ foreach ($ this ->defaultFieldsToCheck as $ key => $ xpath ) {
195
+ $ this ->assertEquals (
196
+ 1 ,
197
+ Xpath::getElementsCountForXpath (sprintf ($ xpath , $ item ->getData ($ key )), $ blockHtml ),
198
+ sprintf ('Item %s wasn \'t found or not equals to %s. ' , $ key , $ item ->getData ($ key ))
199
+ );
200
+ }
201
+ }
202
+
203
+ /**
204
+ * @magentoDataFixture Magento/Sales/_files/refunds_for_items.php
205
+ *
206
+ * @return void
207
+ */
208
+ public function testPrintCreditmemoItem (): void
209
+ {
210
+ $ order = $ this ->orderFactory ->create ()->loadByIncrementId ('100000555 ' );
211
+ $ this ->registerOrder ($ order );
212
+ $ creditmemo = $ order ->getCreditmemosCollection ()->getFirstItem ();
213
+ $ this ->assertNotNull ($ creditmemo ->getId ());
214
+ $ item = $ creditmemo ->getItemsCollection ()->getFirstItem ();
215
+ $ this ->assertNotNull ($ item ->getId ());
216
+ $ block = $ this ->getBlock ('sales_order_printcreditmemo ' , 'sales.order.print.creditmemo.renderers.default ' );
217
+ $ this ->assertNotFalse ($ block );
218
+ $ blockHtml = $ block ->setItem ($ item )->toHtml ();
219
+ $ additionalFields = [
220
+ 'price ' => "//td[contains(@class, 'price')]//span[contains(text(), '%01.2f')] " ,
221
+ 'row_total ' => "//td[contains(@class, 'subtotal')]//span[contains(text(), '%01.2f')] " ,
222
+ 'discount_amount ' => "//td[contains(@class, 'discount')]/span[contains(text(), '%01.2f')] " ,
223
+ ];
224
+ $ this ->defaultFieldsToCheck = array_merge ($ this ->defaultFieldsToCheck , $ additionalFields );
225
+ foreach ($ this ->defaultFieldsToCheck as $ key => $ xpath ) {
226
+ $ this ->assertEquals (
227
+ 1 ,
228
+ Xpath::getElementsCountForXpath (sprintf ($ xpath , $ item ->getData ($ key )), $ blockHtml ),
229
+ sprintf ('Item %s wasn \'t found or not equals to %s. ' , $ key , $ item ->getData ($ key ))
230
+ );
231
+ }
232
+ $ this ->assertEquals (
233
+ 1 ,
234
+ Xpath::getElementsCountForXpath (
235
+ sprintf (
236
+ "//td[contains(@class, 'total')]/span[contains(text(), '%01.2f')] " ,
237
+ $ this ->block ->getTotalAmount ($ item )
238
+ ),
239
+ $ blockHtml
240
+ ),
241
+ sprintf ('Item total wasn \'t found or not equals to %s. ' , $ this ->block ->getTotalAmount ($ item ))
242
+ );
243
+ }
244
+
245
+ /**
246
+ * Get block.
247
+ *
248
+ * @param string $handle
249
+ * @param string $blockName
250
+ * @return AbstractBlock
251
+ */
252
+ private function getBlock (string $ handle , string $ blockName ): AbstractBlock
253
+ {
254
+ $ page = $ this ->pageFactory ->create ();
255
+ $ page ->addHandle (['default ' , $ handle ]);
256
+ $ page ->getLayout ()->generateXml ();
257
+
258
+ return $ page ->getLayout ()->getBlock ($ blockName );
259
+ }
260
+
261
+ /**
262
+ * Register order in registry.
263
+ *
264
+ * @param OrderInterface $order
265
+ * @return void
266
+ */
267
+ private function registerOrder (OrderInterface $ order ): void
268
+ {
269
+ $ this ->registry ->unregister ('current_order ' );
270
+ $ this ->registry ->register ('current_order ' , $ order );
271
+ }
111
272
}
0 commit comments