/**
  * @magentoAppArea adminhtml
  * @magentoDataFixture Magento/Customer/_files/customer.php
  * @magentoAppIsolation enabled
  */
 public function testDelete()
 {
     $fixtureCustomerEmail = '*****@*****.**';
     $customer = $this->customerRepository->get($fixtureCustomerEmail);
     $this->customerRepository->delete($customer);
     /** Ensure that customer was deleted */
     $this->setExpectedException('Magento\\Framework\\Exception\\NoSuchEntityException', 'No such entity with email = customer@example.com, websiteId = 1');
     $this->customerRepository->get($fixtureCustomerEmail);
 }
 /**
  * @magentoAppArea adminhtml
  * @magentoDataFixture Magento/Newsletter/_files/subscribers.php
  */
 public function testCustomerDeletedAdminArea()
 {
     $customer = $this->customerRepository->getById(1);
     $objectManager = Bootstrap::getObjectManager();
     /** @var \Magento\Newsletter\Model\Subscriber $subscriber */
     $subscriber = $objectManager->create('Magento\\Newsletter\\Model\\Subscriber');
     $subscriber->loadByEmail('*****@*****.**');
     $this->assertTrue($subscriber->isSubscribed());
     $this->customerRepository->delete($customer);
     $this->verifySubscriptionNotExist('*****@*****.**');
 }
 /**
  * {@inheritdoc}
  * @SuppressWarnings(PHPMD.CyclomaticComplexity)
  * @SuppressWarnings(PHPMD.NPathComplexity)
  */
 public function createAccountWithPasswordHash(CustomerInterface $customer, $hash, $redirectUrl = '')
 {
     // This logic allows an existing customer to be added to a different store.  No new account is created.
     // The plan is to move this logic into a new method called something like 'registerAccountWithStore'
     if ($customer->getId()) {
         $customer = $this->customerRepository->get($customer->getEmail());
         $websiteId = $customer->getWebsiteId();
         if ($this->isCustomerInStore($websiteId, $customer->getStoreId())) {
             throw new InputException(__('This customer already exists in this store.'));
         }
         // Existing password hash will be used from secured customer data registry when saving customer
     }
     // Make sure we have a storeId to associate this customer with.
     if (!$customer->getStoreId()) {
         if ($customer->getWebsiteId()) {
             $storeId = $this->storeManager->getWebsite($customer->getWebsiteId())->getDefaultStore()->getId();
         } else {
             $storeId = $this->storeManager->getStore()->getId();
         }
         $customer->setStoreId($storeId);
     }
     // Update 'created_in' value with actual store name
     if ($customer->getId() === null) {
         $storeName = $this->storeManager->getStore($customer->getStoreId())->getName();
         $customer->setCreatedIn($storeName);
     }
     $customerAddresses = $customer->getAddresses() ?: [];
     $customer->setAddresses(null);
     try {
         // If customer exists existing hash will be used by Repository
         $customer = $this->customerRepository->save($customer, $hash);
     } catch (AlreadyExistsException $e) {
         throw new InputMismatchException(__('A customer with the same email already exists in an associated website.'));
     } catch (LocalizedException $e) {
         throw $e;
     }
     try {
         foreach ($customerAddresses as $address) {
             $address->setCustomerId($customer->getId());
             $this->addressRepository->save($address);
         }
     } catch (InputException $e) {
         $this->customerRepository->delete($customer);
         throw $e;
     }
     $customer = $this->customerRepository->getById($customer->getId());
     $newLinkToken = $this->mathRandom->getUniqueHash();
     $this->changeResetPasswordLinkToken($customer, $newLinkToken);
     $this->sendEmailConfirmation($customer, $redirectUrl);
     return $customer;
 }