/** * @magentoAppArea adminhtml * @magentoDataFixture Magento/Newsletter/_files/subscribers.php */ public function testCustomerCreated() { $objectManager = Bootstrap::getObjectManager(); /** @var \Magento\Newsletter\Model\Subscriber $subscriber */ $subscriber = $objectManager->create('Magento\\Newsletter\\Model\\Subscriber'); $subscriber->loadByEmail('*****@*****.**'); $this->assertTrue($subscriber->isSubscribed()); $this->assertEquals(0, (int) $subscriber->getCustomerId()); /** @var \Magento\Customer\Api\Data\CustomerInterfaceFactory $customerFactory */ $customerFactory = $objectManager->get('Magento\\Customer\\Api\\Data\\CustomerInterfaceFactory'); $customerDataObject = $customerFactory->create()->setFirstname('Firstname')->setLastname('Lastname')->setEmail('*****@*****.**'); $createdCustomer = $this->customerRepository->save($customerDataObject, $this->accountManagement->getPasswordHash('password')); $subscriber->loadByEmail('*****@*****.**'); $this->assertTrue($subscriber->isSubscribed()); $this->assertEquals((int) $createdCustomer->getId(), (int) $subscriber->getCustomerId()); }
/** * Create a quote object with customer * * @param array $quoteData * @param \Magento\Customer\Api\Data\CustomerInterface $customer * @return \Magento\Quote\Model\Quote */ protected function createQuote($quoteData, $customer) { /** @var \Magento\Customer\Api\AddressRepositoryInterface $addressService */ $addressService = $this->objectManager->create('Magento\\Customer\\Api\\AddressRepositoryInterface'); /** @var array $shippingAddressOverride */ $shippingAddressOverride = empty($quoteData['shipping_address']) ? [] : $quoteData['shipping_address']; /** @var \Magento\Customer\Model\Address $shippingAddress */ $shippingAddress = $this->createCustomerAddress($shippingAddressOverride, $customer->getId()); /** @var \Magento\Quote\Model\Quote\Address $quoteShippingAddress */ $quoteShippingAddress = $this->objectManager->create('Magento\\Quote\\Model\\Quote\\Address'); $quoteShippingAddress->importCustomerAddressData($addressService->getById($shippingAddress->getId())); /** @var array $billingAddressOverride */ $billingAddressOverride = empty($quoteData['billing_address']) ? [] : $quoteData['billing_address']; /** @var \Magento\Customer\Model\Address $billingAddress */ $billingAddress = $this->createCustomerAddress($billingAddressOverride, $customer->getId()); /** @var \Magento\Quote\Model\Quote\Address $quoteBillingAddress */ $quoteBillingAddress = $this->objectManager->create('Magento\\Quote\\Model\\Quote\\Address'); $quoteBillingAddress->importCustomerAddressData($addressService->getById($billingAddress->getId())); /** @var \Magento\Quote\Model\Quote $quote */ $quote = $this->objectManager->create('Magento\\Quote\\Model\\Quote'); $quote->setStoreId(1)->setIsActive(true)->setIsMultiShipping(false)->assignCustomerWithAddressChange($customer, $quoteBillingAddress, $quoteShippingAddress)->setCheckoutMethod('register')->setPasswordHash($this->accountManagement->getPasswordHash(static::CUSTOMER_PASSWORD)); return $quote; }
/** * Validate customer data and set some its data for further usage in quote * * Will return either true or array with error messages * * @param array $data * @return bool|array */ protected function _validateCustomerData(array $data) { $quote = $this->getQuote(); $isCustomerNew = !$quote->getCustomerId(); $customer = $quote->getCustomer(); $customerData = $this->extensibleDataObjectConverter->toFlatArray($customer, [], '\\Magento\\Customer\\Api\\Data\\CustomerInterface'); /** @var Form $customerForm */ $customerForm = $this->_formFactory->create(\Magento\Customer\Api\CustomerMetadataInterface::ENTITY_TYPE_CUSTOMER, 'checkout_register', $customerData, $this->_request->isAjax(), Form::IGNORE_INVISIBLE, []); if ($isCustomerNew) { $customerRequest = $customerForm->prepareRequest($data); $customerData = $customerForm->extractData($customerRequest); } $customerErrors = $customerForm->validateData($customerData); if ($customerErrors !== true) { return ['error' => -1, 'message' => implode(', ', $customerErrors)]; } if (!$isCustomerNew) { return true; } $customer = $this->customerDataFactory->create(); $this->dataObjectHelper->populateWithArray($customer, $customerData, '\\Magento\\Customer\\Api\\Data\\CustomerInterface'); if ($quote->getCheckoutMethod() == self::METHOD_REGISTER) { // We always have $customerRequest here, otherwise we would have been kicked off the function several // lines above $password = $customerRequest->getParam('customer_password'); if ($password != $customerRequest->getParam('confirm_password')) { return ['error' => -1, 'message' => __('Password and password confirmation are not equal.')]; } $quote->setPasswordHash($this->accountManagement->getPasswordHash($password)); } else { // set NOT LOGGED IN group id explicitly, // otherwise copyFieldsetToTarget('customer_account', 'to_quote') will fill it with default group id value $customer->setGroupId(GroupInterface::NOT_LOGGED_IN_ID); } //validate customer $result = $this->accountManagement->validate($customer); if (!$result->isValid()) { return ['error' => -1, 'message' => implode(', ', $result->getMessages())]; } // copy customer/guest email to address $quote->getBillingAddress()->setEmail($customer->getEmail()); // copy customer data to quote $this->_objectCopyService->copyFieldsetToTarget('customer_account', 'to_quote', $this->extensibleDataObjectConverter->toFlatArray($customer, [], '\\Magento\\Customer\\Api\\Data\\CustomerInterface'), $quote); return true; }