FeedTest.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\AdminNotification\Test\Unit\Model;
  7. use Magento\Framework\Config\ConfigOptionsListConstants;
  8. use Magento\Framework\TestFramework\Unit\Helper\ObjectManager as ObjectManagerHelper;
  9. /**
  10. * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
  11. */
  12. class FeedTest extends \PHPUnit\Framework\TestCase
  13. {
  14. /** @var \Magento\AdminNotification\Model\Feed */
  15. protected $feed;
  16. /** @var ObjectManagerHelper */
  17. protected $objectManagerHelper;
  18. /** @var \Magento\AdminNotification\Model\InboxFactory|\PHPUnit_Framework_MockObject_MockObject */
  19. protected $inboxFactory;
  20. /** @var \Magento\AdminNotification\Model\Inbox|\PHPUnit_Framework_MockObject_MockObject */
  21. protected $inboxModel;
  22. /** @var \Magento\Framework\HTTP\Adapter\CurlFactory|\PHPUnit_Framework_MockObject_MockObject */
  23. protected $curlFactory;
  24. /** @var \Magento\Framework\HTTP\Adapter\Curl|\PHPUnit_Framework_MockObject_MockObject */
  25. protected $curl;
  26. /** @var \Magento\Backend\App\ConfigInterface|\PHPUnit_Framework_MockObject_MockObject */
  27. protected $backendConfig;
  28. /** @var \Magento\Framework\App\CacheInterface|\PHPUnit_Framework_MockObject_MockObject */
  29. protected $cacheManager;
  30. /** @var \Magento\Framework\App\State|\PHPUnit_Framework_MockObject_MockObject */
  31. protected $appState;
  32. /** @var \Magento\Framework\App\DeploymentConfig|\PHPUnit_Framework_MockObject_MockObject */
  33. protected $deploymentConfig;
  34. /** @var \Magento\Framework\App\ProductMetadata|\PHPUnit_Framework_MockObject_MockObject */
  35. protected $productMetadata;
  36. /** @var \Magento\Framework\UrlInterface|\PHPUnit_Framework_MockObject_MockObject */
  37. protected $urlBuilder;
  38. protected function setUp()
  39. {
  40. $this->inboxFactory = $this->createPartialMock(
  41. \Magento\AdminNotification\Model\InboxFactory::class,
  42. ['create']
  43. );
  44. $this->curlFactory = $this->createPartialMock(\Magento\Framework\HTTP\Adapter\CurlFactory::class, ['create']);
  45. $this->curl = $this->getMockBuilder(\Magento\Framework\HTTP\Adapter\Curl::class)
  46. ->disableOriginalConstructor()->getMock();
  47. $this->appState = $this->createPartialMock(\Magento\Framework\App\State::class, ['getInstallDate']);
  48. $this->inboxModel = $this->createPartialMock(\Magento\AdminNotification\Model\Inbox::class, [
  49. '__wakeup',
  50. 'parse'
  51. ]);
  52. $this->backendConfig = $this->createPartialMock(
  53. \Magento\Backend\App\ConfigInterface::class,
  54. [
  55. 'getValue',
  56. 'setValue',
  57. 'isSetFlag'
  58. ]
  59. );
  60. $this->cacheManager = $this->createPartialMock(
  61. \Magento\Framework\App\CacheInterface::class,
  62. [
  63. 'load',
  64. 'getFrontend',
  65. 'remove',
  66. 'save',
  67. 'clean'
  68. ]
  69. );
  70. $this->deploymentConfig = $this->getMockBuilder(\Magento\Framework\App\DeploymentConfig::class)
  71. ->disableOriginalConstructor()->getMock();
  72. $this->objectManagerHelper = new ObjectManagerHelper($this);
  73. $this->productMetadata = $this->getMockBuilder(\Magento\Framework\App\ProductMetadata::class)
  74. ->disableOriginalConstructor()->getMock();
  75. $this->urlBuilder = $this->createMock(\Magento\Framework\UrlInterface::class);
  76. $this->feed = $this->objectManagerHelper->getObject(
  77. \Magento\AdminNotification\Model\Feed::class,
  78. [
  79. 'backendConfig' => $this->backendConfig,
  80. 'cacheManager' => $this->cacheManager,
  81. 'inboxFactory' => $this->inboxFactory,
  82. 'appState' => $this->appState,
  83. 'curlFactory' => $this->curlFactory,
  84. 'deploymentConfig' => $this->deploymentConfig,
  85. 'productMetadata' => $this->productMetadata,
  86. 'urlBuilder' => $this->urlBuilder
  87. ]
  88. );
  89. }
  90. /**
  91. * @dataProvider checkUpdateDataProvider
  92. * @param bool $callInbox
  93. * @param string $curlRequest
  94. */
  95. public function testCheckUpdate($callInbox, $curlRequest)
  96. {
  97. $mockName = 'Test Product Name';
  98. $mockVersion = '0.0.0';
  99. $mockEdition = 'Test Edition';
  100. $mockUrl = 'http://test-url';
  101. $this->productMetadata->expects($this->once())->method('getName')->willReturn($mockName);
  102. $this->productMetadata->expects($this->once())->method('getVersion')->willReturn($mockVersion);
  103. $this->productMetadata->expects($this->once())->method('getEdition')->willReturn($mockEdition);
  104. $this->urlBuilder->expects($this->once())->method('getUrl')->with('*/*/*')->willReturn($mockUrl);
  105. $configValues = [
  106. 'timeout' => 2,
  107. 'useragent' => $mockName . '/' . $mockVersion . ' (' . $mockEdition . ')',
  108. 'referer' => $mockUrl
  109. ];
  110. $lastUpdate = 0;
  111. $this->cacheManager->expects($this->once())->method('load')->will(($this->returnValue($lastUpdate)));
  112. $this->curlFactory->expects($this->at(0))->method('create')->will($this->returnValue($this->curl));
  113. $this->curl->expects($this->once())->method('setConfig')->with($configValues)->willReturnSelf();
  114. $this->curl->expects($this->once())->method('read')->will($this->returnValue($curlRequest));
  115. $this->backendConfig->expects($this->at(0))->method('getValue')->will($this->returnValue('1'));
  116. $this->backendConfig->expects($this->once())->method('isSetFlag')->will($this->returnValue(false));
  117. $this->backendConfig->expects($this->at(1))->method('getValue')
  118. ->will($this->returnValue('http://feed.magento.com'));
  119. $this->deploymentConfig->expects($this->once())->method('get')
  120. ->with(ConfigOptionsListConstants::CONFIG_PATH_INSTALL_DATE)
  121. ->will($this->returnValue('Sat, 6 Sep 2014 16:46:11 UTC'));
  122. if ($callInbox) {
  123. $this->inboxFactory->expects($this->once())->method('create')
  124. ->will($this->returnValue($this->inboxModel));
  125. $this->inboxModel->expects($this->once())
  126. ->method('parse')
  127. ->with(
  128. $this->callback(
  129. function ($data) {
  130. $fieldsToCheck = ['title', 'description', 'url'];
  131. return array_reduce(
  132. $fieldsToCheck,
  133. function ($initialValue, $item) use ($data) {
  134. $haystack = $data[0][$item] ?? false;
  135. return $haystack
  136. ? $initialValue && !strpos($haystack, '<') && !strpos($haystack, '>')
  137. : true;
  138. },
  139. true
  140. );
  141. }
  142. )
  143. )
  144. ->will($this->returnSelf());
  145. } else {
  146. $this->inboxFactory->expects($this->never())->method('create');
  147. $this->inboxModel->expects($this->never())->method('parse');
  148. }
  149. $this->feed->checkUpdate();
  150. }
  151. /**
  152. * @return array
  153. */
  154. public function checkUpdateDataProvider()
  155. {
  156. return [
  157. [
  158. true,
  159. 'HEADER
  160. <?xml version="1.0" encoding="utf-8" ?>
  161. <rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom">
  162. <channel>
  163. <title>MagentoCommerce</title>
  164. <item>
  165. <title><![CDATA[Test Title]]></title>
  166. <link><![CDATA[http://magento.com/feed_url]]></link>
  167. <severity>4</severity>
  168. <description><![CDATA[Test Description]]></description>
  169. <pubDate>Tue, 9 Sep 2014 16:46:11 UTC</pubDate>
  170. </item>
  171. </channel>
  172. </rss>',
  173. ],
  174. [
  175. false,
  176. 'HEADER
  177. <?xml version="1.0" encoding="utf-8" ?>
  178. <rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom">
  179. <channel>
  180. <title>MagentoCommerce</title>
  181. <item>
  182. <title><![CDATA[Test Title]]></title>
  183. <link><![CDATA[http://magento.com/feed_url]]></link>
  184. <severity>4</severity>
  185. <description><![CDATA[Test Description]]></description>
  186. <pubDate>Tue, 1 Sep 2014 16:46:11 UTC</pubDate>
  187. </item>
  188. </channel>
  189. </rss>'
  190. ],
  191. [
  192. true,
  193. // @codingStandardsIgnoreStart
  194. 'HEADER
  195. <?xml version="1.0" encoding="utf-8" ?>
  196. <rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom">
  197. <channel>
  198. <title>MagentoCommerce</title>
  199. <item>
  200. <title><![CDATA[<script>alert("Hello!");</script>Test Title]]></title>
  201. <link><![CDATA[http://magento.com/feed_url<script>alert("Hello!");</script>]]></link>
  202. <severity>4</severity>
  203. <description><![CDATA[Test <script>alert("Hello!");</script>Description]]></description>
  204. <pubDate>Tue, 20 Jun 2017 13:14:47 UTC</pubDate>
  205. </item>
  206. </channel>
  207. </rss>'
  208. // @codingStandardsIgnoreEnd
  209. ],
  210. ];
  211. }
  212. }