Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
AdminTest.php
Go to the documentation of this file.
1 <?php
7 
9 
13 class AdminTest extends \PHPUnit\Framework\TestCase
14 {
18  protected $contextMock;
19 
23  protected $storeManagerMock;
24 
28  protected $salesConfigMock;
29 
33  protected $magentoObjectMock;
34 
38  protected $orderMock;
39 
43  protected $adminHelper;
44 
48  protected $priceCurrency;
49 
53  protected $escaperMock;
54 
55  protected function setUp()
56  {
57  $this->contextMock = $this->getMockBuilder(\Magento\Framework\App\Helper\Context::class)
58  ->disableOriginalConstructor()
59  ->getMock();
60  $this->storeManagerMock = $this->getMockBuilder(\Magento\Store\Model\StoreManagerInterface::class)
61  ->disableOriginalConstructor()
62  ->getMock();
63  $this->salesConfigMock = $this->getMockBuilder(\Magento\Sales\Model\Config::class)
64  ->disableOriginalConstructor()
65  ->getMock();
66  $this->priceCurrency = $this->getMockBuilder(
67  \Magento\Framework\Pricing\PriceCurrencyInterface::class
68  )->getMock();
69 
70  $this->escaperMock = $this->getMockBuilder(\Magento\Framework\Escaper::class)
71  ->disableOriginalConstructor()
72  ->getMock();
73 
74  $this->adminHelper = (new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this))->getObject(
75  \Magento\Sales\Helper\Admin::class,
76  [
77  'context' => $this->contextMock,
78  'storeManager' => $this->storeManagerMock,
79  'salesConfig' => $this->salesConfigMock,
80  'priceCurrency' => $this->priceCurrency,
81  'escaper' => $this->escaperMock
82  ]
83  );
84 
85  $this->magentoObjectMock = $this->getMockBuilder(\Magento\Framework\DataObject::class)
86  ->disableOriginalConstructor()
87  ->setMethods(['getOrder', 'getData'])
88  ->getMock();
89 
90  $this->orderMock = $this->getMockBuilder(\Magento\Sales\Model\Order::class)
91  ->disableOriginalConstructor()
92  ->getMock();
93  $this->orderMock->expects($this->any())
94  ->method('formatBasePrice')
95  ->will($this->returnValue('formattedBasePrice'));
96  $this->orderMock->expects($this->any())
97  ->method('formatPrice')
98  ->will($this->returnValue('formattedPrice'));
99  $this->orderMock->expects($this->any())
100  ->method('getData')
101  ->will($this->returnValue('data'));
102  }
103 
113  public function testDisplayPrices(
114  $expected,
115  $dataObjectIsOrder,
116  $isCurrencyDifferent = true,
117  $magentoDataObjectHasOrder = true,
118  $strong = false,
119  $separator = '<br/>'
120  ) {
121  $this->orderMock->expects($this->any())
122  ->method('isCurrencyDifferent')
123  ->will($this->returnValue($isCurrencyDifferent));
124  $storeMock = $this->getMockBuilder(\Magento\Store\Model\Store::class)
125  ->disableOriginalConstructor()
126  ->getMock();
127  $this->storeManagerMock->expects($this->any())
128  ->method('getStore')
129  ->will($this->returnValue($storeMock));
130  $this->priceCurrency->expects($this->any())
131  ->method('format')
132  ->will($this->returnValue('storeFormattedPrice'));
133  $dataObject = $this->orderMock;
134  if (!$dataObjectIsOrder) {
135  $returnRes = false;
136  if ($magentoDataObjectHasOrder) {
137  $returnRes = $this->orderMock;
138  }
139  $this->magentoObjectMock->expects($this->once())
140  ->method('getOrder')
141  ->will($this->returnValue($returnRes));
142  $dataObject = $this->magentoObjectMock;
143  }
144  $basePrice = 10.00;
145  $price = 15.00;
146  $this->assertEquals(
147  $expected,
148  $this->adminHelper->displayPrices($dataObject, $basePrice, $price, $strong, $separator)
149  );
150  }
151 
161  public function testDisplayPriceAttribute(
162  $expected,
163  $dataObjectIsOrder,
164  $isCurrencyDifferent = true,
165  $magentoDataObjectHasOrder = true,
166  $strong = false,
167  $separator = '<br/>'
168  ) {
169  $this->orderMock->expects($this->any())
170  ->method('isCurrencyDifferent')
171  ->will($this->returnValue($isCurrencyDifferent));
172  $storeMock = $this->getMockBuilder(\Magento\Store\Model\Store::class)
173  ->disableOriginalConstructor()
174  ->getMock();
175  $this->storeManagerMock->expects($this->any())
176  ->method('getStore')
177  ->will($this->returnValue($storeMock));
178  $this->priceCurrency->expects($this->any())
179  ->method('format')
180  ->will($this->returnValue('storeFormattedPrice'));
181  $dataObject = $this->orderMock;
182  if (!$dataObjectIsOrder) {
183  $returnRes = false;
184  if ($magentoDataObjectHasOrder) {
185  $returnRes = $this->orderMock;
186  }
187  $this->magentoObjectMock->expects($this->once())
188  ->method('getOrder')
189  ->will($this->returnValue($returnRes));
190  $this->magentoObjectMock->expects($this->any())
191  ->method('getData')
192  ->will($this->returnValue('data'));
193  $dataObject = $this->magentoObjectMock;
194  }
195  $this->assertEquals(
196  $expected,
197  $this->adminHelper->displayPriceAttribute($dataObject, 'code', $strong, $separator)
198  );
199  }
200 
204  public function displayPricesDataProvider()
205  {
206  return [
207  [
208  '<strong>formattedBasePrice</strong><br/>[formattedPrice]',
209  true,
210  ],
211  [
212  '<strong>formattedBasePrice</strong><br/>[formattedPrice]',
213  false,
214  ],
215  [
216  'formattedPrice',
217  true,
218  false,
219  ],
220  [
221  'formattedPrice',
222  false,
223  false,
224  ],
225  [
226  '<strong>formattedPrice</strong>',
227  true,
228  false,
229  true,
230  true,
231  ],
232  [
233  '<strong>formattedPrice</strong>',
234  true,
235  false,
236  true,
237  true,
238  'seperator',
239  ],
240  [
241  '<strong>formattedBasePrice</strong>seperator[formattedPrice]',
242  true,
243  true,
244  true,
245  true,
246  'seperator',
247  ],
248  [
249  'storeFormattedPrice',
250  false,
251  false,
252  false,
253  false,
254  'seperator',
255  ],
256  [
257  '<strong>storeFormattedPrice</strong>',
258  false,
259  false,
260  false,
261  true,
262  'seperator',
263  ],
264 
265  ];
266  }
267 
274  public function testApplySalableProductTypesFilter($itemKey, $type, $calledTimes)
275  {
276  $productMock = $this->getMockBuilder(\Magento\Catalog\Model\Product::class)
277  ->disableOriginalConstructor()
278  ->getMock();
279  $productMock->expects($this->any())
280  ->method('getTypeId')
281  ->will($this->returnValue($type));
282  $orderMock = $this->getMockBuilder(\Magento\Sales\Model\Order\Item::class)
283  ->disableOriginalConstructor()
284  ->setMethods(['__wakeup', 'getProductType'])
285  ->getMock();
286  $orderMock->expects($this->any())
287  ->method('getProductType')
288  ->will($this->returnValue($type));
289  $quoteMock = $this->getMockBuilder(\Magento\Quote\Model\Quote\Item::class)
290  ->disableOriginalConstructor()
291  ->getMock();
292  $quoteMock->expects($this->any())
293  ->method('getProductType')
294  ->will($this->returnValue($type));
295  $items = [
296  'product' => $productMock,
297  'order' => $orderMock,
298  'quote' => $quoteMock,
299  'other' => 'other',
300  ];
301  $collectionClassName = \Magento\Framework\Model\ResourceModel\Db\Collection\AbstractCollection::class;
302  $collectionMock = $this->getMockBuilder($collectionClassName)
303  ->disableOriginalConstructor()
304  ->getMock();
305  $collectionMock->expects($this->any())
306  ->method('getItems')
307  ->will($this->returnValue([$items[$itemKey]]));
308  $collectionMock->expects($this->exactly($calledTimes))
309  ->method('removeItemByKey');
310  $this->salesConfigMock->expects($this->any())
311  ->method('getAvailableProductTypes')
312  ->will($this->returnValue(['validProductType']));
313  $this->adminHelper->applySalableProductTypesFilter($collectionMock);
314  }
315 
320  {
321  return [
322  ['product', 'validProductType', 0],
323  ['product', 'invalidProductType', 1],
324  ['order', 'validProductType', 0],
325  ['order', 'invalidProductType', 1],
326  ['quote', 'validProductType', 0],
327  ['quote', 'invalidProductType', 1],
328  ['other', 'validProductType', 1],
329  ];
330  }
331 
338  public function testEscapeHtmlWithLinks($data, $expected, $allowedTags = null)
339  {
340  $this->escaperMock
341  ->expects($this->any())
342  ->method('escapeHtml')
343  ->will($this->returnValue($expected));
344  $actual = $this->adminHelper->escapeHtmlWithLinks($data, $allowedTags);
345  $this->assertEquals($expected, $actual);
346  }
347 
352  {
353  return [
354  [
355  '<a>some text in tags</a>',
356  '&lt;a&gt;some text in tags&lt;/a&gt;',
357  'allowedTags' => null
358  ],
359  [
360  'Transaction ID: "<a target="_blank" href="https://www.paypal.com/?id=XX123XX">XX123XX</a>"',
361  'Transaction ID: &quot;<a target="_blank" href="https://www.paypal.com/?id=XX123XX">XX123XX</a>&quot;',
362  'allowedTags' => ['b', 'br', 'strong', 'i', 'u', 'a']
363  ],
364  [
365  '<a>some text in tags</a>',
366  '<a>some text in tags</a>',
367  'allowedTags' => ['a']
368  ],
369  'Not replacement with placeholders' => [
370  "<a><script>alert(1)</script></a>",
371  '<a>&lt;script&gt;alert(1)&lt;/script&gt;</a>',
372  'allowedTags' => ['a']
373  ],
374  'Normal usage, url escaped' => [
375  '<a href=\"#\">Foo</a>',
376  '<a href="#">Foo</a>',
377  'allowedTags' => ['a']
378  ],
379  'Normal usage, url not escaped' => [
380  "<a href=http://example.com?foo=1&bar=2&baz[name]=BAZ>Foo</a>",
381  '<a href="http://example.com?foo=1&amp;bar=2&amp;baz[name]=BAZ">Foo</a>',
382  'allowedTags' => ['a']
383  ],
384  'XSS test' => [
385  "<a href=\"javascript&colon;alert(59)\">Foo</a>",
386  '<a href="#">Foo</a>',
387  'allowedTags' => ['a']
388  ],
389  'Additional regex test' => [
390  "<a href=\"http://example1.com\" href=\"http://example2.com\">Foo</a>",
391  '<a href="http://example1.com">Foo</a>',
392  'allowedTags' => ['a']
393  ],
394  'Break of valid urls' => [
395  "<a href=\"http://example.com?foo=text with space\">Foo</a>",
396  '<a href="#">Foo</a>',
397  'allowedTags' => ['a']
398  ],
399  ];
400  }
401 }
testDisplayPrices( $expected, $dataObjectIsOrder, $isCurrencyDifferent=true, $magentoDataObjectHasOrder=true, $strong=false, $separator='< br/>')
Definition: AdminTest.php:113
testEscapeHtmlWithLinks($data, $expected, $allowedTags=null)
Definition: AdminTest.php:338
$price
$type
Definition: item.phtml:13
testDisplayPriceAttribute( $expected, $dataObjectIsOrder, $isCurrencyDifferent=true, $magentoDataObjectHasOrder=true, $strong=false, $separator='< br/>')
Definition: AdminTest.php:161
testApplySalableProductTypesFilter($itemKey, $type, $calledTimes)
Definition: AdminTest.php:274
$items