SessionTest.php 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Persistent\Model;
  7. class SessionTest extends \PHPUnit\Framework\TestCase
  8. {
  9. /**
  10. * Session model
  11. *
  12. * @var \Magento\Persistent\Model\Session
  13. */
  14. protected $session;
  15. /**
  16. * Object manager
  17. *
  18. * @var \Magento\Framework\ObjectManagerInterface
  19. */
  20. protected $objectManager;
  21. /**
  22. * The existing cookies
  23. *
  24. * @var array
  25. */
  26. protected $existingCookies;
  27. public function setUp()
  28. {
  29. $this->objectManager = \Magento\TestFramework\Helper\Bootstrap::getObjectManager();
  30. $this->session = $this->objectManager->create(
  31. \Magento\Persistent\Model\Session::class
  32. );
  33. $this->existingCookies = $_COOKIE;
  34. }
  35. public function tearDown()
  36. {
  37. $_COOKIE = $this->existingCookies;
  38. }
  39. public function testSetPersistentCookie()
  40. {
  41. $this->assertArrayNotHasKey(Session::COOKIE_NAME, $_COOKIE);
  42. $key = 'sessionKey';
  43. $this->session->setKey($key);
  44. $this->session->setPersistentCookie(1000, '/');
  45. $this->assertEquals($key, $_COOKIE[Session::COOKIE_NAME]);
  46. }
  47. public function testRemovePersistendCookie()
  48. {
  49. $_COOKIE[Session::COOKIE_NAME] = 'cookieValue';
  50. $this->session->removePersistentCookie();
  51. $this->assertArrayNotHasKey(Session::COOKIE_NAME, $_COOKIE);
  52. }
  53. /**
  54. * @param int $duration
  55. * @param string $cookieValue
  56. * @dataProvider renewPersistentCookieDataProvider
  57. */
  58. public function testRenewPersistentCookie($duration, $cookieValue = 'cookieValue')
  59. {
  60. $_COOKIE[Session::COOKIE_NAME] = $cookieValue;
  61. $this->session->renewPersistentCookie($duration, '/');
  62. $this->assertEquals($cookieValue, $_COOKIE[Session::COOKIE_NAME]);
  63. }
  64. public function renewPersistentCookieDataProvider()
  65. {
  66. return [
  67. 'no duration' => [null],
  68. 'no cookie' => [1000, null],
  69. 'all' => [1000],
  70. ];
  71. }
  72. /**
  73. * @magentoDataFixture Magento/Customer/_files/customer.php
  74. */
  75. public function testLoadByCookieKey()
  76. {
  77. /** @var \Magento\Persistent\Model\Session $preSession */
  78. $preSession = $this->objectManager->get(\Magento\Persistent\Model\SessionFactory::class)
  79. ->create()
  80. ->loadByCookieKey();
  81. $this->assertNull($preSession->getCustomerId());
  82. $this->session->setCustomerId(1)->save();
  83. $this->session->setPersistentCookie(1000, '/');
  84. /** @var \Magento\Persistent\Model\Session $postSession */
  85. $postSession = $this->objectManager->get(\Magento\Persistent\Model\SessionFactory::class)
  86. ->create()
  87. ->loadByCookieKey();
  88. $this->assertEquals(1, $postSession->getCustomerId());
  89. }
  90. }