LogoutAllTest.php 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Security\Test\Unit\Controller\Adminhtml\Session;
  7. use Magento\Framework\Exception\LocalizedException;
  8. use Magento\Framework\TestFramework\Unit\Helper\ObjectManager;
  9. /**
  10. * Test class for \Magento\Security\Test\Unit\Controller\Adminhtml\Session\LogoutAll testing
  11. *
  12. * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
  13. */
  14. class LogoutAllTest extends \PHPUnit\Framework\TestCase
  15. {
  16. /**
  17. * @var \Magento\Security\Controller\Adminhtml\Session\LogoutAll
  18. */
  19. protected $controller;
  20. /**
  21. * @var \Magento\Backend\App\Action\Context
  22. */
  23. protected $contextMock;
  24. /**
  25. * @var \Magento\Framework\Message\ManagerInterface
  26. */
  27. protected $messageManager;
  28. /**
  29. * @var \Magento\Backend\Model\Session
  30. */
  31. protected $session;
  32. /**
  33. * @var \Magento\Security\Model\AdminSessionsManager
  34. */
  35. protected $sessionsManager;
  36. /**
  37. * @var \Magento\Framework\App\ActionFlag
  38. */
  39. protected $actionFlagMock;
  40. /**
  41. * @var \Magento\Framework\App\ResponseInterface
  42. */
  43. protected $responseMock;
  44. /**
  45. * @var \Magento\Backend\Helper\Data
  46. */
  47. protected $backendHelperMock;
  48. /**
  49. * @var \Magento\Framework\TestFramework\Unit\Helper\ObjectManager
  50. */
  51. protected $objectManager;
  52. /**
  53. * Init mocks for tests
  54. * @return void
  55. */
  56. public function setUp()
  57. {
  58. $this->objectManager = new ObjectManager($this);
  59. $this->contextMock = $this->getMockBuilder(\Magento\Backend\App\Action\Context::class)
  60. ->disableOriginalConstructor()
  61. ->getMock();
  62. $this->messageManager = $this->getMockBuilder(\Magento\Framework\Message\ManagerInterface::class)
  63. ->disableOriginalConstructor()
  64. ->setMethods(['addSuccessMessage', 'addErrorMessage', 'addExceptionMessage'])
  65. ->getMockForAbstractClass();
  66. $this->contextMock->expects($this->any())
  67. ->method('getMessageManager')
  68. ->willReturn($this->messageManager);
  69. $this->session = $this->getMockBuilder(\Magento\Backend\Model\Session::class)
  70. ->disableOriginalConstructor()
  71. ->setMethods(['setIsUrlNotice'])
  72. ->getMock();
  73. $this->contextMock->expects($this->any())
  74. ->method('getSession')
  75. ->willReturn($this->session);
  76. $this->sessionsManager = $this->createPartialMock(
  77. \Magento\Security\Model\AdminSessionsManager::class,
  78. ['logoutOtherUserSessions']
  79. );
  80. $this->actionFlagMock = $this->getMockBuilder(\Magento\Framework\App\ActionFlag::class)
  81. ->disableOriginalConstructor()
  82. ->setMethods(['get'])
  83. ->getMock();
  84. $this->contextMock->expects($this->any())
  85. ->method('getActionFlag')
  86. ->willReturn($this->actionFlagMock);
  87. $this->responseMock = $this->getMockBuilder(\Magento\Framework\App\ResponseInterface::class)
  88. ->disableOriginalConstructor()
  89. ->setMethods(['setRedirect'])
  90. ->getMockForAbstractClass();
  91. $this->contextMock->expects($this->any())
  92. ->method('getResponse')
  93. ->willReturn($this->responseMock);
  94. $this->backendHelperMock = $this->createPartialMock(\Magento\Backend\Helper\Data::class, ['getUrl']);
  95. $this->contextMock->expects($this->any())
  96. ->method('getHelper')
  97. ->willReturn($this->backendHelperMock);
  98. $this->controller = $this->objectManager->getObject(
  99. \Magento\Security\Controller\Adminhtml\Session\LogoutAll::class,
  100. [
  101. 'context' => $this->contextMock,
  102. 'sessionsManager' => $this->sessionsManager
  103. ]
  104. );
  105. }
  106. /**
  107. * @return void
  108. */
  109. public function testExecute()
  110. {
  111. $successMessage = 'All other open sessions for this account were terminated.';
  112. $this->sessionsManager->expects($this->once())
  113. ->method('logoutOtherUserSessions');
  114. $this->messageManager->expects($this->once())
  115. ->method('addSuccessMessage')
  116. ->with($successMessage);
  117. $this->messageManager->expects($this->never())
  118. ->method('addErrorMessage');
  119. $this->messageManager->expects($this->never())
  120. ->method('addExceptionMessage');
  121. $this->responseMock->expects($this->once())
  122. ->method('setRedirect');
  123. $this->actionFlagMock->expects($this->once())
  124. ->method('get')
  125. ->with('', \Magento\Backend\App\AbstractAction::FLAG_IS_URLS_CHECKED);
  126. $this->backendHelperMock->expects($this->once())
  127. ->method('getUrl');
  128. $this->controller->execute();
  129. }
  130. /**
  131. * @return void
  132. */
  133. public function testExecuteLocalizedException()
  134. {
  135. $phrase = new \Magento\Framework\Phrase('some error');
  136. $this->sessionsManager->expects($this->once())
  137. ->method('logoutOtherUserSessions')
  138. ->willThrowException(new LocalizedException($phrase));
  139. $this->messageManager->expects($this->once())
  140. ->method('addErrorMessage')
  141. ->with($phrase);
  142. $this->controller->execute();
  143. }
  144. /**
  145. * @return void
  146. */
  147. public function testExecuteException()
  148. {
  149. $phrase = new \Magento\Framework\Phrase('We couldn\'t logout because of an error.');
  150. $this->sessionsManager->expects($this->once())
  151. ->method('logoutOtherUserSessions')
  152. ->willThrowException(new \Exception());
  153. $this->messageManager->expects($this->once())
  154. ->method('addExceptionMessage')
  155. ->with(new \Exception(), $phrase);
  156. $this->controller->execute();
  157. }
  158. }