SessionTest.php 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Persistent\Test\Unit\Model;
  7. class SessionTest extends \PHPUnit\Framework\TestCase
  8. {
  9. /**
  10. * @var \Magento\Persistent\Model\Session
  11. */
  12. protected $session;
  13. /**
  14. * @var \Magento\Framework\Session\Config\ConfigInterface|\PHPUnit_Framework_MockObject_MockObject
  15. */
  16. protected $configMock;
  17. /**
  18. * @var \Magento\Framework\Stdlib\CookieManagerInterface |\PHPUnit_Framework_MockObject_MockObject
  19. */
  20. protected $cookieManagerMock;
  21. /**
  22. * @var \Magento\Framework\Stdlib\Cookie\CookieMetadataFactory |\PHPUnit_Framework_MockObject_MockObject
  23. */
  24. protected $cookieMetadataFactoryMock;
  25. protected function setUp()
  26. {
  27. $helper = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this);
  28. $this->configMock = $this->createMock(\Magento\Framework\Session\Config\ConfigInterface::class);
  29. $this->cookieManagerMock = $this->createMock(\Magento\Framework\Stdlib\CookieManagerInterface::class);
  30. $this->cookieMetadataFactoryMock = $this->getMockBuilder(
  31. \Magento\Framework\Stdlib\Cookie\CookieMetadataFactory::class
  32. )->disableOriginalConstructor()
  33. ->getMock();
  34. $resourceMock = $this->getMockForAbstractClass(
  35. \Magento\Framework\Model\ResourceModel\Db\AbstractDb::class,
  36. [],
  37. '',
  38. false,
  39. false,
  40. true,
  41. ['__wakeup', 'getIdFieldName', 'getConnection', 'beginTransaction', 'delete', 'commit', 'rollBack']
  42. );
  43. $actionValidatorMock = $this->createMock(\Magento\Framework\Model\ActionValidator\RemoveAction::class);
  44. $actionValidatorMock->expects($this->any())->method('isAllowed')->will($this->returnValue(true));
  45. $context = $helper->getObject(
  46. \Magento\Framework\Model\Context::class,
  47. [
  48. 'actionValidator' => $actionValidatorMock,
  49. ]
  50. );
  51. $this->session = $helper->getObject(
  52. \Magento\Persistent\Model\Session::class,
  53. [
  54. 'sessionConfig' => $this->configMock,
  55. 'cookieManager' => $this->cookieManagerMock,
  56. 'context' => $context,
  57. 'cookieMetadataFactory' => $this->cookieMetadataFactoryMock,
  58. 'request' => $this->createMock(\Magento\Framework\App\Request\Http::class),
  59. 'resource' => $resourceMock,
  60. ]
  61. );
  62. }
  63. public function testLoadByCookieKeyWithNull()
  64. {
  65. $this->cookieManagerMock->expects($this->once())
  66. ->method('getCookie')
  67. ->with(\Magento\Persistent\Model\Session::COOKIE_NAME)
  68. ->will($this->returnValue(null));
  69. $this->session->loadByCookieKey(null);
  70. }
  71. /**
  72. * @covers \Magento\Persistent\Model\Session::removePersistentCookie
  73. */
  74. public function testAfterDeleteCommit()
  75. {
  76. $cookiePath = 'some_path';
  77. $this->configMock->expects($this->once())->method('getCookiePath')->will($this->returnValue($cookiePath));
  78. $cookieMetadataMock = $this->getMockBuilder(\Magento\Framework\Stdlib\Cookie\SensitiveCookieMetadata::class)
  79. ->disableOriginalConstructor()
  80. ->getMock();
  81. $cookieMetadataMock->expects($this->once())
  82. ->method('setPath')
  83. ->with($cookiePath)
  84. ->will($this->returnSelf());
  85. $this->cookieMetadataFactoryMock->expects($this->once())
  86. ->method('createSensitiveCookieMetadata')
  87. ->will($this->returnValue($cookieMetadataMock));
  88. $this->cookieManagerMock->expects(
  89. $this->once()
  90. )->method(
  91. 'deleteCookie'
  92. )->with(
  93. \Magento\Persistent\Model\Session::COOKIE_NAME,
  94. $cookieMetadataMock
  95. );
  96. $this->session->afterDeleteCommit();
  97. }
  98. public function testSetPersistentCookie()
  99. {
  100. $cookiePath = 'some_path';
  101. $duration = 1000;
  102. $key = 'sessionKey';
  103. $this->session->setKey($key);
  104. $cookieMetadataMock = $this->getMockBuilder(\Magento\Framework\Stdlib\Cookie\PublicCookieMetadata::class)
  105. ->disableOriginalConstructor()
  106. ->getMock();
  107. $cookieMetadataMock->expects($this->once())
  108. ->method('setPath')
  109. ->with($cookiePath)
  110. ->will($this->returnSelf());
  111. $cookieMetadataMock->expects($this->once())
  112. ->method('setDuration')
  113. ->with($duration)
  114. ->will($this->returnSelf());
  115. $cookieMetadataMock->expects($this->once())
  116. ->method('setSecure')
  117. ->with(false)
  118. ->will($this->returnSelf());
  119. $cookieMetadataMock->expects($this->once())
  120. ->method('setHttpOnly')
  121. ->with(true)
  122. ->will($this->returnSelf());
  123. $this->cookieMetadataFactoryMock->expects($this->once())
  124. ->method('createPublicCookieMetadata')
  125. ->will($this->returnValue($cookieMetadataMock));
  126. $this->cookieManagerMock->expects($this->once())
  127. ->method('setPublicCookie')
  128. ->with(
  129. \Magento\Persistent\Model\Session::COOKIE_NAME,
  130. $key,
  131. $cookieMetadataMock
  132. );
  133. $this->session->setPersistentCookie($duration, $cookiePath);
  134. }
  135. /**
  136. * @param $numGetCookieCalls
  137. * @param $numCalls
  138. * @param int $cookieDuration
  139. * @param string $cookieValue
  140. * @param string $cookiePath
  141. * @dataProvider renewPersistentCookieDataProvider
  142. */
  143. public function testRenewPersistentCookie(
  144. $numGetCookieCalls,
  145. $numCalls,
  146. $cookieDuration = 1000,
  147. $cookieValue = 'cookieValue',
  148. $cookiePath = 'cookiePath'
  149. ) {
  150. $cookieMetadataMock = $this->getMockBuilder(\Magento\Framework\Stdlib\Cookie\PublicCookieMetadata::class)
  151. ->disableOriginalConstructor()
  152. ->getMock();
  153. $cookieMetadataMock->expects($this->exactly($numCalls))
  154. ->method('setPath')
  155. ->with($cookiePath)
  156. ->will($this->returnSelf());
  157. $cookieMetadataMock->expects($this->exactly($numCalls))
  158. ->method('setDuration')
  159. ->with($cookieDuration)
  160. ->will($this->returnSelf());
  161. $cookieMetadataMock->expects($this->exactly($numCalls))
  162. ->method('setSecure')
  163. ->with(false)
  164. ->will($this->returnSelf());
  165. $cookieMetadataMock->expects($this->exactly($numCalls))
  166. ->method('setHttpOnly')
  167. ->with(true)
  168. ->will($this->returnSelf());
  169. $this->cookieMetadataFactoryMock->expects($this->exactly($numCalls))
  170. ->method('createPublicCookieMetadata')
  171. ->will($this->returnValue($cookieMetadataMock));
  172. $this->cookieManagerMock->expects($this->exactly($numGetCookieCalls))
  173. ->method('getCookie')
  174. ->with(\Magento\Persistent\Model\Session::COOKIE_NAME)
  175. ->will($this->returnValue($cookieValue));
  176. $this->cookieManagerMock->expects($this->exactly($numCalls))
  177. ->method('setPublicCookie')
  178. ->with(
  179. \Magento\Persistent\Model\Session::COOKIE_NAME,
  180. $cookieValue,
  181. $cookieMetadataMock
  182. );
  183. $this->session->renewPersistentCookie($cookieDuration, $cookiePath);
  184. }
  185. /**
  186. * Data provider for testRenewPersistentCookie
  187. *
  188. * @return array
  189. */
  190. public function renewPersistentCookieDataProvider()
  191. {
  192. return [
  193. 'no duration' => [0, 0, null ],
  194. 'no cookie' => [1, 0, 1000, null],
  195. 'all' => [1, 1],
  196. ];
  197. }
  198. }