AdminSessionInfoTest.php 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  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;
  7. use Magento\Framework\Stdlib\DateTime\DateTime;
  8. use Magento\Framework\TestFramework\Unit\Helper\ObjectManager;
  9. use Magento\Security\Model\ConfigInterface;
  10. /**
  11. * Test class for \Magento\Security\Model\AdminSessionInfo testing
  12. */
  13. class AdminSessionInfoTest extends \PHPUnit\Framework\TestCase
  14. {
  15. /**
  16. * @var \Magento\Security\Model\AdminSessionInfo
  17. */
  18. protected $model;
  19. /**
  20. * @var \PHPUnit_Framework_MockObject_MockObject | ConfigInterface
  21. */
  22. protected $securityConfigMock;
  23. /**
  24. * @var DateTime
  25. */
  26. protected $dateTimeMock;
  27. /**
  28. * @var \Magento\Framework\TestFramework\Unit\Helper\ObjectManager
  29. */
  30. protected $objectManager;
  31. /**
  32. * Init mocks for tests
  33. * @return void
  34. */
  35. public function setUp()
  36. {
  37. $this->objectManager = new ObjectManager($this);
  38. $this->securityConfigMock = $this->getMockBuilder(\Magento\Security\Model\ConfigInterface::class)
  39. ->disableOriginalConstructor()
  40. ->getMock();
  41. $this->dateTimeMock = $this->getMockBuilder(DateTime::class)
  42. ->disableOriginalConstructor()
  43. ->getMock();
  44. $this->model = $this->objectManager->getObject(
  45. \Magento\Security\Model\AdminSessionInfo::class,
  46. [
  47. 'securityConfig' => $this->securityConfigMock,
  48. 'dateTime' => $this->dateTimeMock,
  49. ]
  50. );
  51. }
  52. /**
  53. * @return void
  54. */
  55. public function testIsLoggedInStatus()
  56. {
  57. $this->model->setData('status', \Magento\Security\Model\AdminSessionInfo::LOGGED_IN);
  58. $this->model->setUpdatedAt(901);
  59. $this->securityConfigMock->expects($this->once())->method('getAdminSessionLifetime')->willReturn(100);
  60. $this->dateTimeMock->expects($this->once())
  61. ->method('gmtTimestamp')
  62. ->willReturn(1000);
  63. $this->assertEquals(true, $this->model->isLoggedInStatus());
  64. }
  65. /**
  66. * @return void
  67. */
  68. public function testIsLoggedInStatusExpired()
  69. {
  70. $this->model->setData('status', \Magento\Security\Model\AdminSessionInfo::LOGGED_IN);
  71. $this->model->setUpdatedAt(899);
  72. $this->securityConfigMock->expects($this->once())->method('getAdminSessionLifetime')->willReturn(100);
  73. $this->dateTimeMock->expects($this->once())
  74. ->method('gmtTimestamp')
  75. ->willReturn(1000);
  76. $this->assertEquals(false, $this->model->isLoggedInStatus());
  77. $this->assertEquals(\Magento\Security\Model\AdminSessionInfo::LOGGED_OUT, $this->model->getStatus());
  78. }
  79. /**
  80. * @param bool $expectedResult
  81. * @param string $sessionLifetime
  82. * @dataProvider dataProviderSessionLifetime
  83. */
  84. public function testSessionExpired($expectedResult, $sessionLifetime)
  85. {
  86. $timestamp = time();
  87. $this->securityConfigMock->expects($this->once())
  88. ->method('getAdminSessionLifetime')
  89. ->will($this->returnValue($sessionLifetime));
  90. $this->dateTimeMock->expects($this->once())
  91. ->method('gmtTimestamp')
  92. ->willReturn($timestamp);
  93. $this->model->setUpdatedAt(
  94. date("Y-m-d H:i:s", $timestamp - 1)
  95. );
  96. $this->assertEquals($expectedResult, $this->model->isSessionExpired());
  97. }
  98. /**
  99. * @return array
  100. */
  101. public function dataProviderSessionLifetime()
  102. {
  103. return [
  104. ['expectedResult' => true, 'sessionLifetime' => '0'],
  105. ['expectedResult' => true, 'sessionLifetime' => '1'],
  106. ['expectedResult' => false, 'sessionLifetime' => '2']
  107. ];
  108. }
  109. /**
  110. * @return void
  111. */
  112. public function testGetFormattedIp()
  113. {
  114. $formattedIp = '127.0.0.1';
  115. $this->model->setIp($formattedIp);
  116. $this->assertEquals($formattedIp, $this->model->getFormattedIp());
  117. }
  118. /**
  119. * @return void
  120. */
  121. public function testIsOtherSessionsTerminated()
  122. {
  123. $this->assertEquals(false, $this->model->isOtherSessionsTerminated());
  124. }
  125. /**
  126. * @param bool $isOtherSessionsTerminated
  127. * @dataProvider dataProviderIsOtherSessionsTerminated
  128. */
  129. public function testSetIsOtherSessionsTerminated($isOtherSessionsTerminated)
  130. {
  131. $this->assertInstanceOf(
  132. \Magento\Security\Model\AdminSessionInfo::class,
  133. $this->model->setIsOtherSessionsTerminated($isOtherSessionsTerminated)
  134. );
  135. }
  136. /**
  137. * @return array
  138. */
  139. public function dataProviderIsOtherSessionsTerminated()
  140. {
  141. return [[true], [false]];
  142. }
  143. }