CookieTest.php 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Cookie\Test\Unit\Helper;
  7. class CookieTest extends \PHPUnit\Framework\TestCase
  8. {
  9. /**
  10. * @var \Magento\Cookie\Helper\Cookie
  11. */
  12. protected $_object;
  13. /**
  14. * @var \Magento\Framework\App\Request\Http
  15. */
  16. protected $_request;
  17. /**
  18. * @var \Magento\Framework\App\Helper\Context
  19. */
  20. protected $_context;
  21. public function testIsUserNotAllowSaveCookie()
  22. {
  23. $this->_initMock()->_getCookieStub([1 => 1]);
  24. $this->assertFalse($this->_object->isUserNotAllowSaveCookie());
  25. $request = $this->createPartialMock(\Magento\Framework\App\Request\Http::class, ['getCookie']);
  26. $request->expects($this->any())->method('getCookie')->will($this->returnValue(json_encode([])));
  27. $scopeConfig = $this->_getConfigStub();
  28. $context = $this->createPartialMock(
  29. \Magento\Framework\App\Helper\Context::class,
  30. ['getRequest', 'getScopeConfig']
  31. );
  32. $context->expects($this->once())->method('getRequest')->will($this->returnValue($request));
  33. $context->expects($this->once())->method('getScopeConfig')->will($this->returnValue($scopeConfig));
  34. $this->_object = new \Magento\Cookie\Helper\Cookie(
  35. $context,
  36. $this->createMock(\Magento\Store\Model\StoreManager::class),
  37. ['current_store' => $this->_getStoreStub(), 'website' => $this->_getWebsiteStub()]
  38. );
  39. $this->assertTrue($this->_object->isUserNotAllowSaveCookie());
  40. }
  41. public function testGetAcceptedSaveCookiesWebsiteIds()
  42. {
  43. $this->_initMock()->_getCookieStub([1 => 1]);
  44. $this->assertEquals($this->_object->getAcceptedSaveCookiesWebsiteIds(), json_encode([1 => 1]));
  45. }
  46. public function testGetCookieRestrictionLifetime()
  47. {
  48. $this->_request =
  49. $this->createPartialMock(\Magento\Framework\App\Request\Http::class, ['getCookie']);
  50. $scopeConfig = $this->createMock(\Magento\Framework\App\Config\ScopeConfigInterface::class);
  51. $storeStub = $this->_getStoreStub();
  52. $scopeConfig->expects(
  53. $this->once()
  54. )->method(
  55. 'getValue'
  56. )->will(
  57. $this->returnCallback([$this, 'getConfigMethodStub'])
  58. )->with(
  59. $this->equalTo('web/cookie/cookie_restriction_lifetime')
  60. );
  61. $this->_context = $this->createPartialMock(
  62. \Magento\Framework\App\Helper\Context::class,
  63. ['getRequest', 'getScopeConfig']
  64. );
  65. $this->_context->expects($this->once())->method('getRequest')->will($this->returnValue($this->_request));
  66. $this->_context->expects($this->once())->method('getScopeConfig')->will($this->returnValue($scopeConfig));
  67. $this->_object = new \Magento\Cookie\Helper\Cookie(
  68. $this->_context,
  69. $this->createMock(\Magento\Store\Model\StoreManager::class),
  70. ['current_store' => $storeStub, 'website' => $this->_getWebsiteStub()]
  71. );
  72. $this->assertEquals($this->_object->getCookieRestrictionLifetime(), 60 * 60 * 24 * 365);
  73. }
  74. /**
  75. * @return $this
  76. */
  77. protected function _initMock()
  78. {
  79. $scopeConfig = $this->_getConfigStub();
  80. $this->_request =
  81. $this->createPartialMock(\Magento\Framework\App\Request\Http::class, ['getCookie']);
  82. $this->_context = $this->createPartialMock(
  83. \Magento\Framework\App\Helper\Context::class,
  84. ['getRequest', 'getScopeConfig']
  85. );
  86. $this->_context->expects($this->once())->method('getRequest')->will($this->returnValue($this->_request));
  87. $this->_context->expects($this->once())->method('getScopeConfig')->will($this->returnValue($scopeConfig));
  88. $this->_object = new \Magento\Cookie\Helper\Cookie(
  89. $this->_context,
  90. $this->createMock(\Magento\Store\Model\StoreManager::class),
  91. ['current_store' => $this->_getStoreStub(), 'website' => $this->_getWebsiteStub()]
  92. );
  93. return $this;
  94. }
  95. /**
  96. * Create store stub
  97. * @return \Magento\Store\Model\Store
  98. */
  99. protected function _getStoreStub()
  100. {
  101. $store = $this->createMock(\Magento\Store\Model\Store::class);
  102. return $store;
  103. }
  104. /**
  105. * Create config stub
  106. *
  107. * @return \PHPUnit_Framework_MockObject_MockObject
  108. */
  109. protected function _getConfigStub()
  110. {
  111. $scopeConfig = $this->createMock(\Magento\Framework\App\Config\ScopeConfigInterface::class);
  112. $scopeConfig->expects(
  113. $this->any()
  114. )->method(
  115. 'getValue'
  116. )->will(
  117. $this->returnCallback([$this, 'getConfigMethodStub'])
  118. );
  119. return $scopeConfig;
  120. }
  121. /**
  122. * Generate getCookie stub for mock request object
  123. *
  124. * @param array $cookieString
  125. */
  126. protected function _getCookieStub($cookieString = [])
  127. {
  128. $this->_request->expects(
  129. $this->any()
  130. )->method(
  131. 'getCookie'
  132. )->will(
  133. $this->returnValue(json_encode($cookieString))
  134. );
  135. }
  136. /**
  137. * Create Website Stub
  138. * @return \Magento\Store\Model\Website
  139. */
  140. protected function _getWebsiteStub()
  141. {
  142. $websiteMock = $this->createMock(\Magento\Store\Model\Website::class);
  143. $websiteMock->expects($this->any())->method('getId')->will($this->returnValue(1));
  144. return $websiteMock;
  145. }
  146. /**
  147. * Mock get config method
  148. * @static
  149. * @param string $hashName
  150. * @return string
  151. * @throws \InvalidArgumentException
  152. */
  153. public function getConfigMethodStub($hashName)
  154. {
  155. $defaultConfig = [
  156. 'web/cookie/cookie_restriction' => 1,
  157. 'web/cookie/cookie_restriction_lifetime' => 60 * 60 * 24 * 365,
  158. ];
  159. if (array_key_exists($hashName, $defaultConfig)) {
  160. return $defaultConfig[$hashName];
  161. }
  162. throw new \InvalidArgumentException('Unknow id = ' . $hashName);
  163. }
  164. }