public function testGetList()
 {
     $productSku = 'downloadable_sku';
     $sampleData = ['id' => 324, 'store_title' => 'rock melody sample', 'title' => 'just melody sample', 'sort_order' => 21, 'sample_type' => 'file', 'sample_url' => null, 'sample_file' => '/r/o/rock.melody.ogg'];
     $sampleMock = $this->getMock('\\Magento\\Downloadable\\Model\\Sample', ['getId', 'getStoreTitle', 'getTitle', 'getSampleType', 'getSampleFile', 'getSampleUrl', 'getSortOrder', 'getData', '__wakeup'], [], '', false);
     $sampleInterfaceMock = $this->getMock('\\Magento\\Downloadable\\Api\\Data\\SampleInterface');
     $this->repositoryMock->expects($this->once())->method('get')->with($productSku)->will($this->returnValue($this->productMock));
     $this->productTypeMock->expects($this->once())->method('getSamples')->with($this->productMock)->will($this->returnValue([$sampleMock]));
     $this->setSampleAssertions($sampleMock, $sampleData);
     $this->sampleFactoryMock->expects($this->once())->method('create')->willReturn($sampleInterfaceMock);
     $this->assertEquals([$sampleInterfaceMock], $this->service->getList($productSku));
 }
 /**
  * {@inheritdoc}
  */
 public function delete($id)
 {
     /** @var $sample \Magento\Downloadable\Model\Sample */
     $sample = $this->sampleFactory->create()->load($id);
     if (!$sample->getId()) {
         throw new NoSuchEntityException(__('There is no downloadable sample with provided ID.'));
     }
     try {
         $this->resourceModel->delete($sample);
     } catch (\Exception $exception) {
         throw new StateException(__('Cannot delete sample with id %1', $sample->getId()), $exception);
     }
     return true;
 }
 /**
  * Build a sample data object
  *
  * @param \Magento\Downloadable\Model\Sample $resourceData
  * @return \Magento\Downloadable\Model\Sample
  */
 protected function buildSample($resourceData)
 {
     $sample = $this->sampleDataObjectFactory->create();
     $this->setBasicFields($resourceData, $sample);
     return $sample;
 }
 /**
  * Prepare product to save
  *
  * @param \Magento\Catalog\Controller\Adminhtml\Product\Initialization\Helper $subject
  * @param \Magento\Catalog\Model\Product $product
  *
  * @return \Magento\Catalog\Model\Product
  * @SuppressWarnings(PHPMD.UnusedFormalParameter)
  * @SuppressWarnings(PHPMD.CyclomaticComplexity)
  * @SuppressWarnings(PHPMD.NPathComplexity)
  */
 public function afterInitialize(\Magento\Catalog\Controller\Adminhtml\Product\Initialization\Helper $subject, \Magento\Catalog\Model\Product $product)
 {
     if ($downloadable = $this->request->getPost('downloadable')) {
         $product->setDownloadableData($downloadable);
         $extension = $product->getExtensionAttributes();
         if (isset($downloadable['link']) && is_array($downloadable['link'])) {
             $links = [];
             foreach ($downloadable['link'] as $linkData) {
                 if (!$linkData || isset($linkData['is_delete']) && (bool) $linkData['is_delete']) {
                     continue;
                 } else {
                     unset($linkData['link_id']);
                     // TODO: need to implement setLinkFileContent()
                     $link = $this->linkFactory->create(['data' => $linkData]);
                     if (isset($linkData['type'])) {
                         $link->setLinkType($linkData['type']);
                     }
                     if (isset($linkData['file'])) {
                         $link->setLinkFile($linkData['file']);
                     }
                     if (isset($linkData['file_content'])) {
                         $link->setLinkFileContent($linkData['file_content']);
                     }
                     $link->setId(null);
                     if (isset($linkData['sample']['type'])) {
                         $link->setSampleType($linkData['sample']['type']);
                     }
                     if (isset($linkData['sample']['file'])) {
                         $link->setSampleFile($linkData['sample']['file']);
                     }
                     if (isset($linkData['sample']['url'])) {
                         $link->setSampleUrl($linkData['sample']['url']);
                     }
                     if (isset($linkData['sample']['file_content'])) {
                         $link->setSampleFileContent($linkData['file_content']);
                     }
                     $link->setStoreId($product->getStoreId());
                     $link->setWebsiteId($product->getStore()->getWebsiteId());
                     $link->setProductWebsiteIds($product->getWebsiteIds());
                     if (!$link->getSortOrder()) {
                         $link->setSortOrder(1);
                     }
                     if (null === $link->getPrice()) {
                         $link->setPrice(0);
                     }
                     if ($link->getIsUnlimited()) {
                         $link->setNumberOfDownloads(0);
                     }
                     $links[] = $link;
                 }
             }
             $extension->setDownloadableProductLinks($links);
         }
         if (isset($downloadable['sample']) && is_array($downloadable['sample'])) {
             $samples = [];
             foreach ($downloadable['sample'] as $sampleData) {
                 if (!$sampleData || isset($sampleData['is_delete']) && (bool) $sampleData['is_delete']) {
                     continue;
                 } else {
                     unset($sampleData['sample_id']);
                     $sample = $this->sampleFactory->create(['data' => $sampleData]);
                     $sample->setId(null);
                     $sample->setStoreId($product->getStoreId());
                     if (isset($sampleData['type'])) {
                         $sample->setSampleType($sampleData['type']);
                     }
                     if (isset($sampleData['sample_url'])) {
                         $sample->setSampleUrl($sampleData['sample_url']);
                     }
                     if (!$sample->getSortOrder()) {
                         $sample->setSortOrder(1);
                     }
                     $samples[] = $sample;
                 }
             }
             $extension->setDownloadableProductSamples($samples);
         }
         $product->setExtensionAttributes($extension);
         if ($product->getLinksPurchasedSeparately()) {
             $product->setTypeHasRequiredOptions(true)->setRequiredOptions(true);
         } else {
             $product->setTypeHasRequiredOptions(false)->setRequiredOptions(false);
         }
     }
     return $product;
 }