public function testGetCookie() { $key = "cookieName"; $default = "defaultValue"; $this->cookieManager->expects($this->once())->method('getCookie')->with($key, $default); $this->request->getCookie($key, $default); }
public function testGet() { //Data $formKey = 'test_from_key'; //Verification $this->cookieManagerMock->expects($this->once())->method('getCookie')->with(\Magento\Framework\App\PageCache\FormKey::COOKIE_NAME)->will($this->returnValue($formKey)); $this->assertEquals($formKey, $this->formKey->get()); }
public function testSentCountByCookies() { $cookieName = 'testCookieName'; $this->sendfriendDataMock->expects($this->once())->method('getCookieName')->with()->will($this->returnValue($cookieName)); $this->cookieManagerMock->expects($this->once())->method('getCookie')->with($cookieName); $this->cookieManagerMock->expects($this->once())->method('setPublicCookie'); $sendFriendClass = new \ReflectionClass('Magento\\Sendfriend\\Model\\Sendfriend'); $method = $sendFriendClass->getMethod('_sentCountByCookies'); $method->setAccessible(true); $method->invokeArgs($this->model, [true]); }
/** * @dataProvider processProvider * @param bool $isPost */ public function testProcess($isPost) { $this->requestMock->expects($this->once())->method('isPost')->will($this->returnValue($isPost)); if ($isPost) { $publicCookieMetadataMock = $this->getMock('Magento\\Framework\\Stdlib\\Cookie\\PublicCookieMetadata'); $publicCookieMetadataMock->expects($this->once())->method('setPath')->with('/')->will($this->returnSelf()); $publicCookieMetadataMock->expects($this->once())->method('setDuration')->with(Version::COOKIE_PERIOD)->will($this->returnSelf()); $this->cookieMetadataFactoryMock->expects($this->once())->method('createPublicCookieMetadata')->with()->will($this->returnValue($publicCookieMetadataMock)); $this->cookieManagerMock->expects($this->once())->method('setPublicCookie'); } $this->version->process(); }
/** * 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()) { $publicCookieMetadata = $this->cookieMetadataFactory->createPublicCookieMetadata()->setDuration(self::COOKIE_PERIOD)->setPath('/')->setHttpOnly(false); $this->cookieManager->setPublicCookie(self::COOKIE_NAME, $this->generateValue(), $publicCookieMetadata); } }
/** * Set Cookie for msg box when it displays first * * @param FrontController $subject * @param ResponseInterface $response * * @return ResponseInterface * @SuppressWarnings(PHPMD.UnusedFormalParameter) */ public function afterDispatch(FrontController $subject, ResponseInterface $response) { if ($this->request->isPost() && $this->messageManager->hasMessages()) { $publicCookieMetadata = $this->cookieMetadataFactory->createPublicCookieMetadata()->setDuration(self::COOKIE_PERIOD)->setPath('/'); $this->cookieManager->setPublicCookie(self::COOKIE_NAME, 1, $publicCookieMetadata); } return $response; }
public function testSendVaryEmptyData() { $expectedCookieName = Http::COOKIE_VARY_STRING; $cookieMetadataMock = $this->getMock('Magento\\Framework\\Stdlib\\Cookie\\CookieMetadata'); $cookieMetadataMock->expects($this->once())->method('setPath')->with('/')->will($this->returnSelf()); $this->cookieMetadataFactoryMock->expects($this->once())->method('createCookieMetadata')->with()->will($this->returnValue($cookieMetadataMock)); $this->cookieManagerMock->expects($this->once())->method('deleteCookie')->with($expectedCookieName, $cookieMetadataMock); $this->model->sendVary(); }
/** * @param bool $isPost * @param int $numOfCalls * @dataProvider afterDispatchTestDataProvider */ public function testAfterDispatch($isPost, $numOfCalls) { $this->messageManagerMock->expects($this->exactly($numOfCalls))->method('hasMessages')->will($this->returnValue(true)); $this->requestMock->expects($this->once())->method('isPost')->will($this->returnValue($isPost)); $this->cookieMetadataFactoryMock->expects($this->exactly($numOfCalls))->method('createPublicCookieMetadata')->will($this->returnValue($this->publicCookieMetadataMock)); $this->publicCookieMetadataMock->expects($this->exactly($numOfCalls))->method('setDuration')->with(MessageBox::COOKIE_PERIOD)->will($this->returnValue($this->publicCookieMetadataMock)); $this->publicCookieMetadataMock->expects($this->exactly($numOfCalls))->method('setPath')->with('/')->will($this->returnValue($this->publicCookieMetadataMock)); $this->cookieManagerMock->expects($this->exactly($numOfCalls))->method('setPublicCookie')->with(MessageBox::COOKIE_NAME, 1, $this->publicCookieMetadataMock); $this->assertSame($this->responseMock, $this->msgBox->afterDispatch($this->objectMock, $this->responseMock)); }
/** * Postpone cookie expiration time if cookie value defined * * @param int $duration Time in seconds. * @param string $path * @return $this */ public function renewPersistentCookie($duration, $path) { if ($duration === null) { return $this; } $value = $this->_cookieManager->getCookie(self::COOKIE_NAME); if (null !== $value) { $publicCookieMetadata = $this->_cookieMetadataFactory->createPublicCookieMetadata()->setDuration($duration)->setPath($path); $this->_cookieManager->setPublicCookie(self::COOKIE_NAME, $value, $publicCookieMetadata); } return $this; }
/** * Send Vary coookie * * @return void */ public function sendVary() { $data = $this->context->getData(); if (!empty($data)) { ksort($data); $cookieValue = sha1(serialize($data)); $sensitiveCookMetadata = $this->cookieMetadataFactory->createSensitiveCookieMetadata()->setPath('/'); $this->cookieManager->setSensitiveCookie(self::COOKIE_VARY_STRING, $cookieValue, $sensitiveCookMetadata); } else { $cookieMetadata = $this->cookieMetadataFactory->createCookieMetadata()->setPath('/'); $this->cookieManager->deleteCookie(self::COOKIE_VARY_STRING, $cookieMetadata); } }
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->cookieManagerMock->expects($this->once())->method('getCookie')->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)); $metaData = new \Magento\Framework\Stdlib\Cookie\PublicCookieMetadata(); $this->cookieMetadataFactoryMock->expects($this->once())->method('createPublicCookieMetadata')->will($this->returnValue($metaData)); $this->cookieManagerMock->expects($this->once())->method('setPublicCookie')->with(Guest::COOKIE_NAME, $this->anything(), $metaData); $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->_cookieManager->deleteCookie(Store::COOKIE_NAME); } else { $publicCookieMetadata = $this->cookieMetadataFactory->createPublicCookieMetadata()->setDurationOneYear(); $this->_cookieManager->setPublicCookie(Store::COOKIE_NAME, $storage->getStore()->getCode(), $publicCookieMetadata); $this->_httpContext->setValue(Store::ENTITY, $storage->getStore()->getCode(), \Magento\Store\Model\Store::DEFAULT_CODE); } } return; }
/** * Delete store cookie. * * @return $this */ public function deleteCookie() { $this->_cookieManager->deleteCookie(self::COOKIE_NAME); return $this; }
/** * Return count of sent in last period by cookie * * @param bool $increment - flag, increase count before return value * @return int */ protected function _sentCountByCookies($increment = false) { $cookieName = $this->_sendfriendData->getCookieName(); $time = time(); $newTimes = array(); if (isset($this->_lastCookieValue[$cookieName])) { $oldTimes = $this->_lastCookieValue[$cookieName]; } else { $oldTimes = $this->cookieManager->getCookie($cookieName); } if ($oldTimes) { $oldTimes = explode(',', $oldTimes); foreach ($oldTimes as $oldTime) { $periodTime = $time - $this->_sendfriendData->getPeriod(); if (is_numeric($oldTime) and $oldTime >= $periodTime) { $newTimes[] = $oldTime; } } } if ($increment) { $newTimes[] = $time; $newValue = implode(',', $newTimes); $this->cookieManager->setSensitiveCookie($cookieName, $newValue); $this->_lastCookieValue[$cookieName] = $newValue; } return count($newTimes); }
/** * Expire the session cookie * * Sends a session cookie with no value, and with an expiry in the past. * * @return void */ public function expireSessionCookie() { if (!$this->sessionConfig->getUseCookies()) { return; } $metadata = $this->cookieMetadataFactory->createPublicCookieMetadata(); $metadata->setPath($this->sessionConfig->getCookiePath()); $metadata->setDomain($this->sessionConfig->getCookieDomain()); $metadata->setSecure($this->sessionConfig->getCookieSecure()); $metadata->setHttpOnly($this->sessionConfig->getCookieHttpOnly()); $this->cookieManager->deleteCookie($this->getName(), $metadata); $this->clearSubDomainSessionCookie(); }
/** * Get products per page limit * * @return string|bool */ public function getLimit() { return $this->cookieManager->getCookie(self::LIMIT_COOKIE_NAME); }
/** * 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(); $sensitiveCookieMetadata = $this->_cookieMetadataFactory->createSensitiveCookieMetadata()->setPath($path); if ($code == $this->getDefaultCurrency()->getCurrencyCode()) { $this->_cookieManager->deleteCookie(self::COOKIE_CURRENCY, $sensitiveCookieMetadata); } else { $this->_cookieManager->setSensitiveCookie(self::COOKIE_CURRENCY, $code, $sensitiveCookieMetadata); } $this->_httpContext->setValue(\Magento\Core\Helper\Data::CONTEXT_CURRENCY, $code, $this->_storeManager->getWebsite()->getDefaultStore()->getDefaultCurrency()->getCode()); } return $this; }
/** * Retrieve a value from a cookie. * * @param string|null $name * @param string|null $default The default value to return if no value could be found for the given $name. * @return string|null */ public function getCookie($name = null, $default = null) { return $this->_cookieManager->getCookie($name, $default); }
/** * 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); $targetUrl = $request->getBaseUrl(); /** * 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()); $cookieMetadata = $this->_cookieMetadataFactory->createPublicCookieMetadata()->setDurationOneYear(); $this->_cookieManager->setPublicCookie(\Magento\Store\Model\Store::COOKIE_NAME, $currentStore->getCode(), $cookieMetadata); $targetUrl .= '/' . $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(); $cookieMetadata = $this->_cookieMetadataFactory->createPublicCookieMetadata()->setDurationOneYear(); $this->_cookieManager->setPublicCookie(\Magento\Store\Model\Store::COOKIE_NAME, $destinationStoreCode, $cookieMetadata); $this->_sendRedirectHeaders($this->getTargetPath(), $isPermanentRedirectOption); } else { $targetUrl .= '/' . $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 .= '/' . $storeCode . '/' . $this->getTargetPath(); } $this->_sendRedirectHeaders($targetUrl, $isPermanentRedirectOption); } if ($isStoreInUrl && ($storeCode = $this->_storeManager->getStore()->getCode())) { $targetUrl .= '/' . $storeCode . '/' . $this->getTargetPath(); } $queryString = $this->_getQueryString(); if ($queryString) { $targetUrl .= '?' . $queryString; } $request->setRequestUri($targetUrl); $request->setPathInfo($this->getTargetPath()); return true; }
/** * @param \Magento\Framework\App\Action\Action $subject * @param callable $proceed * @param \Magento\Framework\App\RequestInterface $request * @return mixed */ public function aroundDispatch(\Magento\Framework\App\Action\Action $subject, \Closure $proceed, \Magento\Framework\App\RequestInterface $request) { $this->httpContext->setValue(\Magento\Core\Helper\Data::CONTEXT_CURRENCY, $this->session->getCurrencyCode(), $this->storeManager->getWebsite()->getDefaultStore()->getDefaultCurrency()->getCode()); $this->httpContext->setValue(\Magento\Core\Helper\Data::CONTEXT_STORE, $this->httpRequest->getParam('___store', $this->cookieManager->getCookie(\Magento\Store\Model\Store::COOKIE_NAME)), $this->storeManager->getWebsite()->getDefaultStore()->getCode()); return $proceed($request); }
/** * Set guest-view cookie * * @param string $cookieValue * @return void */ private function setGuestViewCookie($cookieValue) { $metadata = $this->cookieMetadataFactory->createPublicCookieMetadata()->setPath(self::COOKIE_PATH)->setDuration(self::COOKIE_LIFETIME)->setHttpOnly(true); $this->cookieManager->setPublicCookie(self::COOKIE_NAME, $cookieValue, $metadata); }
/** * Set persistent shopping cart cookie. * * @param string $value * @param int $duration * @param string $path * @return void */ private function setCookie($value, $duration, $path) { $publicCookieMetadata = $this->_cookieMetadataFactory->createPublicCookieMetadata()->setDuration($duration)->setPath($path)->setHttpOnly(true); $this->_cookieManager->setPublicCookie(self::COOKIE_NAME, $value, $publicCookieMetadata); }
/** * Try to load valid order by $_POST or $_COOKIE * * @param App\RequestInterface $request * @param App\ResponseInterface $response * @return bool * * @SuppressWarnings(PHPMD.CyclomaticComplexity) * @SuppressWarnings(PHPMD.NPathComplexity) */ 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(); $fromCookie = $this->cookieManager->getCookie(self::COOKIE_NAME); if (empty($post) && !$fromCookie) { $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); $metadata = $this->cookieMetadataFactory->createPublicCookieMetadata(); $metadata->setPath(self::COOKIE_PATH); $metadata->setDuration(self::COOKIE_LIFETIME); $this->cookieManager->setPublicCookie(self::COOKIE_NAME, $toCookie, $metadata); } } elseif ($fromCookie) { $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) { // renew cookie $metadata = $this->cookieMetadataFactory->createPublicCookieMetadata(); $metadata->setPath(self::COOKIE_PATH); $metadata->setDuration(self::COOKIE_LIFETIME); $this->cookieManager->setPublicCookie(self::COOKIE_NAME, $fromCookie, $metadata); $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; }