Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
FeedTest.php
Go to the documentation of this file.
1 <?php
8 
11 
15 class FeedTest extends \PHPUnit\Framework\TestCase
16 {
18  protected $feed;
19 
22 
24  protected $inboxFactory;
25 
27  protected $inboxModel;
28 
30  protected $curlFactory;
31 
33  protected $curl;
34 
36  protected $backendConfig;
37 
39  protected $cacheManager;
40 
42  protected $appState;
43 
45  protected $deploymentConfig;
46 
48  protected $productMetadata;
49 
51  protected $urlBuilder;
52 
53  protected function setUp()
54  {
55  $this->inboxFactory = $this->createPartialMock(
56  \Magento\AdminNotification\Model\InboxFactory::class,
57  ['create']
58  );
59  $this->curlFactory = $this->createPartialMock(\Magento\Framework\HTTP\Adapter\CurlFactory::class, ['create']);
60  $this->curl = $this->getMockBuilder(\Magento\Framework\HTTP\Adapter\Curl::class)
61  ->disableOriginalConstructor()->getMock();
62  $this->appState = $this->createPartialMock(\Magento\Framework\App\State::class, ['getInstallDate']);
63  $this->inboxModel = $this->createPartialMock(\Magento\AdminNotification\Model\Inbox::class, [
64  '__wakeup',
65  'parse'
66  ]);
67  $this->backendConfig = $this->createPartialMock(
68  \Magento\Backend\App\ConfigInterface::class,
69  [
70  'getValue',
71  'setValue',
72  'isSetFlag'
73  ]
74  );
75  $this->cacheManager = $this->createPartialMock(
76  \Magento\Framework\App\CacheInterface::class,
77  [
78  'load',
79  'getFrontend',
80  'remove',
81  'save',
82  'clean'
83  ]
84  );
85 
86  $this->deploymentConfig = $this->getMockBuilder(\Magento\Framework\App\DeploymentConfig::class)
87  ->disableOriginalConstructor()->getMock();
88 
89  $this->objectManagerHelper = new ObjectManagerHelper($this);
90 
91  $this->productMetadata = $this->getMockBuilder(\Magento\Framework\App\ProductMetadata::class)
92  ->disableOriginalConstructor()->getMock();
93 
94  $this->urlBuilder = $this->createMock(\Magento\Framework\UrlInterface::class);
95 
96  $this->feed = $this->objectManagerHelper->getObject(
97  \Magento\AdminNotification\Model\Feed::class,
98  [
99  'backendConfig' => $this->backendConfig,
100  'cacheManager' => $this->cacheManager,
101  'inboxFactory' => $this->inboxFactory,
102  'appState' => $this->appState,
103  'curlFactory' => $this->curlFactory,
104  'deploymentConfig' => $this->deploymentConfig,
105  'productMetadata' => $this->productMetadata,
106  'urlBuilder' => $this->urlBuilder
107  ]
108  );
109  }
110 
116  public function testCheckUpdate($callInbox, $curlRequest)
117  {
118  $mockName = 'Test Product Name';
119  $mockVersion = '0.0.0';
120  $mockEdition = 'Test Edition';
121  $mockUrl = 'http://test-url';
122 
123  $this->productMetadata->expects($this->once())->method('getName')->willReturn($mockName);
124  $this->productMetadata->expects($this->once())->method('getVersion')->willReturn($mockVersion);
125  $this->productMetadata->expects($this->once())->method('getEdition')->willReturn($mockEdition);
126  $this->urlBuilder->expects($this->once())->method('getUrl')->with('*/*/*')->willReturn($mockUrl);
127 
128  $configValues = [
129  'timeout' => 2,
130  'useragent' => $mockName . '/' . $mockVersion . ' (' . $mockEdition . ')',
131  'referer' => $mockUrl
132  ];
133 
134  $lastUpdate = 0;
135  $this->cacheManager->expects($this->once())->method('load')->will(($this->returnValue($lastUpdate)));
136  $this->curlFactory->expects($this->at(0))->method('create')->will($this->returnValue($this->curl));
137  $this->curl->expects($this->once())->method('setConfig')->with($configValues)->willReturnSelf();
138  $this->curl->expects($this->once())->method('read')->will($this->returnValue($curlRequest));
139  $this->backendConfig->expects($this->at(0))->method('getValue')->will($this->returnValue('1'));
140  $this->backendConfig->expects($this->once())->method('isSetFlag')->will($this->returnValue(false));
141  $this->backendConfig->expects($this->at(1))->method('getValue')
142  ->will($this->returnValue('http://feed.magento.com'));
143  $this->deploymentConfig->expects($this->once())->method('get')
145  ->will($this->returnValue('Sat, 6 Sep 2014 16:46:11 UTC'));
146  if ($callInbox) {
147  $this->inboxFactory->expects($this->once())->method('create')
148  ->will($this->returnValue($this->inboxModel));
149  $this->inboxModel->expects($this->once())
150  ->method('parse')
151  ->with(
152  $this->callback(
153  function ($data) {
154  $fieldsToCheck = ['title', 'description', 'url'];
155  return array_reduce(
156  $fieldsToCheck,
157  function ($initialValue, $item) use ($data) {
158  $haystack = $data[0][$item] ?? false;
159  return $haystack
160  ? $initialValue && !strpos($haystack, '<') && !strpos($haystack, '>')
161  : true;
162  },
163  true
164  );
165  }
166  )
167  )
168  ->will($this->returnSelf());
169  } else {
170  $this->inboxFactory->expects($this->never())->method('create');
171  $this->inboxModel->expects($this->never())->method('parse');
172  }
173 
174  $this->feed->checkUpdate();
175  }
176 
180  public function checkUpdateDataProvider()
181  {
182  return [
183  [
184  true,
185  'HEADER
186 
187  <?xml version="1.0" encoding="utf-8" ?>
188  <rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom">
189  <channel>
190  <title>MagentoCommerce</title>
191  <item>
192  <title><![CDATA[Test Title]]></title>
193  <link><![CDATA[http://magento.com/feed_url]]></link>
194  <severity>4</severity>
195  <description><![CDATA[Test Description]]></description>
196  <pubDate>Tue, 9 Sep 2014 16:46:11 UTC</pubDate>
197  </item>
198  </channel>
199  </rss>',
200  ],
201  [
202  false,
203  'HEADER
204 
205  <?xml version="1.0" encoding="utf-8" ?>
206  <rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom">
207  <channel>
208  <title>MagentoCommerce</title>
209  <item>
210  <title><![CDATA[Test Title]]></title>
211  <link><![CDATA[http://magento.com/feed_url]]></link>
212  <severity>4</severity>
213  <description><![CDATA[Test Description]]></description>
214  <pubDate>Tue, 1 Sep 2014 16:46:11 UTC</pubDate>
215  </item>
216  </channel>
217  </rss>'
218  ],
219  [
220  true,
221  // @codingStandardsIgnoreStart
222  'HEADER
223 
224  <?xml version="1.0" encoding="utf-8" ?>
225  <rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom">
226  <channel>
227  <title>MagentoCommerce</title>
228  <item>
229  <title><![CDATA[<script>alert("Hello!");</script>Test Title]]></title>
230  <link><![CDATA[http://magento.com/feed_url<script>alert("Hello!");</script>]]></link>
231  <severity>4</severity>
232  <description><![CDATA[Test <script>alert("Hello!");</script>Description]]></description>
233  <pubDate>Tue, 20 Jun 2017 13:14:47 UTC</pubDate>
234  </item>
235  </channel>
236  </rss>'
237  // @codingStandardsIgnoreEnd
238  ],
239  ];
240  }
241 }