/** * Join customers * * @return $this */ protected function _joinCustomers() { /** @var $adapter \Magento\Framework\DB\Adapter\AdapterInterface */ $adapter = $this->getConnection(); /** @var $firstnameAttr \Magento\Eav\Model\Entity\Attribute */ $firstnameAttr = $this->_customerResource->getAttribute('firstname'); /** @var $lastnameAttr \Magento\Eav\Model\Entity\Attribute */ $lastnameAttr = $this->_customerResource->getAttribute('lastname'); $firstnameCondition = array('table_customer_firstname.entity_id = detail.customer_id'); if ($firstnameAttr->getBackend()->isStatic()) { $firstnameField = 'firstname'; } else { $firstnameField = 'value'; $firstnameCondition[] = $adapter->quoteInto('table_customer_firstname.attribute_id = ?', (int) $firstnameAttr->getAttributeId()); } $this->getSelect()->joinInner(array('table_customer_firstname' => $firstnameAttr->getBackend()->getTable()), implode(' AND ', $firstnameCondition), array()); $lastnameCondition = array('table_customer_lastname.entity_id = detail.customer_id'); if ($lastnameAttr->getBackend()->isStatic()) { $lastnameField = 'lastname'; } else { $lastnameField = 'value'; $lastnameCondition[] = $adapter->quoteInto('table_customer_lastname.attribute_id = ?', (int) $lastnameAttr->getAttributeId()); } //Prepare fullname field result $customerFullname = $adapter->getConcatSql(array("table_customer_firstname.{$firstnameField}", "table_customer_lastname.{$lastnameField}"), ' '); $this->getSelect()->reset(\Zend_Db_Select::COLUMNS)->joinInner(array('table_customer_lastname' => $lastnameAttr->getBackend()->getTable()), implode(' AND ', $lastnameCondition), array())->columns(array('customer_id' => 'detail.customer_id', 'customer_name' => $customerFullname, 'review_cnt' => 'COUNT(main_table.review_id)'))->group('detail.customer_id'); return $this; }
/** * Add customer data * * @param unknown_type $filter * @return $this */ public function addCustomerData($filter = null) { $attrFirstname = $this->_customerResource->getAttribute('firstname'); $attrFirstnameId = (int) $attrFirstname->getAttributeId(); $attrFirstnameTableName = $attrFirstname->getBackend()->getTable(); $attrLastname = $this->_customerResource->getAttribute('lastname'); $attrLastnameId = (int) $attrLastname->getAttributeId(); $attrLastnameTableName = $attrLastname->getBackend()->getTable(); $attrEmail = $this->_customerResource->getAttribute('email'); $attrEmailTableName = $attrEmail->getBackend()->getTable(); $adapter = $this->getSelect()->getAdapter(); $customerName = $adapter->getConcatSql(array('cust_fname.value', 'cust_lname.value'), ' '); $this->getSelect()->joinInner(array('cust_email' => $attrEmailTableName), 'cust_email.entity_id = main_table.customer_id', array('email' => 'cust_email.email'))->joinInner(array('cust_fname' => $attrFirstnameTableName), implode(' AND ', array('cust_fname.entity_id = main_table.customer_id', $adapter->quoteInto('cust_fname.attribute_id = ?', (int) $attrFirstnameId))), array('firstname' => 'cust_fname.value'))->joinInner(array('cust_lname' => $attrLastnameTableName), implode(' AND ', array('cust_lname.entity_id = main_table.customer_id', $adapter->quoteInto('cust_lname.attribute_id = ?', (int) $attrLastnameId))), array('lastname' => 'cust_lname.value', 'customer_name' => $customerName)); $this->_joinedFields['customer_name'] = $customerName; $this->_joinedFields['email'] = 'cust_email.email'; if ($filter) { if (isset($filter['customer_name'])) { $likeExpr = '%' . $filter['customer_name'] . '%'; $this->getSelect()->where($this->_joinedFields['customer_name'] . ' LIKE ?', $likeExpr); } if (isset($filter['email'])) { $likeExpr = '%' . $filter['email'] . '%'; $this->getSelect()->where($this->_joinedFields['email'] . ' LIKE ?', $likeExpr); } } return $this; }
protected function setUp() { $this->_website = $this->getMock('Magento\\Store\\Model\\Website', [], [], '', false); $this->_config = $this->getMock('Magento\\Eav\\Model\\Config', [], [], '', false); $this->_attribute = $this->getMock('Magento\\Eav\\Model\\Attribute', [], [], '', false); $this->_storeManager = $this->getMock('Magento\\Store\\Model\\StoreManager', [], [], '', false); $this->_storetMock = $this->getMock('\\Magento\\Store\\Model\\Store', [], [], '', false); $this->_scopeConfigMock = $this->getMock('\\Magento\\Framework\\App\\Config\\ScopeConfigInterface'); $this->_transportBuilderMock = $this->getMock('\\Magento\\Framework\\Mail\\Template\\TransportBuilder', [], [], '', false); $this->_transportMock = $this->getMock('Magento\\Framework\\Mail\\TransportInterface', [], [], '', false); $this->attributeFactoryMock = $this->getMock('Magento\\Customer\\Model\\AttributeFactory', ['create'], [], '', false); $this->attributeCustomerMock = $this->getMock('Magento\\Customer\\Model\\Attribute', [], [], '', false); $this->resourceMock = $this->getMock('\\Magento\\Customer\\Model\\Resource\\Customer', ['getIdFieldName'], [], '', false, false); $this->resourceMock->expects($this->any())->method('getIdFieldName')->will($this->returnValue('id')); $this->registryMock = $this->getMock('Magento\\Framework\\Registry', ['registry'], [], '', false); $this->_encryptor = $this->getMock('Magento\\Framework\\Encryption\\EncryptorInterface'); $helper = new \Magento\TestFramework\Helper\ObjectManager($this); $this->_model = $helper->getObject('Magento\\Customer\\Model\\Customer', ['storeManager' => $this->_storeManager, 'config' => $this->_config, 'transportBuilder' => $this->_transportBuilderMock, 'scopeConfig' => $this->_scopeConfigMock, 'encryptor' => $this->_encryptor, 'attributeFactory' => $this->attributeFactoryMock, 'registry' => $this->registryMock, 'resource' => $this->resourceMock]); }
/** * Resolve customers data based on ids quote table. * * @return void */ public function resolveCustomerNames() { $select = $this->customerResource->getReadConnection()->select(); $customerName = $select->getAdapter()->getConcatSql(['firstname', 'lastname'], ' '); $select->from(['customer' => $this->customerResource->getTable('customer_entity')], ['email'])->columns(['customer_name' => $customerName])->where('customer.entity_id IN (?)', array_column($this->getData(), 'customer_id')); $customersData = $select->getAdapter()->fetchAll($select); foreach ($this->getItems() as $item) { $item->setData(array_merge($item->getData(), current($customersData))); next($customersData); } }
/** * Check for email duplicates before saving customers sharing options * * @return $this * @throws \Magento\Framework\Exception\LocalizedException */ public function beforeSave() { $value = $this->getValue(); if ($value == self::SHARE_GLOBAL) { if ($this->_customerResource->findEmailDuplicates()) { //@codingStandardsIgnoreStart throw new \Magento\Framework\Exception\LocalizedException(__('We can\'t share customer accounts globally when the accounts share identical email addresses on more than one website.')); //@codingStandardsIgnoreEnd } } return $this; }
/** * Check for email duplicates before saving customers sharing options * * @return $this * @throws \Magento\Framework\Exception\LocalizedException */ public function beforeSave() { $value = $this->getValue(); if ($value == self::SHARE_GLOBAL) { if ($this->_customerResource->findEmailDuplicates()) { //@codingStandardsIgnoreStart throw new \Magento\Framework\Exception\LocalizedException(__('Cannot share customer accounts globally because some customer accounts with the same emails exist on multiple websites and cannot be merged.')); //@codingStandardsIgnoreEnd } } return $this; }
/** * @SuppressWarnings(PHPMD.ExcessiveMethodLength) */ public function testSaveWithPasswordHash() { $customerId = 1; $storeId = 2; $passwordHash = 'ukfa4sdfa56s5df02asdf4rt'; $this->prepareMocksForValidation(true); $region = $this->getMockForAbstractClass('Magento\\Customer\\Api\\Data\\RegionInterface', [], '', false); $address = $this->getMockForAbstractClass('Magento\\Customer\\Api\\Data\\AddressInterface', [], '', false, false, true, ['setCustomerId', 'setRegion', 'getRegion', 'getId']); $address2 = $this->getMockForAbstractClass('Magento\\Customer\\Api\\Data\\AddressInterface', [], '', false, false, true, ['setCustomerId', 'setRegion', 'getRegion', 'getId']); $customerModel = $this->getMock('Magento\\Customer\\Model\\Customer', ['getId', 'setId', 'setStoreId', 'getStoreId', 'getAttributeSetId', 'setAttributeSetId', 'setRpToken', 'setRpTokenCreatedAt', 'getDataModel', 'setPasswordHash'], [], '', false); $customerAttributesMetaData = $this->getMockForAbstractClass('Magento\\Framework\\Api\\CustomAttributesDataInterface', [], '', false, false, true, ['getId', 'getEmail', 'getWebsiteId', 'getAddresses', 'setAddresses']); $this->customer->expects($this->atLeastOnce())->method('getId')->willReturn($customerId); $this->customerRegistry->expects($this->atLeastOnce())->method('retrieve')->with($customerId)->willReturn($customerModel); $customerModel->expects($this->atLeastOnce())->method('getDataModel')->willReturn($this->customer); $this->imageProcessor->expects($this->once())->method('save')->with($this->customer, CustomerMetadataInterface::ENTITY_TYPE_CUSTOMER, $this->customer)->willReturn($customerAttributesMetaData); $address->expects($this->once())->method('setCustomerId')->with($customerId)->willReturnSelf(); $address->expects($this->once())->method('getRegion')->willReturn($region); $address->expects($this->atLeastOnce())->method('getId')->willReturn(7); $address->expects($this->once())->method('setRegion')->with($region); $customerAttributesMetaData->expects($this->any())->method('getAddresses')->willReturn([$address]); $customerAttributesMetaData->expects($this->at(1))->method('setAddresses')->with([]); $customerAttributesMetaData->expects($this->at(2))->method('setAddresses')->with([$address]); $this->extensibleDataObjectConverter->expects($this->once())->method('toNestedArray')->with($customerAttributesMetaData, [], '\\Magento\\Customer\\Api\\Data\\CustomerInterface')->willReturn(['customerData']); $this->customerFactory->expects($this->once())->method('create')->with(['data' => ['customerData']])->willReturn($customerModel); $customerModel->expects($this->once())->method('getStoreId')->willReturn(null); $store = $this->getMock('Magento\\Store\\Model\\Store', [], [], '', false); $store->expects($this->once())->method('getId')->willReturn($storeId); $this->storeManager->expects($this->once())->method('getStore')->willReturn($store); $customerModel->expects($this->once())->method('setStoreId')->with($storeId); $customerModel->expects($this->once())->method('setId')->with(null); $customerModel->expects($this->once())->method('getAttributeSetId')->willReturn(null); $customerModel->expects($this->once())->method('setAttributeSetId')->with(\Magento\Customer\Api\CustomerMetadataInterface::ATTRIBUTE_SET_ID_CUSTOMER); $customerModel->expects($this->once())->method('setPasswordHash')->with($passwordHash); $customerModel->expects($this->atLeastOnce())->method('getId')->willReturn($customerId); $this->customerResourceModel->expects($this->once())->method('save')->with($customerModel); $this->customerRegistry->expects($this->once())->method('push')->with($customerModel); $this->customer->expects($this->any())->method('getAddresses')->willReturn([$address, $address2]); $this->addressRepository->expects($this->once())->method('save')->with($address); $customerAttributesMetaData->expects($this->once())->method('getEmail')->willReturn('*****@*****.**'); $customerAttributesMetaData->expects($this->once())->method('getWebsiteId')->willReturn(2); $this->customerRegistry->expects($this->once())->method('retrieveByEmail')->with('*****@*****.**', 2)->willReturn($customerModel); $this->eventManager->expects($this->once())->method('dispatch')->with('customer_save_after_data_object', ['customer_data_object' => $this->customer, 'orig_customer_data_object' => $customerAttributesMetaData]); $this->model->save($this->customer, $passwordHash); }
/** * Test _saveCustomerDefaults * * @magentoDataFixture Magento/Customer/_files/import_export/customer_with_addresses.php */ public function testSaveCustomerDefaults() { /** @var $objectManager \Magento\TestFramework\ObjectManager */ $objectManager = \Magento\TestFramework\Helper\Bootstrap::getObjectManager(); // get not default address $customers = $objectManager->get('Magento\\Framework\\Registry')->registry($this->_fixtureKey); /** @var $notDefaultAddress \Magento\Customer\Model\Address */ $notDefaultAddress = null; /** @var $addressCustomer \Magento\Customer\Model\Customer */ $addressCustomer = null; /** @var $customer \Magento\Customer\Model\Customer */ foreach ($customers as $customer) { /** @var $address \Magento\Customer\Model\Address */ foreach ($customer->getAddressesCollection() as $address) { if (!$customer->getDefaultBillingAddress() && !$customer->getDefaultShippingAddress()) { $notDefaultAddress = $address; $addressCustomer = $customer; break; } if ($notDefaultAddress) { break; } } } $this->assertNotNull($notDefaultAddress, 'Not default address must exists.'); $this->assertNotNull($addressCustomer, 'Not default address customer must exists.'); $addressId = $notDefaultAddress->getId(); $customerId = $addressCustomer->getId(); // set customer defaults $defaults = [$this->customerResource->getTable('customer_entity') => [$customerId => ['default_billing' => $addressId, 'default_shipping' => $addressId]]]; // invoke _saveCustomerDefaults $saveDefaults = new \ReflectionMethod($this->_testClassName, '_saveCustomerDefaults'); $saveDefaults->setAccessible(true); $saveDefaults->invoke($this->_entityAdapter, $defaults); // check DB /** @var $testCustomer \Magento\Customer\Model\Customer */ $testCustomer = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()->create('Magento\\Customer\\Model\\Customer'); $testCustomer->load($customerId); $this->assertEquals($customerId, $testCustomer->getId(), 'Customer must exists.'); $this->assertNotNull($testCustomer->getDefaultBillingAddress(), 'Default billing address must exists.'); $this->assertNotNull($testCustomer->getDefaultShippingAddress(), 'Default shipping address must exists.'); $this->assertEquals($addressId, $testCustomer->getDefaultBillingAddress()->getId(), 'Incorrect default billing address.'); $this->assertEquals($addressId, $testCustomer->getDefaultShippingAddress()->getId(), 'Incorrect default shipping address.'); }
/** * {@inheritdoc} * @SuppressWarnings(PHPMD.CyclomaticComplexity) */ public function save(\Magento\Customer\Api\Data\CustomerInterface $customer, $passwordHash = null) { $this->validate($customer); $origAddresses = $customer->getAddresses(); $customer->setAddresses([]); $customerData = $this->extensibleDataObjectConverter->toNestedArray($customer, [], '\\Magento\\Customer\\Api\\Data\\CustomerInterface'); $customer->setAddresses($origAddresses); $customerModel = $this->customerFactory->create(['data' => $customerData]); $storeId = $customerModel->getStoreId(); if ($storeId === null) { $customerModel->setStoreId($this->storeManager->getStore()->getId()); } $customerModel->setId($customer->getId()); // Need to use attribute set or future updates can cause data loss if (!$customerModel->getAttributeSetId()) { $customerModel->setAttributeSetId(\Magento\Customer\Api\CustomerMetadataInterface::ATTRIBUTE_SET_ID_CUSTOMER); } // Populate model with secure data if ($customer->getId()) { /* * TODO: Check \Magento\Customer\Model\Resource\Customer::changeResetPasswordLinkToken setAttribute * and make sure its consistent */ $customerSecure = $this->customerRegistry->retrieveSecureData($customer->getId()); $customerModel->setRpToken($customerSecure->getRpToken()); $customerModel->setRpTokenCreatedAt($customerSecure->getRpTokenCreatedAt()); $customerModel->setPasswordHash($customerSecure->getPasswordHash()); } else { if ($passwordHash) { $customerModel->setPasswordHash($passwordHash); } } $this->customerResourceModel->save($customerModel); $this->customerRegistry->push($customerModel); $customerId = $customerModel->getId(); if ($customer->getAddresses() !== null) { if ($customer->getId()) { $existingAddresses = $this->getById($customer->getId())->getAddresses(); $getIdFunc = function ($address) { return $address->getId(); }; $existingAddressIds = array_map($getIdFunc, $existingAddresses); } else { $existingAddressIds = []; } $savedAddressIds = []; foreach ($customer->getAddresses() as $address) { $address->setCustomerId($customerId)->setRegion($address->getRegion()); $this->addressRepository->save($address); if ($address->getId()) { $savedAddressIds[] = $address->getId(); } } $addressIdsToDelete = array_diff($existingAddressIds, $savedAddressIds); foreach ($addressIdsToDelete as $addressId) { $this->addressRepository->deleteById($addressId); } } $savedCustomer = $this->get($customer->getEmail(), $customer->getWebsiteId()); $this->eventManager->dispatch('customer_save_after_data_object', ['customer_data_object' => $savedCustomer, 'orig_customer_data_object' => $customer]); return $savedCustomer; }