/** * Set Cookie for msg box when it displays first * * @param \Magento\Framework\App\FrontController $subject * @param \Magento\Framework\App\ResponseInterface $response * * @return \Magento\Framework\App\ResponseInterface * @SuppressWarnings(PHPMD.UnusedFormalParameter) */ public function afterDispatch(\Magento\Framework\App\FrontController $subject, \Magento\Framework\App\ResponseInterface $response) { if ($this->request->isPost() && $this->messageManager->hasMessages()) { $this->cookie->set(self::COOKIE_NAME, 1, self::COOKIE_PERIOD, '/'); } return $response; }
/** * @covers \Magento\Persistent\Model\Session::_afterDeleteCommit * @covers \Magento\Persistent\Model\Session::removePersistentCookie */ public function testAfterDeleteCommit() { $cookiePath = 'some_path'; $this->_configMock->expects($this->once())->method('getCookiePath')->will($this->returnValue($cookiePath)); $this->_cookieMock->expects($this->once())->method('set')->with(\Magento\Persistent\Model\Session::COOKIE_NAME, $this->anything(), $this->anything(), $cookiePath); $this->_model->delete(); }
/** * @dataProvider processProvider * @param bool $isPost */ public function testProcess($isPost) { $this->requestMock->expects($this->once())->method('isPost')->will($this->returnValue($isPost)); if ($isPost) { $this->cookieMock->expects($this->once())->method('set'); } $this->version->process(); }
public function testGet() { //Data $formKey = 'test from key'; //Verification $this->cookieMock->expects($this->once())->method('get')->with(\Magento\Framework\App\PageCache\FormKey::COOKIE_NAME)->will($this->returnValue($formKey)); $this->assertEquals($formKey, $this->formKey->get()); }
/** * Send Vary coookie * @return void */ public function sendVary() { $data = $this->context->getData(); if (!empty($data)) { ksort($data); $vary = sha1(serialize($data)); $this->cookie->set(self::COOKIE_VARY_STRING, $vary, null, '/'); } else { $this->cookie->set(self::COOKIE_VARY_STRING, null, -1, '/'); } }
/** * Handle private content message box cookie * Set cookie if it is not set. * Set or unset cookie on post request * In all other cases do nothing. */ public function testAfterDispatch() { $this->messageManagerMock->expects($this->once())->method('hasMessages')->will($this->returnValue(true)); $this->requestMock->expects($this->once())->method('isPost')->will($this->returnValue(true)); $this->cookieMock->expects($this->once())->method('set')->with($this->equalTo(MessageBox::COOKIE_NAME), 1, $this->equalTo(MessageBox::COOKIE_PERIOD), '/'); $this->assertInstanceOf('\\Magento\\Framework\\App\\ResponseInterface', $this->msgBox->afterDispatch($this->objectMock, $this->responseMock)); }
/** * Try to load valid order by $_POST or $_COOKIE * * @param App\RequestInterface $request * @param App\ResponseInterface $response * @return bool */ public function loadValidOrder(App\RequestInterface $request, App\ResponseInterface $response) { if ($this->_customerSession->isLoggedIn()) { $response->setRedirect($this->_urlBuilder->getUrl('sales/order/history')); return false; } $post = $request->getPost(); $errors = false; /** @var $order \Magento\Sales\Model\Order */ $order = $this->_orderFactory->create(); if (empty($post) && !$this->_coreCookie->get(self::COOKIE_NAME)) { $response->setRedirect($this->_urlBuilder->getUrl('sales/guest/form')); return false; } elseif (!empty($post) && isset($post['oar_order_id']) && isset($post['oar_type'])) { $type = $post['oar_type']; $incrementId = $post['oar_order_id']; $lastName = $post['oar_billing_lastname']; $email = $post['oar_email']; $zip = $post['oar_zip']; if (empty($incrementId) || empty($lastName) || empty($type) || !in_array($type, array('email', 'zip')) || $type == 'email' && empty($email) || $type == 'zip' && empty($zip)) { $errors = true; } if (!$errors) { $order->loadByIncrementId($incrementId); } $errors = true; if ($order->getId()) { $billingAddress = $order->getBillingAddress(); if (strtolower($lastName) == strtolower($billingAddress->getLastname()) && ($type == 'email' && strtolower($email) == strtolower($billingAddress->getEmail()) || $type == 'zip' && strtolower($zip) == strtolower($billingAddress->getPostcode()))) { $errors = false; } } if (!$errors) { $toCookie = base64_encode($order->getProtectCode() . ':' . $incrementId); $this->_coreCookie->set(self::COOKIE_NAME, $toCookie, self::COOKIE_LIFETIME, self::COOKIE_PATH); } } elseif ($this->_coreCookie->get(self::COOKIE_NAME)) { $fromCookie = $this->_coreCookie->get(self::COOKIE_NAME); $cookieData = explode(':', base64_decode($fromCookie)); $protectCode = isset($cookieData[0]) ? $cookieData[0] : null; $incrementId = isset($cookieData[1]) ? $cookieData[1] : null; $errors = true; if (!empty($protectCode) && !empty($incrementId)) { $order->loadByIncrementId($incrementId); if ($order->getProtectCode() == $protectCode) { $this->_coreCookie->renew(self::COOKIE_NAME, self::COOKIE_LIFETIME, self::COOKIE_PATH); $errors = false; } } } if (!$errors && $order->getId()) { $this->_coreRegistry->register('current_order', $order); return true; } $this->messageManager->addError(__('You entered incorrect data. Please try again.')); $response->setRedirect($this->_urlBuilder->getUrl('sales/guest/form')); return false; }
/** * @covers \Magento\Persistent\Model\Observer\Session::synchronizePersistentOnLogin */ public function testSynchronizePersistentOnLogin() { $event = new \Magento\Framework\Event(); $observer = new \Magento\Framework\Event\Observer(array('event' => $event)); /** @var \Magento\Customer\Service\V1\CustomerAccountServiceInterface $customerAccountService */ $customerAccountService = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()->create('Magento\\Customer\\Service\\V1\\CustomerAccountServiceInterface'); /** @var $customer \Magento\Customer\Service\V1\Data\Customer */ $customer = $customerAccountService->getCustomer(1); $event->setData('customer', $customer); $this->_persistentSession->setRememberMeChecked(true); $this->_cookieMock->expects($this->once())->method('set')->with(\Magento\Persistent\Model\Session::COOKIE_NAME, $this->anything(), $this->anything(), $this->_customerSession->getCookiePath()); $this->_model->synchronizePersistentOnLogin($observer); // check that persistent session has been stored for Customer /** @var \Magento\Persistent\Model\Session $sessionModel */ $sessionModel = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()->create('Magento\\Persistent\\Model\\Session'); $sessionModel->loadByCustomerId(1); $this->assertEquals(1, $sessionModel->getCustomerId()); }
/** * Set session UpdatedAt to current time and update cookie expiration time * * @return void */ public function prolong() { $lifetime = $this->_config->getValue(self::XML_PATH_SESSION_LIFETIME); $currentTime = time(); $this->setUpdatedAt($currentTime); $cookieValue = $this->_cookie->get($this->getName()); if ($cookieValue) { $this->_cookie->set($this->getName(), $cookieValue, $lifetime, $this->sessionConfig->getCookiePath(), $this->sessionConfig->getCookieDomain(), $this->sessionConfig->getCookieSecure(), $this->sessionConfig->getCookieHttpOnly()); } }
/** * Renew persistent cookie * * @param Observer $observer * @return void */ public function renewCookie(Observer $observer) { if (!$this->_persistentData->canProcess($observer) || !$this->_persistentData->isEnabled() || !$this->_persistentSession->isPersistent()) { return; } /** @var $request \Magento\Framework\App\RequestInterface */ $request = $observer->getEvent()->getRequest(); if ($this->_customerSession->isLoggedIn() || $request->getFullActionName() == 'customer_account_logout') { $this->_cookie->renew(\Magento\Persistent\Model\Session::COOKIE_NAME, $this->_persistentData->getLifeTime(), $this->_customerSession->getCookiePath()); } }
/** * @covers \Magento\Store\Model\StorageFactory::_checkCookieStore * @covers \Magento\Store\Model\StorageFactory::getActiveStoreByCode * @covers \Magento\Store\Model\StorageFactory::setCurrentStore * * @dataProvider getFromCookieDataProvider */ public function testGetFromCookie($scopeCode, $scopeType) { $this->_arguments['scopeCode'] = $scopeCode; $this->_arguments['scopeType'] = $scopeType; $this->_appStateMock->expects($this->once())->method('isInstalled')->will($this->returnValue(true)); $this->website->expects($this->any())->method('getDefaultGroupId')->will($this->returnValue(11)); $this->group->expects($this->any())->method('getDefaultStoreId')->will($this->returnValue(21)); $this->store->expects($this->once())->method('getId')->will($this->returnValue(21)); $this->store->expects($this->once())->method('getIsActive')->will($this->returnValue(true)); $this->storage->expects($this->any())->method('setCurrentStore')->with('store1'); $this->_objectManagerMock->expects($this->once())->method('create')->will($this->returnValue($this->storage)); $this->_cookie->expects($this->atLeastOnce())->method('get')->will($this->returnValue('store1')); $this->assertEquals($this->storage, $this->_model->get($this->_arguments)); }
public function testLoadValidOrderStoredCookie() { $this->sessionMock->expects($this->once())->method('isLoggedIn')->will($this->returnValue(false)); $orderMock = $this->getMock('Magento\\Sales\\Model\\Order', ['getProtectCode', 'loadByIncrementId', 'getId', 'getBillingAddress', '__wakeup'], [], '', false); $protectedCode = 'protectedCode'; $incrementId = 1; $cookieData = $protectedCode . ':' . $incrementId; $cookieDataHash = base64_encode($cookieData); $this->orderFactoryMock->expects($this->once())->method('create')->will($this->returnValue($orderMock)); $this->cookieMock->expects($this->exactly(3))->method('get')->with(Guest::COOKIE_NAME)->will($this->returnValue($cookieDataHash)); $orderMock->expects($this->once())->method('loadByIncrementId')->with($incrementId); $orderMock->expects($this->exactly(1))->method('getId')->will($this->returnValue($incrementId)); $orderMock->expects($this->once())->method('getProtectCode')->will($this->returnValue($protectedCode)); $this->cookieMock->expects($this->once())->method('renew')->with(Guest::COOKIE_NAME, Guest::COOKIE_LIFETIME, Guest::COOKIE_PATH); $requestMock = $this->getMock('Magento\\Framework\\App\\Request\\Http', [], [], '', false); $responseMock = $this->getMock('Magento\\Framework\\App\\Response\\Http', [], [], '', false); $this->assertTrue($this->guest->loadValidOrder($requestMock, $responseMock)); }
/** * @param \Magento\Store\Model\StoreManagerInterface $storage * @param string $scopeType * @return void */ protected function _checkRequestStore(\Magento\Store\Model\StoreManagerInterface $storage, $scopeType) { $storeCode = $this->request->getParam('___store'); if (empty($storeCode)) { return; } if (!$this->setCurrentStore($storage, $storeCode, $scopeType)) { return; } if ($storage->getStore()->getCode() == $storeCode) { $store = $storage->getStore($storeCode); if ($store->getWebsite()->getDefaultStore()->getId() == $store->getId()) { $this->_cookie->set(Store::COOKIE_NAME, null); } else { $this->_cookie->set(Store::COOKIE_NAME, $storage->getStore()->getCode(), true); $this->_httpContext->setValue(Store::ENTITY, $storage->getStore()->getCode(), \Magento\Store\Model\Store::DEFAULT_CODE); } } return; }
/** * Handle private content version cookie * Set cookie if it is not set. * Increment version on post requests. * In all other cases do nothing. * * @return void */ public function process() { if ($this->request->isPost()) { $this->cookie->set(self::COOKIE_NAME, $this->generateValue(), self::COOKIE_PERIOD, '/'); } }
/** * Get products per page limit * * @return string|bool */ public function getLimit() { return $this->cookie->get(self::LIMIT_COOKIE_NAME); }
public function testSendVaryEmptyData() { $this->_cookieMock->expects($this->once())->method('set')->with(Http::COOKIE_VARY_STRING, null, -1, '/'); $this->_model->sendVary(); }
/** * Perform custom url rewrites * * @param \Magento\Framework\App\RequestInterface $request * @return bool */ public function rewrite(\Magento\Framework\App\RequestInterface $request = null) { if (!$this->_appState->isInstalled()) { return false; } if (is_null($this->getStoreId()) || false === $this->getStoreId()) { $this->setStoreId($this->_storeManager->getStore()->getId()); } /** * We have two cases of incoming paths - with and without slashes at the end ("/somepath/" and "/somepath"). * Each of them matches two url rewrite request paths - with and without slashes at the end * ("/somepath/" and "/somepath"). * Choose any matched rewrite, but in priority order that depends on same presence of slash and query params. */ $requestCases = array(); $pathInfo = $request->getPathInfo(); $origSlash = substr($pathInfo, -1) == '/' ? '/' : ''; $requestPath = trim($pathInfo, '/'); // If there were final slash - add nothing to less priority paths. And vice versa. $altSlash = $origSlash ? '' : '/'; $queryString = $this->_getQueryString(); // Query params in request, matching "path + query" has more priority if ($queryString) { $requestCases[] = $requestPath . $origSlash . '?' . $queryString; $requestCases[] = $requestPath . $altSlash . '?' . $queryString; } $requestCases[] = $requestPath . $origSlash; $requestCases[] = $requestPath . $altSlash; $this->loadByRequestPath($requestCases); /** * Try to find rewrite by request path at first, if no luck - try to find by id_path */ if (!$this->getId() && isset($_GET['___from_store'])) { try { $fromStoreId = $this->_storeManager->getStore($_GET['___from_store'])->getId(); } catch (\Exception $e) { return false; } $this->setStoreId($fromStoreId)->loadByRequestPath($requestCases); if (!$this->getId()) { return false; } $currentStore = $this->_storeManager->getStore(); $this->setStoreId($currentStore->getId())->loadByIdPath($this->getIdPath()); $this->_cookie->set(\Magento\Store\Model\Store::COOKIE_NAME, $currentStore->getCode(), true); $targetUrl = $request->getBaseUrl() . '/' . $this->getRequestPath(); $this->_sendRedirectHeaders($targetUrl, true); } if (!$this->getId()) { return false; } $request->setAlias(\Magento\Framework\Url::REWRITE_REQUEST_PATH_ALIAS, $this->getRequestPath()); $external = substr($this->getTargetPath(), 0, 6); $isPermanentRedirectOption = $this->hasOption('RP'); if ($external === 'http:/' || $external === 'https:') { $destinationStoreCode = $this->_storeManager->getStore($this->getStoreId())->getCode(); $this->_cookie->set(\Magento\Store\Model\Store::COOKIE_NAME, $destinationStoreCode, true); $this->_sendRedirectHeaders($this->getTargetPath(), $isPermanentRedirectOption); } else { $targetUrl = $request->getBaseUrl() . '/' . $this->getTargetPath(); } $isRedirectOption = $this->hasOption('R'); $isStoreInUrl = $this->_scopeConfig->getValue(\Magento\Store\Model\Store::XML_PATH_STORE_IN_URL, \Magento\Store\Model\ScopeInterface::SCOPE_STORE); if ($isRedirectOption || $isPermanentRedirectOption) { if ($isStoreInUrl && ($storeCode = $this->_storeManager->getStore()->getCode())) { $targetUrl = $request->getBaseUrl() . '/' . $storeCode . '/' . $this->getTargetPath(); } $this->_sendRedirectHeaders($targetUrl, $isPermanentRedirectOption); } if ($isStoreInUrl && ($storeCode = $this->_storeManager->getStore()->getCode())) { $targetUrl = $request->getBaseUrl() . '/' . $storeCode . '/' . $this->getTargetPath(); } $queryString = $this->_getQueryString(); if ($queryString) { $targetUrl .= '?' . $queryString; } $request->setRequestUri($targetUrl); $request->setPathInfo($this->getTargetPath()); return true; }
/** * Set current store currency code * * @param string $code * @return string */ public function setCurrentCurrencyCode($code) { $code = strtoupper($code); if (in_array($code, $this->getAvailableCurrencyCodes())) { $this->_getSession()->setCurrencyCode($code); $path = $this->_getSession()->getCookiePath(); if ($code == $this->getDefaultCurrency()->getCurrencyCode()) { $this->_cookie->set(self::COOKIE_CURRENCY, null, null, $path); } else { $this->_cookie->set(self::COOKIE_CURRENCY, $code, null, $path); } $this->_httpContext->setValue(\Magento\Core\Helper\Data::CONTEXT_CURRENCY, $code, $this->_storeManager->getWebsite()->getDefaultStore()->getDefaultCurrency()->getCode()); } return $this; }
/** * Remove persistent cookie * * @return $this */ public function removePersistentCookie() { $this->_cookie->set(self::COOKIE_NAME, null, null, $this->sessionConfig->getCookiePath()); return $this; }
/** * Get form key cookie * * @return string */ public function get() { return $this->cookie->get(self::COOKIE_NAME); }