/**
  * @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);
 }
Example #2
0
 /**
  * @param ProductInterface $product
  * @param array $newEntry
  * @return $this
  * @throws InputException
  * @throws StateException
  * @throws \Magento\Framework\Exception\LocalizedException
  */
 protected function processNewMediaGalleryEntry(ProductInterface $product, array $newEntry)
 {
     /** @var ImageContentInterface $contentDataObject */
     $contentDataObject = $newEntry['content'];
     /** @var \Magento\Catalog\Model\Product\Media\Config $mediaConfig */
     $mediaConfig = $product->getMediaConfig();
     $mediaTmpPath = $mediaConfig->getBaseTmpMediaPath();
     $relativeFilePath = $this->imageProcessor->processImageContent($mediaTmpPath, $contentDataObject);
     $tmpFilePath = $mediaConfig->getTmpMediaShortUrl($relativeFilePath);
     /** @var \Magento\Catalog\Model\Product\Attribute\Backend\Media $galleryAttributeBackend */
     $galleryAttributeBackend = $product->getGalleryAttributeBackend();
     if ($galleryAttributeBackend == null) {
         throw new StateException(__('Requested product does not support images.'));
     }
     $imageFileUri = $galleryAttributeBackend->addImage($product, $tmpFilePath, isset($newEntry['types']) ? $newEntry['types'] : [], true, isset($newEntry['disabled']) ? $newEntry['disabled'] : true);
     // Update additional fields that are still empty after addImage call
     $galleryAttributeBackend->updateImage($product, $imageFileUri, ['label' => $newEntry['label'], 'position' => $newEntry['position'], 'disabled' => $newEntry['disabled'], 'media_type' => $newEntry['media_type']]);
     return $this;
 }
 /**
  * {@inheritdoc}
  * @SuppressWarnings(PHPMD.CyclomaticComplexity)
  * @SuppressWarnings(PHPMD.NPathComplexity)
  */
 public function save(\Magento\Customer\Api\Data\CustomerInterface $customer, $passwordHash = null)
 {
     $this->validate($customer);
     $prevCustomerData = null;
     if ($customer->getId()) {
         $prevCustomerData = $this->getById($customer->getId());
     }
     $customer = $this->imageProcessor->save($customer, CustomerMetadataInterface::ENTITY_TYPE_CUSTOMER, $prevCustomerData);
     $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()) {
         $customerSecure = $this->customerRegistry->retrieveSecureData($customer->getId());
         $customerModel->setRpToken($customerSecure->getRpToken());
         $customerModel->setRpTokenCreatedAt($customerSecure->getRpTokenCreatedAt());
         $customerModel->setPasswordHash($customerSecure->getPasswordHash());
     } else {
         if ($passwordHash) {
             $customerModel->setPasswordHash($passwordHash);
         }
     }
     // If customer email was changed, reset RpToken info
     if ($prevCustomerData && $prevCustomerData->getEmail() !== $customerModel->getEmail()) {
         $customerModel->setRpToken(null);
         $customerModel->setRpTokenCreatedAt(null);
     }
     $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;
 }
 public function testSaveExistingWithNewMediaGalleryEntries()
 {
     $newEntriesData = [["label" => "label_text", 'position' => 10, 'disabled' => false, 'types' => ['image', 'small_image'], 'content' => [ImageContentInterface::NAME => 'filename', ImageContentInterface::TYPE => 'image/jpeg', ImageContentInterface::BASE64_ENCODED_DATA => 'encoded_content'], 'media_type' => 'media_type']];
     $this->setupProductMocksForSave();
     //media gallery data
     $this->productData['media_gallery_entries'] = $newEntriesData;
     $this->extensibleDataObjectConverterMock->expects($this->once())->method('toNestedArray')->will($this->returnValue($this->productData));
     $this->initializedProductMock->setData('media_gallery', []);
     $this->initializedProductMock->expects($this->any())->method('getMediaAttributes')->willReturn(["image" => "imageAttribute", "small_image" => "small_image_attribute"]);
     //setup media attribute backend
     $mediaTmpPath = '/tmp';
     $absolutePath = '/a/b/filename.jpg';
     $this->mediaGalleryProcessor->expects($this->once())->method('clearMediaAttribute')->with($this->initializedProductMock, ['image', 'small_image']);
     $mediaConfigMock = $this->getMockBuilder('Magento\\Catalog\\Model\\Product\\Media\\Config')->disableOriginalConstructor()->getMock();
     $mediaConfigMock->expects($this->once())->method('getTmpMediaShortUrl')->with($absolutePath)->willReturn($mediaTmpPath . $absolutePath);
     $this->initializedProductMock->expects($this->once())->method('getMediaConfig')->willReturn($mediaConfigMock);
     //verify new entries
     $contentDataObject = $this->getMockBuilder('Magento\\Framework\\Api\\ImageContent')->disableOriginalConstructor()->setMethods(null)->getMock();
     $this->contentFactoryMock->expects($this->once())->method('create')->willReturn($contentDataObject);
     $this->imageProcessorMock->expects($this->once())->method('processImageContent')->willReturn($absolutePath);
     $imageFileUri = "imageFileUri";
     $this->mediaGalleryProcessor->expects($this->once())->method('addImage')->with($this->initializedProductMock, $mediaTmpPath . $absolutePath, ['image', 'small_image'], true, false)->willReturn($imageFileUri);
     $this->mediaGalleryProcessor->expects($this->once())->method('updateImage')->with($this->initializedProductMock, $imageFileUri, ['label' => 'label_text', 'position' => 10, 'disabled' => false, 'media_type' => 'media_type']);
     $this->initializedProductMock->expects($this->once())->method('getWebsiteIds')->willReturn([]);
     $this->model->save($this->productMock);
 }