Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
ActivityTest.php
Go to the documentation of this file.
1 <?php
8 
12 
18 class ActivityTest extends \PHPUnit\Framework\TestCase
19 {
23  protected $block;
24 
28  protected $sessionsManager;
29 
34 
38  protected $securityConfig;
39 
43  protected $collectionMock;
44 
48  protected $sessionMock;
49 
53  protected $localeDate;
54 
58  protected $objectManager;
59 
60  /*
61  * @var RemoteAddress
62  */
63  protected $remoteAddressMock;
64 
70  public function setUp()
71  {
72  $this->objectManager = new ObjectManager($this);
73 
74  $this->sessionsInfoCollection = $this->createPartialMock(
75  \Magento\Security\Model\ResourceModel\AdminSessionInfo\CollectionFactory::class,
76  ['create']
77  );
78 
79  $this->sessionsManager = $this->createPartialMock(
80  \Magento\Security\Model\AdminSessionsManager::class,
81  ['getSessionsForCurrentUser']
82  );
83 
84  $this->securityConfig = $this->getMockBuilder(\Magento\Security\Model\ConfigInterface::class)
85  ->disableOriginalConstructor()
86  ->getMock();
87 
88  $this->sessionMock = $this->createMock(\Magento\Security\Model\AdminSessionInfo::class);
89 
90  $this->localeDate = $this->getMockForAbstractClass(
91  \Magento\Framework\Stdlib\DateTime\TimezoneInterface::class,
92  ['formatDateTime'],
93  '',
94  false
95  );
96 
97  $this->collectionMock = $this->createPartialMock(
98  \Magento\Security\Model\ResourceModel\AdminSessionInfo\Collection::class,
99  ['count', 'is_null']
100  );
101 
102  $this->remoteAddressMock = $this->getMockBuilder(RemoteAddress::class)
103  ->disableOriginalConstructor()
104  ->getMock();
105 
106  $this->block = $this->objectManager->getObject(
107  \Magento\Security\Block\Adminhtml\Session\Activity::class,
108  [
109  'sessionsManager' => $this->sessionsManager,
110  'securityConfig' => $this->securityConfig,
111  'localeDate' => $this->localeDate,
112  'remoteAddress' => $this->remoteAddressMock
113  ]
114  );
115  }
116 
121  {
122  $this->sessionsManager->expects($this->once())
123  ->method('getSessionsForCurrentUser')
124  ->willReturn($this->collectionMock);
125  $this->assertInstanceOf(
126  \Magento\Security\Model\ResourceModel\AdminSessionInfo\Collection::class,
127  $this->block->getSessionInfoCollection()
128  );
129  }
130 
136  public function testAreMultipleSessionsActive($expectedResult, $sessionsNumber)
137  {
138  $this->sessionsManager->expects($this->once())
139  ->method('getSessionsForCurrentUser')
140  ->willReturn($this->collectionMock);
141  $this->collectionMock->expects($this->any())
142  ->method('count')
143  ->willReturn($sessionsNumber);
144  $this->assertEquals($expectedResult, $this->block->areMultipleSessionsActive());
145  }
146 
151  {
152  return [
153  ['expectedResult' => false, 'sessionsNumber' => 0],
154  ['expectedResult' => false, 'sessionsNumber' => 1],
155  ['expectedResult' => true, 'sessionsNumber' => 2],
156  ];
157  }
158 
162  public function testGetRemoteIp()
163  {
164  $this->remoteAddressMock->expects($this->once())
165  ->method('getRemoteAddress')
166  ->with(false);
167  $this->block->getRemoteIp();
168  }
169 
174  public function testFormatDateTime($timeString)
175  {
176  $time = new \DateTime($timeString);
177  $this->localeDate->expects($this->any())
178  ->method('formatDateTime')
179  ->with($time, \IntlDateFormatter::MEDIUM, \IntlDateFormatter::MEDIUM)
180  ->willReturn($time);
181  $this->assertEquals($time, $this->block->formatDateTime($timeString));
182  }
183 
187  public function dataProviderTime()
188  {
189  return [
190  ['timeString' => '2015-12-28 13:00:00'],
191  ['timeString' => '2015-12-23 01:10:37']
192  ];
193  }
194 }