123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194 |
- <?php
- /**
- * Copyright © Magento, Inc. All rights reserved.
- * See COPYING.txt for license details.
- */
- namespace Magento\Security\Test\Unit\Block\Adminhtml\Session;
- use Magento\Framework\HTTP\PhpEnvironment\RemoteAddress;
- use Magento\Framework\TestFramework\Unit\Helper\ObjectManager;
- use Magento\Security\Model\ConfigInterface;
- /**
- * Test class for \Magento\Security\Block\Adminhtml\Session\Activity testing
- *
- * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
- */
- class ActivityTest extends \PHPUnit\Framework\TestCase
- {
- /**
- * @var \Magento\Security\Block\Adminhtml\Session\Activity
- */
- protected $block;
- /**
- * @var \Magento\Security\Model\AdminSessionsManager
- */
- protected $sessionsManager;
- /**
- * @var \Magento\Security\Model\ResourceModel\AdminSessionInfo\CollectionFactory
- */
- protected $sessionsInfoCollection;
- /**
- * @var ConfigInterface
- */
- protected $securityConfig;
- /**
- * @var \Magento\Security\Model\ResourceModel\AdminSessionInfo\Collection
- */
- protected $collectionMock;
- /**
- * @var \Magento\Security\Model\AdminSessionInfo
- */
- protected $sessionMock;
- /**
- * @var \Magento\Framework\Stdlib\DateTime\TimezoneInterface
- */
- protected $localeDate;
- /**
- * @var \Magento\Framework\TestFramework\Unit\Helper\ObjectManager
- */
- protected $objectManager;
- /*
- * @var RemoteAddress
- */
- protected $remoteAddressMock;
- /**
- * Init mocks for tests
- *
- * @return void
- */
- public function setUp()
- {
- $this->objectManager = new ObjectManager($this);
- $this->sessionsInfoCollection = $this->createPartialMock(
- \Magento\Security\Model\ResourceModel\AdminSessionInfo\CollectionFactory::class,
- ['create']
- );
- $this->sessionsManager = $this->createPartialMock(
- \Magento\Security\Model\AdminSessionsManager::class,
- ['getSessionsForCurrentUser']
- );
- $this->securityConfig = $this->getMockBuilder(\Magento\Security\Model\ConfigInterface::class)
- ->disableOriginalConstructor()
- ->getMock();
- $this->sessionMock = $this->createMock(\Magento\Security\Model\AdminSessionInfo::class);
- $this->localeDate = $this->getMockForAbstractClass(
- \Magento\Framework\Stdlib\DateTime\TimezoneInterface::class,
- ['formatDateTime'],
- '',
- false
- );
- $this->collectionMock = $this->createPartialMock(
- \Magento\Security\Model\ResourceModel\AdminSessionInfo\Collection::class,
- ['count', 'is_null']
- );
- $this->remoteAddressMock = $this->getMockBuilder(RemoteAddress::class)
- ->disableOriginalConstructor()
- ->getMock();
- $this->block = $this->objectManager->getObject(
- \Magento\Security\Block\Adminhtml\Session\Activity::class,
- [
- 'sessionsManager' => $this->sessionsManager,
- 'securityConfig' => $this->securityConfig,
- 'localeDate' => $this->localeDate,
- 'remoteAddress' => $this->remoteAddressMock
- ]
- );
- }
- /**
- * @return void
- */
- public function testSessionInfoCollectionIsEmpty()
- {
- $this->sessionsManager->expects($this->once())
- ->method('getSessionsForCurrentUser')
- ->willReturn($this->collectionMock);
- $this->assertInstanceOf(
- \Magento\Security\Model\ResourceModel\AdminSessionInfo\Collection::class,
- $this->block->getSessionInfoCollection()
- );
- }
- /**
- * @param bool $expectedResult
- * @param int $sessionsNumber
- * @dataProvider dataProviderAreMultipleSessionsActive
- */
- public function testAreMultipleSessionsActive($expectedResult, $sessionsNumber)
- {
- $this->sessionsManager->expects($this->once())
- ->method('getSessionsForCurrentUser')
- ->willReturn($this->collectionMock);
- $this->collectionMock->expects($this->any())
- ->method('count')
- ->willReturn($sessionsNumber);
- $this->assertEquals($expectedResult, $this->block->areMultipleSessionsActive());
- }
- /**
- * @return array
- */
- public function dataProviderAreMultipleSessionsActive()
- {
- return [
- ['expectedResult' => false, 'sessionsNumber' => 0],
- ['expectedResult' => false, 'sessionsNumber' => 1],
- ['expectedResult' => true, 'sessionsNumber' => 2],
- ];
- }
- /**
- * @return void
- */
- public function testGetRemoteIp()
- {
- $this->remoteAddressMock->expects($this->once())
- ->method('getRemoteAddress')
- ->with(false);
- $this->block->getRemoteIp();
- }
- /**
- * @param string $timeString
- * @dataProvider dataProviderTime
- */
- public function testFormatDateTime($timeString)
- {
- $time = new \DateTime($timeString);
- $this->localeDate->expects($this->any())
- ->method('formatDateTime')
- ->with($time, \IntlDateFormatter::MEDIUM, \IntlDateFormatter::MEDIUM)
- ->willReturn($time);
- $this->assertEquals($time, $this->block->formatDateTime($timeString));
- }
- /**
- * @return array
- */
- public function dataProviderTime()
- {
- return [
- ['timeString' => '2015-12-28 13:00:00'],
- ['timeString' => '2015-12-23 01:10:37']
- ];
- }
- }
|