ActivityTest.php 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Security\Test\Unit\Block\Adminhtml\Session;
  7. use Magento\Framework\HTTP\PhpEnvironment\RemoteAddress;
  8. use Magento\Framework\TestFramework\Unit\Helper\ObjectManager;
  9. use Magento\Security\Model\ConfigInterface;
  10. /**
  11. * Test class for \Magento\Security\Block\Adminhtml\Session\Activity testing
  12. *
  13. * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
  14. */
  15. class ActivityTest extends \PHPUnit\Framework\TestCase
  16. {
  17. /**
  18. * @var \Magento\Security\Block\Adminhtml\Session\Activity
  19. */
  20. protected $block;
  21. /**
  22. * @var \Magento\Security\Model\AdminSessionsManager
  23. */
  24. protected $sessionsManager;
  25. /**
  26. * @var \Magento\Security\Model\ResourceModel\AdminSessionInfo\CollectionFactory
  27. */
  28. protected $sessionsInfoCollection;
  29. /**
  30. * @var ConfigInterface
  31. */
  32. protected $securityConfig;
  33. /**
  34. * @var \Magento\Security\Model\ResourceModel\AdminSessionInfo\Collection
  35. */
  36. protected $collectionMock;
  37. /**
  38. * @var \Magento\Security\Model\AdminSessionInfo
  39. */
  40. protected $sessionMock;
  41. /**
  42. * @var \Magento\Framework\Stdlib\DateTime\TimezoneInterface
  43. */
  44. protected $localeDate;
  45. /**
  46. * @var \Magento\Framework\TestFramework\Unit\Helper\ObjectManager
  47. */
  48. protected $objectManager;
  49. /*
  50. * @var RemoteAddress
  51. */
  52. protected $remoteAddressMock;
  53. /**
  54. * Init mocks for tests
  55. *
  56. * @return void
  57. */
  58. public function setUp()
  59. {
  60. $this->objectManager = new ObjectManager($this);
  61. $this->sessionsInfoCollection = $this->createPartialMock(
  62. \Magento\Security\Model\ResourceModel\AdminSessionInfo\CollectionFactory::class,
  63. ['create']
  64. );
  65. $this->sessionsManager = $this->createPartialMock(
  66. \Magento\Security\Model\AdminSessionsManager::class,
  67. ['getSessionsForCurrentUser']
  68. );
  69. $this->securityConfig = $this->getMockBuilder(\Magento\Security\Model\ConfigInterface::class)
  70. ->disableOriginalConstructor()
  71. ->getMock();
  72. $this->sessionMock = $this->createMock(\Magento\Security\Model\AdminSessionInfo::class);
  73. $this->localeDate = $this->getMockForAbstractClass(
  74. \Magento\Framework\Stdlib\DateTime\TimezoneInterface::class,
  75. ['formatDateTime'],
  76. '',
  77. false
  78. );
  79. $this->collectionMock = $this->createPartialMock(
  80. \Magento\Security\Model\ResourceModel\AdminSessionInfo\Collection::class,
  81. ['count', 'is_null']
  82. );
  83. $this->remoteAddressMock = $this->getMockBuilder(RemoteAddress::class)
  84. ->disableOriginalConstructor()
  85. ->getMock();
  86. $this->block = $this->objectManager->getObject(
  87. \Magento\Security\Block\Adminhtml\Session\Activity::class,
  88. [
  89. 'sessionsManager' => $this->sessionsManager,
  90. 'securityConfig' => $this->securityConfig,
  91. 'localeDate' => $this->localeDate,
  92. 'remoteAddress' => $this->remoteAddressMock
  93. ]
  94. );
  95. }
  96. /**
  97. * @return void
  98. */
  99. public function testSessionInfoCollectionIsEmpty()
  100. {
  101. $this->sessionsManager->expects($this->once())
  102. ->method('getSessionsForCurrentUser')
  103. ->willReturn($this->collectionMock);
  104. $this->assertInstanceOf(
  105. \Magento\Security\Model\ResourceModel\AdminSessionInfo\Collection::class,
  106. $this->block->getSessionInfoCollection()
  107. );
  108. }
  109. /**
  110. * @param bool $expectedResult
  111. * @param int $sessionsNumber
  112. * @dataProvider dataProviderAreMultipleSessionsActive
  113. */
  114. public function testAreMultipleSessionsActive($expectedResult, $sessionsNumber)
  115. {
  116. $this->sessionsManager->expects($this->once())
  117. ->method('getSessionsForCurrentUser')
  118. ->willReturn($this->collectionMock);
  119. $this->collectionMock->expects($this->any())
  120. ->method('count')
  121. ->willReturn($sessionsNumber);
  122. $this->assertEquals($expectedResult, $this->block->areMultipleSessionsActive());
  123. }
  124. /**
  125. * @return array
  126. */
  127. public function dataProviderAreMultipleSessionsActive()
  128. {
  129. return [
  130. ['expectedResult' => false, 'sessionsNumber' => 0],
  131. ['expectedResult' => false, 'sessionsNumber' => 1],
  132. ['expectedResult' => true, 'sessionsNumber' => 2],
  133. ];
  134. }
  135. /**
  136. * @return void
  137. */
  138. public function testGetRemoteIp()
  139. {
  140. $this->remoteAddressMock->expects($this->once())
  141. ->method('getRemoteAddress')
  142. ->with(false);
  143. $this->block->getRemoteIp();
  144. }
  145. /**
  146. * @param string $timeString
  147. * @dataProvider dataProviderTime
  148. */
  149. public function testFormatDateTime($timeString)
  150. {
  151. $time = new \DateTime($timeString);
  152. $this->localeDate->expects($this->any())
  153. ->method('formatDateTime')
  154. ->with($time, \IntlDateFormatter::MEDIUM, \IntlDateFormatter::MEDIUM)
  155. ->willReturn($time);
  156. $this->assertEquals($time, $this->block->formatDateTime($timeString));
  157. }
  158. /**
  159. * @return array
  160. */
  161. public function dataProviderTime()
  162. {
  163. return [
  164. ['timeString' => '2015-12-28 13:00:00'],
  165. ['timeString' => '2015-12-23 01:10:37']
  166. ];
  167. }
  168. }