Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
SessionTest.php
Go to the documentation of this file.
1 <?php
7 
8 class SessionTest extends \PHPUnit\Framework\TestCase
9 {
13  protected $session;
14 
18  protected $configMock;
19 
23  protected $cookieManagerMock;
24 
29 
30  protected function setUp()
31  {
32  $helper = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this);
33  $this->configMock = $this->createMock(\Magento\Framework\Session\Config\ConfigInterface::class);
34  $this->cookieManagerMock = $this->createMock(\Magento\Framework\Stdlib\CookieManagerInterface::class);
35  $this->cookieMetadataFactoryMock = $this->getMockBuilder(
36  \Magento\Framework\Stdlib\Cookie\CookieMetadataFactory::class
37  )->disableOriginalConstructor()
38  ->getMock();
39 
40  $resourceMock = $this->getMockForAbstractClass(
41  \Magento\Framework\Model\ResourceModel\Db\AbstractDb::class,
42  [],
43  '',
44  false,
45  false,
46  true,
47  ['__wakeup', 'getIdFieldName', 'getConnection', 'beginTransaction', 'delete', 'commit', 'rollBack']
48  );
49 
50  $actionValidatorMock = $this->createMock(\Magento\Framework\Model\ActionValidator\RemoveAction::class);
51  $actionValidatorMock->expects($this->any())->method('isAllowed')->will($this->returnValue(true));
52 
53  $context = $helper->getObject(
54  \Magento\Framework\Model\Context::class,
55  [
56  'actionValidator' => $actionValidatorMock,
57  ]
58  );
59 
60  $this->session = $helper->getObject(
61  \Magento\Persistent\Model\Session::class,
62  [
63  'sessionConfig' => $this->configMock,
64  'cookieManager' => $this->cookieManagerMock,
65  'context' => $context,
66  'cookieMetadataFactory' => $this->cookieMetadataFactoryMock,
67  'request' => $this->createMock(\Magento\Framework\App\Request\Http::class),
68  'resource' => $resourceMock,
69  ]
70  );
71  }
72 
73  public function testLoadByCookieKeyWithNull()
74  {
75  $this->cookieManagerMock->expects($this->once())
76  ->method('getCookie')
77  ->with(\Magento\Persistent\Model\Session::COOKIE_NAME)
78  ->will($this->returnValue(null));
79  $this->session->loadByCookieKey(null);
80  }
81 
85  public function testAfterDeleteCommit()
86  {
87  $cookiePath = 'some_path';
88  $this->configMock->expects($this->once())->method('getCookiePath')->will($this->returnValue($cookiePath));
89  $cookieMetadataMock = $this->getMockBuilder(\Magento\Framework\Stdlib\Cookie\SensitiveCookieMetadata::class)
90  ->disableOriginalConstructor()
91  ->getMock();
92  $cookieMetadataMock->expects($this->once())
93  ->method('setPath')
94  ->with($cookiePath)
95  ->will($this->returnSelf());
96  $this->cookieMetadataFactoryMock->expects($this->once())
97  ->method('createSensitiveCookieMetadata')
98  ->will($this->returnValue($cookieMetadataMock));
99  $this->cookieManagerMock->expects(
100  $this->once()
101  )->method(
102  'deleteCookie'
103  )->with(
104  \Magento\Persistent\Model\Session::COOKIE_NAME,
105  $cookieMetadataMock
106  );
107  $this->session->afterDeleteCommit();
108  }
109 
110  public function testSetPersistentCookie()
111  {
112  $cookiePath = 'some_path';
113  $duration = 1000;
114  $key = 'sessionKey';
115  $this->session->setKey($key);
116  $cookieMetadataMock = $this->getMockBuilder(\Magento\Framework\Stdlib\Cookie\PublicCookieMetadata::class)
117  ->disableOriginalConstructor()
118  ->getMock();
119  $cookieMetadataMock->expects($this->once())
120  ->method('setPath')
121  ->with($cookiePath)
122  ->will($this->returnSelf());
123  $cookieMetadataMock->expects($this->once())
124  ->method('setDuration')
125  ->with($duration)
126  ->will($this->returnSelf());
127  $cookieMetadataMock->expects($this->once())
128  ->method('setSecure')
129  ->with(false)
130  ->will($this->returnSelf());
131  $cookieMetadataMock->expects($this->once())
132  ->method('setHttpOnly')
133  ->with(true)
134  ->will($this->returnSelf());
135  $this->cookieMetadataFactoryMock->expects($this->once())
136  ->method('createPublicCookieMetadata')
137  ->will($this->returnValue($cookieMetadataMock));
138  $this->cookieManagerMock->expects($this->once())
139  ->method('setPublicCookie')
140  ->with(
141  \Magento\Persistent\Model\Session::COOKIE_NAME,
142  $key,
143  $cookieMetadataMock
144  );
145  $this->session->setPersistentCookie($duration, $cookiePath);
146  }
147 
156  public function testRenewPersistentCookie(
157  $numGetCookieCalls,
158  $numCalls,
159  $cookieDuration = 1000,
160  $cookieValue = 'cookieValue',
161  $cookiePath = 'cookiePath'
162  ) {
163  $cookieMetadataMock = $this->getMockBuilder(\Magento\Framework\Stdlib\Cookie\PublicCookieMetadata::class)
164  ->disableOriginalConstructor()
165  ->getMock();
166  $cookieMetadataMock->expects($this->exactly($numCalls))
167  ->method('setPath')
168  ->with($cookiePath)
169  ->will($this->returnSelf());
170  $cookieMetadataMock->expects($this->exactly($numCalls))
171  ->method('setDuration')
172  ->with($cookieDuration)
173  ->will($this->returnSelf());
174  $cookieMetadataMock->expects($this->exactly($numCalls))
175  ->method('setSecure')
176  ->with(false)
177  ->will($this->returnSelf());
178  $cookieMetadataMock->expects($this->exactly($numCalls))
179  ->method('setHttpOnly')
180  ->with(true)
181  ->will($this->returnSelf());
182  $this->cookieMetadataFactoryMock->expects($this->exactly($numCalls))
183  ->method('createPublicCookieMetadata')
184  ->will($this->returnValue($cookieMetadataMock));
185  $this->cookieManagerMock->expects($this->exactly($numGetCookieCalls))
186  ->method('getCookie')
187  ->with(\Magento\Persistent\Model\Session::COOKIE_NAME)
188  ->will($this->returnValue($cookieValue));
189  $this->cookieManagerMock->expects($this->exactly($numCalls))
190  ->method('setPublicCookie')
191  ->with(
192  \Magento\Persistent\Model\Session::COOKIE_NAME,
193  $cookieValue,
194  $cookieMetadataMock
195  );
196  $this->session->renewPersistentCookie($cookieDuration, $cookiePath);
197  }
198 
205  {
206  return [
207  'no duration' => [0, 0, null ],
208  'no cookie' => [1, 0, 1000, null],
209  'all' => [1, 1],
210  ];
211  }
212 }
$helper
Definition: iframe.phtml:13
testRenewPersistentCookie( $numGetCookieCalls, $numCalls, $cookieDuration=1000, $cookieValue='cookieValue', $cookiePath='cookiePath')