123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182 |
- <?php
- /**
- * Copyright © Magento, Inc. All rights reserved.
- * See COPYING.txt for license details.
- */
- namespace Magento\Cookie\Test\Unit\Helper;
- class CookieTest extends \PHPUnit\Framework\TestCase
- {
- /**
- * @var \Magento\Cookie\Helper\Cookie
- */
- protected $_object;
- /**
- * @var \Magento\Framework\App\Request\Http
- */
- protected $_request;
- /**
- * @var \Magento\Framework\App\Helper\Context
- */
- protected $_context;
- public function testIsUserNotAllowSaveCookie()
- {
- $this->_initMock()->_getCookieStub([1 => 1]);
- $this->assertFalse($this->_object->isUserNotAllowSaveCookie());
- $request = $this->createPartialMock(\Magento\Framework\App\Request\Http::class, ['getCookie']);
- $request->expects($this->any())->method('getCookie')->will($this->returnValue(json_encode([])));
- $scopeConfig = $this->_getConfigStub();
- $context = $this->createPartialMock(
- \Magento\Framework\App\Helper\Context::class,
- ['getRequest', 'getScopeConfig']
- );
- $context->expects($this->once())->method('getRequest')->will($this->returnValue($request));
- $context->expects($this->once())->method('getScopeConfig')->will($this->returnValue($scopeConfig));
- $this->_object = new \Magento\Cookie\Helper\Cookie(
- $context,
- $this->createMock(\Magento\Store\Model\StoreManager::class),
- ['current_store' => $this->_getStoreStub(), 'website' => $this->_getWebsiteStub()]
- );
- $this->assertTrue($this->_object->isUserNotAllowSaveCookie());
- }
- public function testGetAcceptedSaveCookiesWebsiteIds()
- {
- $this->_initMock()->_getCookieStub([1 => 1]);
- $this->assertEquals($this->_object->getAcceptedSaveCookiesWebsiteIds(), json_encode([1 => 1]));
- }
- public function testGetCookieRestrictionLifetime()
- {
- $this->_request =
- $this->createPartialMock(\Magento\Framework\App\Request\Http::class, ['getCookie']);
- $scopeConfig = $this->createMock(\Magento\Framework\App\Config\ScopeConfigInterface::class);
- $storeStub = $this->_getStoreStub();
- $scopeConfig->expects(
- $this->once()
- )->method(
- 'getValue'
- )->will(
- $this->returnCallback([$this, 'getConfigMethodStub'])
- )->with(
- $this->equalTo('web/cookie/cookie_restriction_lifetime')
- );
- $this->_context = $this->createPartialMock(
- \Magento\Framework\App\Helper\Context::class,
- ['getRequest', 'getScopeConfig']
- );
- $this->_context->expects($this->once())->method('getRequest')->will($this->returnValue($this->_request));
- $this->_context->expects($this->once())->method('getScopeConfig')->will($this->returnValue($scopeConfig));
- $this->_object = new \Magento\Cookie\Helper\Cookie(
- $this->_context,
- $this->createMock(\Magento\Store\Model\StoreManager::class),
- ['current_store' => $storeStub, 'website' => $this->_getWebsiteStub()]
- );
- $this->assertEquals($this->_object->getCookieRestrictionLifetime(), 60 * 60 * 24 * 365);
- }
- /**
- * @return $this
- */
- protected function _initMock()
- {
- $scopeConfig = $this->_getConfigStub();
- $this->_request =
- $this->createPartialMock(\Magento\Framework\App\Request\Http::class, ['getCookie']);
- $this->_context = $this->createPartialMock(
- \Magento\Framework\App\Helper\Context::class,
- ['getRequest', 'getScopeConfig']
- );
- $this->_context->expects($this->once())->method('getRequest')->will($this->returnValue($this->_request));
- $this->_context->expects($this->once())->method('getScopeConfig')->will($this->returnValue($scopeConfig));
- $this->_object = new \Magento\Cookie\Helper\Cookie(
- $this->_context,
- $this->createMock(\Magento\Store\Model\StoreManager::class),
- ['current_store' => $this->_getStoreStub(), 'website' => $this->_getWebsiteStub()]
- );
- return $this;
- }
- /**
- * Create store stub
- * @return \Magento\Store\Model\Store
- */
- protected function _getStoreStub()
- {
- $store = $this->createMock(\Magento\Store\Model\Store::class);
- return $store;
- }
- /**
- * Create config stub
- *
- * @return \PHPUnit_Framework_MockObject_MockObject
- */
- protected function _getConfigStub()
- {
- $scopeConfig = $this->createMock(\Magento\Framework\App\Config\ScopeConfigInterface::class);
- $scopeConfig->expects(
- $this->any()
- )->method(
- 'getValue'
- )->will(
- $this->returnCallback([$this, 'getConfigMethodStub'])
- );
- return $scopeConfig;
- }
- /**
- * Generate getCookie stub for mock request object
- *
- * @param array $cookieString
- */
- protected function _getCookieStub($cookieString = [])
- {
- $this->_request->expects(
- $this->any()
- )->method(
- 'getCookie'
- )->will(
- $this->returnValue(json_encode($cookieString))
- );
- }
- /**
- * Create Website Stub
- * @return \Magento\Store\Model\Website
- */
- protected function _getWebsiteStub()
- {
- $websiteMock = $this->createMock(\Magento\Store\Model\Website::class);
- $websiteMock->expects($this->any())->method('getId')->will($this->returnValue(1));
- return $websiteMock;
- }
- /**
- * Mock get config method
- * @static
- * @param string $hashName
- * @return string
- * @throws \InvalidArgumentException
- */
- public function getConfigMethodStub($hashName)
- {
- $defaultConfig = [
- 'web/cookie/cookie_restriction' => 1,
- 'web/cookie/cookie_restriction_lifetime' => 60 * 60 * 24 * 365,
- ];
- if (array_key_exists($hashName, $defaultConfig)) {
- return $defaultConfig[$hashName];
- }
- throw new \InvalidArgumentException('Unknow id = ' . $hashName);
- }
- }
|