/**
  * @param string|int|float $sortOrder
  * @dataProvider getInvalidSortOrder
  * @expectedException \Magento\Framework\Exception\InputException
  * @expectedExceptionMessage Sort order must be a positive integer.
  */
 public function testIsValidThrowsExceptionIfSortOrderIsInvalid($sortOrder)
 {
     $sampleContentData = ['title' => 'Title', 'sort_order' => $sortOrder, 'sample_type' => 'file'];
     $this->fileValidatorMock->expects($this->any())->method('isValid')->will($this->returnValue(true));
     $this->urlValidatorMock->expects($this->any())->method('isValid')->will($this->returnValue(true));
     $this->validator->isValid($this->getSampleContentMock($sampleContentData));
 }
 /**
  * {@inheritdoc}
  * @SuppressWarnings(PHPMD.CyclomaticComplexity)
  */
 public function save($productSku, SampleContentInterface $sampleContent, $sampleId = null, $isGlobalScopeContent = false)
 {
     $product = $this->productRepository->get($productSku, true);
     if ($sampleId) {
         /** @var $sample \Magento\Downloadable\Model\Sample */
         $sample = $this->sampleFactory->create()->load($sampleId);
         if (!$sample->getId()) {
             throw new NoSuchEntityException(__('There is no downloadable sample with provided ID.'));
         }
         if ($sample->getProductId() != $product->getId()) {
             throw new InputException(__('Provided downloadable sample is not related to given product.'));
         }
         if (!$this->contentValidator->isValid($sampleContent)) {
             throw new InputException(__('Provided sample information is invalid.'));
         }
         if ($isGlobalScopeContent) {
             $product->setStoreId(0);
         }
         $title = $sampleContent->getTitle();
         if (empty($title)) {
             if ($isGlobalScopeContent) {
                 throw new InputException(__('Sample title cannot be empty.'));
             }
             // use title from GLOBAL scope
             $sample->setTitle(null);
         } else {
             $sample->setTitle($sampleContent->getTitle());
         }
         $sample->setProductId($product->getId())->setStoreId($product->getStoreId())->setSortOrder($sampleContent->getSortOrder())->save();
         return $sample->getId();
     } else {
         if ($product->getTypeId() !== \Magento\Downloadable\Model\Product\Type::TYPE_DOWNLOADABLE) {
             throw new InputException(__('Product type of the product must be \'downloadable\'.'));
         }
         if (!$this->contentValidator->isValid($sampleContent)) {
             throw new InputException(__('Provided sample information is invalid.'));
         }
         if (!in_array($sampleContent->getSampleType(), ['url', 'file'])) {
             throw new InputException(__('Invalid sample type.'));
         }
         $title = $sampleContent->getTitle();
         if (empty($title)) {
             throw new InputException(__('Sample title cannot be empty.'));
         }
         $sampleData = ['sample_id' => 0, 'is_delete' => 0, 'type' => $sampleContent->getSampleType(), 'sort_order' => $sampleContent->getSortOrder(), 'title' => $sampleContent->getTitle()];
         if ($sampleContent->getSampleType() == 'file') {
             $sampleData['file'] = $this->jsonEncoder->encode([$this->fileContentUploader->upload($sampleContent->getSampleFile(), 'sample')]);
         } else {
             $sampleData['sample_url'] = $sampleContent->getSampleUrl();
         }
         $downloadableData = ['sample' => [$sampleData]];
         $product->setDownloadableData($downloadableData);
         if ($isGlobalScopeContent) {
             $product->setStoreId(0);
         }
         $product->save();
         return $product->getLastAddedSampleId();
     }
 }
Beispiel #3
0
 /**
  * @param \Magento\Catalog\Api\Data\ProductInterface $product
  * @param SampleInterface $sample
  * @param bool $isGlobalScopeContent
  * @return int
  * @throws InputException
  * @throws NoSuchEntityException
  * @SuppressWarnings(PHPMD.CyclomaticComplexity)
  * @SuppressWarnings(PHPMD.NPathComplexity)
  */
 protected function updateSample(\Magento\Catalog\Api\Data\ProductInterface $product, SampleInterface $sample, $isGlobalScopeContent)
 {
     $sampleId = $sample->getId();
     /** @var $existingSample \Magento\Downloadable\Model\Sample */
     $existingSample = $this->sampleFactory->create()->load($sampleId);
     if (!$existingSample->getId()) {
         throw new NoSuchEntityException(__('There is no downloadable sample with provided ID.'));
     }
     if ($existingSample->getProductId() != $product->getId()) {
         throw new InputException(__('Provided downloadable sample is not related to given product.'));
     }
     $validateFileContent = $sample->getSampleFileContent() === null ? false : true;
     if (!$this->contentValidator->isValid($sample, $validateFileContent)) {
         throw new InputException(__('Provided sample information is invalid.'));
     }
     if ($isGlobalScopeContent) {
         $product->setStoreId(0);
     }
     $title = $sample->getTitle();
     if (empty($title)) {
         if ($isGlobalScopeContent) {
             throw new InputException(__('Sample title cannot be empty.'));
         }
         // use title from GLOBAL scope
         $existingSample->setTitle(null);
     } else {
         $existingSample->setTitle($sample->getTitle());
     }
     if ($sample->getSampleType() === 'file' && $sample->getSampleFileContent() === null) {
         $sample->setSampleFile($existingSample->getSampleFile());
     }
     $this->saveSample($product, $sample, $isGlobalScopeContent);
     return $existingSample->getId();
 }
 public function testSaveWithFile()
 {
     $sampleId = 1;
     $productId = 1;
     $productSku = 'simple';
     $sampleFile = '/s/a/sample.jpg';
     $sampleData = ['id' => $sampleId, 'title' => 'Updated Title', 'sort_order' => 1, 'sample_type' => 'file', 'file' => $sampleFile];
     $this->repositoryMock->expects($this->once())->method('get')->with($productSku, true)->willReturn($this->productMock);
     $this->productMock->expects($this->once())->method('getTypeId')->willReturn('downloadable');
     $this->productMock->expects($this->once())->method('getData')->with('id')->willReturn($productId);
     $sampleMock = $this->getSampleMock($sampleData);
     $this->contentValidatorMock->expects($this->any())->method('isValid')->with($sampleMock)->willReturn(true);
     $sampleMock->expects($this->once())->method('setProductId')->with($productId);
     $sampleMock->expects($this->exactly(2))->method('getFile')->willReturn($sampleFile);
     $this->jsonEncoderMock->expects($this->once())->method('jsonDecode')->willReturn($sampleFile);
     $this->contentUploaderMock->expects($this->once())->method('moveFileFromTmp')->with($sampleMock->getBaseTmpPath(), $sampleMock->getBasePath(), $sampleFile)->willReturn($sampleFile);
     $sampleMock->expects($this->once())->method('setSampleFile')->with($sampleFile);
     $sampleMock->expects($this->once())->method('setSampleUrl')->with(null);
     $this->sampleResourceMock->expects($this->once())->method('save')->with($sampleMock)->willReturn($sampleId);
     $this->assertEquals($sampleId, $this->service->save($productSku, $sampleMock));
 }