123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180 |
- <?php
- /**
- * Copyright © Magento, Inc. All rights reserved.
- * See COPYING.txt for license details.
- */
- namespace Magento\Security\Test\Unit\Controller\Adminhtml\Session;
- use Magento\Framework\Exception\LocalizedException;
- use Magento\Framework\TestFramework\Unit\Helper\ObjectManager;
- /**
- * Test class for \Magento\Security\Test\Unit\Controller\Adminhtml\Session\LogoutAll testing
- *
- * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
- */
- class LogoutAllTest extends \PHPUnit\Framework\TestCase
- {
- /**
- * @var \Magento\Security\Controller\Adminhtml\Session\LogoutAll
- */
- protected $controller;
- /**
- * @var \Magento\Backend\App\Action\Context
- */
- protected $contextMock;
- /**
- * @var \Magento\Framework\Message\ManagerInterface
- */
- protected $messageManager;
- /**
- * @var \Magento\Backend\Model\Session
- */
- protected $session;
- /**
- * @var \Magento\Security\Model\AdminSessionsManager
- */
- protected $sessionsManager;
- /**
- * @var \Magento\Framework\App\ActionFlag
- */
- protected $actionFlagMock;
- /**
- * @var \Magento\Framework\App\ResponseInterface
- */
- protected $responseMock;
- /**
- * @var \Magento\Backend\Helper\Data
- */
- protected $backendHelperMock;
- /**
- * @var \Magento\Framework\TestFramework\Unit\Helper\ObjectManager
- */
- protected $objectManager;
- /**
- * Init mocks for tests
- * @return void
- */
- public function setUp()
- {
- $this->objectManager = new ObjectManager($this);
- $this->contextMock = $this->getMockBuilder(\Magento\Backend\App\Action\Context::class)
- ->disableOriginalConstructor()
- ->getMock();
- $this->messageManager = $this->getMockBuilder(\Magento\Framework\Message\ManagerInterface::class)
- ->disableOriginalConstructor()
- ->setMethods(['addSuccessMessage', 'addErrorMessage', 'addExceptionMessage'])
- ->getMockForAbstractClass();
- $this->contextMock->expects($this->any())
- ->method('getMessageManager')
- ->willReturn($this->messageManager);
- $this->session = $this->getMockBuilder(\Magento\Backend\Model\Session::class)
- ->disableOriginalConstructor()
- ->setMethods(['setIsUrlNotice'])
- ->getMock();
- $this->contextMock->expects($this->any())
- ->method('getSession')
- ->willReturn($this->session);
- $this->sessionsManager = $this->createPartialMock(
- \Magento\Security\Model\AdminSessionsManager::class,
- ['logoutOtherUserSessions']
- );
- $this->actionFlagMock = $this->getMockBuilder(\Magento\Framework\App\ActionFlag::class)
- ->disableOriginalConstructor()
- ->setMethods(['get'])
- ->getMock();
- $this->contextMock->expects($this->any())
- ->method('getActionFlag')
- ->willReturn($this->actionFlagMock);
- $this->responseMock = $this->getMockBuilder(\Magento\Framework\App\ResponseInterface::class)
- ->disableOriginalConstructor()
- ->setMethods(['setRedirect'])
- ->getMockForAbstractClass();
- $this->contextMock->expects($this->any())
- ->method('getResponse')
- ->willReturn($this->responseMock);
- $this->backendHelperMock = $this->createPartialMock(\Magento\Backend\Helper\Data::class, ['getUrl']);
- $this->contextMock->expects($this->any())
- ->method('getHelper')
- ->willReturn($this->backendHelperMock);
- $this->controller = $this->objectManager->getObject(
- \Magento\Security\Controller\Adminhtml\Session\LogoutAll::class,
- [
- 'context' => $this->contextMock,
- 'sessionsManager' => $this->sessionsManager
- ]
- );
- }
- /**
- * @return void
- */
- public function testExecute()
- {
- $successMessage = 'All other open sessions for this account were terminated.';
- $this->sessionsManager->expects($this->once())
- ->method('logoutOtherUserSessions');
- $this->messageManager->expects($this->once())
- ->method('addSuccessMessage')
- ->with($successMessage);
- $this->messageManager->expects($this->never())
- ->method('addErrorMessage');
- $this->messageManager->expects($this->never())
- ->method('addExceptionMessage');
- $this->responseMock->expects($this->once())
- ->method('setRedirect');
- $this->actionFlagMock->expects($this->once())
- ->method('get')
- ->with('', \Magento\Backend\App\AbstractAction::FLAG_IS_URLS_CHECKED);
- $this->backendHelperMock->expects($this->once())
- ->method('getUrl');
- $this->controller->execute();
- }
- /**
- * @return void
- */
- public function testExecuteLocalizedException()
- {
- $phrase = new \Magento\Framework\Phrase('some error');
- $this->sessionsManager->expects($this->once())
- ->method('logoutOtherUserSessions')
- ->willThrowException(new LocalizedException($phrase));
- $this->messageManager->expects($this->once())
- ->method('addErrorMessage')
- ->with($phrase);
- $this->controller->execute();
- }
- /**
- * @return void
- */
- public function testExecuteException()
- {
- $phrase = new \Magento\Framework\Phrase('We couldn\'t logout because of an error.');
- $this->sessionsManager->expects($this->once())
- ->method('logoutOtherUserSessions')
- ->willThrowException(new \Exception());
- $this->messageManager->expects($this->once())
- ->method('addExceptionMessage')
- ->with(new \Exception(), $phrase);
- $this->controller->execute();
- }
- }
|