/** * Perform required checks before cron run * * @param \Magento\Framework\App\Cron $subject * * @return void * @SuppressWarnings(PHPMD.UnusedFormalParameter) * @throws \Magento\Framework\Exception */ public function beforeLaunch(\Magento\Framework\App\Cron $subject) { $this->_sidResolver->setUseSessionInUrl(false); if (false == $this->_appState->isInstalled()) { throw new \Magento\Framework\Exception('Application is not installed yet, please complete the installation first.'); } }
/** * @magentoConfigFixture current_store web/session/use_frontend_sid 1 */ public function testSetSessionIdFromParam() { $this->assertNotEquals('test_id', $this->_model->getSessionId()); $_GET[$this->_sidResolver->getSessionIdQueryParam($this->_model)] = 'test-id'; $this->_model->setSessionId($this->_sidResolver->getSid($this->_model)); $this->assertEquals('test-id', $this->_model->getSessionId()); /* Use not valid identifier */ $_GET[$this->_sidResolver->getSessionIdQueryParam($this->_model)] = 'test_id'; $this->_model->setSessionId($this->_sidResolver->getSid($this->_model)); $this->assertEquals('test-id', $this->_model->getSessionId()); }
/** * {@inheritdoc} * * @param array $arguments * @return array */ public function updatePathParams(array $arguments) { if ($this->_session->getCookieShouldBeReceived() && $this->_sidResolver->getUseSessionInUrl() && $this->_canUseSessionIdInParam) { $arguments += ['_query' => [$this->_sidResolver->getSessionIdQueryParam($this->_session) => $this->_session->getSessionId()]]; } return $arguments; }
/** * Set redirect into response * * @param \Magento\Framework\App\ResponseInterface $response * @param string $path * @param array $arguments * @return void */ public function redirect(\Magento\Framework\App\ResponseInterface $response, $path, $arguments = array()) { if ($this->_session->getCookieShouldBeReceived() && $this->_urlBuilder->getUseSession() && $this->_canUseSessionIdInParam) { $arguments += array('_query' => array($this->_sidResolver->getSessionIdQueryParam($this->_session) => $this->_session->getSessionId())); } $response->setRedirect($this->_urlBuilder->getUrl($path, $arguments)); }
/** * Get storage instance * * @param array $arguments * @return \Magento\Store\Model\StoreManagerInterface * @throws \InvalidArgumentException */ public function get(array $arguments = array()) { $className = $this->_appState->isInstalled() ? $this->_installedStorageClassName : $this->_defaultStorageClassName; if (false == isset($this->_cache[$className])) { /** @var $storage \Magento\Store\Model\StoreManagerInterface */ $storage = $this->_objectManager->create($className, $arguments); if (false === $storage instanceof \Magento\Store\Model\StoreManagerInterface) { throw new \InvalidArgumentException($className . ' doesn\'t implement \\Magento\\Store\\Model\\StoreManagerInterface'); } $this->_cache[$className] = $storage; if ($className === $this->_installedStorageClassName) { $this->_reinitStores($storage, $arguments); $useSid = $this->_scopeConfig->isSetFlag(\Magento\Framework\Session\SidResolver::XML_PATH_USE_FRONTEND_SID, \Magento\Store\Model\ScopeInterface::SCOPE_STORE, $storage->getStore()); $this->_sidResolver->setUseSessionInUrl($useSid); $this->_eventManager->dispatch('core_app_init_current_store_after'); $store = $storage->getStore(true); $logActive = $this->_scopeConfig->isSetFlag('dev/log/active', \Magento\Store\Model\ScopeInterface::SCOPE_STORE, $store); if ($logActive || $this->_appState->getMode() === \Magento\Framework\App\State::MODE_DEVELOPER) { $logFile = $this->_scopeConfig->getValue('dev/log/file', \Magento\Store\Model\ScopeInterface::SCOPE_STORE, $store); $logExceptionFile = $this->_scopeConfig->getValue('dev/log/exception_file', \Magento\Store\Model\ScopeInterface::SCOPE_STORE, $store); $this->_log->unsetLoggers(); $this->_log->addStreamLog(\Magento\Framework\Logger::LOGGER_SYSTEM, $logFile, $this->_writerModel); $this->_log->addStreamLog(\Magento\Framework\Logger::LOGGER_EXCEPTION, $logExceptionFile, $this->_writerModel); } } } return $this->_cache[$className]; }
/** * Configure session handler and start session * * @throws \Magento\Framework\Exception\SessionException * @return $this */ public function start() { if (!$this->isSessionExists()) { \Magento\Framework\Profiler::start('session_start'); try { $this->appState->getAreaCode(); } catch (\Magento\Framework\Exception\LocalizedException $e) { throw new \Magento\Framework\Exception\SessionException( new \Magento\Framework\Phrase( 'Area code not set: Area code must be set before starting a session.' ), $e ); } // Need to apply the config options so they can be ready by session_start $this->initIniOptions(); $this->registerSaveHandler(); // potential custom logic for session id (ex. switching between hosts) $this->setSessionId($this->sidResolver->getSid($this)); session_start(); $this->validator->validate($this); register_shutdown_function([$this, 'writeClose']); $this->_addHost(); \Magento\Framework\Profiler::stop('session_start'); } $this->storage->init(isset($_SESSION) ? $_SESSION : []); return $this; }
/** * @dataProvider getUrlDataProvider * @covers Magento\Catalog\Model\Product\Url::getUrl * @covers Magento\Catalog\Model\Product\Url::getUrlInStore * @covers Magento\Catalog\Model\Product\Url::getProductUrl * * @param $getUrlMethod * @param $routePath * @param $requestPathProduct * @param $storeId * @param $categoryId * @param $routeParams * @param $routeParamsUrl * @param $entityId * @param $idPath * @param $requestPathUrlRewrite * @param $productId * @param $productUrlKey * @SuppressWarnings(PHPMD.ExcessiveParameterList) */ public function testGetUrl($getUrlMethod, $routePath, $requestPathProduct, $storeId, $categoryId, $routeParams, $routeParamsUrl, $entityId, $idPath, $requestPathUrlRewrite, $productId, $productUrlKey) { $product = $this->getMockBuilder('Magento\\Catalog\\Model\\Product')->disableOriginalConstructor()->setMethods(['getStoreId', 'getEntityId', 'getId', 'getUrlKey', 'setRequestPath', 'hasUrlDataObject', 'getRequestPath', 'getCategoryId', 'getDoNotUseCategoryId', '__wakeup'])->getMock(); $product->expects($this->any())->method('getStoreId')->will($this->returnValue($storeId)); $product->expects($this->any())->method('getCategoryId')->will($this->returnValue($categoryId)); $product->expects($this->any())->method('getRequestPath')->will($this->returnValue($requestPathProduct)); $product->expects($this->any())->method('getEntityId')->will($this->returnValue($entityId)); $product->expects($this->any())->method('setRequestPath')->with($requestPathUrlRewrite)->will($this->returnSelf()); $product->expects($this->any())->method('getId')->will($this->returnValue($productId)); $product->expects($this->any())->method('getUrlKey')->will($this->returnValue($productUrlKey)); $this->url->expects($this->any())->method('setScope')->with($storeId)->will($this->returnSelf()); $this->url->expects($this->any())->method('getUrl')->with($routePath, $routeParamsUrl)->will($this->returnValue($requestPathProduct)); $this->urlRewrite->expects($this->any())->method('setStoreId')->with($storeId)->will($this->returnSelf()); $this->urlRewrite->expects($this->any())->method('loadByIdPath')->with($idPath)->will($this->returnSelf()); $this->urlRewrite->expects($this->any())->method('getId')->will($this->returnSelf()); $this->urlRewrite->expects($this->any())->method('getRequestPath')->will($this->returnValue($requestPathUrlRewrite)); switch ($getUrlMethod) { case 'getUrl': $this->assertEquals($requestPathProduct, $this->model->getUrl($product, $routeParams)); break; case 'getUrlInStore': $this->assertEquals($requestPathProduct, $this->model->getUrlInStore($product, $routeParams)); break; case 'getProductUrl': $this->assertEquals($requestPathProduct, $this->model->getProductUrl($product, true)); $this->sidResolver->expects($this->once())->method('getUseSessionInUrl')->will($this->returnValue(true)); $this->assertEquals($requestPathProduct, $this->model->getProductUrl($product, null)); break; } }
public function testSessionUrlVarWithoutMatchedHostsAndBaseUrl() { $requestMock = $this->getRequestMock(); $model = $this->getUrlModel( [ 'session' => $this->sessionMock, 'request' => $requestMock, 'sidResolver' => $this->sidResolverMock, 'scopeResolver' => $this->scopeResolverMock, 'routeParamsResolverFactory' => $this->getRouteParamsResolverFactory(), ] ); $requestMock->expects($this->once())->method('getHttpHost')->will($this->returnValue('localhost')); $this->scopeMock->expects($this->once()) ->method('getBaseUrl') ->will($this->returnValue('http://example.com')); $this->scopeResolverMock->expects($this->any()) ->method('getScope') ->will($this->returnValue($this->scopeMock)); $this->sidResolverMock->expects($this->once())->method('getSessionIdQueryParam') ->will($this->returnValue('SID')); $this->sessionMock->expects($this->once())->method('getSessionId') ->will($this->returnValue('session-id')); $this->assertEquals( '<a href="http://example.com/?SID=session-id">www.example.com</a>', $model->sessionUrlVar('<a href="http://example.com/?___SID=U">www.example.com</a>') ); }
/** * Run test toHtml method * * @param bool $customerId * @return void * * @dataProvider dataProviderToHtml */ public function testToHtml($customerId) { $cacheData = false; $idQueryParam = 'id-query-param'; $sessionId = 'session-id'; $this->additional->setData('cache_lifetime', 789); $this->additional->setData('cache_key', 'cache-key'); $this->eventManagerMock->expects($this->once())->method('dispatch')->with('view_block_abstract_to_html_before', ['block' => $this->additional]); $this->scopeConfigMock->expects($this->once())->method('getValue')->with('advanced/modules_disable_output/Magento_Persistent', \Magento\Store\Model\ScopeInterface::SCOPE_STORE)->willReturn(false); // get cache $this->cacheStateMock->expects($this->at(0))->method('isEnabled')->with(\Magento\Persistent\Block\Header\Additional::CACHE_GROUP)->willReturn(true); // save cache $this->cacheStateMock->expects($this->at(1))->method('isEnabled')->with(\Magento\Persistent\Block\Header\Additional::CACHE_GROUP)->willReturn(false); $this->cacheMock->expects($this->once())->method('load')->willReturn($cacheData); $this->sidResolverMock->expects($this->never())->method('getSessionIdQueryParam')->with($this->sessionMock)->willReturn($idQueryParam); $this->sessionMock->expects($this->never())->method('getSessionId')->willReturn($sessionId); // call protected _toHtml method $sessionMock = $this->getMock('Magento\\Persistent\\Model\\Session', ['getCustomerId'], [], '', false); $this->persistentSessionHelperMock->expects($this->atLeastOnce())->method('getSession')->willReturn($sessionMock); $sessionMock->expects($this->atLeastOnce())->method('getCustomerId')->willReturn($customerId); if ($customerId) { $this->assertEquals('<span><a >Not you?</a></span>', $this->additional->toHtml()); } else { $this->assertEquals('', $this->additional->toHtml()); } }
/** * Save block content to cache storage * * @param string $data * @return $this */ protected function _saveCache($data) { if ($this->getCacheLifetime() === null || !$this->_cacheState->isEnabled(self::CACHE_GROUP)) { return false; } $cacheKey = $this->getCacheKey(); $data = str_replace($this->_sidResolver->getSessionIdQueryParam($this->_session) . '=' . $this->_session->getSessionId(), $this->_getSidPlaceholder($cacheKey), $data); $this->_cache->save($data, $cacheKey, $this->getCacheTags(), $this->getCacheLifetime()); return $this; }
/** * Retrieve Product URL * * @param \Magento\Catalog\Model\Product $product * @param bool $useSid forced SID mode * @return string */ public function getProductUrl($product, $useSid = null) { if ($useSid === null) { $useSid = $this->_sidResolver->getUseSessionInUrl(); } $params = array(); if (!$useSid) { $params['_nosid'] = true; } return $this->getUrl($product, $params); }
/** * Configure session handler and start session * * @return $this */ public function start() { if (!$this->isSessionExists()) { \Magento\Framework\Profiler::start('session_start'); // Need to apply the config options so they can be ready by session_start $this->initIniOptions(); $this->registerSaveHandler(); // potential custom logic for session id (ex. switching between hosts) $this->setSessionId($this->sidResolver->getSid($this)); session_start(); $this->validator->validate($this); register_shutdown_function(array($this, 'writeClose')); $this->_addHost(); \Magento\Framework\Profiler::stop('session_start'); } $this->storage->init(isset($_SESSION) ? $_SESSION : array()); return $this; }
/** * Get storage instance * * @param array $arguments * @return \Magento\Store\Model\StoreManagerInterface * @throws \InvalidArgumentException */ public function get(array $arguments = []) { $className = $this->_storageClassName; if (false == isset($this->_cache[$className])) { /** @var $storage \Magento\Store\Model\StoreManagerInterface */ $storage = $this->_objectManager->create($className, $arguments); if (false === $storage instanceof \Magento\Store\Model\StoreManagerInterface) { throw new \InvalidArgumentException($className . ' doesn\'t implement \\Magento\\Store\\Model\\StoreManagerInterface'); } $this->_cache[$className] = $storage; if ($className === $this->_storageClassName) { $this->_reinitStores($storage, $arguments); $useSid = $this->_scopeConfig->isSetFlag(\Magento\Framework\Session\SidResolver::XML_PATH_USE_FRONTEND_SID, \Magento\Store\Model\ScopeInterface::SCOPE_STORE, $storage->getStore()); $this->_sidResolver->setUseSessionInUrl($useSid); $this->_eventManager->dispatch('core_app_init_current_store_after'); } } return $this->_cache[$className]; }
/** * Configure session handler and start session * * @param string $sessionName * @return $this */ public function start($sessionName = null) { if (!$this->isSessionExists()) { \Magento\Framework\Profiler::start('session_start'); if (!empty($sessionName)) { $this->setName($sessionName); } $this->registerSaveHandler(); // potential custom logic for session id (ex. switching between hosts) $this->setSessionId($this->sidResolver->getSid($this)); session_start(); $this->validator->validate($this); register_shutdown_function(array($this, 'writeClose')); $this->_addHost(); \Magento\Framework\Profiler::stop('session_start'); } $this->storage->init(isset($_SESSION) ? $_SESSION : array()); return $this; }
/** * Replace Session ID value in URL * * @param string $html * @return string */ public function sessionUrlVar($html) { return preg_replace_callback( '#(\?|&|&)___SID=([SU])(&|&)?#', // @codingStandardsIgnoreStart /** * Callback function for session replace * * @param array $match * @return string */ // @codingStandardsIgnoreEnd function ($match) { if ($this->useSessionIdForUrl($match[2] == 'S' ? true : false)) { return $match[1] . $this->_sidResolver->getSessionIdQueryParam($this->_session) . '=' . $this->_session->getSessionId() . (isset($match[3]) ? $match[3] : ''); } else { if ($match[1] == '?') { return isset($match[3]) ? '?' : ''; } elseif ($match[1] == '&' || $match[1] == '&') { return isset($match[3]) ? $match[3] : ''; } } }, $html ); }
/** * Retrieve current url for store * * @param bool|string $fromStore * @return string * @SuppressWarnings(PHPMD.CyclomaticComplexity) * @SuppressWarnings(PHPMD.NPathComplexity) */ public function getCurrentUrl($fromStore = true) { $sidQueryParam = $this->_sidResolver->getSessionIdQueryParam($this->_getSession()); $requestString = $this->_url->escape(ltrim($this->_request->getRequestString(), '/')); $storeUrl = $this->getUrl('', ['_secure' => $this->_storeManager->getStore()->isCurrentlySecure()]); if (!filter_var($storeUrl, FILTER_VALIDATE_URL)) { return $storeUrl; } $storeParsedUrl = parse_url($storeUrl); $storeParsedQuery = []; if (isset($storeParsedUrl['query'])) { parse_str($storeParsedUrl['query'], $storeParsedQuery); } $currQuery = $this->_request->getQueryValue(); if (isset($currQuery[$sidQueryParam]) && !empty($currQuery[$sidQueryParam]) && $this->_getSession()->getSessionIdForHost($storeUrl) != $currQuery[$sidQueryParam]) { unset($currQuery[$sidQueryParam]); } foreach ($currQuery as $key => $value) { $storeParsedQuery[$key] = $value; } if (!$this->isUseStoreInUrl()) { $storeParsedQuery['___store'] = $this->getCode(); } if ($fromStore !== false) { $storeParsedQuery['___from_store'] = $fromStore === true ? $this->_storeManager->getStore()->getCode() : $fromStore; } $currentUrl = $storeParsedUrl['scheme'] . '://' . $storeParsedUrl['host'] . (isset($storeParsedUrl['port']) ? ':' . $storeParsedUrl['port'] : '') . $storeParsedUrl['path'] . $requestString . ($storeParsedQuery ? '?' . http_build_query($storeParsedQuery, '', '&') : ''); return $currentUrl; }
/** * Replace Session ID value in URL * * @param string $html * @return string */ public function sessionUrlVar($html) { return preg_replace_callback('#(\\?|&|&)___SID=([SU])(&|&)?#', function ($match) { if ($this->useSessionIdForUrl($match[2] == 'S' ? true : false)) { return $match[1] . $this->_sidResolver->getSessionIdQueryParam($this->_session) . '=' . $this->_session->getSessionId() . (isset($match[3]) ? $match[3] : ''); } else { if ($match[1] == '?') { return isset($match[3]) ? '?' : ''; } elseif ($match[1] == '&' || $match[1] == '&') { return isset($match[3]) ? $match[3] : ''; } } }, $html); }