/** * @param bool $isWebsiteScope * @param array $websites * @param int $quoteId * @dataProvider dispatchDataProvider */ public function testDispatch($isWebsiteScope, $websites, $quoteId) { $this->configMock->expects($this->once())->method('isWebsiteScope')->will($this->returnValue($isWebsiteScope)); $customerDataObjectMock = $this->getMockBuilder('Magento\\Customer\\Service\\V1\\Data\\Customer')->disableOriginalConstructor()->getMock(); $customerDataObjectMock->expects($this->any())->method('getGroupId')->will($this->returnValue(1)); $customerDataObjectMock->expects($this->any())->method('getWebsiteId')->will($this->returnValue(2)); if ($isWebsiteScope) { $websites = $websites[0]; $this->storeManagerMock->expects($this->once())->method('getWebsite')->with(2)->will($this->returnValue($websites)); } else { $this->storeManagerMock->expects($this->once())->method('getWebsites')->will($this->returnValue($websites)); } $origCustomerDataObjectMock = $this->getMockBuilder('Magento\\Customer\\Service\\V1\\Data\\Customer')->disableOriginalConstructor()->getMock(); $origCustomerDataObjectMock->expects($this->any())->method('getGroupId')->will($this->returnValue(2)); $this->eventMock->expects($this->any())->method('getCustomerDataObject')->will($this->returnValue($customerDataObjectMock)); $this->eventMock->expects($this->any())->method('getOrigCustomerDataObject')->will($this->returnValue($origCustomerDataObjectMock)); /** @var \PHPUnit_Framework_MockObject_MockObject|\Magento\Sales\Model\Quote $quoteMock */ $quoteMock = $this->getMockBuilder('Magento\\Sales\\Model\\Quote')->setMethods(array('setWebsite', 'loadByCustomer', 'getId', 'setCustomerGroupId', 'collectTotals', 'save', '__wakeup'))->disableOriginalConstructor()->getMock(); $websiteCount = count($websites); $this->quoteFactoryMock->expects($this->exactly($websiteCount))->method('create')->will($this->returnValue($quoteMock)); $quoteMock->expects($this->exactly($websiteCount))->method('setWebsite'); $quoteMock->expects($this->exactly($websiteCount))->method('loadByCustomer'); $quoteMock->expects($this->exactly($websiteCount))->method('getId')->will($this->returnValue($quoteId)); if ($quoteId) { $quoteMock->expects($this->exactly($websiteCount))->method('setCustomerGroupId'); $quoteMock->expects($this->exactly($websiteCount))->method('collectTotals'); $quoteMock->expects($this->exactly($websiteCount))->method('save'); } else { $quoteMock->expects($this->never())->method('setCustomerGroupId'); $quoteMock->expects($this->never())->method('collectTotals'); $quoteMock->expects($this->never())->method('save'); } $this->customerQuote->dispatch($this->observerMock); }
/** * Set new customer group to all his quotes * * @param \Magento\Framework\Event\Observer $observer * @return void */ public function dispatch(\Magento\Framework\Event\Observer $observer) { /** @var CustomerData $customerDataObject */ $customerDataObject = $observer->getEvent()->getCustomerDataObject(); /** @var CustomerData $origCustomerDataObject */ $origCustomerDataObject = $observer->getEvent()->getOrigCustomerDataObject(); if ($customerDataObject->getGroupId() !== $origCustomerDataObject->getGroupId()) { /** * It is needed to process customer's quotes for all websites * if customer accounts are shared between all of them */ /** @var $websites \Magento\Store\Model\Website[] */ $websites = $this->_config->isWebsiteScope() ? array($this->_storeManager->getWebsite($customerDataObject->getWebsiteId())) : $this->_storeManager->getWebsites(); foreach ($websites as $website) { $quote = $this->_quoteFactory->create(); $quote->setWebsite($website); $quote->loadByCustomer($customerDataObject->getId()); if ($quote->getId()) { $quote->setCustomerGroupId($customerDataObject->getGroupId()); $quote->collectTotals(); $quote->save(); } } } }
/** * @param bool $isWebsiteScope * @param array $websites * @param int $quoteId * @dataProvider dispatchDataProvider */ public function testDispatch($isWebsiteScope, $websites, $quoteId) { $this->configMock->expects($this->once())->method('isWebsiteScope')->will($this->returnValue($isWebsiteScope)); $customerDataObjectMock = $this->getMockBuilder('Magento\\Customer\\Api\\Data\\CustomerInterface')->disableOriginalConstructor()->getMock(); $customerDataObjectMock->expects($this->any())->method('getGroupId')->will($this->returnValue(1)); $customerDataObjectMock->expects($this->any())->method('getWebsiteId')->will($this->returnValue(2)); if ($isWebsiteScope) { $websites = $websites[0]; $this->storeManagerMock->expects($this->once())->method('getWebsite')->with(2)->will($this->returnValue($websites)); } else { $this->storeManagerMock->expects($this->once())->method('getWebsites')->will($this->returnValue($websites)); } $origCustomerDataObjectMock = $this->getMockBuilder('Magento\\Customer\\Api\\Data\\CustomerInterface')->disableOriginalConstructor()->getMock(); $origCustomerDataObjectMock->expects($this->any())->method('getGroupId')->will($this->returnValue(2)); $this->eventMock->expects($this->any())->method('getCustomerDataObject')->will($this->returnValue($customerDataObjectMock)); $this->eventMock->expects($this->any())->method('getOrigCustomerDataObject')->will($this->returnValue($origCustomerDataObjectMock)); /** @var \PHPUnit_Framework_MockObject_MockObject|\Magento\Quote\Model\Quote $quoteMock */ $quoteMock = $this->getMockBuilder('Magento\\Quote\\Model\\Quote')->setMethods(['setWebsite', 'setCustomerGroupId', 'collectTotals', '__wakeup'])->disableOriginalConstructor()->getMock(); $websiteCount = count($websites); if ($quoteId) { $this->quoteRepositoryMock->expects($this->exactly($websiteCount))->method('getForCustomer')->will($this->returnValue($quoteMock)); $quoteMock->expects($this->exactly($websiteCount))->method('setWebsite'); $quoteMock->expects($this->exactly($websiteCount))->method('setCustomerGroupId'); $quoteMock->expects($this->exactly($websiteCount))->method('collectTotals'); $this->quoteRepositoryMock->expects($this->exactly($websiteCount))->method('save')->with($quoteMock); } else { $this->quoteRepositoryMock->expects($this->exactly($websiteCount))->method('getForCustomer')->willThrowException(new \Magento\Framework\Exception\NoSuchEntityException()); $quoteMock->expects($this->never())->method('setCustomerGroupId'); $quoteMock->expects($this->never())->method('collectTotals'); $this->quoteRepositoryMock->expects($this->never())->method('save'); } $this->customerQuote->dispatch($this->observerMock); }
/** * @param \Magento\Customer\Model\Config\Share $configShare * @param \Magento\Store\Model\StoreManagerInterface $storeManager * @param string $namespace * @param array $data */ public function __construct(\Magento\Customer\Model\Config\Share $configShare, \Magento\Store\Model\StoreManagerInterface $storeManager, $namespace = 'customer', array $data = []) { if ($configShare->isWebsiteScope()) { $namespace .= '_' . $storeManager->getWebsite()->getCode(); } parent::__construct($namespace, $data); }
/** * Set new customer group to all his quotes * * @param Observer $observer * @return void */ public function dispatch(Observer $observer) { /** @var \Magento\Customer\Api\Data\CustomerInterface $customer */ $customer = $observer->getEvent()->getCustomerDataObject(); /** @var \Magento\Customer\Api\Data\CustomerInterface $origCustomer */ $origCustomer = $observer->getEvent()->getOrigCustomerDataObject(); if ($customer->getGroupId() !== $origCustomer->getGroupId()) { /** * It is needed to process customer's quotes for all websites * if customer accounts are shared between all of them */ /** @var $websites \Magento\Store\Model\Website[] */ $websites = $this->config->isWebsiteScope() ? [$this->storeManager->getWebsite($customer->getWebsiteId())] : $this->storeManager->getWebsites(); foreach ($websites as $website) { try { $quote = $this->quoteRepository->getForCustomer($customer->getId()); $quote->setWebsite($website); $quote->setCustomerGroupId($customer->getGroupId()); $quote->collectTotals(); $this->quoteRepository->save($quote); } catch (\Magento\Framework\Exception\NoSuchEntityException $e) { } } } }
/** * {@inheritdoc} */ protected function _prepareLayout() { $this->setId('customerViewAccordion'); $this->addItem('lastOrders', array('title' => __('Recent Orders'), 'ajax' => true, 'content_url' => $this->getUrl('customer/*/lastOrders', array('_current' => true)))); $customerId = $this->_coreRegistry->registry(RegistryConstants::CURRENT_CUSTOMER_ID); $customer = $this->getCustomer($customerId); $websiteIds = $this->_shareConfig->getSharedWebsiteIds($customer->getWebsiteId()); // add shopping cart block of each website foreach ($websiteIds as $websiteId) { $website = $this->_storeManager->getWebsite($websiteId); // count cart items $cartItemsCount = $this->_quoteFactory->create()->setWebsite($website)->loadByCustomer($customerId)->getItemsCollection(false)->addFieldToFilter('parent_item_id', array('null' => true))->getSize(); // prepare title for cart $title = __('Shopping Cart - %1 item(s)', $cartItemsCount); if (count($websiteIds) > 1) { $title = __('Shopping Cart of %1 - %2 item(s)', $website->getName(), $cartItemsCount); } // add cart ajax accordion $this->addItem('shopingCart' . $websiteId, array('title' => $title, 'ajax' => true, 'content_url' => $this->getUrl('customer/*/viewCart', array('_current' => true, 'website_id' => $websiteId)))); } // count wishlist items $wishlistCount = $this->_itemsFactory->create()->addCustomerIdFilter($customerId)->addStoreData()->getSize(); // add wishlist ajax accordion $this->addItem('wishlist', array('title' => __('Wishlist - %1 item(s)', $wishlistCount), 'ajax' => true, 'content_url' => $this->getUrl('customer/*/viewWishlist', array('_current' => true)))); }
/** * Add shopping cart grid of each website * * @return $this */ protected function _prepareLayout() { $sharedWebsiteIds = $this->_shareConfig->getSharedWebsiteIds($this->_getCustomer()->getWebsiteId()); $isShared = count($sharedWebsiteIds) > 1; foreach ($sharedWebsiteIds as $websiteId) { $blockName = 'customer_cart_' . $websiteId; $block = $this->getLayout()->createBlock('Magento\\Customer\\Block\\Adminhtml\\Edit\\Tab\\Cart', $blockName, ['data' => ['website_id' => $websiteId]]); if ($isShared) { $websiteName = $this->_storeManager->getWebsite($websiteId)->getName(); $block->setCartHeader(__('Shopping Cart from %1', $websiteName)); } $this->setChild($blockName, $block); } return parent::_prepareLayout(); }
/** * @SuppressWarnings(PHPMD.ExcessiveMethodLength) */ public function testCreateAccountWithPassword() { $websiteId = 1; $storeId = null; $defaultStoreId = 1; $customerId = 1; $customerEmail = '*****@*****.**'; $password = '******'; $hash = '4nj54lkj5jfi03j49f8bgujfgsd'; $newLinkToken = '2jh43j5h2345jh23lh452h345hfuzasd96ofu'; $templateIdentifier = 'Template Identifier'; $sender = 'Sender'; $this->string->expects($this->any())->method('strlen')->willReturnCallback(function ($string) { return strlen($string); }); $this->encryptor->expects($this->once())->method('getHash')->with($password, true)->willReturn($hash); $address = $this->getMockBuilder('Magento\\Customer\\Api\\Data\\AddressInterface')->disableOriginalConstructor()->getMock(); $address->expects($this->once())->method('setCustomerId')->with($customerId); $store = $this->getMockBuilder('Magento\\Store\\Model\\Store')->disableOriginalConstructor()->getMock(); $store->expects($this->once())->method('getId')->willReturn($defaultStoreId); $website = $this->getMockBuilder('Magento\\Store\\Model\\Website')->disableOriginalConstructor()->getMock(); $website->expects($this->atLeastOnce())->method('getStoreIds')->willReturn([1, 2, 3]); $website->expects($this->once())->method('getDefaultStore')->willReturn($store); $customer = $this->getMockBuilder('Magento\\Customer\\Api\\Data\\CustomerInterface')->getMock(); $customer->expects($this->atLeastOnce())->method('getId')->willReturn($customerId); $customer->expects($this->atLeastOnce())->method('getEmail')->willReturn($customerEmail); $customer->expects($this->atLeastOnce())->method('getWebsiteId')->willReturn($websiteId); $customer->expects($this->atLeastOnce())->method('getStoreId')->willReturn($storeId); $customer->expects($this->once())->method('setStoreId')->with($defaultStoreId); $customer->expects($this->once())->method('getAddresses')->willReturn([$address]); $customer->expects($this->once())->method('setAddresses')->with(null); $this->customerRepository->expects($this->once())->method('get')->with($customerEmail)->willReturn($customer); $this->share->expects($this->once())->method('isWebsiteScope')->willReturn(true); $this->storeManager->expects($this->atLeastOnce())->method('getWebsite')->with($websiteId)->willReturn($website); $this->customerRepository->expects($this->atLeastOnce())->method('save')->willReturn($customer); $this->addressRepository->expects($this->atLeastOnce())->method('save')->with($address); $this->customerRepository->expects($this->once())->method('getById')->with($customerId)->willReturn($customer); $this->random->expects($this->once())->method('getUniqueHash')->willReturn($newLinkToken); $customerSecure = $this->getMockBuilder('Magento\\Customer\\Model\\Data\\CustomerSecure')->setMethods(['setRpToken', 'setRpTokenCreatedAt', 'getPasswordHash'])->disableOriginalConstructor()->getMock(); $customerSecure->expects($this->any())->method('setRpToken')->with($newLinkToken); $customerSecure->expects($this->any())->method('setRpTokenCreatedAt'); $customerSecure->expects($this->any())->method('getPasswordHash')->willReturn($hash); $this->customerRegistry->expects($this->atLeastOnce())->method('retrieveSecureData')->willReturn($customerSecure); $this->dataObjectProcessor->expects($this->once())->method('buildOutputDataArray')->willReturn([]); $this->scopeConfig->expects($this->at(1))->method('getValue')->with(AccountManagement::XML_PATH_REGISTER_EMAIL_TEMPLATE, ScopeInterface::SCOPE_STORE, $defaultStoreId)->willReturn($templateIdentifier); $this->scopeConfig->expects($this->at(2))->method('getValue')->willReturn($sender); $transport = $this->getMockBuilder('Magento\\Framework\\Mail\\TransportInterface')->getMock(); $this->transportBuilder->expects($this->once())->method('setTemplateIdentifier')->with($templateIdentifier)->willReturnSelf(); $this->transportBuilder->expects($this->once())->method('setTemplateOptions')->willReturnSelf(); $this->transportBuilder->expects($this->once())->method('setTemplateVars')->willReturnSelf(); $this->transportBuilder->expects($this->once())->method('setFrom')->with($sender)->willReturnSelf(); $this->transportBuilder->expects($this->once())->method('addTo')->willReturnSelf(); $this->transportBuilder->expects($this->once())->method('getTransport')->willReturn($transport); $transport->expects($this->once())->method('sendMessage'); $this->accountManagement->createAccount($customer, $password); }
/** * @SuppressWarnings(PHPMD.ExcessiveMethodLength) */ public function testCreateAccountWithPassword() { $websiteId = 1; $storeId = null; $defaultStoreId = 1; $customerId = 1; $customerEmail = '*****@*****.**'; $hash = '4nj54lkj5jfi03j49f8bgujfgsd'; $newLinkToken = '2jh43j5h2345jh23lh452h345hfuzasd96ofu'; $templateIdentifier = 'Template Identifier'; $sender = 'Sender'; $password = '******'; $minPasswordLength = 5; $minCharacterSetsNum = 2; $this->scopeConfig->expects($this->any())->method('getValue')->willReturnMap([[AccountManagement::XML_PATH_MINIMUM_PASSWORD_LENGTH, 'default', null, $minPasswordLength], [AccountManagement::XML_PATH_REQUIRED_CHARACTER_CLASSES_NUMBER, 'default', null, $minCharacterSetsNum], [AccountManagement::XML_PATH_REGISTER_EMAIL_TEMPLATE, ScopeInterface::SCOPE_STORE, $defaultStoreId, $templateIdentifier], [AccountManagement::XML_PATH_REGISTER_EMAIL_IDENTITY, ScopeInterface::SCOPE_STORE, 1, $sender]]); $this->string->expects($this->any())->method('strlen')->with($password)->willReturn(iconv_strlen($password, 'UTF-8')); $this->encryptor->expects($this->once())->method('getHash')->with($password, true)->willReturn($hash); $address = $this->getMockBuilder('Magento\\Customer\\Api\\Data\\AddressInterface')->disableOriginalConstructor()->getMock(); $address->expects($this->once())->method('setCustomerId')->with($customerId); $store = $this->getMockBuilder('Magento\\Store\\Model\\Store')->disableOriginalConstructor()->getMock(); $store->expects($this->once())->method('getId')->willReturn($defaultStoreId); $website = $this->getMockBuilder('Magento\\Store\\Model\\Website')->disableOriginalConstructor()->getMock(); $website->expects($this->atLeastOnce())->method('getStoreIds')->willReturn([1, 2, 3]); $website->expects($this->once())->method('getDefaultStore')->willReturn($store); $customer = $this->getMockBuilder('Magento\\Customer\\Api\\Data\\CustomerInterface')->getMock(); $customer->expects($this->atLeastOnce())->method('getId')->willReturn($customerId); $customer->expects($this->atLeastOnce())->method('getEmail')->willReturn($customerEmail); $customer->expects($this->atLeastOnce())->method('getWebsiteId')->willReturn($websiteId); $customer->expects($this->atLeastOnce())->method('getStoreId')->willReturn($storeId); $customer->expects($this->once())->method('setStoreId')->with($defaultStoreId); $customer->expects($this->once())->method('getAddresses')->willReturn([$address]); $customer->expects($this->once())->method('setAddresses')->with(null); $this->customerRepository->expects($this->once())->method('get')->with($customerEmail)->willReturn($customer); $this->share->expects($this->once())->method('isWebsiteScope')->willReturn(true); $this->storeManager->expects($this->atLeastOnce())->method('getWebsite')->with($websiteId)->willReturn($website); $this->customerRepository->expects($this->atLeastOnce())->method('save')->willReturn($customer); $this->addressRepository->expects($this->atLeastOnce())->method('save')->with($address); $this->customerRepository->expects($this->once())->method('getById')->with($customerId)->willReturn($customer); $this->random->expects($this->once())->method('getUniqueHash')->willReturn($newLinkToken); $customerSecure = $this->getMockBuilder('Magento\\Customer\\Model\\Data\\CustomerSecure')->setMethods(['setRpToken', 'setRpTokenCreatedAt', 'getPasswordHash'])->disableOriginalConstructor()->getMock(); $customerSecure->expects($this->any())->method('setRpToken')->with($newLinkToken); $customerSecure->expects($this->any())->method('setRpTokenCreatedAt'); $customerSecure->expects($this->any())->method('getPasswordHash')->willReturn($hash); $this->customerRegistry->expects($this->atLeastOnce())->method('retrieveSecureData')->willReturn($customerSecure); $this->emailNotificationMock->expects($this->once())->method('newAccount')->willReturnSelf(); $this->accountManagement->createAccount($customer, $password); }
/** * {@inheritDoc} */ public function isCustomerInStore($customerWebsiteId, $storeId) { $ids = []; if ((bool) $this->configShare->isWebsiteScope()) { $ids = $this->storeManager->getWebsite($customerWebsiteId)->getStoreIds(); } else { foreach ($this->storeManager->getStores() as $store) { $ids[] = $store->getId(); } } return in_array($storeId, $ids); }