/** * @magentoDataFixture Magento/Downloadable/_files/product_downloadable_with_files.php * @magentoAppArea adminhtml */ public function testDeleteTypeSpecificData() { $product = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()->create('Magento\\Catalog\\Model\\Product'); $product->load(1); $product->setOrigData(); $downloadableData = array(); $links = $this->_model->getLinks($product); $this->assertNotEmpty($links); $samples = $this->_model->getSamples($product); $this->assertNotEmpty($samples->getData()); foreach ($links as $link) { $downloadableData['link'][] = $link->getData(); } foreach ($samples as $sample) { $downloadableData['sample'][] = $sample->getData(); } $product->setDownloadableData($downloadableData); $this->_model->deleteTypeSpecificData($product); $product = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()->create('Magento\\Catalog\\Model\\Product'); $product->load(1); $links = $this->_model->getLinks($product); $this->assertEmpty($links); $samples = $this->_model->getSamples($product); $this->assertEmpty($samples->getData()); }
/** * @magentoDataFixture Magento/Downloadable/_files/product_downloadable_with_files.php * @magentoAppArea adminhtml */ public function testSaveTypeSpecificData() { $product = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()->create('Magento\\Catalog\\Model\\Product'); $product->load(1); $product->setOrigData(); $downloadableData = []; $links = $this->_model->getLinks($product); $this->assertNotEmpty($links); $samples = $this->_model->getSamples($product); $this->assertNotEmpty($samples->getData()); $i = 0; foreach ($links as $link) { $i++; $linkData = $link->getData(); $linkData['is_delete'] = 0; $linkData['type'] = \Magento\Downloadable\Helper\Download::LINK_TYPE_FILE; $linkData['title'] = 'Updated downloadable link #' . $i; $downloadableData['link'][] = $linkData; } $i = 0; foreach ($samples as $sample) { $i++; $sampleData = $sample->getData(); $sampleData['is_delete'] = 0; $sampleData['type'] = \Magento\Downloadable\Helper\Download::LINK_TYPE_FILE; $sampleData['title'] = 'Updated downloadable sample #' . $i; $downloadableData['sample'][] = $sampleData; } $product->setDownloadableData($downloadableData); $this->_model->save($product); $product = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()->create('Magento\\Catalog\\Model\\Product'); $product->load(1); $expectedLink = ['default_price' => null, 'default_title' => null, 'is_shareable' => '2', 'link_file' => null, 'link_type' => 'file', 'link_url' => null, 'number_of_downloads' => '15', 'price' => '15.0000', 'product' => $product, 'product_id' => 1, 'sample_file' => null, 'sample_type' => 'file', 'sample_url' => null, 'sort_order' => '0', 'store_title' => 'Updated downloadable link #1', 'title' => 'Updated downloadable link #1', 'website_price' => '15.0000']; $links = $this->_model->getLinks($product); $this->assertNotEmpty($links); $this->assertCount(1, $links); $link = reset($links); foreach ($expectedLink as $key => $value) { $this->assertTrue($link->hasData($key), 'Key ' . $key . ' not exist!'); $this->assertArrayHasKey($key, $link); $this->assertEquals($value, $link->getData($key)); } $expectedSample = ['default_title' => null, 'product_id' => '1', 'sample_file' => null, 'sample_type' => 'file', 'sample_url' => null, 'sort_order' => '0', 'store_title' => 'Updated downloadable sample #1', 'title' => 'Updated downloadable sample #1']; $samples = $this->_model->getSamples($product); $this->assertNotEmpty($samples->getData()); $this->assertEquals(1, $samples->count()); /** @var \Magento\Downloadable\Model\Sample $sample */ $sample = $samples->getFirstItem()->getData(); foreach ($expectedSample as $key => $value) { $this->assertArrayHasKey($key, $sample); $this->assertEquals($value, $sample[$key]); } }
/** * {@inheritdoc} */ public function getLinks($productSku) { $linkList = []; /** @var \Magento\Catalog\Model\Product $product */ $product = $this->productRepository->get($productSku); $links = $this->downloadableType->getLinks($product); /** @var \Magento\Downloadable\Model\Link $link */ foreach ($links as $link) { $linkList[] = $this->buildLink($link); } return $linkList; }
/** * @param \Magento\Catalog\Api\Data\ProductInterface $product * @return array */ public function getLinksByProduct(\Magento\Catalog\Api\Data\ProductInterface $product) { $linkList = []; $links = $this->downloadableType->getLinks($product); /** @var \Magento\Downloadable\Model\Link $link */ foreach ($links as $link) { $linkList[] = $this->buildLink($link); } return $linkList; }
/** * @magentoDataFixture Magento/Downloadable/_files/product_downloadable_with_files.php * @magentoAppArea adminhtml * @SuppressWarnings(PHPMD.ExcessiveMethodLength) * @SuppressWarnings(PHPMD.CyclomaticComplexity) * @SuppressWarnings(PHPMD.NPathComplexity) */ public function testSaveTypeSpecificData() { $objectManager = \Magento\TestFramework\Helper\Bootstrap::getObjectManager(); $product = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()->create('Magento\\Catalog\\Model\\Product'); $product->load(1); $product->setOrigData(); $downloadableData = []; $links = $this->_model->getLinks($product); $this->assertNotEmpty($links); $samples = $this->_model->getSamples($product); $this->assertNotEmpty($samples->getData()); $i = 0; foreach ($links as $link) { $i++; $linkData = $link->getData(); $linkData['is_delete'] = 0; $linkData['type'] = \Magento\Downloadable\Helper\Download::LINK_TYPE_FILE; $linkData['title'] = 'Updated downloadable link #' . $i; $downloadableData['link'][] = $linkData; } $i = 0; foreach ($samples as $sample) { $i++; $sampleData = $sample->getData(); $sampleData['is_delete'] = 0; $sampleData['type'] = \Magento\Downloadable\Helper\Download::LINK_TYPE_FILE; $sampleData['title'] = 'Updated downloadable sample #' . $i; $downloadableData['sample'][] = $sampleData; } $product->setDownloadableData($downloadableData); $sampleFactory = $objectManager->create('Magento\\Downloadable\\Api\\Data\\SampleInterfaceFactory'); $linkFactory = $objectManager->create('Magento\\Downloadable\\Api\\Data\\LinkInterfaceFactory'); $extension = $product->getExtensionAttributes(); $links = []; foreach ($downloadableData['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 = $linkFactory->create(['data' => $linkData]); $link->setId(null); if (isset($linkData['sample'])) { $link->setSampleType($linkData['sample']['type']); $link->setSampleFileData($linkData['sample']['file']); $link->setSampleUrl($linkData['sample']['url']); } $link->setLinkType($linkData['type']); $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($downloadableData['sample']) && is_array($downloadableData['sample'])) { $samples = []; foreach ($downloadableData['sample'] as $sampleData) { if (!$sampleData || isset($sampleData['is_delete']) && (bool) $sampleData['is_delete']) { continue; } else { unset($sampleData['sample_id']); $sample = $sampleFactory->create(['data' => $sampleData]); $sample->setId(null); $sample->setStoreId($product->getStoreId()); $sample->setSampleType($sampleData['type']); $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); } $product->save(); /** @var \Magento\Catalog\Model\Product $product */ $product = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()->create('Magento\\Catalog\\Model\\Product'); $product->load(1); $expectedLink = ['is_shareable' => '2', 'link_type' => 'file', 'link_file' => '/j/e/jellyfish_2_4.jpg', 'number_of_downloads' => '15', 'price' => '15.0000', 'sample_file' => '/n/d/jellyfish_1_3.jpg', 'sample_type' => 'file', 'sort_order' => '1', 'title' => 'Updated downloadable link #1']; $links = $product->getExtensionAttributes()->getDownloadableProductLinks(); $this->assertNotEmpty($links); $this->assertCount(1, $links); /** @var \Magento\Downloadable\Model\Link $link */ $link = reset($links); foreach ($expectedLink as $key => $value) { $this->assertTrue($link->hasData($key), 'Key ' . $key . ' not exist!'); $this->assertArrayHasKey($key, $link); $this->assertEquals($value, $link->getData($key)); } $expectedSample = ['sample_type' => 'file', 'sort_order' => '1', 'title' => 'Updated downloadable sample #1']; $samples = $product->getExtensionAttributes()->getDownloadableProductSamples(); $sample = reset($samples); $this->assertNotEmpty($sample->getData()); $this->assertEquals(1, count($samples)); /** @var \Magento\Downloadable\Model\Sample $sample */ $sample = $sample->getData(); /** @var \Magento\User\Api\Data\UserInterface $testAttribute */ foreach ($expectedSample as $key => $value) { $this->assertArrayHasKey($key, $sample); $this->assertEquals($value, $sample[$key]); } }