contextMock = $this->getMockBuilder(\Magento\Framework\App\Helper\Context::class) ->disableOriginalConstructor() ->getMock(); $this->storeManagerMock = $this->getMockBuilder(\Magento\Store\Model\StoreManagerInterface::class) ->disableOriginalConstructor() ->getMock(); $this->salesConfigMock = $this->getMockBuilder(\Magento\Sales\Model\Config::class) ->disableOriginalConstructor() ->getMock(); $this->priceCurrency = $this->getMockBuilder( \Magento\Framework\Pricing\PriceCurrencyInterface::class )->getMock(); $this->escaperMock = $this->getMockBuilder(\Magento\Framework\Escaper::class) ->disableOriginalConstructor() ->getMock(); $this->adminHelper = (new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this))->getObject( \Magento\Sales\Helper\Admin::class, [ 'context' => $this->contextMock, 'storeManager' => $this->storeManagerMock, 'salesConfig' => $this->salesConfigMock, 'priceCurrency' => $this->priceCurrency, 'escaper' => $this->escaperMock ] ); $this->magentoObjectMock = $this->getMockBuilder(\Magento\Framework\DataObject::class) ->disableOriginalConstructor() ->setMethods(['getOrder', 'getData']) ->getMock(); $this->orderMock = $this->getMockBuilder(\Magento\Sales\Model\Order::class) ->disableOriginalConstructor() ->getMock(); $this->orderMock->expects($this->any()) ->method('formatBasePrice') ->will($this->returnValue('formattedBasePrice')); $this->orderMock->expects($this->any()) ->method('formatPrice') ->will($this->returnValue('formattedPrice')); $this->orderMock->expects($this->any()) ->method('getData') ->will($this->returnValue('data')); } /** * @param string $expected * @param bool $dataObjectIsOrder * @param bool $isCurrencyDifferent * @param bool $magentoDataObjectHasOrder * @param bool $strong * @param string $separator * @dataProvider displayPricesDataProvider */ public function testDisplayPrices( $expected, $dataObjectIsOrder, $isCurrencyDifferent = true, $magentoDataObjectHasOrder = true, $strong = false, $separator = '
' ) { $this->orderMock->expects($this->any()) ->method('isCurrencyDifferent') ->will($this->returnValue($isCurrencyDifferent)); $storeMock = $this->getMockBuilder(\Magento\Store\Model\Store::class) ->disableOriginalConstructor() ->getMock(); $this->storeManagerMock->expects($this->any()) ->method('getStore') ->will($this->returnValue($storeMock)); $this->priceCurrency->expects($this->any()) ->method('format') ->will($this->returnValue('storeFormattedPrice')); $dataObject = $this->orderMock; if (!$dataObjectIsOrder) { $returnRes = false; if ($magentoDataObjectHasOrder) { $returnRes = $this->orderMock; } $this->magentoObjectMock->expects($this->once()) ->method('getOrder') ->will($this->returnValue($returnRes)); $dataObject = $this->magentoObjectMock; } $basePrice = 10.00; $price = 15.00; $this->assertEquals( $expected, $this->adminHelper->displayPrices($dataObject, $basePrice, $price, $strong, $separator) ); } /** * @param string $expected * @param bool $dataObjectIsOrder * @param bool $isCurrencyDifferent * @param bool $magentoDataObjectHasOrder * @param bool $strong * @param string $separator * @dataProvider displayPricesDataProvider */ public function testDisplayPriceAttribute( $expected, $dataObjectIsOrder, $isCurrencyDifferent = true, $magentoDataObjectHasOrder = true, $strong = false, $separator = '
' ) { $this->orderMock->expects($this->any()) ->method('isCurrencyDifferent') ->will($this->returnValue($isCurrencyDifferent)); $storeMock = $this->getMockBuilder(\Magento\Store\Model\Store::class) ->disableOriginalConstructor() ->getMock(); $this->storeManagerMock->expects($this->any()) ->method('getStore') ->will($this->returnValue($storeMock)); $this->priceCurrency->expects($this->any()) ->method('format') ->will($this->returnValue('storeFormattedPrice')); $dataObject = $this->orderMock; if (!$dataObjectIsOrder) { $returnRes = false; if ($magentoDataObjectHasOrder) { $returnRes = $this->orderMock; } $this->magentoObjectMock->expects($this->once()) ->method('getOrder') ->will($this->returnValue($returnRes)); $this->magentoObjectMock->expects($this->any()) ->method('getData') ->will($this->returnValue('data')); $dataObject = $this->magentoObjectMock; } $this->assertEquals( $expected, $this->adminHelper->displayPriceAttribute($dataObject, 'code', $strong, $separator) ); } /** * @return array */ public function displayPricesDataProvider() { return [ [ 'formattedBasePrice
[formattedPrice]', true, ], [ 'formattedBasePrice
[formattedPrice]', false, ], [ 'formattedPrice', true, false, ], [ 'formattedPrice', false, false, ], [ 'formattedPrice', true, false, true, true, ], [ 'formattedPrice', true, false, true, true, 'seperator', ], [ 'formattedBasePriceseperator[formattedPrice]', true, true, true, true, 'seperator', ], [ 'storeFormattedPrice', false, false, false, false, 'seperator', ], [ 'storeFormattedPrice', false, false, false, true, 'seperator', ], ]; } /** * @param string $itemKey * @param string $type * @param int $calledTimes * @dataProvider applySalableProductTypesFilterDataProvider */ public function testApplySalableProductTypesFilter($itemKey, $type, $calledTimes) { $productMock = $this->getMockBuilder(\Magento\Catalog\Model\Product::class) ->disableOriginalConstructor() ->getMock(); $productMock->expects($this->any()) ->method('getTypeId') ->will($this->returnValue($type)); $orderMock = $this->getMockBuilder(\Magento\Sales\Model\Order\Item::class) ->disableOriginalConstructor() ->setMethods(['__wakeup', 'getProductType']) ->getMock(); $orderMock->expects($this->any()) ->method('getProductType') ->will($this->returnValue($type)); $quoteMock = $this->getMockBuilder(\Magento\Quote\Model\Quote\Item::class) ->disableOriginalConstructor() ->getMock(); $quoteMock->expects($this->any()) ->method('getProductType') ->will($this->returnValue($type)); $items = [ 'product' => $productMock, 'order' => $orderMock, 'quote' => $quoteMock, 'other' => 'other', ]; $collectionClassName = \Magento\Framework\Model\ResourceModel\Db\Collection\AbstractCollection::class; $collectionMock = $this->getMockBuilder($collectionClassName) ->disableOriginalConstructor() ->getMock(); $collectionMock->expects($this->any()) ->method('getItems') ->will($this->returnValue([$items[$itemKey]])); $collectionMock->expects($this->exactly($calledTimes)) ->method('removeItemByKey'); $this->salesConfigMock->expects($this->any()) ->method('getAvailableProductTypes') ->will($this->returnValue(['validProductType'])); $this->adminHelper->applySalableProductTypesFilter($collectionMock); } /** * @return array */ public function applySalableProductTypesFilterDataProvider() { return [ ['product', 'validProductType', 0], ['product', 'invalidProductType', 1], ['order', 'validProductType', 0], ['order', 'invalidProductType', 1], ['quote', 'validProductType', 0], ['quote', 'invalidProductType', 1], ['other', 'validProductType', 1], ]; } /** * @param string $data * @param string $expected * @param null|array $allowedTags * @dataProvider escapeHtmlWithLinksDataProvider */ public function testEscapeHtmlWithLinks($data, $expected, $allowedTags = null) { $this->escaperMock ->expects($this->any()) ->method('escapeHtml') ->will($this->returnValue($expected)); $actual = $this->adminHelper->escapeHtmlWithLinks($data, $allowedTags); $this->assertEquals($expected, $actual); } /** * @return array */ public function escapeHtmlWithLinksDataProvider() { return [ [ 'some text in tags', '<a>some text in tags</a>', 'allowedTags' => null ], [ 'Transaction ID: "XX123XX"', 'Transaction ID: "XX123XX"', 'allowedTags' => ['b', 'br', 'strong', 'i', 'u', 'a'] ], [ 'some text in tags', 'some text in tags', 'allowedTags' => ['a'] ], 'Not replacement with placeholders' => [ "", '<script>alert(1)</script>', 'allowedTags' => ['a'] ], 'Normal usage, url escaped' => [ 'Foo', 'Foo', 'allowedTags' => ['a'] ], 'Normal usage, url not escaped' => [ "Foo", 'Foo', 'allowedTags' => ['a'] ], 'XSS test' => [ "Foo", 'Foo', 'allowedTags' => ['a'] ], 'Additional regex test' => [ "Foo", 'Foo', 'allowedTags' => ['a'] ], 'Break of valid urls' => [ "Foo", 'Foo', 'allowedTags' => ['a'] ], ]; } }