Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
OauthServiceTest.php
Go to the documentation of this file.
1 <?php
10 
12 
16 class OauthServiceTest extends \PHPUnit\Framework\TestCase
17 {
18  const VALUE_CONSUMER_ID = 1;
19 
20  const VALUE_CONSUMER_KEY = 'asdfghjklaqwerfdtyuiomnbgfdhbsoi';
21 
22  const VALUE_TOKEN_TYPE = 'access';
23 
25  protected $_consumerFactory;
26 
29 
31  private $_consumerMock;
32 
34  private $_emptyConsumerMock;
35 
39  private $_tokenMock;
40 
42  private $_service;
43 
45  private $_consumerData;
46 
50  private $_tokenFactoryMock;
51 
55  protected function setUp()
56  {
57  $this->_consumerFactory = $this->getMockBuilder(\Magento\Integration\Model\Oauth\ConsumerFactory::class)
58  ->disableOriginalConstructor()
59  ->setMethods(['create'])
60  ->getMock();
61  $this->_tokenProviderMock = $this->getMockBuilder(
62  \Magento\Integration\Model\Oauth\Token\Provider::class
63  )->disableOriginalConstructor()->getMock();
64  $this->_tokenMock = $this->getMockBuilder(
65  \Magento\Integration\Model\Oauth\Token::class
66  )->disableOriginalConstructor()->setMethods(
67  ['createVerifierToken', 'getType', '__wakeup', 'delete']
68  )->getMock();
69 
70  $this->_tokenFactoryMock = $this->createPartialMock(
71  \Magento\Integration\Model\Oauth\TokenFactory::class,
72  ['create']
73  );
74  $this->_tokenFactoryMock->expects($this->any())->method('create')->will($this->returnValue($this->_tokenMock));
75  $this->_consumerMock = $this->getMockBuilder(
76  \Magento\Integration\Model\Oauth\Consumer::class
77  )->disableOriginalConstructor()->setMethods(
78  ['getData', 'getId', 'load', 'save', 'delete', '__wakeup']
79  )->getMock();
80  $this->_consumerData = [
81  'entity_id' => self::VALUE_CONSUMER_ID,
82  'key' => self::VALUE_CONSUMER_KEY,
83  'secret' => 'iuyytrfdsdfbnnhbmkkjlkjl',
84  'created_at' => '',
85  'updated_at' => '',
86  'callback_url' => '',
87  'rejected_callback_url' => ''
88  ];
89  $this->_consumerFactory->expects(
90  $this->any()
91  )->method(
92  'create'
93  )->will(
94  $this->returnValue($this->_consumerMock)
95  );
96 
97  $this->_service = new \Magento\Integration\Model\OauthService(
98  $this->createMock(\Magento\Store\Model\StoreManagerInterface::class),
99  $this->_consumerFactory,
100  $this->_tokenFactoryMock,
101  $this->createMock(\Magento\Integration\Helper\Oauth\Data::class),
102  $this->createMock(\Magento\Framework\HTTP\ZendClient::class),
103  $this->createMock(\Psr\Log\LoggerInterface::class),
104  $this->createMock(\Magento\Framework\Oauth\Helper\Oauth::class),
105  $this->_tokenProviderMock
106  );
107  $this->_emptyConsumerMock = $this->getMockBuilder(
108  \Magento\Integration\Model\Integration::class
109  )->disableOriginalConstructor()->setMethods(
110  ['getData', 'load', 'getId', 'save', 'delete', '__wakeup']
111  )->getMock();
112  $this->_emptyConsumerMock->expects($this->any())->method('getId')->will($this->returnValue(null));
113  }
114 
118  public function testDelete()
119  {
120  $this->_consumerMock->expects(
121  $this->once()
122  )->method(
123  'getId'
124  )->will(
125  $this->returnValue(self::VALUE_CONSUMER_ID)
126  );
127  $this->_consumerMock->expects(
128  $this->once()
129  )->method(
130  'load'
131  )->with(
132  self::VALUE_CONSUMER_ID
133  )->will(
134  $this->returnValue($this->_consumerMock)
135  );
136  $this->_consumerMock->expects($this->once())->method('delete')->will($this->returnValue($this->_consumerMock));
137  $this->_consumerMock->expects($this->any())->method('getData')->will($this->returnValue($this->_consumerData));
138  $consumerData = $this->_service->deleteConsumer(self::VALUE_CONSUMER_ID);
139  $this->assertEquals($this->_consumerData['entity_id'], $consumerData['entity_id']);
140  }
141 
147  public function testDeleteException()
148  {
149  $this->_consumerMock->expects($this->any())->method('getId')->will($this->returnValue(null));
150  $this->_consumerMock->expects($this->once())->method('load')->will($this->returnSelf());
151  $this->_consumerMock->expects($this->never())->method('delete');
152  $this->_service->deleteConsumer(self::VALUE_CONSUMER_ID);
153  }
154 
159  {
160  $this->_consumerMock->expects(
161  $this->any()
162  )->method(
163  'load'
164  )->with(
165  self::VALUE_CONSUMER_ID
166  )->will(
167  $this->returnValue($this->_consumerMock)
168  );
169 
170  $this->_tokenProviderMock->expects(
171  $this->any()
172  )->method(
173  'getIntegrationTokenByConsumerId'
174  )->will(
175  $this->returnValue($this->_tokenMock)
176  );
177 
178  $this->_tokenProviderMock->expects($this->any())->method('createRequestToken')->with($this->_consumerMock);
179 
180  $this->_tokenProviderMock->expects($this->any())->method('getAccessToken')->with($this->_consumerMock);
181 
182  $this->_tokenFactoryMock->expects($this->any())->method('create')->will($this->returnValue($this->_tokenMock));
183 
184  $this->_tokenMock->expects($this->once())->method('delete');
185 
186  $this->_tokenMock->expects($this->once())->method('createVerifierToken')->with(self::VALUE_CONSUMER_ID);
187 
188  $this->_tokenProviderMock->expects($this->once())->method('createRequestToken')->with($this->_consumerMock);
189 
190  $this->_tokenProviderMock->expects($this->once())->method('getAccessToken')->with($this->_consumerMock);
191 
192  $this->assertTrue($this->_service->createAccessToken(self::VALUE_CONSUMER_ID, true));
193  }
194 
199  {
200  $this->_consumerMock->expects(
201  $this->any()
202  )->method(
203  'load'
204  )->with(
205  self::VALUE_CONSUMER_ID
206  )->will(
207  $this->returnValue($this->_consumerMock)
208  );
209 
210  $this->_tokenProviderMock->expects(
211  $this->any()
212  )->method(
213  'getIntegrationTokenByConsumerId'
214  )->will(
215  $this->returnValue($this->_tokenMock)
216  );
217 
218  $this->_tokenMock->expects($this->never())->method('delete');
219 
220  $this->assertFalse($this->_service->createAccessToken(self::VALUE_CONSUMER_ID, false));
221  }
222 
227  {
228  $this->_consumerMock->expects(
229  $this->any()
230  )->method(
231  'load'
232  )->with(
233  0
234  )->will(
235  $this->returnValue($this->_consumerMock)
236  );
237 
238  $this->_tokenProviderMock->expects(
239  $this->any()
240  )->method(
241  'getIntegrationTokenByConsumerId'
242  )->will(
243  $this->throwException(
244  new \Magento\Framework\Oauth\Exception(
245  __('A token with consumer ID 0 does not exist')
246  )
247  )
248  );
249 
250  $this->_tokenMock->expects($this->never())->method('delete');
251 
252  $this->_tokenFactoryMock->expects(
253  $this->once()
254  )->method(
255  'create'
256  )->will(
257  $this->returnValue($this->_tokenMock)
258  );
259 
260  $this->_tokenMock->expects($this->once())->method('createVerifierToken');
261 
262  $this->_tokenProviderMock->expects($this->once())->method('createRequestToken');
263 
264  $this->_tokenProviderMock->expects($this->once())->method('getAccessToken');
265 
266  $this->assertTrue($this->_service->createAccessToken(0, false));
267  }
268 
272  public function testLoadConsumer()
273  {
274  $this->_consumerMock->expects(
275  $this->once()
276  )->method(
277  'load'
278  )->with(
279  self::VALUE_CONSUMER_ID
280  )->will(
281  $this->returnValue($this->_consumerMock)
282  );
283  $this->_consumerMock->expects($this->any())->method('getData')->will($this->returnValue($this->_consumerData));
284  $consumer = $this->_service->loadConsumer(self::VALUE_CONSUMER_ID);
285  $consumerData = $consumer->getData();
286  $this->assertEquals($this->_consumerData['entity_id'], $consumerData['entity_id']);
287  }
288 
293  public function testLoadConsumerException()
294  {
295  $this->_consumerMock->expects(
296  $this->once()
297  )->method(
298  'load'
299  )->will(
300  $this->throwException(
301  new \Magento\Framework\Oauth\Exception(
302  __(
303  "The oAuth consumer account couldn't be loaded due to an unexpected error. "
304  . "Please try again later."
305  )
306  )
307  )
308  );
309  $this->_service->loadConsumer(self::VALUE_CONSUMER_ID);
310 
311  $this->expectExceptionMessage(
312  "The oAuth consumer account couldn't be loaded due to an unexpected error. Please try again later."
313  );
314  }
315 
319  public function testLoadConsumerByKey()
320  {
321  $this->_consumerMock->expects(
322  $this->once()
323  )->method(
324  'load'
325  )->with(
326  self::VALUE_CONSUMER_KEY,
327  'key'
328  )->will(
329  $this->returnValue($this->_consumerMock)
330  );
331  $this->_consumerMock->expects($this->any())->method('getData')->will($this->returnValue($this->_consumerData));
332  $consumer = $this->_service->loadConsumerByKey(self::VALUE_CONSUMER_KEY);
333  $consumerData = $consumer->getData();
334  $this->assertEquals($this->_consumerData['key'], $consumerData['key']);
335  }
336 
342  {
343  $this->_consumerMock->expects(
344  $this->once()
345  )->method(
346  'load'
347  )->will(
348  $this->throwException(
349  new \Magento\Framework\Oauth\Exception(
350  __(
351  "The oAuth consumer account couldn't be loaded due to an unexpected error. "
352  . "Please try again later."
353  )
354  )
355  )
356  );
357  $this->_service->loadConsumerByKey(self::VALUE_CONSUMER_KEY);
358 
359  $this->expectExceptionMessage(
360  "The oAuth consumer account couldn't be loaded due to an unexpected error. Please try again later."
361  );
362  }
363 
367  public function testDeleteToken()
368  {
369  $this->_consumerMock->expects(
370  $this->any()
371  )->method(
372  'load'
373  )->with(
374  self::VALUE_CONSUMER_ID
375  )->will(
376  $this->returnValue($this->_consumerMock)
377  );
378 
379  $this->_tokenProviderMock->expects(
380  $this->any()
381  )->method(
382  'getIntegrationTokenByConsumerId'
383  )->will(
384  $this->returnValue($this->_tokenMock)
385  );
386 
387  $this->_tokenMock->expects($this->once())->method('delete');
388 
389  $this->assertTrue($this->_service->deleteIntegrationToken(self::VALUE_CONSUMER_ID));
390  }
391 
395  public function testDeleteTokenNegative()
396  {
397  $this->_consumerMock->expects(
398  $this->any()
399  )->method(
400  'load'
401  )->with(
402  self::VALUE_CONSUMER_ID
403  )->will(
404  $this->returnValue($this->_consumerMock)
405  );
406 
407  $this->_tokenProviderMock->expects(
408  $this->any()
409  )->method(
410  'getIntegrationTokenByConsumerId'
411  )->will(
412  $this->returnValue($this->_tokenMock)
413  );
414 
415  $this->_tokenMock->expects($this->never())->method('delete');
416 
417  $this->assertFalse($this->_service->deleteIntegrationToken(null));
418  }
419 
423  public function testGetAccessTokenNoAccess()
424  {
425  $this->_consumerMock->expects(
426  $this->any()
427  )->method(
428  'load'
429  )->with(
430  self::VALUE_CONSUMER_ID
431  )->will(
432  $this->returnValue($this->_consumerMock)
433  );
434 
435  $this->_tokenProviderMock->expects(
436  $this->any()
437  )->method(
438  'getIntegrationTokenByConsumerId'
439  )->will(
440  $this->returnValue($this->_tokenMock)
441  );
442 
443  $this->assertFalse($this->_service->getAccessToken(self::VALUE_CONSUMER_ID), false);
444  }
445 
449  public function testGetAccessSuccess()
450  {
451  $this->_consumerMock->expects(
452  $this->any()
453  )->method(
454  'load'
455  )->with(
456  self::VALUE_CONSUMER_ID
457  )->will(
458  $this->returnValue($this->_consumerMock)
459  );
460 
461  $this->_tokenMock->expects($this->once())->method('getType')->will($this->returnValue(Token::TYPE_ACCESS));
462 
463  $this->_tokenProviderMock->expects(
464  $this->any()
465  )->method(
466  'getIntegrationTokenByConsumerId'
467  )->will(
468  $this->returnValue($this->_tokenMock)
469  );
470 
471  $this->assertEquals($this->_service->getAccessToken(self::VALUE_CONSUMER_ID), $this->_tokenMock);
472  }
473 }
__()
Definition: __.php:13