/** * @magentoConfigFixture current_store persistent/options/enabled 1 * @magentoConfigFixture current_store persistent/options/remember_enabled 1 * @magentoConfigFixture current_store persistent/options/remember_default 1 * @magentoAppArea frontend * @magentoAppIsolation enabled */ public function testEmulateWelcomeBlock() { $this->_customerSession->loginById(1); $httpContext = new \Magento\Framework\App\Http\Context(); $httpContext->setValue(Context::CONTEXT_AUTH, 1, 1); $block = $this->_objectManager->create('Magento\\Sales\\Block\\Reorder\\Sidebar', ['httpContext' => $httpContext]); $this->_observer->emulateWelcomeBlock($block); $customerName = $this->_escaper->escapeHtml($this->_customerViewHelper->getCustomerName($this->customerRepository->getById($this->_persistentSessionHelper->getSession()->getCustomerId()))); $translation = __('Welcome, %1!', $customerName); $this->assertStringMatchesFormat('%A' . $translation . '%A', $block->getWelcome()); $this->_customerSession->logout(); }
/** * @param Observer $observer * @return void * @SuppressWarnings(PHPMD.CyclomaticComplexity) */ public function execute(Observer $observer) { /** @var $customer \Magento\Customer\Api\Data\CustomerInterface */ $customer = $observer->getEvent()->getCustomer(); // Check if customer is valid (remove persistent cookie for invalid customer) if (!$customer || !$customer->getId() || !$this->_persistentSession->isRememberMeChecked()) { $this->_sessionFactory->create()->removePersistentCookie(); return; } $persistentLifeTime = $this->_persistentData->getLifeTime(); // Delete persistent session, if persistent could not be applied if ($this->_persistentData->isEnabled() && $persistentLifeTime <= 0) { // Remove current customer persistent session $this->_sessionFactory->create()->deleteByCustomerId($customer->getId()); return; } /** @var $sessionModel \Magento\Persistent\Model\Session */ $sessionModel = $this->_persistentSession->getSession(); // Check if session is wrong or not exists, so create new session if (!$sessionModel->getId() || $sessionModel->getCustomerId() != $customer->getId()) { /** @var \Magento\Persistent\Model\Session $sessionModel */ $sessionModel = $this->_sessionFactory->create(); $sessionModel->setLoadExpired()->loadByCustomerId($customer->getId()); if (!$sessionModel->getId()) { /** @var \Magento\Persistent\Model\Session $sessionModel */ $sessionModel = $this->_sessionFactory->create(); $sessionModel->setCustomerId($customer->getId())->save(); } $this->_persistentSession->setSession($sessionModel); } // Set new cookie if ($sessionModel->getId()) { $sessionModel->setPersistentCookie($persistentLifeTime, $this->_customerSession->getCookiePath()); } }
/** * Set persistent data to customer session * * @param \Magento\Framework\Event\Observer $observer * @SuppressWarnings(PHPMD.CyclomaticComplexity) * * @return $this */ public function execute(\Magento\Framework\Event\Observer $observer) { if (!$this->_persistentData->canProcess($observer) || !$this->_persistentData->isShoppingCartPersist()) { return $this; } if ($this->_persistentSession->isPersistent() && !$this->_customerSession->isLoggedIn()) { /** @var \Magento\Customer\Api\Data\CustomerInterface $customer */ $customer = $this->customerRepository->getById($this->_persistentSession->getSession()->getCustomerId()); if ($defaultShipping = $customer->getDefaultShipping()) { /** @var \Magento\Customer\Model\Data\Address $address */ $address = $this->addressRepository->getById($defaultShipping); if ($address) { $this->_customerSession->setDefaultTaxShippingAddress(['country_id' => $address->getCountryId(), 'region_id' => $address->getRegion() ? $address->getRegionId() : null, 'postcode' => $address->getPostcode()]); } } if ($defaultBilling = $customer->getDefaultBilling()) { $address = $this->addressRepository->getById($defaultBilling); if ($address) { $this->_customerSession->setDefaultTaxBillingAddress(['country_id' => $address->getCountryId(), 'region_id' => $address->getRegion() ? $address->getRegionId() : null, 'postcode' => $address->getPostcode()]); } } $this->_customerSession->setCustomerId($customer->getId())->setCustomerGroupId($customer->getGroupId()); } return $this; }
/** * Render additional header html * * @return string */ protected function _toHtml() { if ($this->_persistentSessionHelper->getSession()->getCustomerId()) { return '<span><a ' . $this->getLinkAttributes() . ' >' . __('Not you?') . '</a></span>'; } return ''; }
/** * Emulate 'welcome' block with persistent data * * @param \Magento\Framework\View\Element\AbstractBlock $block * @return $this */ public function emulateWelcomeBlock($block) { $escapedName = $this->_escaper->escapeHtml($this->_customerViewHelper->getCustomerName($this->customerRepository->getById($this->_persistentSession->getSession()->getCustomerId())), null); $this->_applyAccountLinksPersistentData(); $welcomeMessage = __('Welcome, %1!', $escapedName) . ' ' . $this->_layout->getBlock('header.additional')->toHtml(); $block->setWelcome($welcomeMessage); return $this; }
/** * Reset quote reward point amount * * @param \Magento\Customer\CustomerData\Customer $subject * @param \Closure $proceed * * @return array * @SuppressWarnings(PHPMD.UnusedFormalParameter) */ public function aroundGetSectionData(\Magento\Customer\CustomerData\Customer $subject, \Closure $proceed) { /** unset customer first name */ if (!$this->customerSession->isLoggedIn() && $this->persistentData->isEnabled() && $this->persistentSession->isPersistent()) { return []; } return $proceed(); }
/** * @param \Magento\Checkout\Model\DefaultConfigProvider $subject * @param array $result * @return array * @SuppressWarnings(PHPMD.UnusedFormalParameter) */ public function afterGetConfig(\Magento\Checkout\Model\DefaultConfigProvider $subject, array $result) { if ($this->persistentHelper->isEnabled() && $this->persistentSession->isPersistent() && !$this->customerSession->isLoggedIn()) { /** @var $quoteIdMask \Magento\Quote\Model\QuoteIdMask */ $quoteIdMask = $this->quoteIdMaskFactory->create(); $result['quoteData']['entity_id'] = $quoteIdMask->load($this->checkoutSession->getQuote()->getId(), 'quote_id')->getMaskedId(); } return $result; }
/** * Set quote to be loaded even if not active * * @param \Magento\Framework\Event\Observer $observer * @return void * @SuppressWarnings(PHPMD.UnusedFormalParameter) */ public function execute(\Magento\Framework\Event\Observer $observer) { if (!($this->_persistentSession->isPersistent() && !$this->_customerSession->isLoggedIn() && !$this->_persistentData->isShoppingCartPersist())) { return; } if ($this->_checkoutSession) { $this->_checkoutSession->setLoadInactive(); } }
/** * Unload persistent session (if set in config) * * @SuppressWarnings(PHPMD.UnusedFormalParameter) * @param Observer $observer * @return void */ public function execute(Observer $observer) { if (!$this->_persistentData->isEnabled() || !$this->_persistentData->getClearOnLogout()) { return; } $this->_sessionFactory->create()->removePersistentCookie(); // Unset persistent session $this->_persistentSession->setSession(null); }
/** * Apply persistent data * * @param \Magento\Framework\Event\Observer $observer * @return $this */ public function execute($observer) { if (!$this->_persistentData->canProcess($observer) || !$this->_persistentSession->isPersistent() || $this->_customerSession->isLoggedIn()) { return $this; } /** @var \Magento\Persistent\Model\Persistent\Config $persistentConfig */ $persistentConfig = $this->_persistentConfigFactory->create(); $persistentConfig->setConfigFilePath($this->_persistentData->getPersistentConfigFilePath())->fire(); return $this; }
/** * Make persistent quote to be guest * * @param \Magento\Framework\Event\Observer $observer * @return void */ public function execute($observer) { /** @var $action \Magento\Persistent\Controller\Index */ $action = $observer->getEvent()->getControllerAction(); if ($action instanceof \Magento\Persistent\Controller\Index) { if ($this->_persistentSession->isPersistent() && !$this->_customerSession->isLoggedIn() || $this->_persistentData->isShoppingCartPersist()) { $this->quoteManager->setGuest(true); } } }
/** * Prevent clear checkout session * * @param \Magento\Framework\Event\Observer $observer * @return void */ public function execute(\Magento\Framework\Event\Observer $observer) { /** @var $action \Magento\Persistent\Controller\Index */ $action = $observer->getEvent()->getControllerAction(); if ($action instanceof \Magento\Persistent\Controller\Index) { if ($this->_persistentSession->isPersistent() && !$this->_customerSession->isLoggedIn() || !$this->_persistentData->isShoppingCartPersist()) { $action->setClearCheckoutSession(false); } } }
/** * Process address additional data * * @param AddressAdditionalDataProcessor $subject * @param AddressAdditionalData $additionalData * @return void * @SuppressWarnings(PHPMD.UnusedFormalParameter) */ public function beforeProcess(AddressAdditionalDataProcessor $subject, AddressAdditionalData $additionalData) { if (!$this->persistentHelper->isEnabled() || !$this->persistentHelper->isRememberMeEnabled()) { return; } $checkboxStatus = $additionalData->getExtensionAttributes()->getPersistentRememberMe(); $isRememberMeChecked = empty($checkboxStatus) ? false : true; $this->persistentSession->setRememberMeChecked($isRememberMeChecked); $this->checkoutSession->setRememberMeChecked($isRememberMeChecked); }
/** * Renew persistent cookie * * @param Observer $observer * @return void */ public function execute(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->_sessionFactory->create()->renewPersistentCookie($this->_persistentData->getLifeTime(), $this->_customerSession->getCookiePath()); } }
/** * Unset persistent cookie and make customer's quote as a guest * * @param \Magento\Framework\Event\Observer $observer * @return void */ public function execute($observer) { if (!$this->_persistentData->canProcess($observer) || !$this->_persistentSession->isPersistent()) { return; } $this->_persistentSession->getSession()->removePersistentCookie(); if (!$this->_customerSession->isLoggedIn()) { $this->_customerSession->setCustomerId(null)->setCustomerGroupId(null); } $this->quoteManager->setGuest(); }
/** * Set persistent data to customer session * * @param \Magento\Framework\Event\Observer $observer * @return $this */ public function execute($observer) { if (!$this->_persistentData->canProcess($observer) || !$this->_persistentData->isShoppingCartPersist()) { return $this; } if ($this->_persistentSession->isPersistent() && !$this->_customerSession->isLoggedIn()) { $customer = $this->_customerAccountService->getCustomer($this->_persistentSession->getSession()->getCustomerId()); $this->_customerSession->setCustomerId($customer->getId())->setCustomerGroupId($customer->getGroupId()); } return $this; }
/** * Set persistent data to customer session * * @param \Magento\Framework\Event\Observer $observer * @return $this */ public function execute(\Magento\Framework\Event\Observer $observer) { if (!$this->_persistentData->canProcess($observer) || !$this->_persistentData->isShoppingCartPersist()) { return $this; } if ($this->_persistentSession->isPersistent() && !$this->_customerSession->isLoggedIn()) { $customer = $this->customerRepository->getById($this->_persistentSession->getSession()->getCustomerId()); $this->_customerSession->setCustomerId($customer->getId())->setCustomerGroupId($customer->getGroupId()); } return $this; }
/** * Check and clear session data if persistent session expired * * @param \Magento\Framework\Event\Observer $observer * @return void */ public function execute(\Magento\Framework\Event\Observer $observer) { if (!$this->_persistentData->canProcess($observer)) { return; } if ($this->_persistentData->isEnabled() && !$this->_persistentSession->isPersistent() && !$this->_customerSession->isLoggedIn() && $this->_checkoutSession->getQuoteId() && !$observer->getControllerAction() instanceof \Magento\Checkout\Controller\Onepage) { $this->_eventManager->dispatch('persistent_session_expired'); $this->quoteManager->expire(); $this->_customerSession->setCustomerId(null)->setCustomerGroupId(null); } }
/** * Update customer id and customer group id if user is in persistent session * * @param \Magento\Framework\Event\Observer $observer * @return void */ public function execute(\Magento\Framework\Event\Observer $observer) { if (!$this->_persistentSession->isPersistent()) { return; } $customerCookies = $observer->getEvent()->getCustomerCookies(); if ($customerCookies instanceof \Magento\Framework\DataObject) { $persistentCustomer = $this->customerRepository->getById($this->_persistentSession->getSession()->getCustomerId()); $customerCookies->setCustomerId($persistentCustomer->getId()); $customerCookies->setCustomerGroupId($persistentCustomer->getGroupId()); } }
/** * @magentoAppArea frontend * @magentoConfigFixture current_store persistent/options/shopping_cart 1 * @magentoConfigFixture current_store persistent/options/logout_clear 0 * @magentoConfigFixture current_store persistent/options/enabled 1 */ public function testEmulateCustomer() { $observer = new \Magento\Framework\Event\Observer(); $this->_customerSession->loginById(1); $this->_customerSession->logout(); $this->assertNull($this->_customerSession->getCustomerId()); $this->assertEquals(\Magento\Customer\Model\GroupManagement::NOT_LOGGED_IN_ID, $this->_customerSession->getCustomerGroupId()); $this->_observer->execute($observer); $customer = $this->customerRepository->getById($this->_persistentSessionHelper->getSession()->getCustomerId()); $this->assertEquals($customer->getId(), $this->_customerSession->getCustomerId()); $this->assertEquals($customer->getGroupId(), $this->_customerSession->getCustomerGroupId()); }
/** * @magentoAppArea frontend * @magentoConfigFixture current_store persistent/options/shopping_cart 1 * @magentoConfigFixture current_store persistent/options/logout_clear 0 * @magentoConfigFixture current_store persistent/options/enabled 1 */ public function testEmulateCustomer() { $observer = new \Magento\Framework\Event\Observer(); $this->_customerSession->loginById(1); $this->_customerSession->logout(); $this->assertNull($this->_customerSession->getCustomerId()); $this->assertEquals(\Magento\Customer\Service\V1\CustomerGroupServiceInterface::NOT_LOGGED_IN_ID, $this->_customerSession->getCustomerGroupId()); $this->_observer->execute($observer); $customer = $this->_customerAccountService->getCustomer($this->_persistentSessionHelper->getSession()->getCustomerId()); $this->assertEquals($customer->getId(), $this->_customerSession->getCustomerId()); $this->assertEquals($customer->getGroupId(), $this->_customerSession->getCustomerGroupId()); }
/** * @magentoConfigFixture current_store persistent/options/customer 1 * @magentoConfigFixture current_store persistent/options/enabled 1 * @magentoConfigFixture current_store persistent/options/remember_enabled 1 * @magentoConfigFixture current_store persistent/options/remember_default 1 * @magentoAppArea frontend * @magentoAppIsolation enabled */ public function testToHtml() { $this->_customerSession->loginById(1); /** @var \Magento\Customer\Helper\View $customerViewHelper */ $customerViewHelper = $this->_objectManager->create('Magento\\Customer\\Helper\\View'); $customerAccountService = $this->_objectManager->create('Magento\\Customer\\Service\\V1\\CustomerAccountServiceInterface'); /** @var \Magento\Framework\Escaper $escaper */ $escaper = $this->_objectManager->create('Magento\\Framework\\Escaper'); $persistentName = $escaper->escapeHtml($customerViewHelper->getCustomerName($customerAccountService->getCustomer($this->_persistentSessionHelper->getSession()->getCustomerId()))); $translation = __('(Not %1?)', $persistentName); $this->assertStringMatchesFormat('%A<span>%A<a%Ahref="' . $this->_block->getHref() . '"%A>' . $translation . '</a>%A</span>%A', $this->_block->toHtml()); $this->_customerSession->logout(); }
/** * @magentoConfigFixture current_store persistent/options/enabled 1 * @magentoConfigFixture current_store persistent/options/remember_enabled 1 * @magentoConfigFixture current_store persistent/options/remember_default 1 * @magentoAppArea frontend * @magentoConfigFixture current_store persistent/options/shopping_cart 1 * @magentoConfigFixture current_store persistent/options/logout_clear 0 */ public function testEmulateQuote() { $requestMock = $this->getMockBuilder('Magento\\Framework\\App\\Request\\Http')->disableOriginalConstructor()->setMethods([])->getMock(); $requestMock->expects($this->once())->method('getFullActionName')->will($this->returnValue('valid_action')); $event = new \Magento\Framework\Event(['request' => $requestMock]); $observer = new \Magento\Framework\Event\Observer(); $observer->setEvent($event); $this->_customerSession->loginById(1); $customer = $this->customerRepository->getById($this->_persistentSessionHelper->getSession()->getCustomerId()); $this->_checkoutSession->expects($this->once())->method('setCustomerData')->with($customer); $this->_customerSession->logout(); $this->_observer->execute($observer); }
/** * Synchronize persistent session info * * @param Observer $observer * @return void */ public function execute(Observer $observer) { if (!$this->_persistentData->isEnabled() || !$this->_persistentSession->isPersistent()) { return; } /** @var $sessionModel \Magento\Persistent\Model\Session */ $sessionModel = $this->_persistentSession->getSession(); /** @var $request \Magento\Framework\App\RequestInterface */ $request = $observer->getEvent()->getRequest(); // Quote Id could be changed only by logged in customer if ($this->_customerSession->isLoggedIn() || $request && $request->getActionName() == 'logout' && $request->getControllerName() == 'account') { $sessionModel->save(); } }
/** * Prevent express checkout * * @param \Magento\Framework\Event\Observer $observer * @return void */ public function execute(\Magento\Framework\Event\Observer $observer) { if (!($this->_persistentSession->isPersistent() && !$this->_customerSession->isLoggedIn())) { return; } /** @var $controllerAction \Magento\Checkout\Controller\Express\RedirectLoginInterface*/ $controllerAction = $observer->getEvent()->getControllerAction(); if (!$controllerAction || !$controllerAction instanceof \Magento\Checkout\Controller\Express\RedirectLoginInterface || $controllerAction->getRedirectActionName() != $controllerAction->getRequest()->getActionName()) { return; } $this->messageManager->addNotice(__('To check out, please sign in using your email address.')); $customerBeforeAuthUrl = $this->_url->getUrl('persistent/index/expressCheckout'); $this->_expressRedirectHelper->redirectLogin($controllerAction, $customerBeforeAuthUrl); }
/** * Set Checked status of "Remember Me" * * @param Observer $observer * @return void */ public function execute(Observer $observer) { if (!$this->_persistentData->canProcess($observer) || !$this->_persistentData->isEnabled() || !$this->_persistentData->isRememberMeEnabled()) { return; } /** @var $controllerAction \Magento\Framework\App\RequestInterface */ $request = $observer->getEvent()->getRequest(); if ($request) { $rememberMeCheckbox = $request->getPost('persistent_remember_me'); $this->_persistentSession->setRememberMeChecked((bool) $rememberMeCheckbox); if ($request->getFullActionName() == 'checkout_onepage_saveBilling' || $request->getFullActionName() == 'customer_account_createpost') { $this->_checkoutSession->setRememberMeChecked((bool) $rememberMeCheckbox); } } }
/** * Set persistent data into quote * * @param \Magento\Framework\Event\Observer $observer * @return void */ public function execute($observer) { if (!$this->_persistentSession->isPersistent()) { return; } /** @var $quote \Magento\Quote\Model\Quote */ $quote = $observer->getEvent()->getQuote(); if (!$quote) { return; } if ($this->_persistentSession->isPersistent() && !$this->_customerSession->isLoggedIn() && !$this->_persistentData->isShoppingCartPersist() && $this->quoteManager->isPersistent()) { //Quote is not actual customer's quote, just persistent $quote->setIsActive(false)->setIsPersistent(true); } }
/** * 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()); } }
/** * Set Checked status of "Remember Me" * * @param Observer $observer * @return void */ public function execute(Observer $observer) { if (!$this->_persistentData->canProcess($observer) || !$this->_persistentData->isEnabled() || !$this->_persistentData->isRememberMeEnabled()) { return; } /** @var $request \Magento\Framework\App\RequestInterface */ $request = $observer->getEvent()->getRequest(); if ($request && $request->isXmlHttpRequest()) { $requestData = []; $content = $request->getContent(); if ($content) { $requestData = \Zend_Json::decode($content); } $isRememberMeChecked = empty($requestData['persistent_remember_me']) ? false : true; $this->_persistentSession->setRememberMeChecked((bool) $isRememberMeChecked); } }
/** * @covers \Magento\Persistent\Observer\SynchronizePersistentOnLoginObserver::execute */ public function testSynchronizePersistentOnLogin() { $event = new \Magento\Framework\Event(); $observer = new \Magento\Framework\Event\Observer(['event' => $event]); /** @var \Magento\Customer\Api\CustomerRepositoryInterface $customerRepository */ $customerRepository = $this->_objectManager->create('Magento\\Customer\\Api\\CustomerRepositoryInterface'); /** @var $customer \Magento\Customer\Api\Data\CustomerInterface */ $customer = $customerRepository->getById(1); $event->setData('customer', $customer); $this->_persistentSession->setRememberMeChecked(true); $this->_model->execute($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()); }