ConsumerTest.php 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Integration\Test\Unit\Model\Oauth;
  7. use Magento\Framework\Url\Validator as UrlValidator;
  8. use Zend\Validator\Uri as ZendUriValidator;
  9. use Magento\Integration\Model\Oauth\Consumer\Validator\KeyLength;
  10. /**
  11. * Test for \Magento\Integration\Model\Oauth\Consumer
  12. * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
  13. */
  14. class ConsumerTest extends \PHPUnit\Framework\TestCase
  15. {
  16. /**
  17. * @var \Magento\Integration\Model\Oauth\Consumer
  18. */
  19. protected $consumerModel;
  20. /**
  21. * @var \Magento\Framework\Model\Context|\PHPUnit_Framework_MockObject_MockObject
  22. */
  23. protected $contextMock;
  24. /**
  25. * @var \Magento\Framework\Registry|\PHPUnit_Framework_MockObject_MockObject
  26. */
  27. protected $registryMock;
  28. /**
  29. * @var KeyLength
  30. */
  31. protected $keyLengthValidator;
  32. /**
  33. * @var \Magento\Integration\Model\Oauth\Consumer\Validator\KeyLengthFactory
  34. */
  35. protected $keyLengthValidatorFactory;
  36. /**
  37. * @var UrlValidator
  38. */
  39. protected $urlValidator;
  40. /**
  41. * @var \Magento\Integration\Helper\Oauth\Data|\PHPUnit_Framework_MockObject_MockObject
  42. */
  43. protected $oauthDataMock;
  44. /**
  45. * @var \Magento\Framework\Model\ResourceModel\AbstractResource|\PHPUnit_Framework_MockObject_MockObject
  46. */
  47. protected $resourceMock;
  48. /**
  49. * @var \Magento\Framework\Data\Collection\AbstractDb|\PHPUnit_Framework_MockObject_MockObject
  50. */
  51. protected $resourceCollectionMock;
  52. /**
  53. * @var array
  54. */
  55. protected $validDataArray;
  56. protected function setUp()
  57. {
  58. $this->contextMock = $this->createPartialMock(\Magento\Framework\Model\Context::class, ['getEventDispatcher']);
  59. $eventManagerMock = $this->getMockForAbstractClass(
  60. \Magento\Framework\Event\ManagerInterface::class,
  61. [],
  62. '',
  63. false,
  64. true,
  65. true,
  66. ['dispatch']
  67. );
  68. $this->contextMock->expects($this->once())
  69. ->method('getEventDispatcher')
  70. ->will($this->returnValue($eventManagerMock));
  71. $this->registryMock = $this->createMock(\Magento\Framework\Registry::class);
  72. $this->keyLengthValidator = new KeyLength();
  73. $this->urlValidator = new UrlValidator(new ZendUriValidator());
  74. $this->oauthDataMock = $this->createPartialMock(
  75. \Magento\Integration\Helper\Oauth\Data::class,
  76. ['getConsumerExpirationPeriod']
  77. );
  78. $this->oauthDataMock->expects($this->any())
  79. ->method('getConsumerExpirationPeriod')
  80. ->will($this->returnValue(\Magento\Integration\Helper\Oauth\Data::CONSUMER_EXPIRATION_PERIOD_DEFAULT));
  81. $this->resourceMock = $this->createPartialMock(
  82. \Magento\Integration\Model\ResourceModel\Oauth\Consumer::class,
  83. ['getIdFieldName', 'selectByCompositeKey', 'deleteOldEntries']
  84. );
  85. $this->resourceCollectionMock = $this->createMock(\Magento\Framework\Data\Collection\AbstractDb::class);
  86. $this->consumerModel = new \Magento\Integration\Model\Oauth\Consumer(
  87. $this->contextMock,
  88. $this->registryMock,
  89. $this->keyLengthValidator,
  90. $this->urlValidator,
  91. $this->oauthDataMock,
  92. $this->resourceMock,
  93. $this->resourceCollectionMock
  94. );
  95. $this->validDataArray = [
  96. 'key' => md5(uniqid()),
  97. 'secret' => md5(uniqid()),
  98. 'callback_url' => 'http://example.com/callback',
  99. 'rejected_callback_url' => 'http://example.com/rejectedCallback'
  100. ];
  101. }
  102. public function testBeforeSave()
  103. {
  104. try {
  105. $this->consumerModel->setData($this->validDataArray);
  106. $this->consumerModel->beforeSave();
  107. } catch (\Exception $e) {
  108. $this->fail('Exception not expected for beforeSave with valid data.');
  109. }
  110. }
  111. public function testValidate()
  112. {
  113. $this->consumerModel->setData($this->validDataArray);
  114. $this->assertTrue($this->consumerModel->validate());
  115. }
  116. /**
  117. * @expectedException \Magento\Framework\Exception\LocalizedException
  118. * @expectedExceptionMessage Invalid Callback URL
  119. */
  120. public function testValidateInvalidData()
  121. {
  122. $this->validDataArray['callback_url'] = 'invalid';
  123. $this->consumerModel->setData($this->validDataArray);
  124. $this->consumerModel->validate();
  125. }
  126. /**
  127. * @expectedException \Magento\Framework\Exception\LocalizedException
  128. * @expectedExceptionMessage Invalid Callback URL
  129. */
  130. public function testValidateInvalidCallback()
  131. {
  132. $this->validDataArray['callback_url'] = 'invalid';
  133. $this->consumerModel->setData($this->validDataArray);
  134. $this->consumerModel->validate();
  135. }
  136. /**
  137. * @expectedException \Magento\Framework\Exception\LocalizedException
  138. * @expectedExceptionMessage Invalid Rejected Callback URL
  139. */
  140. public function testValidateInvalidRejectedCallback()
  141. {
  142. $this->validDataArray['rejected_callback_url'] = 'invalid';
  143. $this->consumerModel->setData($this->validDataArray);
  144. $this->consumerModel->validate();
  145. }
  146. /**
  147. * @expectedException \Magento\Framework\Exception\LocalizedException
  148. * @expectedExceptionMessage Consumer Key 'invalid' is less than 32 characters long
  149. */
  150. public function testValidateInvalidConsumerKey()
  151. {
  152. $this->validDataArray['key'] = 'invalid';
  153. $this->consumerModel->setData($this->validDataArray);
  154. $this->consumerModel->validate();
  155. }
  156. /**
  157. * @expectedException \Magento\Framework\Exception\LocalizedException
  158. * @expectedExceptionMessage Consumer Secret 'invalid' is less than 32 characters long
  159. */
  160. public function testValidateInvalidConsumerSecret()
  161. {
  162. $this->validDataArray['secret'] = 'invalid';
  163. $this->consumerModel->setData($this->validDataArray);
  164. $this->consumerModel->validate();
  165. }
  166. public function testGetConsumerExpirationPeriodValid()
  167. {
  168. $dateHelperMock = $this->getMockBuilder(\Magento\Framework\Stdlib\DateTime\DateTime::class)
  169. ->disableOriginalConstructor()
  170. ->getMock();
  171. $dateHelperMock->expects($this->at(0))->method('gmtTimestamp')->willReturn(time());
  172. $dateHelperMock->expects($this->at(1))->method('gmtTimestamp')->willReturn(time() - 100);
  173. $dateHelper = new \ReflectionProperty(\Magento\Integration\Model\Oauth\Consumer::class, '_dateHelper');
  174. $dateHelper->setAccessible(true);
  175. $dateHelper->setValue($this->consumerModel, $dateHelperMock);
  176. $this->consumerModel->setUpdatedAt(time());
  177. $this->assertTrue($this->consumerModel->isValidForTokenExchange());
  178. }
  179. public function testGetConsumerExpirationPeriodExpired()
  180. {
  181. $dateHelperMock = $this->getMockBuilder(\Magento\Framework\Stdlib\DateTime\DateTime::class)
  182. ->disableOriginalConstructor()
  183. ->getMock();
  184. $dateHelperMock->expects($this->at(0))->method('gmtTimestamp')->willReturn(time());
  185. $dateHelperMock->expects($this->at(1))->method('gmtTimestamp')->willReturn(time() - 1000);
  186. $dateHelper = new \ReflectionProperty(\Magento\Integration\Model\Oauth\Consumer::class, '_dateHelper');
  187. $dateHelper->setAccessible(true);
  188. $dateHelper->setValue($this->consumerModel, $dateHelperMock);
  189. $this->consumerModel->setUpdatedAt(time());
  190. $this->assertFalse($this->consumerModel->isValidForTokenExchange());
  191. }
  192. }