inboxFactory = $this->createPartialMock( \Magento\AdminNotification\Model\InboxFactory::class, ['create'] ); $this->curlFactory = $this->createPartialMock(\Magento\Framework\HTTP\Adapter\CurlFactory::class, ['create']); $this->curl = $this->getMockBuilder(\Magento\Framework\HTTP\Adapter\Curl::class) ->disableOriginalConstructor()->getMock(); $this->appState = $this->createPartialMock(\Magento\Framework\App\State::class, ['getInstallDate']); $this->inboxModel = $this->createPartialMock(\Magento\AdminNotification\Model\Inbox::class, [ '__wakeup', 'parse' ]); $this->backendConfig = $this->createPartialMock( \Magento\Backend\App\ConfigInterface::class, [ 'getValue', 'setValue', 'isSetFlag' ] ); $this->cacheManager = $this->createPartialMock( \Magento\Framework\App\CacheInterface::class, [ 'load', 'getFrontend', 'remove', 'save', 'clean' ] ); $this->deploymentConfig = $this->getMockBuilder(\Magento\Framework\App\DeploymentConfig::class) ->disableOriginalConstructor()->getMock(); $this->objectManagerHelper = new ObjectManagerHelper($this); $this->productMetadata = $this->getMockBuilder(\Magento\Framework\App\ProductMetadata::class) ->disableOriginalConstructor()->getMock(); $this->urlBuilder = $this->createMock(\Magento\Framework\UrlInterface::class); $this->feed = $this->objectManagerHelper->getObject( \Magento\AdminNotification\Model\Feed::class, [ 'backendConfig' => $this->backendConfig, 'cacheManager' => $this->cacheManager, 'inboxFactory' => $this->inboxFactory, 'appState' => $this->appState, 'curlFactory' => $this->curlFactory, 'deploymentConfig' => $this->deploymentConfig, 'productMetadata' => $this->productMetadata, 'urlBuilder' => $this->urlBuilder ] ); } /** * @dataProvider checkUpdateDataProvider * @param bool $callInbox * @param string $curlRequest */ public function testCheckUpdate($callInbox, $curlRequest) { $mockName = 'Test Product Name'; $mockVersion = '0.0.0'; $mockEdition = 'Test Edition'; $mockUrl = 'http://test-url'; $this->productMetadata->expects($this->once())->method('getName')->willReturn($mockName); $this->productMetadata->expects($this->once())->method('getVersion')->willReturn($mockVersion); $this->productMetadata->expects($this->once())->method('getEdition')->willReturn($mockEdition); $this->urlBuilder->expects($this->once())->method('getUrl')->with('*/*/*')->willReturn($mockUrl); $configValues = [ 'timeout' => 2, 'useragent' => $mockName . '/' . $mockVersion . ' (' . $mockEdition . ')', 'referer' => $mockUrl ]; $lastUpdate = 0; $this->cacheManager->expects($this->once())->method('load')->will(($this->returnValue($lastUpdate))); $this->curlFactory->expects($this->at(0))->method('create')->will($this->returnValue($this->curl)); $this->curl->expects($this->once())->method('setConfig')->with($configValues)->willReturnSelf(); $this->curl->expects($this->once())->method('read')->will($this->returnValue($curlRequest)); $this->backendConfig->expects($this->at(0))->method('getValue')->will($this->returnValue('1')); $this->backendConfig->expects($this->once())->method('isSetFlag')->will($this->returnValue(false)); $this->backendConfig->expects($this->at(1))->method('getValue') ->will($this->returnValue('http://feed.magento.com')); $this->deploymentConfig->expects($this->once())->method('get') ->with(ConfigOptionsListConstants::CONFIG_PATH_INSTALL_DATE) ->will($this->returnValue('Sat, 6 Sep 2014 16:46:11 UTC')); if ($callInbox) { $this->inboxFactory->expects($this->once())->method('create') ->will($this->returnValue($this->inboxModel)); $this->inboxModel->expects($this->once()) ->method('parse') ->with( $this->callback( function ($data) { $fieldsToCheck = ['title', 'description', 'url']; return array_reduce( $fieldsToCheck, function ($initialValue, $item) use ($data) { $haystack = $data[0][$item] ?? false; return $haystack ? $initialValue && !strpos($haystack, '<') && !strpos($haystack, '>') : true; }, true ); } ) ) ->will($this->returnSelf()); } else { $this->inboxFactory->expects($this->never())->method('create'); $this->inboxModel->expects($this->never())->method('parse'); } $this->feed->checkUpdate(); } /** * @return array */ public function checkUpdateDataProvider() { return [ [ true, 'HEADER MagentoCommerce <![CDATA[Test Title]]> 4 Tue, 9 Sep 2014 16:46:11 UTC ', ], [ false, 'HEADER MagentoCommerce <![CDATA[Test Title]]> 4 Tue, 1 Sep 2014 16:46:11 UTC ' ], [ true, // @codingStandardsIgnoreStart 'HEADER MagentoCommerce <![CDATA[<script>alert("Hello!");</script>Test Title]]> alert("Hello!");]]> 4 alert("Hello!");Description]]> Tue, 20 Jun 2017 13:14:47 UTC ' // @codingStandardsIgnoreEnd ], ]; } }