@@ -127,6 +127,21 @@ class AccountManagementTest extends \PHPUnit\Framework\TestCase
127
127
*/
128
128
private $ accountConfirmation ;
129
129
130
+ /**
131
+ * @var \PHPUnit_Framework_MockObject_MockObject|\Magento\Framework\Session\SessionManagerInterface
132
+ */
133
+ private $ sessionManager ;
134
+
135
+ /**
136
+ * @var \PHPUnit_Framework_MockObject_MockObject|\Magento\Customer\Model\ResourceModel\Visitor\CollectionFactory
137
+ */
138
+ private $ visitorCollectionFactory ;
139
+
140
+ /**
141
+ * @var \PHPUnit_Framework_MockObject_MockObject|\Magento\Framework\Session\SaveHandlerInterface
142
+ */
143
+ private $ saveHandler ;
144
+
130
145
/**
131
146
* @SuppressWarnings(PHPMD.ExcessiveMethodLength)
132
147
*/
@@ -179,6 +194,19 @@ protected function setUp()
179
194
$ this ->dateTimeFactory = $ this ->createMock (DateTimeFactory::class);
180
195
$ this ->accountConfirmation = $ this ->createMock (AccountConfirmation::class);
181
196
197
+ $ this ->visitorCollectionFactory = $ this ->getMockBuilder (
198
+ \Magento \Customer \Model \ResourceModel \Visitor \CollectionFactory::class
199
+ )
200
+ ->disableOriginalConstructor ()
201
+ ->setMethods (['create ' ])
202
+ ->getMock ();
203
+ $ this ->sessionManager = $ this ->getMockBuilder (\Magento \Framework \Session \SessionManagerInterface::class)
204
+ ->disableOriginalConstructor ()
205
+ ->getMock ();
206
+ $ this ->saveHandler = $ this ->getMockBuilder (\Magento \Framework \Session \SaveHandlerInterface::class)
207
+ ->disableOriginalConstructor ()
208
+ ->getMock ();
209
+
182
210
$ this ->objectManagerHelper = new ObjectManagerHelper ($ this );
183
211
$ this ->accountManagement = $ this ->objectManagerHelper ->getObject (
184
212
\Magento \Customer \Model \AccountManagement::class,
@@ -207,7 +235,10 @@ protected function setUp()
207
235
'objectFactory ' => $ this ->objectFactory ,
208
236
'extensibleDataObjectConverter ' => $ this ->extensibleDataObjectConverter ,
209
237
'dateTimeFactory ' => $ this ->dateTimeFactory ,
210
- 'accountConfirmation ' => $ this ->accountConfirmation
238
+ 'accountConfirmation ' => $ this ->accountConfirmation ,
239
+ 'sessionManager ' => $ this ->sessionManager ,
240
+ 'saveHandler ' => $ this ->saveHandler ,
241
+ 'visitorCollectionFactory ' => $ this ->visitorCollectionFactory ,
211
242
]
212
243
);
213
244
$ reflection = new \ReflectionClass (get_class ($ this ->accountManagement ));
@@ -1276,7 +1307,16 @@ private function reInitModel()
1276
1307
{
1277
1308
$ this ->customerSecure = $ this ->getMockBuilder (\Magento \Customer \Model \Data \CustomerSecure::class)
1278
1309
->disableOriginalConstructor ()
1279
- ->setMethods (['getRpToken ' , 'getRpTokenCreatedAt ' ])
1310
+ ->setMethods (
1311
+ [
1312
+ 'getRpToken ' ,
1313
+ 'getRpTokenCreatedAt ' ,
1314
+ 'getPasswordHash ' ,
1315
+ 'setPasswordHash ' ,
1316
+ 'setRpToken ' ,
1317
+ 'setRpTokenCreatedAt ' ,
1318
+ ]
1319
+ )
1280
1320
->getMock ();
1281
1321
1282
1322
$ this ->customerSecure
@@ -1297,6 +1337,48 @@ private function reInitModel()
1297
1337
->getMock ();
1298
1338
1299
1339
$ this ->prepareDateTimeFactory ();
1340
+ $ this ->sessionManager = $ this ->getMockBuilder (\Magento \Framework \Session \SessionManagerInterface::class)
1341
+ ->disableOriginalConstructor ()
1342
+ ->setMethods (['destroy ' , 'start ' , 'writeClose ' ])
1343
+ ->getMockForAbstractClass ();
1344
+ $ this ->visitorCollectionFactory = $ this ->getMockBuilder (
1345
+ \Magento \Customer \Model \ResourceModel \Visitor \CollectionFactory::class
1346
+ )
1347
+ ->disableOriginalConstructor ()
1348
+ ->setMethods (['create ' ])
1349
+ ->getMock ();
1350
+ $ this ->saveHandler = $ this ->getMockBuilder (\Magento \Framework \Session \SaveHandlerInterface::class)
1351
+ ->disableOriginalConstructor ()
1352
+ ->setMethods (['destroy ' ])
1353
+ ->getMockForAbstractClass ();
1354
+
1355
+ $ dateTime = '2017-10-25 18:57:08 ' ;
1356
+ $ timestamp = '1508983028 ' ;
1357
+ $ dateTimeMock = $ this ->getMockBuilder (\DateTime::class)
1358
+ ->disableOriginalConstructor ()
1359
+ ->setMethods (['format ' , 'getTimestamp ' , 'setTimestamp ' ])
1360
+ ->getMock ();
1361
+
1362
+ $ dateTimeMock ->expects ($ this ->any ())
1363
+ ->method ('format ' )
1364
+ ->with (\Magento \Framework \Stdlib \DateTime::DATETIME_PHP_FORMAT )
1365
+ ->willReturn ($ dateTime );
1366
+
1367
+ $ dateTimeMock
1368
+ ->expects ($ this ->any ())
1369
+ ->method ('getTimestamp ' )
1370
+ ->willReturn ($ timestamp );
1371
+
1372
+ $ dateTimeMock
1373
+ ->expects ($ this ->any ())
1374
+ ->method ('setTimestamp ' )
1375
+ ->willReturnSelf ();
1376
+
1377
+ $ dateTimeFactory = $ this ->getMockBuilder (DateTimeFactory::class)
1378
+ ->disableOriginalConstructor ()
1379
+ ->setMethods (['create ' ])
1380
+ ->getMock ();
1381
+ $ dateTimeFactory ->expects ($ this ->any ())->method ('create ' )->willReturn ($ dateTimeMock );
1300
1382
1301
1383
$ this ->objectManagerHelper = new ObjectManagerHelper ($ this );
1302
1384
$ this ->accountManagement = $ this ->objectManagerHelper ->getObject (
@@ -1306,7 +1388,16 @@ private function reInitModel()
1306
1388
'customerRegistry ' => $ this ->customerRegistry ,
1307
1389
'customerRepository ' => $ this ->customerRepository ,
1308
1390
'customerModel ' => $ this ->customer ,
1309
- 'dateTimeFactory ' => $ this ->dateTimeFactory ,
1391
+ 'dateTimeFactory ' => $ dateTimeFactory ,
1392
+ 'stringHelper ' => $ this ->string ,
1393
+ 'scopeConfig ' => $ this ->scopeConfig ,
1394
+ 'sessionManager ' => $ this ->sessionManager ,
1395
+ 'visitorCollectionFactory ' => $ this ->visitorCollectionFactory ,
1396
+ 'saveHandler ' => $ this ->saveHandler ,
1397
+ 'encryptor ' => $ this ->encryptor ,
1398
+ 'dataProcessor ' => $ this ->dataObjectProcessor ,
1399
+ 'storeManager ' => $ this ->storeManager ,
1400
+ 'transportBuilder ' => $ this ->transportBuilder ,
1310
1401
]
1311
1402
);
1312
1403
$ reflection = new \ReflectionClass (get_class ($ this ->accountManagement ));
@@ -1326,6 +1417,7 @@ public function testChangePassword()
1326
1417
$ newPassword = 'abcdefg ' ;
1327
1418
$ passwordHash = '1a2b3f4c ' ;
1328
1419
1420
+ $ this ->reInitModel ();
1329
1421
$ customer = $ this ->getMockBuilder (\Magento \Customer \Api \Data \CustomerInterface::class)
1330
1422
->getMock ();
1331
1423
$ customer ->expects ($ this ->any ())
@@ -1341,24 +1433,20 @@ public function testChangePassword()
1341
1433
$ this ->authenticationMock ->expects ($ this ->once ())
1342
1434
->method ('authenticate ' );
1343
1435
1344
- $ customerSecure = $ this ->getMockBuilder (\Magento \Customer \Model \Data \CustomerSecure::class)
1345
- ->setMethods (['setRpToken ' , 'setRpTokenCreatedAt ' , 'getPasswordHash ' ])
1346
- ->disableOriginalConstructor ()
1347
- ->getMock ();
1348
- $ customerSecure ->expects ($ this ->once ())
1436
+ $ this ->customerSecure ->expects ($ this ->once ())
1349
1437
->method ('setRpToken ' )
1350
1438
->with (null );
1351
- $ customerSecure ->expects ($ this ->once ())
1439
+ $ this -> customerSecure ->expects ($ this ->once ())
1352
1440
->method ('setRpTokenCreatedAt ' )
1353
1441
->willReturnSelf ();
1354
- $ customerSecure ->expects ($ this ->any ())
1442
+ $ this -> customerSecure ->expects ($ this ->any ())
1355
1443
->method ('getPasswordHash ' )
1356
1444
->willReturn ($ passwordHash );
1357
1445
1358
1446
$ this ->customerRegistry ->expects ($ this ->any ())
1359
1447
->method ('retrieveSecureData ' )
1360
1448
->with ($ customerId )
1361
- ->willReturn ($ customerSecure );
1449
+ ->willReturn ($ this -> customerSecure );
1362
1450
1363
1451
$ this ->scopeConfig ->expects ($ this ->any ())
1364
1452
->method ('getValue ' )
@@ -1388,9 +1476,85 @@ public function testChangePassword()
1388
1476
->method ('save ' )
1389
1477
->with ($ customer );
1390
1478
1479
+ $ this ->sessionManager ->expects ($ this ->atLeastOnce ())->method ('start ' );
1480
+ $ this ->sessionManager ->expects ($ this ->atLeastOnce ())->method ('writeClose ' );
1481
+ $ this ->sessionManager ->expects ($ this ->atLeastOnce ())->method ('getSessionId ' );
1482
+
1483
+ $ visitor = $ this ->getMockBuilder (\Magento \Customer \Model \Visitor::class)
1484
+ ->disableOriginalConstructor ()
1485
+ ->setMethods (['getSessionId ' ])
1486
+ ->getMock ();
1487
+ $ visitor ->expects ($ this ->at (0 ))->method ('getSessionId ' )->willReturn ('session_id_1 ' );
1488
+ $ visitor ->expects ($ this ->at (1 ))->method ('getSessionId ' )->willReturn ('session_id_2 ' );
1489
+ $ visitorCollection = $ this ->getMockBuilder (
1490
+ \Magento \Customer \Model \ResourceModel \Visitor \CollectionFactory::class
1491
+ )
1492
+ ->disableOriginalConstructor ()->setMethods (['addFieldToFilter ' , 'getItems ' ])->getMock ();
1493
+ $ visitorCollection ->expects ($ this ->atLeastOnce ())->method ('addFieldToFilter ' )->willReturnSelf ();
1494
+ $ visitorCollection ->expects ($ this ->atLeastOnce ())->method ('getItems ' )->willReturn ([$ visitor , $ visitor ]);
1495
+ $ this ->visitorCollectionFactory ->expects ($ this ->atLeastOnce ())->method ('create ' )
1496
+ ->willReturn ($ visitorCollection );
1497
+ $ this ->saveHandler ->expects ($ this ->at (0 ))->method ('destroy ' )->with ('session_id_1 ' );
1498
+ $ this ->saveHandler ->expects ($ this ->at (1 ))->method ('destroy ' )->with ('session_id_2 ' );
1499
+
1391
1500
$ this ->assertTrue ($ this ->accountManagement ->changePassword ($ email , $ currentPassword , $ newPassword ));
1392
1501
}
1393
1502
1503
+ public function testResetPassword ()
1504
+ {
1505
+ $ customerEmail = 'customer@example.com ' ;
1506
+ $ customerId = '1 ' ;
1507
+ $ resetToken = 'newStringToken ' ;
1508
+ $ newPassword = 'new_password ' ;
1509
+
1510
+ $ this ->reInitModel ();
1511
+ $ customer = $ this ->getMockBuilder (\Magento \Customer \Api \Data \CustomerInterface::class)->getMock ();
1512
+ $ customer ->expects ($ this ->any ())->method ('getId ' )->willReturn ($ customerId );
1513
+ $ this ->customerRepository ->expects ($ this ->atLeastOnce ())->method ('get ' )->with ($ customerEmail )
1514
+ ->willReturn ($ customer );
1515
+ $ this ->customer ->expects ($ this ->atLeastOnce ())->method ('getResetPasswordLinkExpirationPeriod ' )
1516
+ ->willReturn (100000 );
1517
+ $ this ->string ->expects ($ this ->any ())->method ('strlen ' )->willReturnCallback (
1518
+ function ($ string ) {
1519
+ return strlen ($ string );
1520
+ }
1521
+ );
1522
+ $ this ->customerRegistry ->expects ($ this ->atLeastOnce ())->method ('retrieveSecureData ' )
1523
+ ->willReturn ($ this ->customerSecure );
1524
+
1525
+ $ this ->customerSecure ->expects ($ this ->once ())
1526
+ ->method ('setRpToken ' )
1527
+ ->with (null );
1528
+ $ this ->customerSecure ->expects ($ this ->once ())
1529
+ ->method ('setRpTokenCreatedAt ' )
1530
+ ->with (null );
1531
+ $ this ->customerSecure ->expects ($ this ->any ())
1532
+ ->method ('setPasswordHash ' )
1533
+ ->willReturn (null );
1534
+
1535
+ $ this ->sessionManager ->expects ($ this ->atLeastOnce ())->method ('destroy ' );
1536
+ $ this ->sessionManager ->expects ($ this ->atLeastOnce ())->method ('start ' );
1537
+ $ this ->sessionManager ->expects ($ this ->atLeastOnce ())->method ('writeClose ' );
1538
+ $ this ->sessionManager ->expects ($ this ->atLeastOnce ())->method ('getSessionId ' );
1539
+ $ visitor = $ this ->getMockBuilder (\Magento \Customer \Model \Visitor::class)
1540
+ ->disableOriginalConstructor ()
1541
+ ->setMethods (['getSessionId ' ])
1542
+ ->getMock ();
1543
+ $ visitor ->expects ($ this ->at (0 ))->method ('getSessionId ' )->willReturn ('session_id_1 ' );
1544
+ $ visitor ->expects ($ this ->at (1 ))->method ('getSessionId ' )->willReturn ('session_id_2 ' );
1545
+ $ visitorCollection = $ this ->getMockBuilder (
1546
+ \Magento \Customer \Model \ResourceModel \Visitor \CollectionFactory::class
1547
+ )
1548
+ ->disableOriginalConstructor ()->setMethods (['addFieldToFilter ' , 'getItems ' ])->getMock ();
1549
+ $ visitorCollection ->expects ($ this ->atLeastOnce ())->method ('addFieldToFilter ' )->willReturnSelf ();
1550
+ $ visitorCollection ->expects ($ this ->atLeastOnce ())->method ('getItems ' )->willReturn ([$ visitor , $ visitor ]);
1551
+ $ this ->visitorCollectionFactory ->expects ($ this ->atLeastOnce ())->method ('create ' )
1552
+ ->willReturn ($ visitorCollection );
1553
+ $ this ->saveHandler ->expects ($ this ->at (0 ))->method ('destroy ' )->with ('session_id_1 ' );
1554
+ $ this ->saveHandler ->expects ($ this ->at (1 ))->method ('destroy ' )->with ('session_id_2 ' );
1555
+ $ this ->assertTrue ($ this ->accountManagement ->resetPassword ($ customerEmail , $ resetToken , $ newPassword ));
1556
+ }
1557
+
1394
1558
/**
1395
1559
* @return void
1396
1560
*/
0 commit comments