AbstractCarrierOnlineTest.php 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Shipping\Test\Unit\Model\Carrier;
  7. use \Magento\Shipping\Model\Carrier\AbstractCarrierOnline;
  8. use Magento\Quote\Model\Quote\Address\RateRequest;
  9. use Magento\Framework\TestFramework\Unit\Helper\ObjectManager as ObjectManagerHelper;
  10. class AbstractCarrierOnlineTest extends \PHPUnit\Framework\TestCase
  11. {
  12. /**
  13. * Test identification number of product
  14. *
  15. * @var int
  16. */
  17. protected $productId = 1;
  18. /**
  19. * @var AbstractCarrierOnline|\PHPUnit_Framework_MockObject_MockObject
  20. */
  21. protected $carrier;
  22. /**
  23. * @var \PHPUnit_Framework_MockObject_MockObject
  24. */
  25. protected $stockRegistry;
  26. /**
  27. * @var \PHPUnit_Framework_MockObject_MockObject
  28. */
  29. protected $stockItemData;
  30. protected function setUp()
  31. {
  32. $this->stockRegistry = $this->createMock(\Magento\CatalogInventory\Model\StockRegistry::class);
  33. $this->stockItemData = $this->createMock(\Magento\CatalogInventory\Model\Stock\Item::class);
  34. $this->stockRegistry->expects($this->any())->method('getStockItem')
  35. ->with($this->productId, 10)
  36. ->will($this->returnValue($this->stockItemData));
  37. $objectManagerHelper = new ObjectManagerHelper($this);
  38. $carrierArgs = $objectManagerHelper->getConstructArguments(
  39. \Magento\Shipping\Model\Carrier\AbstractCarrierOnline::class,
  40. [
  41. 'stockRegistry' => $this->stockRegistry,
  42. 'xmlSecurity' => new \Magento\Framework\Xml\Security(),
  43. ]
  44. );
  45. $this->carrier = $this->getMockBuilder(\Magento\Shipping\Model\Carrier\AbstractCarrierOnline::class)
  46. ->setConstructorArgs($carrierArgs)
  47. ->setMethods(['getConfigData', '_doShipmentRequest', 'collectRates'])
  48. ->getMock();
  49. }
  50. /**
  51. * @covers \Magento\Shipping\Model\Shipping::composePackagesForCarrier
  52. */
  53. public function testComposePackages()
  54. {
  55. $this->carrier->expects($this->any())->method('getConfigData')->will($this->returnCallback(function ($key) {
  56. $configData = [
  57. 'max_package_weight' => 10,
  58. 'showmethod' => 1,
  59. ];
  60. return isset($configData[$key]) ? $configData[$key] : 0;
  61. }));
  62. $product = $this->createMock(\Magento\Catalog\Model\Product::class);
  63. $product->expects($this->any())->method('getId')->will($this->returnValue($this->productId));
  64. $item = $this->getMockBuilder(\Magento\Quote\Model\Quote\Item::class)
  65. ->disableOriginalConstructor()
  66. ->setMethods(['getProduct', 'getQty', 'getWeight', '__wakeup', 'getStore'])
  67. ->getMock();
  68. $item->expects($this->any())->method('getProduct')->will($this->returnValue($product));
  69. $store = $this->createPartialMock(\Magento\Store\Model\Store::class, ['getWebsiteId']);
  70. $store->expects($this->any())
  71. ->method('getWebsiteId')
  72. ->will($this->returnValue(10));
  73. $item->expects($this->any())->method('getStore')->will($this->returnValue($store));
  74. $request = new RateRequest();
  75. $request->setData('all_items', [$item]);
  76. $request->setData('dest_postcode', 1);
  77. /** Testable service calls to CatalogInventory module */
  78. $this->stockRegistry->expects($this->atLeastOnce())->method('getStockItem')->with($this->productId);
  79. $this->stockItemData->expects($this->atLeastOnce())->method('getEnableQtyIncrements')
  80. ->will($this->returnValue(true));
  81. $this->stockItemData->expects($this->atLeastOnce())->method('getQtyIncrements')
  82. ->will($this->returnValue(5));
  83. $this->stockItemData->expects($this->atLeastOnce())->method('getIsQtyDecimal')->will($this->returnValue(true));
  84. $this->stockItemData->expects($this->atLeastOnce())->method('getIsDecimalDivided')
  85. ->will($this->returnValue(true));
  86. $this->carrier->processAdditionalValidation($request);
  87. }
  88. public function testParseXml()
  89. {
  90. $xmlString = "<?xml version=\"1.0\" encoding=\"UTF-8\"?><GetResponse><value>42</value>></GetResponse>";
  91. $simpleXmlElement = $this->carrier->parseXml($xmlString);
  92. $this->assertEquals('GetResponse', $simpleXmlElement->getName());
  93. $this->assertEquals(42, (int)$simpleXmlElement->value);
  94. $this->assertInstanceOf('SimpleXMLElement', $simpleXmlElement);
  95. $customSimpleXmlElement = $this->carrier->parseXml(
  96. $xmlString,
  97. \Magento\Shipping\Model\Simplexml\Element::class
  98. );
  99. $this->assertInstanceOf(\Magento\Shipping\Model\Simplexml\Element::class, $customSimpleXmlElement);
  100. }
  101. /**
  102. * @expectedException \Magento\Framework\Exception\LocalizedException
  103. * @expectedExceptionMessage The security validation of the XML document has failed.
  104. */
  105. public function testParseXmlXXEXml()
  106. {
  107. $xmlString = '<!DOCTYPE scan [
  108. <!ENTITY test SYSTEM "php://filter/read=convert.base64-encode/resource='
  109. . __DIR__ . '/AbstractCarrierOnline/xxe-xml.txt">]><scan>&test;</scan>';
  110. $xmlElement = $this->carrier->parseXml($xmlString);
  111. // @codingStandardsIgnoreLine
  112. echo $xmlElement->asXML();
  113. }
  114. /**
  115. * @expectedException \Magento\Framework\Exception\LocalizedException
  116. * @expectedExceptionMessage The security validation of the XML document has failed.
  117. */
  118. public function testParseXmlXQBXml()
  119. {
  120. $xmlString = '<?xml version="1.0"?>
  121. <!DOCTYPE test [
  122. <!ENTITY value "value">
  123. <!ENTITY value1 "&value;&value;&value;&value;&value;&value;&value;&value;&value;&value;">
  124. <!ENTITY value2 "&value1;&value1;&value1;&value1;&value1;&value1;&value1;&value1;&value1;&value1;">
  125. ]>
  126. <test>&value2;</test>';
  127. $this->carrier->parseXml($xmlString);
  128. }
  129. }