EmulateCustomerObserverTest.php 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. <?php
  2. /**
  3. *
  4. * Copyright © Magento, Inc. All rights reserved.
  5. * See COPYING.txt for license details.
  6. */
  7. namespace Magento\Persistent\Observer;
  8. /**
  9. * @magentoDataFixture Magento/Persistent/_files/persistent.php
  10. */
  11. class EmulateCustomerObserverTest extends \PHPUnit\Framework\TestCase
  12. {
  13. /**
  14. * @var \Magento\Customer\Api\CustomerRepositoryInterface
  15. */
  16. protected $customerRepository;
  17. /**
  18. * @var \Magento\Persistent\Helper\Session
  19. */
  20. protected $_persistentSessionHelper;
  21. /**
  22. * @var \Magento\Framework\ObjectManagerInterface
  23. */
  24. protected $_objectManager;
  25. /**
  26. * @var \Magento\Persistent\Observer\EmulateCustomerObserver
  27. */
  28. protected $_observer;
  29. /**
  30. * @var \Magento\Customer\Model\Session
  31. */
  32. protected $_customerSession;
  33. public function setUp()
  34. {
  35. $this->_objectManager = \Magento\TestFramework\Helper\Bootstrap::getObjectManager();
  36. $this->_customerSession = $this->_objectManager->get(\Magento\Customer\Model\Session::class);
  37. $this->customerRepository = $this->_objectManager->create(
  38. \Magento\Customer\Api\CustomerRepositoryInterface::class
  39. );
  40. $this->_persistentSessionHelper = $this->_objectManager->create(\Magento\Persistent\Helper\Session::class);
  41. $this->_observer = $this->_objectManager->create(
  42. \Magento\Persistent\Observer\EmulateCustomerObserver::class,
  43. [
  44. 'customerRepository' => $this->customerRepository,
  45. 'persistentSession' => $this->_persistentSessionHelper
  46. ]
  47. );
  48. }
  49. /**
  50. * @magentoAppArea frontend
  51. * @magentoConfigFixture current_store persistent/options/shopping_cart 1
  52. * @magentoConfigFixture current_store persistent/options/logout_clear 0
  53. * @magentoConfigFixture current_store persistent/options/enabled 1
  54. */
  55. public function testEmulateCustomer()
  56. {
  57. $observer = new \Magento\Framework\Event\Observer();
  58. $this->_customerSession->loginById(1);
  59. $this->_customerSession->logout();
  60. $this->assertNull($this->_customerSession->getCustomerId());
  61. $this->assertEquals(
  62. \Magento\Customer\Model\GroupManagement::NOT_LOGGED_IN_ID,
  63. $this->_customerSession->getCustomerGroupId()
  64. );
  65. $this->_observer->execute($observer);
  66. $customer = $this->customerRepository->getById(
  67. $this->_persistentSessionHelper->getSession()->getCustomerId()
  68. );
  69. $this->assertEquals(
  70. $customer->getId(),
  71. $this->_customerSession->getCustomerId()
  72. );
  73. $this->assertEquals(
  74. $customer->getGroupId(),
  75. $this->_customerSession->getCustomerGroupId()
  76. );
  77. }
  78. }