/** * @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()); } }
/** * Render additional header html * * @return string */ protected function _toHtml() { if ($this->_persistentSessionHelper->getSession()->getCustomerId()) { return '<span><a ' . $this->getLinkAttributes() . ' >' . __('Not you?') . '</a></span>'; } return ''; }
/** * 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; }
/** * 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; }
/** * 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; }
/** * 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; }
/** * @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()); }
/** * 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\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(); } }
/** * Emulate quote by persistent data * * @param \Magento\Framework\Event\Observer $observer * @return void */ public function execute($observer) { $stopActions = ['persistent_index_saveMethod', 'customer_account_createpost']; if (!$this->_persistentData->canProcess($observer) || !$this->_persistentSession->isPersistent() || $this->_customerSession->isLoggedIn()) { return; } $actionName = $observer->getEvent()->getRequest()->getFullActionName(); if (in_array($actionName, $stopActions)) { return; } if ($this->_persistentData->isShoppingCartPersist()) { $this->_checkoutSession->setCustomerData($this->customerRepository->getById($this->_persistentSession->getSession()->getCustomerId())); if (!$this->_checkoutSession->hasQuote()) { $this->_checkoutSession->getQuote(); } } }
/** * Make quote to be guest * * @param bool $checkQuote Check quote to be persistent (not stolen) * @return void */ public function setGuest($checkQuote = false) { /** @var $quote \Magento\Quote\Model\Quote */ $quote = $this->checkoutSession->getQuote(); if ($quote && $quote->getId()) { if ($checkQuote && !$this->persistentData->isShoppingCartPersist() && !$quote->getIsPersistent()) { $this->checkoutSession->clearQuote()->clearStorage(); return; } $quote->getPaymentsCollection()->walk('delete'); $quote->getAddressesCollection()->walk('delete'); $this->_setQuotePersistent = false; $quote->setIsActive(true)->setCustomerId(null)->setCustomerEmail(null)->setCustomerFirstname(null)->setCustomerLastname(null)->setCustomerGroupId(\Magento\Customer\Api\Data\GroupInterface::NOT_LOGGED_IN_ID)->setIsPersistent(false)->removeAllAddresses(); //Create guest addresses $quote->getShippingAddress(); $quote->getBillingAddress(); $quote->collectTotals(); $this->quoteRepository->save($quote); } $this->persistentSession->getSession()->removePersistentCookie(); }
/** * Render additional header html * * @return string */ protected function _toHtml() { $persistentName = $this->_escaper->escapeHtml($this->_customerViewHelper->getCustomerName($this->_customerAccountService->getCustomer($this->_persistentSessionHelper->getSession()->getCustomerId()))); return '<span><a ' . $this->getLinkAttributes() . ' >' . $this->escapeHtml(__('(Not %1?)', $persistentName)) . '</a></span>'; }