SessionTest.php 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  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\Framework\App\PageCache\FormKey;
  8. use Magento\Framework\Stdlib\Cookie\CookieMetadataFactory;
  9. use Magento\Framework\Stdlib\Cookie\PublicCookieMetadata;
  10. use Magento\TestFramework\Helper\Bootstrap;
  11. /**
  12. * @magentoDataFixture Magento/Customer/_files/customer.php
  13. * @magentoAppIsolation enabled
  14. */
  15. class SessionTest extends \PHPUnit\Framework\TestCase
  16. {
  17. /**
  18. * @var \Magento\Customer\Model\Session
  19. */
  20. protected $_customerSession;
  21. /**
  22. * @var FormKey
  23. */
  24. protected $formKey;
  25. /** @var PublicCookieMetadata $cookieMetadata */
  26. protected $cookieMetadata;
  27. protected function setUp()
  28. {
  29. $this->_customerSession = Bootstrap::getObjectManager()->create(
  30. \Magento\Customer\Model\Session::class
  31. );
  32. /** @var CookieMetadataFactory $cookieMetadataFactory */
  33. $cookieMetadataFactory = Bootstrap::getObjectManager()->get(CookieMetadataFactory::class);
  34. $this->cookieMetadata = $cookieMetadataFactory
  35. ->createPublicCookieMetadata();
  36. $this->cookieMetadata->setDomain($this->_customerSession->getCookieDomain());
  37. $this->cookieMetadata->setPath($this->_customerSession->getCookiePath());
  38. $this->cookieMetadata->setDuration($this->_customerSession->getCookieLifetime());
  39. $this->formKey = Bootstrap::getObjectManager()->get(FormKey::class);
  40. $this->formKey->set(
  41. 'form_key',
  42. $this->cookieMetadata
  43. );
  44. }
  45. public function testLoginById()
  46. {
  47. $this->assertTrue($this->_customerSession->loginById(1));
  48. // fixture
  49. $this->assertTrue($this->_customerSession->isLoggedIn());
  50. }
  51. public function testLoginByIdCustomerDataLoadedCorrectly()
  52. {
  53. $fixtureCustomerId = 1;
  54. /** @var \Magento\Customer\Model\Session $customerSession */
  55. $customerSession = Bootstrap::getObjectManager()->get(\Magento\Customer\Model\Session::class);
  56. $customerSession->loginById($fixtureCustomerId);
  57. $customerData = $customerSession->getCustomerData();
  58. $this->assertEquals($fixtureCustomerId, $customerData->getId(), "Customer data was loaded incorrectly");
  59. }
  60. /**
  61. * Verifies that logging in flushes form_key
  62. */
  63. public function testLoginActionFlushesFormKey()
  64. {
  65. $beforeKey = $this->formKey->get();
  66. $this->_customerSession->loginById(1);
  67. $afterKey = $this->formKey->get();
  68. $this->assertNotEquals($beforeKey, $afterKey);
  69. }
  70. /**
  71. * Verifies that logging out flushes form_key
  72. */
  73. public function testLogoutActionFlushesFormKey()
  74. {
  75. $this->_customerSession->loginById(1);
  76. $this->formKey->set(
  77. 'form_key',
  78. $this->cookieMetadata
  79. );
  80. $beforeKey = $this->formKey->get();
  81. $this->_customerSession->logout();
  82. $afterKey = $this->formKey->get();
  83. $this->assertNotEquals($beforeKey, $afterKey);
  84. }
  85. }