Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
AbstractCarrierOnlineTest.php
Go to the documentation of this file.
1 <?php
8 
9 use \Magento\Shipping\Model\Carrier\AbstractCarrierOnline;
10 
13 
14 class AbstractCarrierOnlineTest extends \PHPUnit\Framework\TestCase
15 {
21  protected $productId = 1;
22 
26  protected $carrier;
27 
31  protected $stockRegistry;
32 
36  protected $stockItemData;
37 
38  protected function setUp()
39  {
40  $this->stockRegistry = $this->createMock(\Magento\CatalogInventory\Model\StockRegistry::class);
41  $this->stockItemData = $this->createMock(\Magento\CatalogInventory\Model\Stock\Item::class);
42 
43  $this->stockRegistry->expects($this->any())->method('getStockItem')
44  ->with($this->productId, 10)
45  ->will($this->returnValue($this->stockItemData));
46 
47  $objectManagerHelper = new ObjectManagerHelper($this);
48  $carrierArgs = $objectManagerHelper->getConstructArguments(
49  \Magento\Shipping\Model\Carrier\AbstractCarrierOnline::class,
50  [
51  'stockRegistry' => $this->stockRegistry,
52  'xmlSecurity' => new \Magento\Framework\Xml\Security(),
53  ]
54  );
55  $this->carrier = $this->getMockBuilder(\Magento\Shipping\Model\Carrier\AbstractCarrierOnline::class)
56  ->setConstructorArgs($carrierArgs)
57  ->setMethods(['getConfigData', '_doShipmentRequest', 'collectRates'])
58  ->getMock();
59  }
60 
64  public function testComposePackages()
65  {
66  $this->carrier->expects($this->any())->method('getConfigData')->will($this->returnCallback(function ($key) {
67  $configData = [
68  'max_package_weight' => 10,
69  'showmethod' => 1,
70  ];
71  return isset($configData[$key]) ? $configData[$key] : 0;
72  }));
73 
74  $product = $this->createMock(\Magento\Catalog\Model\Product::class);
75  $product->expects($this->any())->method('getId')->will($this->returnValue($this->productId));
76 
77  $item = $this->getMockBuilder(\Magento\Quote\Model\Quote\Item::class)
78  ->disableOriginalConstructor()
79  ->setMethods(['getProduct', 'getQty', 'getWeight', '__wakeup', 'getStore'])
80  ->getMock();
81  $item->expects($this->any())->method('getProduct')->will($this->returnValue($product));
82 
83  $store = $this->createPartialMock(\Magento\Store\Model\Store::class, ['getWebsiteId']);
84  $store->expects($this->any())
85  ->method('getWebsiteId')
86  ->will($this->returnValue(10));
87  $item->expects($this->any())->method('getStore')->will($this->returnValue($store));
88 
89  $request = new RateRequest();
90  $request->setData('all_items', [$item]);
91  $request->setData('dest_postcode', 1);
92 
94  $this->stockRegistry->expects($this->atLeastOnce())->method('getStockItem')->with($this->productId);
95  $this->stockItemData->expects($this->atLeastOnce())->method('getEnableQtyIncrements')
96  ->will($this->returnValue(true));
97  $this->stockItemData->expects($this->atLeastOnce())->method('getQtyIncrements')
98  ->will($this->returnValue(5));
99  $this->stockItemData->expects($this->atLeastOnce())->method('getIsQtyDecimal')->will($this->returnValue(true));
100  $this->stockItemData->expects($this->atLeastOnce())->method('getIsDecimalDivided')
101  ->will($this->returnValue(true));
102 
103  $this->carrier->processAdditionalValidation($request);
104  }
105 
106  public function testParseXml()
107  {
108  $xmlString = "<?xml version=\"1.0\" encoding=\"UTF-8\"?><GetResponse><value>42</value>></GetResponse>";
109  $simpleXmlElement = $this->carrier->parseXml($xmlString);
110  $this->assertEquals('GetResponse', $simpleXmlElement->getName());
111  $this->assertEquals(42, (int)$simpleXmlElement->value);
112  $this->assertInstanceOf('SimpleXMLElement', $simpleXmlElement);
113  $customSimpleXmlElement = $this->carrier->parseXml(
114  $xmlString,
115  \Magento\Shipping\Model\Simplexml\Element::class
116  );
117  $this->assertInstanceOf(\Magento\Shipping\Model\Simplexml\Element::class, $customSimpleXmlElement);
118  }
119 
124  public function testParseXmlXXEXml()
125  {
126  $xmlString = '<!DOCTYPE scan [
127  <!ENTITY test SYSTEM "php://filter/read=convert.base64-encode/resource='
128  . __DIR__ . '/AbstractCarrierOnline/xxe-xml.txt">]><scan>&test;</scan>';
129 
130  $xmlElement = $this->carrier->parseXml($xmlString);
131 
132  // @codingStandardsIgnoreLine
133  echo $xmlElement->asXML();
134  }
135 
140  public function testParseXmlXQBXml()
141  {
142  $xmlString = '<?xml version="1.0"?>
143  <!DOCTYPE test [
144  <!ENTITY value "value">
145  <!ENTITY value1 "&value;&value;&value;&value;&value;&value;&value;&value;&value;&value;">
146  <!ENTITY value2 "&value1;&value1;&value1;&value1;&value1;&value1;&value1;&value1;&value1;&value1;">
147  ]>
148  <test>&value2;</test>';
149 
150  $this->carrier->parseXml($xmlString);
151  }
152 }
defined('TESTS_BP')||define('TESTS_BP' __DIR__
Definition: _bootstrap.php:60