AuthSessionTest.php 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Security\Test\Unit\Model\Plugin;
  7. use Magento\Framework\TestFramework\Unit\Helper\ObjectManager;
  8. use Magento\Security\Model\SecurityCookie;
  9. /**
  10. * Test class for \Magento\Security\Model\Plugin\AuthSession testing
  11. */
  12. class AuthSessionTest extends \PHPUnit\Framework\TestCase
  13. {
  14. /** @var \Magento\Security\Model\Plugin\AuthSession */
  15. protected $model;
  16. /** @var \Magento\Framework\App\RequestInterface */
  17. protected $requestMock;
  18. /** @var \Magento\Framework\Message\ManagerInterface */
  19. protected $messageManagerMock;
  20. /** @var \Magento\Security\Model\AdminSessionsManager */
  21. protected $adminSessionsManagerMock;
  22. /** @var SecurityCookie */
  23. protected $securityCookieMock;
  24. /** @var \Magento\Backend\Model\Auth\Session */
  25. protected $authSessionMock;
  26. /** @var \Magento\Security\Model\AdminSessionInfo */
  27. protected $currentSessionMock;
  28. /** @var \Magento\Framework\TestFramework\Unit\Helper\ObjectManager */
  29. protected $objectManager;
  30. /**
  31. * Init mocks for tests
  32. * @return void
  33. */
  34. public function setUp()
  35. {
  36. $this->objectManager = new ObjectManager($this);
  37. $this->requestMock = $this->getMockForAbstractClass(
  38. \Magento\Framework\App\RequestInterface::class,
  39. ['getParam', 'getModuleName', 'getActionName'],
  40. '',
  41. false
  42. );
  43. $this->messageManagerMock = $this->createMock(\Magento\Framework\Message\ManagerInterface::class);
  44. $this->adminSessionsManagerMock = $this->createPartialMock(
  45. \Magento\Security\Model\AdminSessionsManager::class,
  46. ['getCurrentSession', 'processProlong', 'getLogoutReasonMessage']
  47. );
  48. $this->securityCookieMock = $this->createPartialMock(SecurityCookie::class, ['setLogoutReasonCookie']);
  49. $this->authSessionMock = $this->createPartialMock(\Magento\Backend\Model\Auth\Session::class, ['destroy']);
  50. $this->currentSessionMock = $this->createPartialMock(
  51. \Magento\Security\Model\AdminSessionInfo::class,
  52. ['isLoggedInStatus', 'getStatus', 'isActive']
  53. );
  54. $this->model = $this->objectManager->getObject(
  55. \Magento\Security\Model\Plugin\AuthSession::class,
  56. [
  57. 'request' => $this->requestMock,
  58. 'messageManager' => $this->messageManagerMock,
  59. 'sessionsManager' => $this->adminSessionsManagerMock,
  60. 'securityCookie' => $this->securityCookieMock
  61. ]
  62. );
  63. $this->adminSessionsManagerMock->expects($this->any())
  64. ->method('getCurrentSession')
  65. ->willReturn($this->currentSessionMock);
  66. }
  67. /**
  68. * @return void
  69. */
  70. public function testAroundProlongSessionIsNotActiveAndIsNotAjaxRequest()
  71. {
  72. $result = 'result';
  73. $errorMessage = 'Error Message';
  74. $proceed = function () use ($result) {
  75. return $result;
  76. };
  77. $this->currentSessionMock->expects($this->once())
  78. ->method('isLoggedInStatus')
  79. ->willReturn(false);
  80. $this->authSessionMock->expects($this->once())
  81. ->method('destroy');
  82. $this->requestMock->expects($this->once())
  83. ->method('getParam')
  84. ->with('isAjax')
  85. ->willReturn(false);
  86. $this->adminSessionsManagerMock->expects($this->once())
  87. ->method('getLogoutReasonMessage')
  88. ->willReturn($errorMessage);
  89. $this->messageManagerMock->expects($this->once())
  90. ->method('addErrorMessage')
  91. ->with($errorMessage);
  92. $this->model->aroundProlong($this->authSessionMock, $proceed);
  93. }
  94. /**
  95. * @return void
  96. */
  97. public function testAroundProlongSessionIsNotActiveAndIsAjaxRequest()
  98. {
  99. $result = 'result';
  100. $status = 1;
  101. $proceed = function () use ($result) {
  102. return $result;
  103. };
  104. $this->currentSessionMock->expects($this->any())
  105. ->method('isActive')
  106. ->willReturn(false);
  107. $this->authSessionMock->expects($this->once())
  108. ->method('destroy');
  109. $this->requestMock->expects($this->once())
  110. ->method('getParam')
  111. ->with('isAjax')
  112. ->willReturn(true);
  113. $this->currentSessionMock->expects($this->once())
  114. ->method('getStatus')
  115. ->willReturn($status);
  116. $this->securityCookieMock->expects($this->once())
  117. ->method('setLogoutReasonCookie')
  118. ->with($status)
  119. ->willReturnSelf();
  120. $this->model->aroundProlong($this->authSessionMock, $proceed);
  121. }
  122. /**
  123. * @return void
  124. */
  125. public function testAroundProlongSessionIsActive()
  126. {
  127. $result = 'result';
  128. $proceed = function () use ($result) {
  129. return $result;
  130. };
  131. $this->currentSessionMock->expects($this->any())
  132. ->method('isLoggedInStatus')
  133. ->willReturn(true);
  134. $this->adminSessionsManagerMock->expects($this->any())
  135. ->method('processProlong');
  136. $this->assertEquals($result, $this->model->aroundProlong($this->authSessionMock, $proceed));
  137. }
  138. }