Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
SecurityManagerTest.php
Go to the documentation of this file.
1 <?php
7 
13 
18 class SecurityManagerTest extends \PHPUnit\Framework\TestCase
19 {
21  protected $model;
22 
25 
28 
31 
34 
37 
39  protected $objectManager;
40 
44  protected $eventManagerMock;
45 
49  protected $dateTimeMock;
50 
51  /*
52  * @var RemoteAddress
53  */
54  protected $remoteAddressMock;
55 
60  public function setUp()
61  {
62  $this->objectManager = new ObjectManager($this);
63 
64  $this->securityConfigMock = $this->getMockBuilder(\Magento\Security\Model\ConfigInterface::class)
65  ->disableOriginalConstructor()
66  ->getMock();
67 
68  $this->passwordResetRequestEventCollectionFactoryMock = $this->createPartialMock(
69  \Magento\Security\Model\ResourceModel\PasswordResetRequestEvent\CollectionFactory::class,
70  ['create']
71  );
72 
73  $this->passwordResetRequestEventCollectionMock = $this->createPartialMock(
74  \Magento\Security\Model\ResourceModel\PasswordResetRequestEvent\Collection::class,
75  ['deleteRecordsOlderThen']
76  );
77 
78  $this->passwordResetRequestEventFactoryMock = $this->createPartialMock(
79  \Magento\Security\Model\PasswordResetRequestEventFactory::class,
80  ['create']
81  );
82 
83  $this->passwordResetRequestEventMock = $this->createPartialMock(
84  \Magento\Security\Model\PasswordResetRequestEvent::class,
85  ['setRequestType', 'setAccountReference', 'setIp', 'save']
86  );
87 
88  $securityChecker = $this->createMock(\Magento\Security\Model\SecurityChecker\SecurityCheckerInterface::class);
89 
90  $this->eventManagerMock = $this->getMockForAbstractClass(
91  \Magento\Framework\Event\ManagerInterface::class,
92  [],
93  '',
94  false,
95  true,
96  true,
97  ['dispatch']
98  );
99 
100  $this->dateTimeMock = $this->getMockBuilder(DateTime::class)
101  ->disableOriginalConstructor()
102  ->getMock();
103 
104  $this->remoteAddressMock = $this->getMockBuilder(RemoteAddress::class)
105  ->disableOriginalConstructor()
106  ->getMock();
107 
108  $this->model = $this->objectManager->getObject(
109  SecurityManager::class,
110  [
111  'securityConfig' => $this->securityConfigMock,
112  'passwordResetRequestEventFactory' => $this->passwordResetRequestEventFactoryMock,
113  'passwordResetRequestEventCollectionFactory' => $this->passwordResetRequestEventCollectionFactoryMock,
114  'eventManager' => $this->eventManagerMock,
115  'securityCheckers' => [$securityChecker],
116  'dateTime' => $this->dateTimeMock,
117  'remoteAddress' => $this->remoteAddressMock
118  ]
119  );
120  }
121 
125  public function testConstructorException()
126  {
127  $securityChecker = $this->createMock(\Magento\Framework\Message\ManagerInterface::class);
128 
129  $this->expectException(\Magento\Framework\Exception\LocalizedException::class);
130  $this->expectExceptionMessage(
131  (string)__('Incorrect Security Checker class. It has to implement SecurityCheckerInterface')
132  );
133 
134  $this->model->__construct(
135  $this->securityConfigMock,
136  $this->passwordResetRequestEventFactoryMock,
137  $this->passwordResetRequestEventCollectionFactoryMock,
138  $this->eventManagerMock,
139  $this->dateTimeMock,
140  $this->remoteAddressMock,
141  [$securityChecker]
142  );
143  }
144 
148  public function testPerformSecurityCheck()
149  {
151  $accountReference = \Magento\Security\Model\Config\Source\ResetMethod::OPTION_BY_IP_AND_EMAIL;
152  $longIp = 12345;
153 
154  $this->remoteAddressMock->expects($this->once())
155  ->method('getRemoteAddress')
156  ->will($this->returnValue($longIp));
157 
158  $this->passwordResetRequestEventFactoryMock->expects($this->once())
159  ->method('create')
160  ->willReturn($this->passwordResetRequestEventMock);
161 
162  $this->passwordResetRequestEventMock->expects($this->once())
163  ->method('setRequestType')
164  ->with($requestType)
165  ->willReturnSelf();
166 
167  $this->passwordResetRequestEventMock->expects($this->once())
168  ->method('setAccountReference')
169  ->with($accountReference)
170  ->willReturnSelf();
171 
172  $this->passwordResetRequestEventMock->expects($this->once())
173  ->method('setIp')
174  ->with($longIp)
175  ->willReturnSelf();
176 
177  $this->passwordResetRequestEventMock->expects($this->once())
178  ->method('save')
179  ->willReturnSelf();
180 
181  $this->model->performSecurityCheck($requestType, $accountReference);
182  }
183 
187  public function testCleanExpiredRecords()
188  {
189  $timestamp = time();
190 
191  $this->passwordResetRequestEventCollectionFactoryMock->expects($this->once())
192  ->method('create')
193  ->willReturn($this->passwordResetRequestEventCollectionMock);
194 
195  $this->dateTimeMock->expects($this->once())
196  ->method('gmtTimestamp')
197  ->willReturn($timestamp);
198 
199  $this->passwordResetRequestEventCollectionMock->expects($this->once())
200  ->method('deleteRecordsOlderThen')
201  ->with(
203  )
204  ->willReturnSelf();
205 
206  $this->model->cleanExpiredRecords();
207  }
208 }
__()
Definition: __.php:13