/** * 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); } }
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()); }
/** * Set Cookie for msg box when it displays first * * @param FrontController $subject * @param \Magento\Framework\App\ResponseInterface|\Magento\Framework\Controller\ResultInterface $result * * @return \Magento\Framework\App\ResponseInterface|\Magento\Framework\Controller\ResultInterface * @SuppressWarnings(PHPMD.UnusedFormalParameter) */ public function afterDispatch(FrontController $subject, $result) { if ($this->request->isPost() && $this->messageManager->hasMessages()) { $publicCookieMetadata = $this->cookieMetadataFactory->createPublicCookieMetadata()->setDuration(self::COOKIE_PERIOD)->setPath('/')->setHttpOnly(false); $this->cookieManager->setPublicCookie(self::COOKIE_NAME, 1, $publicCookieMetadata); } return $result; }
/** * get FB cookie * @param CookieManagerInterface $cookieManager * @param CookieMetadataFactory $cookieMetadataFactory * @param \BelVG\FacebookFree\Helper\Data $dataHelper */ public function __construct(CookieManagerInterface $cookieManager, CookieMetadataFactory $cookieMetadataFactory, \BelVG\FacebookFree\Helper\Data $dataHelper) { $this->cookieManager = $cookieManager; $this->cookieMetadataFactory = $cookieMetadataFactory; $this->dataHelper = $dataHelper; $cookieName = self::FB_COOKIE_PREFIX . $this->dataHelper->getAppId(); $this->fbCookie = $this->cookieManager->getCookie($cookieName); }
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(); }
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('setSensitiveCookie'); $sendFriendClass = new \ReflectionClass('Magento\\Sendfriend\\Model\\Sendfriend'); $method = $sendFriendClass->getMethod('_sentCountByCookies'); $method->setAccessible(true); $method->invokeArgs($this->model, [true]); }
/** * @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->publicCookieMetadataMock->expects($this->exactly($numOfCalls))->method('setHttpOnly')->with(false)->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)); }
/** * Send Vary cookie * * @return void */ public function sendVary() { $varyString = $this->context->getVaryString(); if ($varyString) { $sensitiveCookMetadata = $this->cookieMetadataFactory->createSensitiveCookieMetadata()->setPath('/'); $this->cookieManager->setSensitiveCookie(self::COOKIE_VARY_STRING, $varyString, $sensitiveCookMetadata); } elseif ($this->request->get(self::COOKIE_VARY_STRING)) { $cookieMetadata = $this->cookieMetadataFactory->createSensitiveCookieMetadata()->setPath('/'); $this->cookieManager->deleteCookie(self::COOKIE_VARY_STRING, $cookieMetadata); } }
/** * @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()); $publicCookieMetadataMock->expects($this->once())->method('setHttpOnly')->with(false)->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(); }
/** * Send Vary cookie * * @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 testDelete() { $cookiePath = '/'; $cookieDomain = 'example.com'; /** @var PublicCookieMetadata|\PHPUnit_Framework_MockObject_MockObject $metadata */ $metadata = $this->getMockBuilder('Magento\\Framework\\Stdlib\\Cookie\\PublicCookieMetadata')->disableOriginalConstructor()->getMock(); $this->cookieMetadataFactory->expects(static::once())->method('createCookieMetadata')->willReturn($metadata); $this->sessionManager->expects(static::once())->method('getCookiePath')->willReturn($cookiePath); $metadata->expects(static::once())->method('setPath')->with($cookiePath)->willReturnSelf(); $this->sessionManager->expects(static::once())->method('getCookieDomain')->willReturn($cookieDomain); $metadata->expects(static::once())->method('setDomain')->with($cookieDomain)->willReturnSelf(); $this->cookieManagerMock->expects(static::once())->method('deleteCookie')->with(FormKey::COOKIE_NAME, $metadata); $this->formKey->delete(); }
/** * get user code. * * @param mixed $id * * @return string */ protected function getUserCode($id) { $ipAddress = $this->_phpEnvironmentRequest->getClientIp(true); $cookiefrontend = $this->_cookieManager->getCookie('frontend'); $usercode = $ipAddress . $cookiefrontend . $id; return md5($usercode); }
public function testAfterRenderResultWithWrongArray() { $messageType = 'message1type'; $messageText = 'message1text'; $messages = [['type' => $messageType, 'text' => $messageText]]; /** @var Redirect|\PHPUnit_Framework_MockObject_MockObject $resultMock */ $resultMock = $this->getMockBuilder(Redirect::class)->disableOriginalConstructor()->getMock(); /** @var PublicCookieMetadata|\PHPUnit_Framework_MockObject_MockObject $cookieMetadataMock */ $cookieMetadataMock = $this->getMockBuilder(PublicCookieMetadata::class)->disableOriginalConstructor()->getMock(); $this->cookieMetadataFactoryMock->expects($this->once())->method('createPublicCookieMetadata')->willReturn($cookieMetadataMock); $this->cookieManagerMock->expects($this->once())->method('setPublicCookie')->with(MessagePlugin::MESSAGES_COOKIES_NAME, \Zend_Json::encode($messages), $cookieMetadataMock); $this->cookieManagerMock->expects($this->once())->method('getCookie')->with(MessagePlugin::MESSAGES_COOKIES_NAME, \Zend_Json::encode([]))->willReturn(\Zend_Json::encode('string')); $this->dataMock->expects($this->any())->method('jsonDecode')->willReturnCallback(function ($data) { return \Zend_Json::decode($data); }); $this->dataMock->expects($this->any())->method('jsonEncode')->willReturnCallback(function ($data) { return \Zend_Json::encode($data); }); /** @var MessageInterface|\PHPUnit_Framework_MockObject_MockObject $messageMock */ $messageMock = $this->getMockBuilder(MessageInterface::class)->getMock(); $messageMock->expects($this->once())->method('getType')->willReturn($messageType); $this->interpretationStrategyMock->expects($this->once())->method('interpret')->with($messageMock)->willReturn($messageText); /** @var Collection|\PHPUnit_Framework_MockObject_MockObject $collectionMock */ $collectionMock = $this->getMockBuilder(Collection::class)->disableOriginalConstructor()->getMock(); $collectionMock->expects($this->once())->method('getItems')->willReturn([$messageMock]); $this->managerMock->expects($this->once())->method('getMessages')->with(true, null)->willReturn($collectionMock); $this->assertEquals($resultMock, $this->model->afterRenderResult($resultMock, $resultMock)); }
/** * Return messages stored in cookies * * @return array */ protected function getCookiesMessages() { try { $messages = $this->jsonHelper->jsonDecode($this->cookieManager->getCookie(self::MESSAGES_COOKIES_NAME, $this->jsonHelper->jsonEncode([]))); if (!is_array($messages)) { $messages = []; } } catch (\Zend_Json_Exception $e) { $messages = []; } return $messages; }
/** @inheritdoc */ public function processHttpRequest($getVar) { /* get code from cookie */ $cookie = $this->_cookieManager->getCookie(static::COOKIE_REFERRAL_CODE); $voCookie = new ReferralCookie($cookie); /* replace cookie value if GET code is not equal to cookie value */ if ($getVar && $getVar != $voCookie->getCode()) { $tsSaved = $this->_toolDate->getUtcNow(); $saved = $tsSaved->format('Ymd'); $voCookie->setCode($getVar); $voCookie->setDateSaved($saved); $cookie = $voCookie->generateCookieValue(); $meta = new \Magento\Framework\Stdlib\Cookie\PublicCookieMetadata(); $meta->setPath('/'); $meta->setDurationOneYear(); $this->_cookieManager->setPublicCookie(static::COOKIE_REFERRAL_CODE, $cookie, $meta); } /* save referral code into the registry */ $code = $voCookie->getCode(); $this->replaceCodeInRegistry($code); }
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)); $metaDataMock = $this->getMock('Magento\\Framework\\Stdlib\\Cookie\\PublicCookieMetadata', [], [], '', false); $metaDataMock->expects($this->once())->method('setPath')->with(Guest::COOKIE_PATH)->will($this->returnSelf()); $metaDataMock->expects($this->once())->method('setHttpOnly')->with(true)->will($this->returnSelf()); $this->cookieMetadataFactoryMock->expects($this->once())->method('createPublicCookieMetadata')->will($this->returnValue($metaDataMock)); $this->cookieManagerMock->expects($this->once())->method('setPublicCookie')->with(Guest::COOKIE_NAME, $this->anything(), $metaDataMock); $requestMock = $this->getMock('Magento\\Framework\\App\\Request\\Http', [], [], '', false); $this->assertTrue($this->guest->loadValidOrder($requestMock)); }
/** * 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(); }
/** * 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); }
/** * Delete store cookie. * * @return $this */ public function deleteCookie() { $cookieMetadata = $this->_cookieMetadataFactory->createPublicCookieMetadata()->setPath($this->getStorePath()); $this->_cookieManager->deleteCookie(self::COOKIE_NAME, $cookieMetadata); 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 = []; 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); }
/** * {@inheritdoc} */ public function deleteStoreCookie(StoreInterface $store) { $cookieMetadata = $this->cookieMetadataFactory->createPublicCookieMetadata()->setPath($store->getStorePath()); $this->cookieManager->deleteCookie(self::COOKIE_NAME, $cookieMetadata); }
/** * @return void */ public function delete() { $this->cookieManager->deleteCookie(self::COOKIE_NAME, $this->cookieMetadataFactory->createCookieMetadata()->setPath($this->sessionManager->getCookiePath())->setDomain($this->sessionManager->getCookieDomain())); }
/** * Set guest-view cookie * * @param string $cookieValue * @return void */ private function setGuestViewCookie($cookieValue) { $metadata = $this->cookieMetadataFactory->createPublicCookieMetadata()->setPath(self::COOKIE_PATH)->setHttpOnly(true); $this->cookieManager->setPublicCookie(self::COOKIE_NAME, $cookieValue, $metadata); }
/** * Get form key cookie * * @return string */ public function get() { return $this->cookieManager->getCookie(self::COOKIE_NAME); }
/** * Delete debug cookie. * * @return $this */ public function deleteDebugCookie() { $cookieMetadata = $this->_cookieMetadataFactory->createPublicCookieMetadata(); $this->_cookieManager->deleteCookie(self::COOKIE_NAME, $cookieMetadata); return $this; }