VisitorTest.php 4.5 KB

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