AuthTest.php 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  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. /**
  9. * Test class for \Magento\Security\Model\Plugin\Auth testing
  10. */
  11. class AuthTest extends \PHPUnit\Framework\TestCase
  12. {
  13. /**
  14. * @var \Magento\Security\Model\Plugin\Auth
  15. */
  16. protected $model;
  17. /**
  18. * @var \Magento\Security\Model\AdminSessionsManager
  19. */
  20. protected $sessionsManager;
  21. /**
  22. * @var \Magento\Framework\Message\ManagerInterface
  23. */
  24. protected $messageManager;
  25. /**
  26. * @var \Magento\Security\Model\AdminSessionInfo
  27. */
  28. protected $currentSession;
  29. /**
  30. * @var \Magento\Backend\Model\Auth
  31. */
  32. protected $authMock;
  33. /**
  34. * @var \Magento\Framework\TestFramework\Unit\Helper\ObjectManager
  35. */
  36. protected $objectManager;
  37. /**
  38. * Init mocks for tests
  39. * @return void
  40. */
  41. public function setUp()
  42. {
  43. $this->objectManager = new ObjectManager($this);
  44. $this->sessionsManager = $this->createPartialMock(
  45. \Magento\Security\Model\AdminSessionsManager::class,
  46. ['processLogin', 'processLogout', 'getCurrentSession']
  47. );
  48. $this->messageManager = $this->getMockForAbstractClass(
  49. \Magento\Framework\Message\ManagerInterface::class,
  50. ['addWarning'],
  51. '',
  52. false
  53. );
  54. $this->currentSession = $this->createPartialMock(
  55. \Magento\Security\Model\AdminSessionInfo::class,
  56. ['isOtherSessionsTerminated']
  57. );
  58. $this->authMock = $this->createMock(\Magento\Backend\Model\Auth::class);
  59. $this->model = $this->objectManager->getObject(
  60. \Magento\Security\Model\Plugin\Auth::class,
  61. [
  62. 'sessionsManager' => $this->sessionsManager,
  63. 'messageManager' =>$this->messageManager
  64. ]
  65. );
  66. }
  67. /**
  68. * @return void
  69. */
  70. public function testAfterLogin()
  71. {
  72. $warningMessage = __('All other open sessions for this account were terminated.');
  73. $this->sessionsManager->expects($this->once())
  74. ->method('processLogin');
  75. $this->sessionsManager->expects($this->once())
  76. ->method('getCurrentSession')
  77. ->willReturn($this->currentSession);
  78. $this->currentSession->expects($this->once())
  79. ->method('isOtherSessionsTerminated')
  80. ->willReturn(true);
  81. $this->messageManager->expects($this->once())
  82. ->method('addWarning')
  83. ->with($warningMessage);
  84. $this->model->afterLogin($this->authMock);
  85. }
  86. /**
  87. * @return void
  88. */
  89. public function testBeforeLogout()
  90. {
  91. $this->sessionsManager->expects($this->once())->method('processLogout');
  92. $this->model->beforeLogout($this->authMock);
  93. }
  94. }