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'] ]; } }