Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
CookieTest.php
Go to the documentation of this file.
1 <?php
7 
8 class CookieTest extends \PHPUnit\Framework\TestCase
9 {
13  protected $_object;
14 
18  protected $_request;
19 
23  protected $_context;
24 
25  public function testIsUserNotAllowSaveCookie()
26  {
27  $this->_initMock()->_getCookieStub([1 => 1]);
28  $this->assertFalse($this->_object->isUserNotAllowSaveCookie());
29  $request = $this->createPartialMock(\Magento\Framework\App\Request\Http::class, ['getCookie']);
30  $request->expects($this->any())->method('getCookie')->will($this->returnValue(json_encode([])));
31  $scopeConfig = $this->_getConfigStub();
32  $context = $this->createPartialMock(
33  \Magento\Framework\App\Helper\Context::class,
34  ['getRequest', 'getScopeConfig']
35  );
36  $context->expects($this->once())->method('getRequest')->will($this->returnValue($request));
37  $context->expects($this->once())->method('getScopeConfig')->will($this->returnValue($scopeConfig));
38  $this->_object = new \Magento\Cookie\Helper\Cookie(
39  $context,
40  $this->createMock(\Magento\Store\Model\StoreManager::class),
41  ['current_store' => $this->_getStoreStub(), 'website' => $this->_getWebsiteStub()]
42  );
43  $this->assertTrue($this->_object->isUserNotAllowSaveCookie());
44  }
45 
47  {
48  $this->_initMock()->_getCookieStub([1 => 1]);
49  $this->assertEquals($this->_object->getAcceptedSaveCookiesWebsiteIds(), json_encode([1 => 1]));
50  }
51 
53  {
54  $this->_request =
55  $this->createPartialMock(\Magento\Framework\App\Request\Http::class, ['getCookie']);
56  $scopeConfig = $this->createMock(\Magento\Framework\App\Config\ScopeConfigInterface::class);
57  $storeStub = $this->_getStoreStub();
58  $scopeConfig->expects(
59  $this->once()
60  )->method(
61  'getValue'
62  )->will(
63  $this->returnCallback([$this, 'getConfigMethodStub'])
64  )->with(
65  $this->equalTo('web/cookie/cookie_restriction_lifetime')
66  );
67  $this->_context = $this->createPartialMock(
68  \Magento\Framework\App\Helper\Context::class,
69  ['getRequest', 'getScopeConfig']
70  );
71  $this->_context->expects($this->once())->method('getRequest')->will($this->returnValue($this->_request));
72  $this->_context->expects($this->once())->method('getScopeConfig')->will($this->returnValue($scopeConfig));
73 
74  $this->_object = new \Magento\Cookie\Helper\Cookie(
75  $this->_context,
76  $this->createMock(\Magento\Store\Model\StoreManager::class),
77  ['current_store' => $storeStub, 'website' => $this->_getWebsiteStub()]
78  );
79  $this->assertEquals($this->_object->getCookieRestrictionLifetime(), 60 * 60 * 24 * 365);
80  }
81 
85  protected function _initMock()
86  {
87  $scopeConfig = $this->_getConfigStub();
88  $this->_request =
89  $this->createPartialMock(\Magento\Framework\App\Request\Http::class, ['getCookie']);
90  $this->_context = $this->createPartialMock(
91  \Magento\Framework\App\Helper\Context::class,
92  ['getRequest', 'getScopeConfig']
93  );
94  $this->_context->expects($this->once())->method('getRequest')->will($this->returnValue($this->_request));
95  $this->_context->expects($this->once())->method('getScopeConfig')->will($this->returnValue($scopeConfig));
96  $this->_object = new \Magento\Cookie\Helper\Cookie(
97  $this->_context,
98  $this->createMock(\Magento\Store\Model\StoreManager::class),
99  ['current_store' => $this->_getStoreStub(), 'website' => $this->_getWebsiteStub()]
100  );
101  return $this;
102  }
103 
108  protected function _getStoreStub()
109  {
110  $store = $this->createMock(\Magento\Store\Model\Store::class);
111  return $store;
112  }
113 
119  protected function _getConfigStub()
120  {
121  $scopeConfig = $this->createMock(\Magento\Framework\App\Config\ScopeConfigInterface::class);
122  $scopeConfig->expects(
123  $this->any()
124  )->method(
125  'getValue'
126  )->will(
127  $this->returnCallback([$this, 'getConfigMethodStub'])
128  );
129 
130  return $scopeConfig;
131  }
132 
138  protected function _getCookieStub($cookieString = [])
139  {
140  $this->_request->expects(
141  $this->any()
142  )->method(
143  'getCookie'
144  )->will(
145  $this->returnValue(json_encode($cookieString))
146  );
147  }
148 
153  protected function _getWebsiteStub()
154  {
155  $websiteMock = $this->createMock(\Magento\Store\Model\Website::class);
156 
157  $websiteMock->expects($this->any())->method('getId')->will($this->returnValue(1));
158 
159  return $websiteMock;
160  }
161 
169  public function getConfigMethodStub($hashName)
170  {
171  $defaultConfig = [
172  'web/cookie/cookie_restriction' => 1,
173  'web/cookie/cookie_restriction_lifetime' => 60 * 60 * 24 * 365,
174  ];
175 
176  if (array_key_exists($hashName, $defaultConfig)) {
177  return $defaultConfig[$hashName];
178  }
179 
180  throw new \InvalidArgumentException('Unknow id = ' . $hashName);
181  }
182 }