Esempio n. 1
0
 public function testGetList()
 {
     $productSku = 'downloadable_sku';
     $linkData = ['id' => 324, 'store_title' => 'rock melody', 'title' => 'just melody', 'price' => 23, 'number_of_downloads' => 3, 'sort_order' => 21, 'is_shareable' => 2, 'sample_type' => 'file', 'sample_url' => null, 'sample_file' => '/r/o/rock.melody.ogg', 'link_type' => 'url', 'link_url' => 'http://link.url', 'link_file' => null];
     $linkMock = $this->getMock('Magento\\Downloadable\\Model\\Link', ['getId', 'getStoreTitle', 'getTitle', 'getPrice', 'getNumberOfDownloads', 'getSortOrder', 'getIsShareable', 'getData', '__wakeup', 'setId'], [], '', false);
     $linkInterfaceMock = $this->getMock('\\Magento\\Downloadable\\Api\\Data\\LinkInterface');
     $this->repositoryMock->expects($this->once())->method('get')->with($productSku)->will($this->returnValue($this->productMock));
     $this->productTypeMock->expects($this->once())->method('getLinks')->with($this->productMock)->will($this->returnValue([$linkMock]));
     $this->setLinkAssertions($linkMock, $linkData);
     $this->linkFactoryMock->expects($this->once())->method('create')->willReturn($linkInterfaceMock);
     $this->assertEquals([$linkInterfaceMock], $this->service->getList($productSku));
 }
Esempio n. 2
0
 /**
  * Build a link data object
  *
  * @param \Magento\Downloadable\Model\Link $resourceData
  * @return \Magento\Downloadable\Model\Link
  */
 protected function buildLink($resourceData)
 {
     /** @var \Magento\Downloadable\Model\Link $link */
     $link = $this->linkDataObjectFactory->create();
     $this->setBasicFields($resourceData, $link);
     $link->setPrice($resourceData->getPrice());
     $link->setNumberOfDownloads($resourceData->getNumberOfDownloads());
     $link->setIsShareable($resourceData->getIsShareable());
     $link->setLinkType($resourceData->getLinkType());
     $link->setLinkFile($resourceData->getLinkFile());
     $link->setLinkUrl($resourceData->getLinkUrl());
     return $link;
 }
 /**
  * {@inheritdoc}
  */
 public function delete($id)
 {
     /** @var $link \Magento\Downloadable\Model\Link */
     $link = $this->linkFactory->create()->load($id);
     if (!$link->getId()) {
         throw new NoSuchEntityException(__('There is no downloadable link with provided ID.'));
     }
     try {
         $this->resourceModel->delete($link);
     } catch (\Exception $exception) {
         throw new StateException(__('Cannot delete link with id %1', $link->getId()), $exception);
     }
     return true;
 }
 /**
  * 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;
 }