1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
namespace Magento\Customer\Model;
use Magento\TestFramework\Helper\Bootstrap;
class VisitorTest extends \PHPUnit\Framework\TestCase
{
/**
* @magentoAppArea frontend
* @magentoDataFixture Magento/Customer/_files/customer.php
*/
public function testBindCustomerLogin()
{
/** @var \Magento\Customer\Model\Visitor $visitor */
$visitor = Bootstrap::getObjectManager()->get(\Magento\Customer\Model\Visitor::class);
$visitor->unsCustomerId();
$visitor->unsDoCustomerLogin();
$customer = $this->_loginCustomer('customer@example.com', 'password');
// Visitor has not customer ID yet
$this->assertTrue($visitor->getDoCustomerLogin());
$this->assertEquals($customer->getId(), $visitor->getCustomerId());
// Visitor already has customer ID
$visitor->unsDoCustomerLogin();
$this->_loginCustomer('customer@example.com', 'password');
$this->assertNull($visitor->getDoCustomerLogin());
}
/**
* @magentoAppArea frontend
* @magentoDataFixture Magento/Customer/_files/customer.php
*/
public function testBindCustomerLogout()
{
/** @var \Magento\Customer\Model\Visitor $visitor */
$visitor = Bootstrap::getObjectManager()->get(\Magento\Customer\Model\Visitor::class);
$this->_loginCustomer('customer@example.com', 'password');
$visitor->setCustomerId(1);
$visitor->unsDoCustomerLogout();
$this->_logoutCustomer(1);
// Visitor has customer ID => check that do_customer_logout flag is set
$this->assertTrue($visitor->getDoCustomerLogout());
$this->_loginCustomer('customer@example.com', 'password');
$visitor->unsCustomerId();
$visitor->unsDoCustomerLogout();
$this->_logoutCustomer(1);
// Visitor has no customer ID => check that do_customer_logout flag not changed
$this->assertNull($visitor->getDoCustomerLogout());
}
/**
* @magentoAppArea frontend
*/
public function testClean()
{
$customerIdNow = 1;
$lastVisitNow = date('Y-m-d H:i:s', time());
$sessionIdNow = 'asaswljxvgklasdflkjasieasd';
$customerIdPast = null;
$lastVisitPast = date('Y-m-d H:i:s', time() - 172800);
$sessionIdPast = 'kui0aa57nqddl8vk7k6ohgi352';
/** @var \Magento\Customer\Model\Visitor $visitor */
$visitor = Bootstrap::getObjectManager()->get(\Magento\Customer\Model\Visitor::class);
$visitor->setCustomerId($customerIdPast);
$visitor->setSessionId($sessionIdPast);
$visitor->setLastVisitAt($lastVisitPast);
$visitor->save();
$visitorIdPast = $visitor->getId();
$visitor->unsetData();
$visitor->setCustomerId($customerIdNow);
$visitor->setSessionId($sessionIdNow);
$visitor->setLastVisitAt($lastVisitNow);
$visitor->save();
$visitorIdNow = $visitor->getId();
$visitor->unsetData();
$visitor->clean();
$visitor->load($visitorIdPast);
$this->assertEquals([], $visitor->getData());
$visitor->unsetData();
$visitor->load($visitorIdNow);
$this->assertEquals(
[
'visitor_id' => $visitorIdNow,
'customer_id' => $customerIdNow,
'session_id' => $sessionIdNow,
'last_visit_at' => $lastVisitNow
],
$visitor->getData()
);
}
/**
* Authenticate customer and return its DTO
* @param string $username
* @param string $password
* @return \Magento\Customer\Api\Data\CustomerInterface
*/
protected function _loginCustomer($username, $password)
{
/** @var \Magento\Customer\Api\AccountManagementInterface $accountManagement */
$accountManagement = Bootstrap::getObjectManager()->create(
\Magento\Customer\Api\AccountManagementInterface::class
);
return $accountManagement->authenticate($username, $password);
}
/**
* Log out customer
* @param int $customerId
*/
public function _logoutCustomer($customerId)
{
/** @var \Magento\Customer\Model\Session $customerSession */
$customerSession = Bootstrap::getObjectManager()->get(\Magento\Customer\Model\Session::class);
$customerSession->setCustomerId($customerId);
$customerSession->logout();
}
}