AdminTokenServiceTest.php 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  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;
  7. use Magento\Integration\Model\Oauth\Token;
  8. class AdminTokenServiceTest extends \PHPUnit\Framework\TestCase
  9. {
  10. /** \Magento\Integration\Model\AdminTokenService */
  11. protected $_tokenService;
  12. /** \Magento\Integration\Model\Oauth\TokenFactory|\PHPUnit_Framework_MockObject_MockObject */
  13. protected $_tokenFactoryMock;
  14. /** \Magento\User\Model\User|\PHPUnit_Framework_MockObject_MockObject */
  15. protected $_userModelMock;
  16. /** \Magento\Integration\Model\ResourceModel\Oauth\Token\Collection|\PHPUnit_Framework_MockObject_MockObject */
  17. protected $_tokenModelCollectionMock;
  18. /** \Magento\Integration\Model\ResourceModel\Oauth\Token\CollectionFactory
  19. * |\PHPUnit_Framework_MockObject_MockObject */
  20. protected $_tokenModelCollectionFactoryMock;
  21. /** @var \Magento\Integration\Model\CredentialsValidator|\PHPUnit_Framework_MockObject_MockObject */
  22. protected $validatorHelperMock;
  23. /** @var \Magento\Integration\Model\Oauth\Token|\PHPUnit_Framework_MockObject_MockObject */
  24. private $_tokenMock;
  25. protected function setUp()
  26. {
  27. $this->_tokenFactoryMock = $this->getMockBuilder(\Magento\Integration\Model\Oauth\TokenFactory::class)
  28. ->setMethods(['create'])
  29. ->disableOriginalConstructor()
  30. ->getMock();
  31. $this->_tokenFactoryMock->expects($this->any())->method('create')->will($this->returnValue($this->_tokenMock));
  32. $this->_userModelMock = $this->getMockBuilder(\Magento\User\Model\User::class)
  33. ->disableOriginalConstructor()
  34. ->getMock();
  35. $this->_tokenMock = $this->getMockBuilder(\Magento\Integration\Model\Oauth\Token::class)
  36. ->disableOriginalConstructor()
  37. ->setMethods(['getToken', 'loadByAdminId', 'delete', '__wakeup'])->getMock();
  38. $this->_tokenModelCollectionMock = $this->getMockBuilder(
  39. \Magento\Integration\Model\ResourceModel\Oauth\Token\Collection::class
  40. )->disableOriginalConstructor()->setMethods(
  41. ['addFilterByAdminId', 'getSize', '__wakeup', '_beforeLoad', '_afterLoad', 'getIterator', '_fetchAll']
  42. )->getMock();
  43. $this->_tokenModelCollectionFactoryMock = $this->getMockBuilder(
  44. \Magento\Integration\Model\ResourceModel\Oauth\Token\CollectionFactory::class
  45. )->setMethods(['create'])->disableOriginalConstructor()->getMock();
  46. $this->_tokenModelCollectionFactoryMock->expects($this->once())
  47. ->method('create')
  48. ->will($this->returnValue($this->_tokenModelCollectionMock));
  49. $this->validatorHelperMock = $this->getMockBuilder(
  50. \Magento\Integration\Model\CredentialsValidator::class
  51. )->disableOriginalConstructor()->getMock();
  52. $this->_tokenService = new \Magento\Integration\Model\AdminTokenService(
  53. $this->_tokenFactoryMock,
  54. $this->_userModelMock,
  55. $this->_tokenModelCollectionFactoryMock,
  56. $this->validatorHelperMock
  57. );
  58. }
  59. public function testRevokeAdminAccessToken()
  60. {
  61. $adminId = 1;
  62. $this->_tokenModelCollectionMock->expects($this->once())
  63. ->method('addFilterByAdminId')
  64. ->with($adminId)
  65. ->will($this->returnValue($this->_tokenModelCollectionMock));
  66. $this->_tokenModelCollectionMock->expects($this->any())
  67. ->method('getSize')
  68. ->will($this->returnValue(1));
  69. $this->_tokenModelCollectionMock->expects($this->once())
  70. ->method('getIterator')
  71. ->will($this->returnValue(new \ArrayIterator([$this->_tokenMock])));
  72. $this->_tokenModelCollectionMock->expects($this->any())
  73. ->method('_fetchAll')
  74. ->with(null)
  75. ->will($this->returnValue(1));
  76. $this->_tokenMock->expects($this->once())
  77. ->method('delete')
  78. ->will($this->returnValue($this->_tokenMock));
  79. $this->assertTrue($this->_tokenService->revokeAdminAccessToken($adminId));
  80. }
  81. /**
  82. * @expectedException \Magento\Framework\Exception\LocalizedException
  83. * @expectedExceptionMessage This user has no tokens.
  84. */
  85. public function testRevokeAdminAccessTokenWithoutAdminId()
  86. {
  87. $this->_tokenModelCollectionMock->expects($this->once())
  88. ->method('addFilterByAdminId')
  89. ->with(null)
  90. ->will($this->returnValue($this->_tokenModelCollectionMock));
  91. $this->_tokenMock->expects($this->never())
  92. ->method('delete')
  93. ->will($this->returnValue($this->_tokenMock));
  94. $this->_tokenService->revokeAdminAccessToken(null);
  95. }
  96. /**
  97. * @expectedException \Magento\Framework\Exception\LocalizedException
  98. * @expectedExceptionMessage The tokens couldn't be revoked.
  99. */
  100. public function testRevokeAdminAccessTokenCannotRevoked()
  101. {
  102. $exception = new \Exception();
  103. $adminId = 1;
  104. $this->_tokenModelCollectionMock->expects($this->once())
  105. ->method('addFilterByAdminId')
  106. ->with($adminId)
  107. ->will($this->returnValue($this->_tokenModelCollectionMock));
  108. $this->_tokenModelCollectionMock->expects($this->once())
  109. ->method('getSize')
  110. ->will($this->returnValue(1));
  111. $this->_tokenModelCollectionMock->expects($this->once())
  112. ->method('getIterator')
  113. ->will($this->returnValue(new \ArrayIterator([$this->_tokenMock])));
  114. $this->_tokenMock->expects($this->once())
  115. ->method('delete')
  116. ->will($this->throwException($exception));
  117. $this->_tokenService->revokeAdminAccessToken($adminId);
  118. }
  119. }